Skip to content

Commit 84e8a70

Browse files
committed
Centralized some exception messaging, quality improvement.
1 parent da3ca54 commit 84e8a70

5 files changed

Lines changed: 21 additions & 14 deletions

File tree

SqlServerSimulator/Parser/Expression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public static Expression Parse(Simulation simulation, IEnumerator<Token> tokens,
3434
{
3535
case Keyword.As:
3636
if (expression is null || !tokens.TryMoveNext(out token) || token is not Name alias)
37-
throw new SimulatedSqlException("Incorrect syntax near the keyword 'as'.", 156, 15, 1);
37+
throw SimulatedSqlException.SyntaxErrorNear(name);
3838

3939
expression = new NamedExpression(expression, alias.Value);
4040
_ = tokens.TryMoveNext(out token);
4141
return expression;
4242
case Keyword.From:
4343
if (expression is null)
44-
throw new SimulatedSqlException("Incorrect syntax near the keyword 'from'.", 156, 15, 1);
44+
throw SimulatedSqlException.SyntaxErrorNear(name);
4545

4646
return expression;
4747
}

SqlServerSimulator/Parser/Selection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static Selection Parse(Simulation simulation, IEnumerator<Token> tokens,
3030
case null: // "Select" with no "From".
3131
return new(new(
3232
columnIndexes,
33-
[[.. expressions.Select(x => x.Run(column => throw new SimulatedSqlException($"Invalid column name '{column}'.", 207, 16, 1)))]]
33+
[[.. expressions.Select(x => x.Run(column => throw SimulatedSqlException.InvalidColumnName(column)))]]
3434
));
3535

3636
case UnquotedString unquotedString:
@@ -41,7 +41,7 @@ public static Selection Parse(Simulation simulation, IEnumerator<Token> tokens,
4141
{
4242
case StringToken tableName:
4343
if (!simulation.Tables.TryGetValue(tableName.Value, out var table) && !simulation.SystemTables.Value.TryGetValue(tableName.Value, out table))
44-
throw new SimulatedSqlException($"Invalid object name {tableName}.", 208, 16, 1);
44+
throw SimulatedSqlException.InvalidObjectName(tableName);
4545

4646
if (tokens.TryMoveNext(out token))
4747
{
@@ -61,7 +61,7 @@ public static Selection Parse(Simulation simulation, IEnumerator<Token> tokens,
6161
table.Rows.Select<object?[], object?[]>(row => [..expressions.Select(x => x.Run(columnName =>
6262
{
6363
var columnIndex = table.Columns.FindIndex(column => Collation.Default.Equals(column.Name, columnName.Last()));
64-
return columnIndex == -1 ? throw new SimulatedSqlException($"Invalid column name '{columnName}'.", 207, 16, 1) : row[columnIndex]; }))])));
64+
return columnIndex == -1 ? throw SimulatedSqlException.InvalidColumnName(columnName) : row[columnIndex]; }))])));
6565
}
6666

6767
throw new NotSupportedException($"Simulated selection processor expected a source table, found {token}.");
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace SqlServerSimulator.Parser.Tokens;
22

33
sealed class Comma : Token
4-
#pragma warning restore
54
{
65
public override string ToString() => ",";
76
}

SqlServerSimulator/SimulatedSqlException.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SqlServerSimulator.Parser;
2-
using System.Collections.Immutable;
2+
using SqlServerSimulator.Parser.Tokens;
33
using System.Data.Common;
44
using System.Globalization;
55

@@ -18,25 +18,25 @@ internal SimulatedSqlException(string? message)
1818
{
1919
}
2020

21-
internal SimulatedSqlException(string message, int number, byte @class, byte state)
21+
private SimulatedSqlException(string message, int number, byte @class, byte state)
2222
: this(message, new SimulatedSqlError(message, number, @class, state))
2323
{
2424
}
2525

26-
internal SimulatedSqlException(string? message, params ReadOnlySpan<SimulatedSqlError> errors)
26+
private SimulatedSqlException(string? message, params ReadOnlySpan<SimulatedSqlError> errors)
2727
: base(message ?? "Simulated exception with no message.")
2828
{
2929
base.HResult = unchecked((int)0x80131904);
3030
base.Source = "Core Microsoft SqlClient Data Provider";
3131

3232
if (errors.Length == 0)
3333
{
34-
this.Errors = ImmutableArray.Create([new SimulatedSqlError(base.Message, 0, 0, 0)]);
34+
this.Errors = [new SimulatedSqlError(base.Message, 0, 0, 0)];
3535

3636
return;
3737
}
3838

39-
this.Errors = ImmutableArray.Create(errors);
39+
this.Errors = [.. errors];
4040

4141
var firstError = errors[0];
4242

@@ -84,5 +84,13 @@ internal SimulatedSqlException(string? message, params ReadOnlySpan<SimulatedSql
8484

8585
public IReadOnlyList<SimulatedSqlError> Errors { get; }
8686

87+
internal static SimulatedSqlException InvalidColumnName(string name) => new($"Invalid column name '{name}'.", 207, 16, 1);
88+
89+
internal static SimulatedSqlException InvalidColumnName(IEnumerable<string> name) => InvalidColumnName(string.Join('.', name));
90+
91+
internal static SimulatedSqlException InvalidObjectName(StringToken name) => new($"Invalid object name {name}.", 208, 16, 1);
92+
8793
internal static SimulatedSqlException SyntaxErrorNear(Token token) => new($"Incorrect syntax near '{token}'.", 102, 15, 1);
94+
95+
internal static SimulatedSqlException ThereIsAlreadyAnObject(string name) => new($"There is already an object named '{name}' in the database.", 2714, 16, 6);
8896
}

SqlServerSimulator/Simulation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ internal IEnumerable<SimulatedStatementOutcome> CreateResultSetsForCommand(Simul
209209
break;
210210

211211
if (!this.Tables.TryAdd(table.Name, table))
212-
throw new SimulatedSqlException($"There is already an object named '{table.Name}' in the database.", 2714, 16, 6);
212+
throw SimulatedSqlException.ThereIsAlreadyAnObject(table.Name);
213213

214214
continue;
215215
}
@@ -229,7 +229,7 @@ internal IEnumerable<SimulatedStatementOutcome> CreateResultSetsForCommand(Simul
229229
break;
230230

231231
if (!this.Tables.TryGetValue(destinationTableToken.Value, out var destinationTable))
232-
throw new SimulatedSqlException($"Invalid object name '{destinationTableToken.Value}'.", 208, 16, 0);
232+
throw SimulatedSqlException.InvalidObjectName(destinationTableToken);
233233

234234
Column[] destinationColumns;
235235
if ((token = tokens.RequireNext()) is OpenParentheses)
@@ -239,7 +239,7 @@ internal IEnumerable<SimulatedStatementOutcome> CreateResultSetsForCommand(Simul
239239
{
240240
var columnName = column.Value;
241241
var tableColumn = destinationTable.Columns.FirstOrDefault(c => Collation.Default.Equals(c.Name, columnName))
242-
?? throw new SimulatedSqlException($"Invalid column name '{columnName}'.", 207, 16, 1);
242+
?? throw SimulatedSqlException.InvalidColumnName(columnName);
243243
usedColumns.Add(tableColumn);
244244
}
245245

0 commit comments

Comments
 (0)