-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an extension method ExecuteScript() to execute a SQL script.
- Loading branch information
1 parent
7622a25
commit 758f901
Showing
5 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="SqlServerScriptBlock.cs" company="P.O.S Informatique"> | ||
// Copyright (c) P.O.S Informatique. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace PosInformatique.Testing.Databases.SqlServer | ||
{ | ||
internal class SqlServerScriptBlock | ||
{ | ||
public SqlServerScriptBlock(string code, int count) | ||
{ | ||
this.Code = code; | ||
this.Count = count; | ||
} | ||
|
||
public string Code { get; } | ||
|
||
public int Count { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="SqlServerScriptParser.cs" company="P.O.S Informatique"> | ||
// Copyright (c) P.O.S Informatique. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace PosInformatique.Testing.Databases.SqlServer | ||
{ | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
internal sealed class SqlServerScriptParser | ||
{ | ||
private readonly TextReader script; | ||
|
||
private bool isEndOfScript; | ||
|
||
public SqlServerScriptParser(TextReader script) | ||
{ | ||
this.script = script; | ||
} | ||
|
||
public SqlServerScriptBlock? ReadNextBlock() | ||
{ | ||
if (this.isEndOfScript) | ||
{ | ||
return null; | ||
} | ||
|
||
var codeBuilder = new StringBuilder(); | ||
|
||
var count = 1; | ||
|
||
while (true) | ||
{ | ||
var line = this.script.ReadLine(); | ||
|
||
if (line is null) | ||
{ | ||
// End of the script reach. | ||
this.isEndOfScript = true; | ||
break; | ||
} | ||
|
||
line = line.Trim(); | ||
|
||
if (line.StartsWith("GO")) | ||
{ | ||
// Parse the number after the "GO". | ||
var textAfterGo = line.Substring(2).Trim(); | ||
|
||
if (textAfterGo != string.Empty) | ||
{ | ||
count = Convert.ToInt32(textAfterGo, CultureInfo.InvariantCulture); | ||
} | ||
|
||
// If no code parsed, we continue to parse the block. | ||
if (codeBuilder.Length == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
// Else, we stop the read of the script. | ||
break; | ||
} | ||
|
||
codeBuilder.AppendLine(line); | ||
} | ||
|
||
return new SqlServerScriptBlock(codeBuilder.ToString(), count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters