Skip to content

Commit

Permalink
Fix a bug when parsing the GO instruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesTourreau committed Oct 17, 2024
1 parent 758f901 commit 01f9962
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Testing.Databases.SqlServer/SqlServerScriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ namespace PosInformatique.Testing.Databases.SqlServer
{
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

internal sealed class SqlServerScriptParser
{
private static readonly Regex GoInstruction = new Regex("^GO\\s*(?<count>\\d+)?\\b", RegexOptions.Compiled);

private readonly TextReader script;

private bool isEndOfScript;
Expand Down Expand Up @@ -44,14 +47,14 @@ public SqlServerScriptParser(TextReader script)

line = line.Trim();

if (line.StartsWith("GO"))
{
// Parse the number after the "GO".
var textAfterGo = line.Substring(2).Trim();
var goInstructionMatch = GoInstruction.Match(line);

if (textAfterGo != string.Empty)
if (goInstructionMatch.Success)
{
// Retrieve the number after the "GO".
if (goInstructionMatch.Groups["count"].Success)
{
count = Convert.ToInt32(textAfterGo, CultureInfo.InvariantCulture);
count = Convert.ToInt32(goInstructionMatch.Groups["count"].Value, CultureInfo.InvariantCulture);
}

// If no code parsed, we continue to parse the block.
Expand All @@ -67,6 +70,11 @@ public SqlServerScriptParser(TextReader script)
codeBuilder.AppendLine(line);
}

if (codeBuilder.Length == 0)
{
return null;
}

return new SqlServerScriptBlock(codeBuilder.ToString(), count);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ public void ExecuteScript_String()

var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");

database.ExecuteScript(@"
CREATE TABLE TableTest
(
Id INT NOT NULL
)
GO
GO
INSERT INTO [TableTest] ([Id]) VALUES (0)
GO
UPDATE [TableTest]
SET [Id] = [Id] + 1
GO 10");

var table = database.ExecuteQuery("SELECT * FROM [TableTest]");

table.Rows.Should().HaveCount(1);

table.Rows[0]["Id"].Should().Be(10);
}

[Fact]
public void ExecuteScript_String_WithEmptyLinesAtTheEnd()
{
var server = new SqlServer(ConnectionString);

var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");

database.ExecuteScript(@"
CREATE TABLE TableTest
(
Expand Down Expand Up @@ -131,6 +162,37 @@ public void ExecuteScript_StringReader()

var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");

database.ExecuteScript(new StringReader(@"
CREATE TABLE TableTest
(
Id INT NOT NULL
)
GO
GO
INSERT INTO [TableTest] ([Id]) VALUES (0)
GO
UPDATE [TableTest]
SET [Id] = [Id] + 1
GO 10"));

var table = database.ExecuteQuery("SELECT * FROM [TableTest]");

table.Rows.Should().HaveCount(1);

table.Rows[0]["Id"].Should().Be(10);
}

[Fact]
public void ExecuteScript_StringReader_WithEmptyLinesAtTheEnd()
{
var server = new SqlServer(ConnectionString);

var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");

database.ExecuteScript(new StringReader(@"
CREATE TABLE TableTest
(
Expand All @@ -148,6 +210,7 @@ UPDATE [TableTest]
GO 10
"));

var table = database.ExecuteQuery("SELECT * FROM [TableTest]");
Expand Down

0 comments on commit 01f9962

Please sign in to comment.