From c5b047809658361fd253418126140eb81c84f81f Mon Sep 17 00:00:00 2001 From: sethreno Date: Thu, 5 Sep 2013 17:52:18 -0500 Subject: [PATCH] refactored Eliminated unnecessary method from DBHelper. --- model/BatchSqlParser.cs | 2 +- model/DBHelper.cs | 6 +- ...elperTester.cs => BatchSqlParserTester.cs} | 594 +++++++++--------- test/test.csproj | 2 +- 4 files changed, 300 insertions(+), 304 deletions(-) rename test/{DBHelperTester.cs => BatchSqlParserTester.cs} (80%) diff --git a/model/BatchSqlParser.cs b/model/BatchSqlParser.cs index 34338c07..ab5a4195 100644 --- a/model/BatchSqlParser.cs +++ b/model/BatchSqlParser.cs @@ -10,7 +10,7 @@ internal enum State { InQuotes } - internal class BatchSqlParser { + public class BatchSqlParser { private static bool IsWhitespace(char c) { return Regex.Match(c.ToString(), "\\s", RegexOptions.Multiline).Success; } diff --git a/model/DBHelper.cs b/model/DBHelper.cs index abb34971..d6e6e86b 100644 --- a/model/DBHelper.cs +++ b/model/DBHelper.cs @@ -21,7 +21,7 @@ public static void ExecBatchSql(string conn, string sql) { using (var cn = new SqlConnection(conn)) { cn.Open(); using (SqlCommand cm = cn.CreateCommand()) { - foreach (string script in SplitBatchSql(sql)) { + foreach (string script in BatchSqlParser.SplitBatch(sql)) { if (EchoSql) Console.WriteLine(script); cm.CommandText = script; try { @@ -38,10 +38,6 @@ public static void ExecBatchSql(string conn, string sql) { } } - public static string[] SplitBatchSql(string batchSql) { - return BatchSqlParser.SplitBatch(batchSql); - } - public static void DropDb(string conn) { var cnBuilder = new SqlConnectionStringBuilder(conn); string dbName = cnBuilder.InitialCatalog; diff --git a/test/DBHelperTester.cs b/test/BatchSqlParserTester.cs similarity index 80% rename from test/DBHelperTester.cs rename to test/BatchSqlParserTester.cs index 53106e76..1475cc2a 100644 --- a/test/DBHelperTester.cs +++ b/test/BatchSqlParserTester.cs @@ -1,312 +1,312 @@ -using System; -using model; -using NUnit.Framework; - -namespace test { - [TestFixture] - public class DBHelperTester { - [Test] - public void CanParseCommentBeforeGoStatement() { - const string script = @"SELECT FOO -/*TEST*/ GO -BAR"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(2, scripts.Length); - } - - [Test] - public void CanParseCommentWithQuoteChar() { - const string script = @"/* Add the Url column to the subtext_Log table if it doesn't exist */ - ADD [Url] VARCHAR(255) NULL -GO - AND COLUMN_NAME = 'BlogGroup') IS NULL"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(2, scripts.Length); - } - - [Test] - public void CanParseDashDashCommentWithQuoteChar() { - const string script = @"-- Add the Url column to the subtext_Log table if it doesn't exist -SELECT * FROM BLAH -GO -PRINT 'FOO'"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(2, scripts.Length); - } - - [Test] - public void CanParseGoWithDashDashCommentAfter() { - const string script = @"SELECT * FROM foo; - GO -- Hello Phil -CREATE PROCEDURE dbo.Test AS SELECT * FROM foo"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(2, scripts.Length); - } - - [Test] - public void CanParseLineEndingInDashDashComment() { - const string script = @"SELECT * FROM BLAH -- Comment -GO -FOOBAR -GO"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(2, scripts.Length); - } - - [Test] - public void CanParseNestedComments() { - const string script = @"/* -select 1 -/* nested comment */ -go -delete from users --- */"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(1, scripts.Length, "This contains a comment and no scripts."); - } - - [Test] - public void CanParseQuotedCorrectly() { - const string script = @"INSERT INTO #Indexes - EXEC sp_helpindex 'dbo.subtext_URLs'"; - - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(script, scripts[0], "Script text should not be modified"); - } - - [Test] - public void CanParseSimpleScript() { - string script = "Test" + Environment.NewLine + "go"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(1, scripts.Length); - Assert.AreEqual("Test" + Environment.NewLine, scripts[0]); - } - - [Test] - public void CanParseSimpleScriptEndingInNewLine() { - string script = "Test" + Environment.NewLine + "GO" + Environment.NewLine; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(1, scripts.Length); - Assert.AreEqual("Test" + Environment.NewLine, scripts[0]); - } - - [Test] - public void CanParseSuccessiveGoStatements() { - const string script = @"GO -GO"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(0, scripts.Length, "Expected no scripts since they would be empty."); - } - - [Test] - public void MultiLineQuoteShouldNotBeSplitByGoKeyword() { - string script = "PRINT '" + Environment.NewLine - + "GO" + Environment.NewLine - + "SELECT * FROM BLAH" + Environment.NewLine - + "GO" + Environment.NewLine - + "'"; - - string[] scripts = DBHelper.SplitBatchSql(script); - - Assert.AreEqual(script, scripts[0]); - Assert.AreEqual(1, scripts.Length, "expected only one script"); - } - - [Test] - public void MultiLineQuoteShouldNotIgnoreDoubleQuote() { - string script = "PRINT '" + Environment.NewLine - + "''" + Environment.NewLine - + "GO" + Environment.NewLine - + "/*" + Environment.NewLine - + "GO" - + "'"; - - string[] scripts = DBHelper.SplitBatchSql(script); - - Assert.AreEqual(1, scripts.Length); - Assert.AreEqual(script, scripts[0]); - } - - /// - /// Makes sure that ParseScript parses correctly. - /// - [Test] - public void ParseScriptParsesCorrectly() { - const string script = @"SET QUOTED_IDENTIFIER OFF --- Comment -Go - -SET ANSI_NULLS ON - - -GO - -GO - - -SET ANSI_NULLS ON - - -CREATE TABLE [].[blog_Gost] ( - [HostUserName] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , - [Password] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , - [Salt] [nvarchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , - [DateCreated] [datetime] NOT NULL -) ON [PRIMARY] -gO - -"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(3, scripts.Length, "This should parse to three scripts."); - //for (int i = 0; i < scripts.Length; i++) { - // Script sqlScript = scripts[i]; - // Assert.IsFalse(sqlScript.ScriptText.StartsWith("GO"), "Script '" + i + "' failed had a GO statement"); - //} - - //string expectedThirdScriptBeginning = "SET ANSI_NULLS ON " - // + Environment.NewLine - // + Environment.NewLine - // + Environment.NewLine + - // "CREATE TABLE [].[blog_Gost]"; - - //Assert.AreEqual(expectedThirdScriptBeginning, - // scripts[2].OriginalScriptText.Substring(0, expectedThirdScriptBeginning.Length), - // "Script not parsed correctly"); - - //scripts.TemplateParameters.SetValue("username", "haacked"); - - //expectedThirdScriptBeginning = "SET ANSI_NULLS ON " - // + Environment.NewLine - // + Environment.NewLine - // + Environment.NewLine + "CREATE TABLE [haacked].[blog_Gost]"; - - //Assert.AreEqual(expectedThirdScriptBeginning, - // scripts[2].ScriptText.Substring(0, expectedThirdScriptBeginning.Length), - // "Script not parsed correctly"); - } - - [Test] - public void SemiColonDoesNotSplitScript() { - const string script = "CREATE PROC Blah AS SELECT FOO; SELECT Bar;"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(1, scripts.Length, "Expected no scripts since they would be empty."); - } - - [Test] - public void SlashStarCommentAfterGoThrowsException() { - const string script = @"PRINT 'blah' -GO /* blah */"; - - Assert.AreEqual(2, DBHelper.SplitBatchSql(script).Length); - //why should this throw an exception? - //UnitTestHelper.AssertThrows(() => DBHelper.SplitBatchSql(script)); - } - - [Test] - public void TestCommentFollowingGO() { - string[] scripts = DBHelper.SplitBatchSql("/*script1*/GO/*script2*/"); - Assert.AreEqual(2, scripts.Length); - Assert.AreEqual("/*script1*/", scripts[0]); - Assert.AreEqual("/*script2*/", scripts[1]); - } - - [Test] - public void TestCommentPrecedingGO() { - string[] scripts = DBHelper.SplitBatchSql("/*script1*/GO--script2"); - Assert.AreEqual(2, scripts.Length); - Assert.AreEqual("/*script1*/", scripts[0]); - Assert.AreEqual("--script2", scripts[1]); - } - - [Test] - public void TestLeadingTablsDashDash() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql("\t\t\t\t-- comment"); - Assert.AreEqual("\t\t\t\t-- comment", scripts[0]); - } - - [Test] - public void TestLeadingTabsParameter() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql("\t\t\t\t@param"); - Assert.AreEqual("\t\t\t\t@param", scripts[0]); - } - - [Test] - public void TestLeadingTabsSingleQuotes() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql("\t\t\t\t'AddProjectToSourceSafe',"); - Assert.AreEqual("\t\t\t\t'AddProjectToSourceSafe',", scripts[0]); - } - - [Test] - public void TestLeadingTabsSlashStar() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql("\t\t\t\t/* comment */"); - Assert.AreEqual("\t\t\t\t/* comment */", scripts[0]); - } - - [Test] - public void TestScriptWithGOTO() { - string script = @"script 1 -GO -script 2 -GOTO <-- not a GO <-- niether is this -NOGO <-- also not a GO <-- still no -"; - string[] scripts = DBHelper.SplitBatchSql(script); - Assert.AreEqual(2, scripts.Length); - } - - [Test] - public void TestSplitGOInComment() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql(@" +using System; +using model; +using NUnit.Framework; + +namespace test { + [TestFixture] + public class BatchSqlParserTester { + [Test] + public void CanParseCommentBeforeGoStatement() { + const string script = @"SELECT FOO +/*TEST*/ GO +BAR"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(2, scripts.Length); + } + + [Test] + public void CanParseCommentWithQuoteChar() { + const string script = @"/* Add the Url column to the subtext_Log table if it doesn't exist */ + ADD [Url] VARCHAR(255) NULL +GO + AND COLUMN_NAME = 'BlogGroup') IS NULL"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(2, scripts.Length); + } + + [Test] + public void CanParseDashDashCommentWithQuoteChar() { + const string script = @"-- Add the Url column to the subtext_Log table if it doesn't exist +SELECT * FROM BLAH +GO +PRINT 'FOO'"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(2, scripts.Length); + } + + [Test] + public void CanParseGoWithDashDashCommentAfter() { + const string script = @"SELECT * FROM foo; + GO -- Hello Phil +CREATE PROCEDURE dbo.Test AS SELECT * FROM foo"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(2, scripts.Length); + } + + [Test] + public void CanParseLineEndingInDashDashComment() { + const string script = @"SELECT * FROM BLAH -- Comment +GO +FOOBAR +GO"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(2, scripts.Length); + } + + [Test] + public void CanParseNestedComments() { + const string script = @"/* +select 1 +/* nested comment */ +go +delete from users +-- */"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(1, scripts.Length, "This contains a comment and no scripts."); + } + + [Test] + public void CanParseQuotedCorrectly() { + const string script = @"INSERT INTO #Indexes + EXEC sp_helpindex 'dbo.subtext_URLs'"; + + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(script, scripts[0], "Script text should not be modified"); + } + + [Test] + public void CanParseSimpleScript() { + string script = "Test" + Environment.NewLine + "go"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(1, scripts.Length); + Assert.AreEqual("Test" + Environment.NewLine, scripts[0]); + } + + [Test] + public void CanParseSimpleScriptEndingInNewLine() { + string script = "Test" + Environment.NewLine + "GO" + Environment.NewLine; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(1, scripts.Length); + Assert.AreEqual("Test" + Environment.NewLine, scripts[0]); + } + + [Test] + public void CanParseSuccessiveGoStatements() { + const string script = @"GO +GO"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(0, scripts.Length, "Expected no scripts since they would be empty."); + } + + [Test] + public void MultiLineQuoteShouldNotBeSplitByGoKeyword() { + string script = "PRINT '" + Environment.NewLine + + "GO" + Environment.NewLine + + "SELECT * FROM BLAH" + Environment.NewLine + + "GO" + Environment.NewLine + + "'"; + + string[] scripts = BatchSqlParser.SplitBatch(script); + + Assert.AreEqual(script, scripts[0]); + Assert.AreEqual(1, scripts.Length, "expected only one script"); + } + + [Test] + public void MultiLineQuoteShouldNotIgnoreDoubleQuote() { + string script = "PRINT '" + Environment.NewLine + + "''" + Environment.NewLine + + "GO" + Environment.NewLine + + "/*" + Environment.NewLine + + "GO" + + "'"; + + string[] scripts = BatchSqlParser.SplitBatch(script); + + Assert.AreEqual(1, scripts.Length); + Assert.AreEqual(script, scripts[0]); + } + + /// + /// Makes sure that ParseScript parses correctly. + /// + [Test] + public void ParseScriptParsesCorrectly() { + const string script = @"SET QUOTED_IDENTIFIER OFF +-- Comment +Go + +SET ANSI_NULLS ON + + +GO + +GO + + +SET ANSI_NULLS ON + + +CREATE TABLE [].[blog_Gost] ( + [HostUserName] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , + [Password] [nvarchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , + [Salt] [nvarchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , + [DateCreated] [datetime] NOT NULL +) ON [PRIMARY] +gO + +"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(3, scripts.Length, "This should parse to three scripts."); + //for (int i = 0; i < scripts.Length; i++) { + // Script sqlScript = scripts[i]; + // Assert.IsFalse(sqlScript.ScriptText.StartsWith("GO"), "Script '" + i + "' failed had a GO statement"); + //} + + //string expectedThirdScriptBeginning = "SET ANSI_NULLS ON " + // + Environment.NewLine + // + Environment.NewLine + // + Environment.NewLine + + // "CREATE TABLE [].[blog_Gost]"; + + //Assert.AreEqual(expectedThirdScriptBeginning, + // scripts[2].OriginalScriptText.Substring(0, expectedThirdScriptBeginning.Length), + // "Script not parsed correctly"); + + //scripts.TemplateParameters.SetValue("username", "haacked"); + + //expectedThirdScriptBeginning = "SET ANSI_NULLS ON " + // + Environment.NewLine + // + Environment.NewLine + // + Environment.NewLine + "CREATE TABLE [haacked].[blog_Gost]"; + + //Assert.AreEqual(expectedThirdScriptBeginning, + // scripts[2].ScriptText.Substring(0, expectedThirdScriptBeginning.Length), + // "Script not parsed correctly"); + } + + [Test] + public void SemiColonDoesNotSplitScript() { + const string script = "CREATE PROC Blah AS SELECT FOO; SELECT Bar;"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(1, scripts.Length, "Expected no scripts since they would be empty."); + } + + [Test] + public void SlashStarCommentAfterGoThrowsException() { + const string script = @"PRINT 'blah' +GO /* blah */"; + + Assert.AreEqual(2, BatchSqlParser.SplitBatch(script).Length); + //why should this throw an exception? + //UnitTestHelper.AssertThrows(() => BatchSqlParser.SplitBatch(script)); + } + + [Test] + public void TestCommentFollowingGO() { + string[] scripts = BatchSqlParser.SplitBatch("/*script1*/GO/*script2*/"); + Assert.AreEqual(2, scripts.Length); + Assert.AreEqual("/*script1*/", scripts[0]); + Assert.AreEqual("/*script2*/", scripts[1]); + } + + [Test] + public void TestCommentPrecedingGO() { + string[] scripts = BatchSqlParser.SplitBatch("/*script1*/GO--script2"); + Assert.AreEqual(2, scripts.Length); + Assert.AreEqual("/*script1*/", scripts[0]); + Assert.AreEqual("--script2", scripts[1]); + } + + [Test] + public void TestLeadingTablsDashDash() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch("\t\t\t\t-- comment"); + Assert.AreEqual("\t\t\t\t-- comment", scripts[0]); + } + + [Test] + public void TestLeadingTabsParameter() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch("\t\t\t\t@param"); + Assert.AreEqual("\t\t\t\t@param", scripts[0]); + } + + [Test] + public void TestLeadingTabsSingleQuotes() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch("\t\t\t\t'AddProjectToSourceSafe',"); + Assert.AreEqual("\t\t\t\t'AddProjectToSourceSafe',", scripts[0]); + } + + [Test] + public void TestLeadingTabsSlashStar() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch("\t\t\t\t/* comment */"); + Assert.AreEqual("\t\t\t\t/* comment */", scripts[0]); + } + + [Test] + public void TestScriptWithGOTO() { + string script = @"script 1 +GO +script 2 +GOTO <-- not a GO <-- niether is this +NOGO <-- also not a GO <-- still no +"; + string[] scripts = BatchSqlParser.SplitBatch(script); + Assert.AreEqual(2, scripts.Length); + } + + [Test] + public void TestSplitGOInComment() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch(@" 1:1 -- GO eff yourself 1:2 -"); - //shoud be 1 script - Assert.AreEqual(1, scripts.Length); - } - - [Test] - public void TestSplitGOInQuotes() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql(@" +"); + //shoud be 1 script + Assert.AreEqual(1, scripts.Length); + } + + [Test] + public void TestSplitGOInQuotes() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch(@" 1:1 ' GO ' 1:2 -"); - //should be 1 script - Assert.AreEqual(1, scripts.Length); - } - - [Test] - public void TestSplitGONoEndLine() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql(@" +"); + //should be 1 script + Assert.AreEqual(1, scripts.Length); + } + + [Test] + public void TestSplitGONoEndLine() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch(@" 1:1 1:2 -GO"); - //should be 1 script with no 'GO' - Assert.AreEqual(1, scripts.Length); - Assert.IsFalse(scripts[0].Contains("GO")); - } - - [Test] - public void TestSplitMultipleGOs() { - string[] scripts = null; - scripts = DBHelper.SplitBatchSql(@" +GO"); + //should be 1 script with no 'GO' + Assert.AreEqual(1, scripts.Length); + Assert.IsFalse(scripts[0].Contains("GO")); + } + + [Test] + public void TestSplitMultipleGOs() { + string[] scripts = null; + scripts = BatchSqlParser.SplitBatch(@" 1:1 GO GO GO GO 2:1 -"); - //should be 2 scripts - Assert.AreEqual(2, scripts.Length); - } - } +"); + //should be 2 scripts + Assert.AreEqual(2, scripts.Length); + } + } } \ No newline at end of file diff --git a/test/test.csproj b/test/test.csproj index f47e0103..bcd1fc7b 100644 --- a/test/test.csproj +++ b/test/test.csproj @@ -90,7 +90,7 @@ - +