Skip to content

Commit

Permalink
Issue #357: Fixed error with splitting on batch separator inside quot…
Browse files Browse the repository at this point in the history
…es (#360)
  • Loading branch information
erikbra authored Aug 13, 2023
1 parent 2e3ddd4 commit 250cdc5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System.Linq;
using FluentAssertions;
using grate.Infrastructure;
using grate.Migration;
using Microsoft.Extensions.Logging.Abstractions;
Expand Down Expand Up @@ -36,20 +37,20 @@ USING btree

batches.Should().HaveCount(4);
}

[Test]
public void Ignores_semicolons_in_strings()
public void Ignores_semicolons_in_backslash_escaped_strings()
{
var original = @"
DO '
DO E'
BEGIN
IF NOT EXISTS(
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = ''random''
WHERE schema_name = \'random\'
)
THEN
EXECUTE ''CREATE SCHEMA random'';
EXECUTE \'CREATE SCHEMA random\';
END IF;
END
';
Expand All @@ -59,48 +60,67 @@ FROM information_schema.schemata
batches.Should().HaveCount(1);
}

[Test]
public void Ignores_semicolons_in_backslash_escaped_strings()
[TestCase("$$")]
[TestCase("$sometag$")]
public void Ignores_semicolons_in_dollar_quoted_strings(string tag)
{
var original = @"
DO E'
var original = @$"
DO
{tag}
BEGIN
IF NOT EXISTS(
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = \'random\'
WHERE schema_name = 'random'
)
THEN
EXECUTE \'CREATE SCHEMA random\';
EXECUTE 'CREATE SCHEMA random';
END IF;
END
';
{tag};
";
var batches = Splitter.Split(original);
batches.Should().HaveCount(1);
}

[Test]
public void Splits_on_semicolon_after_single_quotes_when_there_is_another_semicolon_in_the_quote()
{
var original = @"SELECT 1 WHERE whatnot = '; ' ; MOO";
var batches = Splitter.Split(original).ToList();
batches.Should().HaveCount(2);

batches.First().Should().Be("SELECT 1 WHERE whatnot = '; ' ");
batches.Last().Should().Be(" MOO");
}

[Test]
public void Ignores_semicolon_in_single_quotes_when_there_is_no_other_semicolon()
{
var original = @"SELECT 1 WHERE whatnot = '; '";
var batches = Splitter.Split(original);
batches.Should().HaveCount(1);
}

[TestCase("$$")]
[TestCase("$sometag$")]
public void Ignores_semicolons_in_dollar_quoted_strings(string tag)
[Test]
public void Ignores_semicolons_in_strings()
{
var original = @$"
DO
{tag}
var original = @"
DO '
BEGIN
IF NOT EXISTS(
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = 'random'
WHERE schema_name = ''random''
)
THEN
EXECUTE 'CREATE SCHEMA random';
EXECUTE ''CREATE SCHEMA random'';
END IF;
END
{tag};
';
";
var batches = Splitter.Split(original);
var batches = Splitter.Split(original);

batches.Should().HaveCount(1);
}
}
8 changes: 4 additions & 4 deletions grate/Infrastructure/PostgreSqlSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public string StatementSeparatorRegex
get
{
const string strings = @"(?<KEEP1>'([^']|\'\')*')";
const string backslashEscapedSrings = @"(?<KEEP1>E(?<!\\)('[\S\s]*?(?<!\\)'))";
const string backslashEscapedStrings = @"(?<KEEP1>E(?<!\\)('[\S\s]*?(?<!\\)'))";
const string dollarQuotedStrings = @"(?<KEEP1>\$(?'tag'\w*)\$[\S\s]*?\$\k'tag'\$)";
const string dashComments = @"(?<KEEP1>--.*$)";
const string dashComments = "(?<KEEP1>--.*$)";
const string starComments = @"(?<KEEP1>/\*[\S\s]*?\*/)";
const string separator = @"(?<KEEP1>.*)(?<BATCHSPLITTER>;)(?<KEEP2>.*)";
return strings + "|" + backslashEscapedSrings + "|" + dollarQuotedStrings + "|" + dashComments + "|" + starComments + "|" + separator;
const string separator = "(?<KEEP1>.*)(?<BATCHSPLITTER>(;)(?=(?:[^']|'[^']*')*$))(?<KEEP2>.*)";
return strings + "|" + backslashEscapedStrings + "|" + dollarQuotedStrings + "|" + dashComments + "|" + starComments + "|" + separator;
}
}

Expand Down

0 comments on commit 250cdc5

Please sign in to comment.