dataplat / Invoke-SqlCmd2

PowerShell module containing Invoke-SqlCmd2
MIT License
68 stars 35 forks source link

Running with ParseGo also splits if there are GO in comments #29

Open jeffchulg opened 3 years ago

jeffchulg commented 3 years ago

Hi,

there is an option -ParseGO which splits a file or query into multiple pieces of code and runs each splitted code once at a time.

The problem is that the split doesn't take into account that GO could be in comments.

Would it be possible add an option to Invoke-SQLCmd2 to find and replace any GO inside a comment (especially those starting with /* and ending with */) by a space before actually splitting ?

Some links that could help:

jeffchulg commented 3 years ago

Hi, here is what I tested and worked:

I changed the $GoSplitterRegex definition in line 440 from $GoSplitterRegex = [regex]'(?smi)^[\s]*GO[\s]*$' to $GoSplitterRegex = [regex]'(?smi)^\s*GO(?:\s+\d+)?\s*$'

Then I added following line just below: ` $GoSanitizerRegex = '(?sm)/*+?^\sgo(?:\s+\d+)?\s$.+?[*]/'

After that I changed the lines starting 493 from

            if ($ParseGO) {
                Write-Debug -Message "Stripping GOs from source"
                $Pieces = $GoSplitterRegex.Split($Query)
            }

to

            if ($ParseGO) {
                Write-Debug -Message "Stripping GOs from source"
                $SanitizedQuery = $Query -split $GoSanitizerRegex -join ""

                if($SanitizedQuery -ne $Query) {
                    Write-Debug "Removed some comments with 'GO' inside it"
                }

                $Pieces = $GoSplitterRegex.Split($SanitizedQuery)
            }