Invoke-SqlCmd2 icon indicating copy to clipboard operation
Invoke-SqlCmd2 copied to clipboard

Running with ParseGo also splits if there are GO in comments

Open jeffchulg opened this issue 4 years ago • 1 comments

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:

  • https://stackoverflow.com/questions/43738916/remove-comment-blocks-in-powershell
  • https://fullparam.wordpress.com/2012/03/07/remove-sql-block-comments-via-regex/
  • https://larrysteinle.com/2011/02/09/use-regular-expressions-to-clean-sql-statements/
  • https://stackoverflow.com/questions/53637395/how-can-i-check-if-a-t-sql-file-contains-go-statements-inside-block-comments-using-powershell/53652440

jeffchulg avatar Feb 26 '21 06:02 jeffchulg

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)
            }

jeffchulg avatar Feb 26 '21 07:02 jeffchulg