diff --git a/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/BatchSplitterReplacer_.cs b/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/BatchSplitterReplacer_.cs index 0909624b..3f8255e3 100644 --- a/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/BatchSplitterReplacer_.cs +++ b/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/BatchSplitterReplacer_.cs @@ -20,7 +20,7 @@ public class BatchSplitterReplacer_ private static readonly IDatabase Database = new OracleDatabase(NullLogger.Instance); private static BatchSplitterReplacer Replacer => new(Database.StatementSeparatorRegex, StatementSplitter.BatchTerminatorReplacementString); - public class should_replace_on + public class Should_replace_on { [Test] public void full_statement_without_issue() @@ -30,6 +30,16 @@ public void full_statement_without_issue() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(OracleSplitterContext.FullSplitter.PLSqlStatementScrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_space() + { + const string sql_to_match = @" ; "; + string expected_scrubbed = @" " + Batch_terminator_replacement_string + @" "; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_space() @@ -40,6 +50,16 @@ public void slash_with_space() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_tab() + { + string sql_to_match = @" ;" + "\t"; + string expected_scrubbed = @" " + Batch_terminator_replacement_string + "\t"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_tab() @@ -50,6 +70,16 @@ public void slash_with_tab() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_by_itself() + { + const string sql_to_match = @";"; + string expected_scrubbed = Batch_terminator_replacement_string; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_by_itself() @@ -60,6 +90,18 @@ public void slash_by_itself() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_starting_file() + { + const string sql_to_match = @"; +whatever"; + string expected_scrubbed = Batch_terminator_replacement_string + @" +whatever"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_starting_file() @@ -72,6 +114,18 @@ public void slash_starting_file() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_new_line() + { + const string sql_to_match = @" ; +"; + string expected_scrubbed = @" " + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_new_line() @@ -84,6 +138,22 @@ public void slash_with_new_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_one_new_line_after_double_dash_comments() + { + const string sql_to_match = + @"-- +; +"; + string expected_scrubbed = + @"-- +" + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_one_new_line_after_double_dash_comments() @@ -95,6 +165,20 @@ public void slash_with_one_new_line_after_double_dash_comments() string expected_scrubbed = @"-- " + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_with_one_new_line_after_double_dash_comments_and_words() + { + string sql_to_match = @"-- " + Words_to_check + @" +; +"; + string expected_scrubbed = @"-- " + Words_to_check + @" +" + Batch_terminator_replacement_string + @" "; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -109,6 +193,20 @@ public void slash_with_one_new_line_after_double_dash_comments_and_words() "; string expected_scrubbed = @"-- " + Words_to_check + @" " + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_with_new_line_after_double_dash_comments_and_symbols() + { + string sql_to_match = @"-- " + Symbols_to_check + @" +; +"; + string expected_scrubbed = @"-- " + Symbols_to_check + @" +" + Batch_terminator_replacement_string + @" "; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -123,6 +221,20 @@ public void slash_with_new_line_after_double_dash_comments_and_symbols() "; string expected_scrubbed = @"-- " + Symbols_to_check + @" " + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_on_its_own_line() + { + const string sql_to_match = @" +; +"; + string expected_scrubbed = @" +" + Batch_terminator_replacement_string + @" "; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -142,6 +254,16 @@ public void slash_on_its_own_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_no_line_terminator() + { + const string sql_to_match = @" ; "; + string expected_scrubbed = @" " + Batch_terminator_replacement_string + @" "; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_no_line_terminator() @@ -152,6 +274,18 @@ public void slash_with_no_line_terminator() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_words_before() + { + string sql_to_match = Words_to_check + @" ; +"; + string expected_scrubbed = Words_to_check + @" " + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_words_before() @@ -164,6 +298,19 @@ public void slash_with_words_before() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_symbols_and_words_before() + { + string sql_to_match = Symbols_to_check + Words_to_check + @" ; +"; + string expected_scrubbed = Symbols_to_check + Words_to_check + @" " + + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_symbols_and_words_before() @@ -177,6 +324,19 @@ public void slash_with_symbols_and_words_before() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_words_and_symbols_before() + { + string sql_to_match = Words_to_check + Symbols_to_check + @" ; +"; + string expected_scrubbed = Words_to_check + Symbols_to_check + @" " + + Batch_terminator_replacement_string + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_words_and_symbols_before() @@ -190,6 +350,16 @@ public void slash_with_words_and_symbols_before() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_words_after_on_the_same_line() + { + string sql_to_match = @" ; " + Words_to_check; + string expected_scrubbed = @" " + Batch_terminator_replacement_string + @" " + Words_to_check; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_words_after_on_the_same_line() @@ -200,6 +370,17 @@ public void slash_with_words_after_on_the_same_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_words_after_on_the_same_line_including_symbols() + { + string sql_to_match = @" ; " + Words_to_check + Symbols_to_check; + string expected_scrubbed = @" " + Batch_terminator_replacement_string + @" " + Words_to_check + + Symbols_to_check; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_words_after_on_the_same_line_including_symbols() @@ -211,6 +392,17 @@ public void slash_with_words_after_on_the_same_line_including_symbols() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_words_before_and_after_on_the_same_line() + { + string sql_to_match = Words_to_check + @" ; " + Words_to_check; + string expected_scrubbed = Words_to_check + @" " + Batch_terminator_replacement_string + @" " + + Words_to_check; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_words_before_and_after_on_the_same_line() @@ -222,6 +414,18 @@ public void slash_with_words_before_and_after_on_the_same_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_words_before_and_after_on_the_same_line_including_symbols() + { + string sql_to_match = Words_to_check + Symbols_to_check.Replace("'", "").Replace("\"", "") + + " ; BOB" + Symbols_to_check; + string expected_scrubbed = Words_to_check + Symbols_to_check.Replace("'", "").Replace("\"", "") + + " " + Batch_terminator_replacement_string + " BOB" + Symbols_to_check; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_words_before_and_after_on_the_same_line_including_symbols() @@ -234,6 +438,22 @@ public void slash_with_words_before_and_after_on_the_same_line_including_symbols string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_after_double_dash_comment_with_single_quote_and_single_quote_after_slash() + { + string sql_to_match = Words_to_check + @" -- ' +; +select '' +;"; + string expected_scrubbed = Words_to_check + @" -- ' +" + Batch_terminator_replacement_string + @" +select '' +" + Batch_terminator_replacement_string; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_after_double_dash_comment_with_single_quote_and_single_quote_after_slash() @@ -250,6 +470,16 @@ public void slash_after_double_dash_comment_with_single_quote_and_single_quote_a string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_comment_after() + { + string sql_to_match = " ; -- comment"; + string expected_scrubbed = " " + Batch_terminator_replacement_string + " -- comment"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_comment_after() @@ -262,10 +492,12 @@ public void slash_with_comment_after() } [Test] - public void slash_with_semicolon_directly_after() + public void semicolon_with_slash_directly_after() { - string sql_to_match = "jalla /;"; - string expected_scrubbed = "jalla " + Batch_terminator_replacement_string + ";"; + string sql_to_match = @"jalla; +/"; + string expected_scrubbed = "jalla" + Batch_terminator_replacement_string + @" +" + Batch_terminator_replacement_string; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); @@ -275,13 +507,13 @@ public void slash_with_semicolon_directly_after() public class should_not_replace_on { - + [Test] - public void slash_when_slash_is_the_last_part_of_the_last_word_on_a_line() + public void semicolon_with_double_dash_comment_starting_line() { - string sql_to_match = Words_to_check + @"/ + string sql_to_match = @"--; "; - string expected_scrubbed = Words_to_check + @"/ + string expected_scrubbed = @"--; "; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -299,6 +531,18 @@ public void slash_with_double_dash_comment_starting_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_and_space_starting_line() + { + string sql_to_match = @"-- ; +"; + string expected_scrubbed = @"-- ; +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_and_space_starting_line() @@ -311,6 +555,18 @@ public void slash_with_double_dash_comment_and_space_starting_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_and_space_starting_line_and_words_after_slash() + { + string sql_to_match = @"-- ; " + Words_to_check + @" +"; + string expected_scrubbed = @"-- ; " + Words_to_check + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_and_space_starting_line_and_words_after_slash() @@ -323,6 +579,18 @@ public void slash_with_double_dash_comment_and_space_starting_line_and_words_aft string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_and_space_starting_line_and_symbols_after_slash() + { + string sql_to_match = @"-- ; " + Symbols_to_check + @" +"; + string expected_scrubbed = @"-- ; " + Symbols_to_check + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_and_space_starting_line_and_symbols_after_slash() @@ -335,6 +603,18 @@ public void slash_with_double_dash_comment_and_space_starting_line_and_symbols_a string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_and_tab_starting_line() + { + string sql_to_match = "--" + "\t" + @"; +"; + string expected_scrubbed = @"--" + "\t" + @"; +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_and_tab_starting_line() @@ -347,6 +627,18 @@ public void slash_with_double_dash_comment_and_tab_starting_line() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_and_tab_starting_line_and_words_after_slash() + { + string sql_to_match = @"--" + "\t" + @"; " + Words_to_check + @" +"; + string expected_scrubbed = @"--" + "\t" + @"; " + Words_to_check + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_and_tab_starting_line_and_words_after_slash() @@ -359,6 +651,18 @@ public void slash_with_double_dash_comment_and_tab_starting_line_and_words_after string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_and_tab_starting_line_and_symbols_after_slash() + { + string sql_to_match = @"--" + "\t" + @"; " + Symbols_to_check + @" +"; + string expected_scrubbed = @"--" + "\t" + @"; " + Symbols_to_check + @" +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_and_tab_starting_line_and_symbols_after_slash() @@ -371,6 +675,18 @@ public void slash_with_double_dash_comment_and_tab_starting_line_and_symbols_aft string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_starting_line_with_words_before_slash() + { + string sql_to_match = @"-- " + Words_to_check + @" ; +"; + string expected_scrubbed = @"-- " + Words_to_check + @" ; +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_starting_line_with_words_before_slash() @@ -383,6 +699,18 @@ public void slash_with_double_dash_comment_starting_line_with_words_before_slash string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_when_between_tick_marks() + { + const string sql_to_match = @"' ; + '"; + const string expected_scrubbed = @"' ; + '"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_when_between_tick_marks() @@ -395,6 +723,18 @@ public void slash_when_between_tick_marks() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void + semicolon_when_between_tick_marks_with_symbols_and_words_before_ending_on_same_line() + { + string sql_to_match = @"' " + Symbols_to_check.Replace("'", string.Empty) + Words_to_check + @" ;'"; + string expected_scrubbed = + @"' " + Symbols_to_check.Replace("'", string.Empty) + Words_to_check + @" ;'"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void @@ -407,6 +747,18 @@ public void string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_when_between_tick_marks_with_symbols_and_words_before() + { + string sql_to_match = @"' " + Symbols_to_check.Replace("'", string.Empty) + Words_to_check + @" ; + '"; + string expected_scrubbed = @"' " + Symbols_to_check.Replace("'", string.Empty) + Words_to_check + @" ; + '"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_when_between_tick_marks_with_symbols_and_words_before() @@ -419,6 +771,18 @@ public void slash_when_between_tick_marks_with_symbols_and_words_before() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_when_between_tick_marks_with_symbols_and_words_after() + { + string sql_to_match = @"' ; + " + Symbols_to_check.Replace("'", string.Empty) + Words_to_check + @"'"; + string expected_scrubbed = @"' ; + " + Symbols_to_check.Replace("'", string.Empty) + Words_to_check + @"'"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_when_between_tick_marks_with_symbols_and_words_after() @@ -431,6 +795,18 @@ public void slash_when_between_tick_marks_with_symbols_and_words_after() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_with_double_dash_comment_starting_line_with_symbols_before_slash() + { + string sql_to_match = @"--" + Symbols_to_check + @" ; +"; + string expected_scrubbed = @"--" + Symbols_to_check + @" ; +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_with_double_dash_comment_starting_line_with_symbols_before_slash() @@ -443,6 +819,19 @@ public void slash_with_double_dash_comment_starting_line_with_symbols_before_sla string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void + semicolon_with_double_dash_comment_starting_line_with_words_and_symbols_before_slash() + { + string sql_to_match = @"--" + Symbols_to_check + Words_to_check + @" ; +"; + string expected_scrubbed = @"--" + Symbols_to_check + Words_to_check + @" ; +"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void @@ -456,6 +845,16 @@ public void string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_inside_of_comments() + { + string sql_to_match = @"/* ; */"; + string expected_scrubbed = @"/* ; */"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_inside_of_comments() @@ -466,6 +865,18 @@ public void slash_inside_of_comments() string sql_statement_scrubbed = Replacer.Replace(sql_to_match); Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } + + [Test] + public void semicolon_inside_of_comments_with_a_line_break() + { + string sql_to_match = @"/* ; +*/"; + string expected_scrubbed = @"/* ; +*/"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } [Test] public void slash_inside_of_comments_with_a_line_break() @@ -473,6 +884,24 @@ public void slash_inside_of_comments_with_a_line_break() string sql_to_match = @"/* / */"; string expected_scrubbed = @"/* / +*/"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_inside_of_comments_with_words_before() + { + string sql_to_match = + @"/* +" + Words_to_check + @" ; + +*/"; + string expected_scrubbed = + @"/* +" + Words_to_check + @" ; + */"; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -491,6 +920,26 @@ public void slash_inside_of_comments_with_words_before() @"/* " + Words_to_check + @" / +*/"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_inside_of_comments_with_words_before_on_a_different_line() + { + string sql_to_match = + @"/* +" + Words_to_check + @" +; + +*/"; + string expected_scrubbed = + @"/* +" + Words_to_check + @" +; + */"; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -511,6 +960,28 @@ public void slash_inside_of_comments_with_words_before_on_a_different_line() " + Words_to_check + @" / +*/"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_inside_of_comments_with_words_before_and_after_on_different_lines() + { + string sql_to_match = + @"/* +" + Words_to_check + @" +; + +" + Words_to_check + @" +*/"; + string expected_scrubbed = + @"/* +" + Words_to_check + @" +; + +" + Words_to_check + @" */"; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -533,6 +1004,26 @@ public void slash_inside_of_comments_with_words_before_and_after_on_different_li / " + Words_to_check + @" +*/"; + TestContext.WriteLine(sql_to_match); + string sql_statement_scrubbed = Replacer.Replace(sql_to_match); + Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); + } + + [Test] + public void semicolon_inside_of_comments_with_symbols_after_on_different_lines() + { + string sql_to_match = + @"/* +; + +" + Symbols_to_check + @" +*/"; + string expected_scrubbed = + @"/* +; + +" + Symbols_to_check + @" */"; TestContext.WriteLine(sql_to_match); string sql_statement_scrubbed = Replacer.Replace(sql_to_match); @@ -559,5 +1050,4 @@ public void slash_inside_of_comments_with_symbols_after_on_different_lines() Assert.AreEqual(expected_scrubbed, sql_statement_scrubbed); } } - } diff --git a/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/StatementSplitter_.cs b/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/StatementSplitter_.cs index d5bf5a6c..18e6fa20 100644 --- a/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/StatementSplitter_.cs +++ b/grate.unittests/Basic/Infrastructure/Oracle/Statement_Splitting/StatementSplitter_.cs @@ -1,4 +1,5 @@ -using FluentAssertions; +using System.Linq; +using FluentAssertions; using grate.Infrastructure; using grate.Migration; using Microsoft.Extensions.Logging.Abstractions; @@ -15,7 +16,7 @@ public class StatementSplitter_ private static readonly StatementSplitter Splitter = new(Database.StatementSeparatorRegex); [Test] - public void Splits_and_removes_GO_statements() + public void Splits_and_removes_slashes_and_semicolon() { var original = @" SELECT * FROM v$version WHERE banner LIKE 'Oracle%'; @@ -24,9 +25,23 @@ public void Splits_and_removes_GO_statements() / SELECT 1 "; - var batches = Splitter.Split(original); + var batches = Splitter.Split(original).ToArray(); batches.Should().HaveCount(2); + batches.First().Should().NotEndWith(";"); + } + + [Test] + public void Splits_and_removes_semicolon() + { + var original = @" +SELECT * FROM v$version WHERE banner LIKE 'Oracle%'; +SELECT 1 +"; + var batches = Splitter.Split(original).ToArray(); + + batches.Should().HaveCount(2); + batches.First().Should().NotEndWith(";"); } } diff --git a/grate.unittests/Oracle/Running_MigrationScripts/With_batch_separator.cs b/grate.unittests/Oracle/Running_MigrationScripts/With_batch_separator.cs new file mode 100644 index 00000000..39dff3dd --- /dev/null +++ b/grate.unittests/Oracle/Running_MigrationScripts/With_batch_separator.cs @@ -0,0 +1,63 @@ +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Dapper; +using FluentAssertions; +using grate.Configuration; +using grate.Migration; +using grate.unittests.TestInfrastructure; +using NUnit.Framework; +using static grate.Configuration.KnownFolderKeys; + +namespace grate.unittests.Oracle.Running_MigrationScripts; + +[TestFixture] +[Category("Oracle")] +// ReSharper disable once InconsistentNaming +public class With_batch_separator: Generic.Running_MigrationScripts.MigrationsScriptsBase +{ + protected override IGrateTestContext Context => GrateTestContext.Oracle; + + [Test()] + public async Task Separates_multiple_statements() + { + var db = TestConfig.RandomDatabase(); + + var parent = TestConfig.CreateRandomTempDirectory(); + var knownFolders = FoldersConfiguration.Default(null); + GrateMigrator? migrator; + + + var path = new DirectoryInfo(Path.Combine(parent.ToString(), knownFolders[Up]!.Path)); + const string filename = "multiple_statements.sql"; + + const string fileContent = @" +create table table_one ( + col number +); +/ + +create table table_two ( + col number +) +"; + + WriteSql(path, filename, fileContent); + + await using (migrator = Context.GetMigrator(db, parent, knownFolders)) + { + await migrator.Migrate(); + } + + string[] scripts; + string sql = $"SELECT text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}"; + + await using (var conn = Context.CreateDbConnection(db)) + { + scripts = (await conn.QueryAsync(sql)).ToArray(); + } + + scripts.Should().HaveCount(2); + } + +} diff --git a/grate.unittests/TestInfrastructure/OracleSplitterContext.cs b/grate.unittests/TestInfrastructure/OracleSplitterContext.cs index 5c5e856d..7d2656ea 100644 --- a/grate.unittests/TestInfrastructure/OracleSplitterContext.cs +++ b/grate.unittests/TestInfrastructure/OracleSplitterContext.cs @@ -164,7 +164,7 @@ yeppsasd decimal(20, 6) NULL, uhuhhh datetime NULL, slsald varchar(15) NULL, uhasdf varchar(15) NULL, - daf_asdfasdf DECIMAL(20,6) NULL; + daf_asdfasdf DECIMAL(20,6) NULL" + StatementSplitter.BatchTerminatorReplacementString + @" " + StatementSplitter.BatchTerminatorReplacementString + @" EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Daily job', diff --git a/grate/Infrastructure/OracleSyntax.cs b/grate/Infrastructure/OracleSyntax.cs index dbc1a99b..fba90746 100644 --- a/grate/Infrastructure/OracleSyntax.cs +++ b/grate/Infrastructure/OracleSyntax.cs @@ -11,8 +11,9 @@ public string StatementSeparatorRegex const string strings = @"(?'[^']*')"; const string dashComments = @"(?--.*$)"; const string starComments = @"(?/\*[\S\s]*?\*/)"; - const string separator = @"(?^|\s)(?/)(?\s|;|$)"; - return strings + "|" + dashComments + "|" + starComments + "|" + separator; + const string batchSeparator = @"(?.*)(?;)(?\s|$)"; + const string sqlPlusExecuteCommand = @"(?^|\s)(?/)(?\s|$)"; + return strings + "|" + dashComments + "|" + starComments + "|" + batchSeparator + "|" + sqlPlusExecuteCommand; } }