Skip to content

Commit c966c21

Browse files
committed
Improved SimulatedSqlException creation consistency.
1 parent d7a8d72 commit c966c21

5 files changed

Lines changed: 9 additions & 15 deletions

File tree

SqlServerSimulator/Parser/ParserContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal sealed class ParserContext(SimulatedDbCommand command)
4949
public object? GetVariableValue(string name) =>
5050
variables.TryGetValue(name, out var value)
5151
? value.TypeValue.Value
52-
: throw new SimulatedSqlException($"Must declare the scalar variable \"@{name}\".");
52+
: throw SimulatedSqlException.MustDeclareScalarVariable(name);
5353

5454
/// <summary>
5555
/// Advances <see cref="Token"/> to the next token, if one exists.

SqlServerSimulator/Parser/Tokenizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static Token ParseAtOrDoubleAtPrefixedString(string command, ref int ind
8888
{
8989
var start = index;
9090
if (++index == command.Length)
91-
throw new SimulatedSqlException("Must declare the scalar variable \"@\".");
91+
throw SimulatedSqlException.MustDeclareScalarVariable(string.Empty);
9292

9393
bool doubleAt;
9494
if (command[index] == '@')

SqlServerSimulator/Parser/Tokens/Numeric.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public Numeric(string command, int index, int length) : base(command, index, len
2626
return;
2727
}
2828

29-
throw new SimulatedSqlException($"Simulated command tokenizer couldn't parse {number} as a number.");
29+
throw new NotSupportedException($"Simulated command tokenizer couldn't parse {number} as a number.");
3030
}
3131
}

SqlServerSimulator/SimulatedSqlException.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@ namespace SqlServerSimulator;
1313
/// </summary>
1414
internal sealed class SimulatedSqlException : DbException
1515
{
16-
internal SimulatedSqlException(string? message)
17-
: this(message, [])
18-
{
19-
}
20-
2116
private SimulatedSqlException(string message, int number, byte @class, byte state)
2217
: this(message, new SimulatedSqlError(message, number, @class, state))
2318
{
2419
}
2520

26-
private SimulatedSqlException(string? message, params ReadOnlySpan<SimulatedSqlError> errors)
27-
: base(message ?? "Simulated exception with no message.")
21+
private SimulatedSqlException(string message, params ReadOnlySpan<SimulatedSqlError> errors)
22+
: base(message)
2823
{
2924
base.HResult = unchecked((int)0x80131904);
3025
base.Source = "Core Microsoft SqlClient Data Provider";
@@ -106,6 +101,8 @@ internal static SimulatedSqlException ColumnReferenceNotAllowed(IEnumerable<stri
106101

107102
internal static SimulatedSqlException InvalidObjectName(StringToken name) => new($"Invalid object name {name}.", 208, 16, 1);
108103

104+
internal static SimulatedSqlException MustDeclareScalarVariable(string name) => new($"Must declare the scalar variable \"@{name}\".", 137, 15, 2);
105+
109106
internal static SimulatedSqlException SyntaxErrorNearKeyword(ReservedKeyword token) => new($"Incorrect syntax near the keyword '{token}'.", 156, 15, 1);
110107

111108
internal static SimulatedSqlException SyntaxErrorNear(Token? token) => new($"Incorrect syntax near '{token}'.", 102, 15, 1);

SqlServerSimulator/Simulation.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ internal IEnumerable<SimulatedStatementOutcome> CreateResultSetsForCommand(Simul
9090
do
9191
{
9292
suppressAdvanceToken = false;
93-
if (context.GetNextRequired() is not Name columnName)
94-
throw new SimulatedSqlException("Simulated table creation requires named columns.");
95-
96-
if (context.GetNextRequired() is not Name type)
97-
throw new SimulatedSqlException("Simulated table creation requires columns to have a type.");
93+
var columnName = context.GetNextRequired<Name>();
94+
var type = context.GetNextRequired<Name>();
9895

9996
var nullable = true;
10097

0 commit comments

Comments
 (0)