From 01f99627158676137cedfb576d87700e4f005ca1 Mon Sep 17 00:00:00 2001 From: Gilles TOURREAU Date: Thu, 17 Oct 2024 08:52:06 +0200 Subject: [PATCH] Fix a bug when parsing the GO instruction. --- .../SqlServerScriptParser.cs | 20 ++++-- .../SqlServerDatabaseExtensionsTest.cs | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/Testing.Databases.SqlServer/SqlServerScriptParser.cs b/src/Testing.Databases.SqlServer/SqlServerScriptParser.cs index 0ee91d0..7375344 100644 --- a/src/Testing.Databases.SqlServer/SqlServerScriptParser.cs +++ b/src/Testing.Databases.SqlServer/SqlServerScriptParser.cs @@ -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*(?\\d+)?\\b", RegexOptions.Compiled); + private readonly TextReader script; private bool isEndOfScript; @@ -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. @@ -67,6 +70,11 @@ public SqlServerScriptParser(TextReader script) codeBuilder.AppendLine(line); } + if (codeBuilder.Length == 0) + { + return null; + } + return new SqlServerScriptBlock(codeBuilder.ToString(), count); } } diff --git a/tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs b/tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs index 538041a..ffdfdde 100644 --- a/tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs +++ b/tests/Testing.Databases.SqlServer.Tests/SqlServerDatabaseExtensionsTest.cs @@ -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 ( @@ -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 ( @@ -148,6 +210,7 @@ UPDATE [TableTest] GO 10 + ")); var table = database.ExecuteQuery("SELECT * FROM [TableTest]");