<commit_before>
filter Invoke-Query
{
    <#
    .SYNOPSIS
    Executes a SQL query against the database.
    
    .DESCRIPTION
    All migrations eventually come down to this method.  It takes raw SQL and executes it against the database.  Queries are split on `GO` statements, and each query is sent individually to the database.
    
    You can pipe queries to this method, too!
    
    .EXAMPLE
    Invoke-Query -Query 'create table rivet.Migrations( )'
    
    Executes the create table syntax above against the database.
    
    .EXAMPLE
    Invoke-Query -Query 'select count(*) from MyTable' -Database MyOtherDatabase
    
    Executes a query against the non-current database.  Returns the rows as objects.
    
    .EXAMPLE
    'select count(*) from sys.tables' | Invoke-Query -AsScalar
    
    Demonstrates how queries can be piped into `Invoke-Query`.  Also shows how a result can be returned as a scalar.
    #>
    [CmdletBinding(DefaultParameterSetName='AsReader')]
    param(
        [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
        [string]
        $Query,
        
        [Parameter(Mandatory=$true,ParameterSetName='ExecuteScalar')]
        [Switch]
        $AsScalar,
        
        [Parameter(Mandatory=$true,ParameterSetName='ExecuteNonQuery')]
        [Switch]
        $NonQuery,
        
        [UInt32]
        # The time in seconds to wait for the command to execute. The default is 30 seconds.
        $CommandTimeout = 30
    )

    $invokeMigrationParams = @{
                              }
    if( $PSBoundParameters.ContainsKey( 'AsScalar' ) )
    {
        $invokeMigrationParams.AsScalar = $true
    }
    
    if( $PSBoundParameters.ContainsKey( 'NonQuery' ) )
    {
        $invokeMigrationParams.NonQuery = $true
    }
    
    if( $PSBoundParameters.ContainsKey( 'CommandTimeout' ) )
    {
        $invokeMigrationParams.CommandTimeout = $CommandTimeout
    }

    $currentQuery = New-Object Text.StringBuilder
    $inComment = $false
    $commentCouldBeStarting = $false
    $commentCouldBeEnding = $false
    $prevChar = $null
    $currentChar = $null
    $commentDepth = 0
    $currentLine = New-Object Text.StringBuilder
    
    Invoke-Command { 
            $Query.ToCharArray()
            "`nGO`n".ToCharArray() # We add `nGO`n to ensure we send the last query to the pipeline. 
        } | 
        ForEach-Object {
            $prevChar = $currentChar
            $currentChar = $_
            
            if( $inComment -and $prevChar -eq '*' -and $currentChar -eq '/' )
            {
                $commentDepth--
                $inComment = ($commentDepth -gt 0)
            }

            if( $prevChar -eq '/' -and $currentChar -eq '*' )
            {
                $inComment = $true
                $commentDepth++
            }

            if( $currentChar -eq "`r" )
            {
                return
            }
            
            [void] $currentLine.Append( $currentChar )

            if( $currentChar -eq "`n" )
            {
                $trimmedLine = $currentLine.ToString().Trim() 
                if( -not $inComment -and $trimmedLine -match "^GO\b" )
                {
                    if( $currentQuery.Length -gt 0 )
                    {
                        $currentQuery.ToString()<commit_msg>Bug fix: Invoke-Query not trimming newline from individual queries.<commit_after>
filter Invoke-Query
{
    <#
    .SYNOPSIS
    Executes a SQL query against the database.
    
    .DESCRIPTION
    All migrations eventually come down to this method.  It takes raw SQL and executes it against the database.  Queries are split on `GO` statements, and each query is sent individually to the database.
    
    You can pipe queries to this method, too!
    
    .EXAMPLE
    Invoke-Query -Query 'create table rivet.Migrations( )'
    
    Executes the create table syntax above against the database.
    
    .EXAMPLE
    Invoke-Query -Query 'select count(*) from MyTable' -Database MyOtherDatabase
    
    Executes a query against the non-current database.  Returns the rows as objects.
    
    .EXAMPLE
    'select count(*) from sys.tables' | Invoke-Query -AsScalar
    
    Demonstrates how queries can be piped into `Invoke-Query`.  Also shows how a result can be returned as a scalar.
    #>
    [CmdletBinding(DefaultParameterSetName='AsReader')]
    param(
        [Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
        [string]
        $Query,
        
        [Parameter(Mandatory=$true,ParameterSetName='ExecuteScalar')]
        [Switch]
        $AsScalar,
        
        [Parameter(Mandatory=$true,ParameterSetName='ExecuteNonQuery')]
        [Switch]
        $NonQuery,
        
        [UInt32]
        # The time in seconds to wait for the command to execute. The default is 30 seconds.
        $CommandTimeout = 30
    )

    $invokeMigrationParams = @{
                              }
    if( $PSBoundParameters.ContainsKey( 'AsScalar' ) )
    {
        $invokeMigrationParams.AsScalar = $true
    }
    
    if( $PSBoundParameters.ContainsKey( 'NonQuery' ) )
    {
        $invokeMigrationParams.NonQuery = $true
    }
    
    if( $PSBoundParameters.ContainsKey( 'CommandTimeout' ) )
    {
        $invokeMigrationParams.CommandTimeout = $CommandTimeout
    }

    $currentQuery = New-Object Text.StringBuilder
    $inComment = $false
    $commentCouldBeStarting = $false
    $commentCouldBeEnding = $false
    $prevChar = $null
    $currentChar = $null
    $commentDepth = 0
    $currentLine = New-Object Text.StringBuilder
    
    Invoke-Command { 
            $Query.ToCharArray()
            "`nGO`n".ToCharArray() # We add `nGO`n to ensure we send the last query to the pipeline. 
        } | 
        ForEach-Object {
            $prevChar = $currentChar
            $currentChar = $_
            
            if( $inComment -and $prevChar -eq '*' -and $currentChar -eq '/' )
            {
                $commentDepth--
                $inComment = ($commentDepth -gt 0)
            }

            if( $prevChar -eq '/' -and $currentChar -eq '*' )
            {
                $inComment = $true
                $commentDepth++
            }

            if( $currentChar -eq "`r" )
            {
                return
            }
            
            [void] $currentLine.Append( $currentChar )

            if( $currentChar -eq "`n" )
            {
                $trimmedLine = $currentLine.ToString().Trim() 
                if( -not $inComment -and $trimmedLine -match "^GO\b" )
                {
                    if( $currentQuery.Length -gt 0 )
                    {
                        $currentQuery.ToString().Trim()
{'fraction_non_alphanumeric': 0.09512449679439391, 'fraction_numerical': 0.002981959147159684, 'mean_word_length': 1.7787903893951946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1839039', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1836, 'n_words': 501}