Skip to content

Commit 01f9962

Browse files
Fix a bug when parsing the GO instruction.
1 parent 758f901 commit 01f9962

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/Testing.Databases.SqlServer/SqlServerScriptParser.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ namespace PosInformatique.Testing.Databases.SqlServer
88
{
99
using System.Globalization;
1010
using System.Text;
11+
using System.Text.RegularExpressions;
1112

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

1619
private bool isEndOfScript;
@@ -44,14 +47,14 @@ public SqlServerScriptParser(TextReader script)
4447

4548
line = line.Trim();
4649

47-
if (line.StartsWith("GO"))
48-
{
49-
// Parse the number after the "GO".
50-
var textAfterGo = line.Substring(2).Trim();
50+
var goInstructionMatch = GoInstruction.Match(line);
5151

52-
if (textAfterGo != string.Empty)
52+
if (goInstructionMatch.Success)
53+
{
54+
// Retrieve the number after the "GO".
55+
if (goInstructionMatch.Groups["count"].Success)
5356
{
54-
count = Convert.ToInt32(textAfterGo, CultureInfo.InvariantCulture);
57+
count = Convert.ToInt32(goInstructionMatch.Groups["count"].Value, CultureInfo.InvariantCulture);
5558
}
5659

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

73+
if (codeBuilder.Length == 0)
74+
{
75+
return null;
76+
}
77+
7078
return new SqlServerScriptBlock(codeBuilder.ToString(), count);
7179
}
7280
}

tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs

+63
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,37 @@ public void ExecuteScript_String()
9898

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

101+
database.ExecuteScript(@"
102+
CREATE TABLE TableTest
103+
(
104+
Id INT NOT NULL
105+
)
106+
107+
GO
108+
GO
109+
110+
INSERT INTO [TableTest] ([Id]) VALUES (0)
111+
112+
GO
113+
UPDATE [TableTest]
114+
SET [Id] = [Id] + 1
115+
116+
GO 10");
117+
118+
var table = database.ExecuteQuery("SELECT * FROM [TableTest]");
119+
120+
table.Rows.Should().HaveCount(1);
121+
122+
table.Rows[0]["Id"].Should().Be(10);
123+
}
124+
125+
[Fact]
126+
public void ExecuteScript_String_WithEmptyLinesAtTheEnd()
127+
{
128+
var server = new SqlServer(ConnectionString);
129+
130+
var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");
131+
101132
database.ExecuteScript(@"
102133
CREATE TABLE TableTest
103134
(
@@ -131,6 +162,37 @@ public void ExecuteScript_StringReader()
131162

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

165+
database.ExecuteScript(new StringReader(@"
166+
CREATE TABLE TableTest
167+
(
168+
Id INT NOT NULL
169+
)
170+
171+
GO
172+
GO
173+
174+
INSERT INTO [TableTest] ([Id]) VALUES (0)
175+
176+
GO
177+
UPDATE [TableTest]
178+
SET [Id] = [Id] + 1
179+
180+
GO 10"));
181+
182+
var table = database.ExecuteQuery("SELECT * FROM [TableTest]");
183+
184+
table.Rows.Should().HaveCount(1);
185+
186+
table.Rows[0]["Id"].Should().Be(10);
187+
}
188+
189+
[Fact]
190+
public void ExecuteScript_StringReader_WithEmptyLinesAtTheEnd()
191+
{
192+
var server = new SqlServer(ConnectionString);
193+
194+
var database = server.CreateEmptyDatabase("SqlServerDatabaseExtensionsTest");
195+
134196
database.ExecuteScript(new StringReader(@"
135197
CREATE TABLE TableTest
136198
(
@@ -148,6 +210,7 @@ UPDATE [TableTest]
148210
149211
GO 10
150212
213+
151214
"));
152215

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

0 commit comments

Comments
 (0)