Skip to content

Commit

Permalink
Bugfix for parsing SQL files
Browse files Browse the repository at this point in the history
Horton.exe was using this code to split up .sql files into
statements to be processed:
public static string[] ParseSqlScript(string script)
{
    var regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
    return regex.Split(script);
}
Therefore, a line beginning with "GO" would split up batches to be
executed.
This is fine, except when we have a large sql file that contains
line breaks.  Say, something like this:

INSERT INTO [MyTable] ([Description]) VALUES ('Blah blah blah

Goods connected directly with

Blah blah blah');

Did you spot it?

One of the lines begins with "Goods connected directly..." which matches
the regex "^GO".   So that insert statement will be split in two and
cause an error because of mismatched quote marks.   The solution is to
fix the regex in horton.exe by using a negative lookahead for a
non-whitespace character:

var regex = new Regex("^GO(?!\\S)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  • Loading branch information
Matthew Reishus authored and Matthew Reishus committed Apr 17, 2017
1 parent 61a696f commit 02c7a95
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/Horton/SqlServer/SchemaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void ApplyMigration(ScriptFile migration)

public static string[] ParseSqlScript(string script)
{
var regex = new Regex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);
var regex = new Regex("^GO(?!\\S)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regex.Split(script);
}

Expand Down

1 comment on commit 02c7a95

@mreishus
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I did just realize this would still cause a failure:

INSERT INTO [MyTable] ([Description]) VALUES ('Blah blah blah

Go to the store

Blah blah blah');

Please sign in to comment.