|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Semicolon-less statement sequences: SQL Server accepts one statement |
| 7 | +/// immediately followed by another with no separating <c>;</c>, and SSMS |
| 8 | +/// generates such batches. The simulator recognizes the second statement's |
| 9 | +/// leading keyword through a single shared predicate |
| 10 | +/// (<c>Simulation.IsStatementBoundary</c>) that every consumer — the dispatch |
| 11 | +/// loop, the SELECT projection-list terminator, the EXEC-argument scanner, and |
| 12 | +/// the principal-DDL parse-and-discard tail — routes through, so the full |
| 13 | +/// statement-keyword set (including <c>EXEC</c>/<c>EXECUTE</c>, <c>USE</c>, |
| 14 | +/// <c>GRANT</c>, <c>FETCH</c>, …) terminates the preceding statement uniformly. |
| 15 | +/// The cross-product below pairs each kind of preceding statement with each |
| 16 | +/// kind of following statement, joined by a newline only, and proves the batch |
| 17 | +/// parses and runs to the trailing sentinel <c>SELECT 42</c>. |
| 18 | +/// </summary> |
| 19 | +[TestClass] |
| 20 | +public sealed class StatementBoundaryTests |
| 21 | +{ |
| 22 | + // Shared prelude gives every combination a table, a declared+opened |
| 23 | + // cursor, and an @rc slot so FETCH / GRANT / EXEC @rc all have targets. |
| 24 | + // The load-bearing junction is the newline-only join between the |
| 25 | + // preceding and following statements — no `;` separates them. The trailing |
| 26 | + // SELECT 42 sentinel is read from the LAST result set (a following such as |
| 27 | + // SELECT / FETCH emits its own result set first), proving the whole batch |
| 28 | + // parsed and ran to completion; pre-fix these threw Msg 102. |
| 29 | + private static int RunPair(string preceding, string following) |
| 30 | + { |
| 31 | + using var reader = new Simulation().ExecuteReader($""" |
| 32 | + create table t (id int not null); |
| 33 | + insert into t values (1); |
| 34 | + declare @rc int; |
| 35 | + declare c cursor for select id from t; |
| 36 | + open c; |
| 37 | + {preceding} |
| 38 | + {following}; |
| 39 | + select 42 |
| 40 | + """); |
| 41 | + object? last = null; |
| 42 | + do |
| 43 | + { |
| 44 | + while (reader.Read()) |
| 45 | + last = reader[0]; |
| 46 | + } |
| 47 | + while (reader.NextResult()); |
| 48 | + return (int)last!; |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + [DataRow("select 1")] |
| 53 | + [DataRow("declare @x int")] |
| 54 | + [DataRow("if 1 = 1 select 1")] |
| 55 | + [DataRow("set nocount on")] |
| 56 | + [DataRow("insert into t values (2)")] |
| 57 | + public void FollowedByExecProc_NoSemicolon_Parses(string preceding) |
| 58 | + => AreEqual(42, RunPair(preceding, "exec xp_qv N'a', N'b'")); |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + [DataRow("select 1")] |
| 62 | + [DataRow("declare @x int")] |
| 63 | + [DataRow("if 1 = 1 select 1")] |
| 64 | + [DataRow("set nocount on")] |
| 65 | + [DataRow("insert into t values (2)")] |
| 66 | + public void FollowedByExecReturnCapture_NoSemicolon_Parses(string preceding) |
| 67 | + => AreEqual(42, RunPair(preceding, "exec @rc = xp_qv N'a', N'b'")); |
| 68 | + |
| 69 | + [TestMethod] |
| 70 | + [DataRow("select 1")] |
| 71 | + [DataRow("declare @x int")] |
| 72 | + [DataRow("if 1 = 1 select 1")] |
| 73 | + [DataRow("set nocount on")] |
| 74 | + [DataRow("insert into t values (2)")] |
| 75 | + public void FollowedByUse_NoSemicolon_Parses(string preceding) |
| 76 | + => AreEqual(42, RunPair(preceding, "use master")); |
| 77 | + |
| 78 | + [TestMethod] |
| 79 | + [DataRow("select 1")] |
| 80 | + [DataRow("declare @x int")] |
| 81 | + [DataRow("if 1 = 1 select 1")] |
| 82 | + [DataRow("set nocount on")] |
| 83 | + [DataRow("insert into t values (2)")] |
| 84 | + public void FollowedByGrant_NoSemicolon_Parses(string preceding) |
| 85 | + => AreEqual(42, RunPair(preceding, "grant select on t to public")); |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + [DataRow("select 1")] |
| 89 | + [DataRow("declare @x int")] |
| 90 | + [DataRow("if 1 = 1 select 1")] |
| 91 | + [DataRow("set nocount on")] |
| 92 | + [DataRow("insert into t values (2)")] |
| 93 | + public void FollowedByFetch_NoSemicolon_Parses(string preceding) |
| 94 | + => AreEqual(42, RunPair(preceding, "fetch c")); |
| 95 | + |
| 96 | + [TestMethod] |
| 97 | + [DataRow("select 1")] |
| 98 | + [DataRow("declare @x int")] |
| 99 | + [DataRow("if 1 = 1 select 1")] |
| 100 | + [DataRow("set nocount on")] |
| 101 | + [DataRow("insert into t values (2)")] |
| 102 | + public void FollowedBySelect_NoSemicolon_Parses(string preceding) |
| 103 | + => AreEqual(42, RunPair(preceding, "select 1")); |
| 104 | + |
| 105 | + // WITH after a SELECT projection stays the specific Msg 319 ("Incorrect |
| 106 | + // syntax near … expects a preceding statement terminated with a |
| 107 | + // semicolon") rather than being swallowed as a generic boundary — the |
| 108 | + // CTE-continuation case is checked ahead of the shared boundary predicate. |
| 109 | + [TestMethod] |
| 110 | + public void SelectFollowedByCte_WithoutSemicolon_RaisesMsg319() |
| 111 | + => new Simulation().AssertSqlError( |
| 112 | + "select 1 with x as (select 1 as n) select n from x", |
| 113 | + 319); |
| 114 | + |
| 115 | + // The exact SSMS Object-Explorer AlwaysOn availability probe: three |
| 116 | + // statements, only the middle two separated by a semicolon. xp_qv returns |
| 117 | + // status 0 (AlwaysOn not available), so ISNULL(@alwayson, -1) yields 0. |
| 118 | + [TestMethod] |
| 119 | + public void SsmsAlwaysOnProbe_ReturnsZero() |
| 120 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 121 | + DECLARE @alwayson INT |
| 122 | + EXECUTE @alwayson = master.dbo.xp_qv N'3641190370', @@SERVICENAME; |
| 123 | + SELECT ISNULL(@alwayson,-1) AS [AlwaysOn] |
| 124 | + """)); |
| 125 | + |
| 126 | + [TestMethod] |
| 127 | + [DataRow("exec xp_qv N'x', N'y'")] |
| 128 | + [DataRow("exec dbo.xp_qv N'x', N'y'")] |
| 129 | + [DataRow("exec master.dbo.xp_qv N'x', N'y'")] |
| 130 | + public void XpQv_YieldsNoResultSet(string call) |
| 131 | + { |
| 132 | + using var reader = new Simulation().ExecuteReader(call); |
| 133 | + IsFalse(reader.Read()); |
| 134 | + } |
| 135 | + |
| 136 | + [TestMethod] |
| 137 | + public void XpQv_ReturnStatus_IsZero() |
| 138 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 139 | + declare @rc int; |
| 140 | + exec @rc = xp_qv N'x', N'y'; |
| 141 | + select @rc |
| 142 | + """)); |
| 143 | +} |
0 commit comments