Skip to content

Commit 381454e

Browse files
committed
Ongoing WIP
1 parent ae112a2 commit 381454e

4 files changed

Lines changed: 71 additions & 13 deletions

File tree

SqlServerSimulator.Tests/PagesBackendIntegrationTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Data;
12
using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
23

34
namespace SqlServerSimulator;
@@ -122,4 +123,51 @@ public void ParameterValue(string commandText, string name, int value)
122123

123124
AreEqual(commandText.EndsWith("+ 1") ? value + 1 : value, result);
124125
}
126+
127+
[TestMethod]
128+
public void AtAtVersion_ReturnsSimulatorString()
129+
=> AreEqual("SQL Server Simulator", ExecuteScalar("select @@version"));
130+
131+
[TestMethod]
132+
[DataRow(DbType.Boolean, true)]
133+
[DataRow(DbType.Boolean, false)]
134+
[DataRow(DbType.Byte, (byte)0)]
135+
[DataRow(DbType.Byte, (byte)200)]
136+
[DataRow(DbType.Int16, (short)-1)]
137+
[DataRow(DbType.Int16, (short)32000)]
138+
[DataRow(DbType.AnsiString, "ansi")]
139+
[DataRow(DbType.String, "unicodeé")]
140+
public void TypedParameter_RoundTripsThroughPagesBackend(DbType dbType, object value)
141+
{
142+
using var connection = new Simulation(StorageBackend.Pages).CreateOpenConnection();
143+
using var command = connection.CreateCommand();
144+
command.CommandText = "select @p";
145+
var parameter = command.CreateParameter();
146+
parameter.ParameterName = "p";
147+
parameter.DbType = dbType;
148+
parameter.Value = value;
149+
_ = command.Parameters.Add(parameter);
150+
151+
AreEqual(value, command.ExecuteScalar());
152+
}
153+
154+
[TestMethod]
155+
[DataRow(DbType.Boolean)]
156+
[DataRow(DbType.Byte)]
157+
[DataRow(DbType.Int16)]
158+
[DataRow(DbType.AnsiString)]
159+
[DataRow(DbType.String)]
160+
public void TypedNullParameter_ReturnsDBNull(DbType dbType)
161+
{
162+
using var connection = new Simulation(StorageBackend.Pages).CreateOpenConnection();
163+
using var command = connection.CreateCommand();
164+
command.CommandText = "select @p";
165+
var parameter = command.CreateParameter();
166+
parameter.ParameterName = "p";
167+
parameter.DbType = dbType;
168+
parameter.Value = null;
169+
_ = command.Parameters.Add(parameter);
170+
171+
AreEqual(DBNull.Value, command.ExecuteScalar());
172+
}
125173
}

SqlServerSimulator.Tests/PagesBackendTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ public void Reader_RoundsTripsRowAndExposesScalar()
3737
}
3838

3939
[TestMethod]
40-
public void StringLiteralExpression_NotYetSupported_OnPages()
40+
public void ColumnReference_NotYetSupported_OnPages()
4141
{
42-
// The Value.RunSql translation shim only maps int literals to SqlValue
43-
// today; string literals (here via @@VERSION) hit NotSupportedException.
44-
// Demonstrates that the parallel evaluator surface is honest about its coverage.
42+
// Reference.RunSql isn't implemented yet, so a tableless `select x`
43+
// surfaces NotSupportedException via the base Expression.RunSql.
44+
// Demonstrates that the parallel evaluator surface is honest about its
45+
// coverage; this lifts when Reference.RunSql arrives alongside Pages-
46+
// backed tables.
4547
var simulation = new Simulation(StorageBackend.Pages);
46-
_ = Throws<NotSupportedException>(() => simulation.ExecuteScalar("select @@version"));
48+
_ = Throws<NotSupportedException>(() => simulation.ExecuteScalar("select x"));
4749
}
4850

4951
[TestMethod]

SqlServerSimulator/DataType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private sealed class DbBoolean : BitwiseCompatibleDataType
168168

169169
protected override int Precedence => 20;
170170

171-
public override DataValue ConvertFrom(DataValue value) => new(Convert.ToBoolean(value, CultureInfo.InvariantCulture), this);
171+
public override DataValue ConvertFrom(DataValue value) => new(value.Value is null ? null : Convert.ToBoolean(value.Value, CultureInfo.InvariantCulture), this);
172172

173173
public override int Compare(DataValue x, DataValue y) => throw new NotImplementedException();
174174

@@ -197,7 +197,7 @@ private sealed class DbByte : BitwiseCompatibleDataType
197197

198198
protected override int Precedence => 19;
199199

200-
public override DataValue ConvertFrom(DataValue value) => new(Convert.ToByte(value, CultureInfo.InvariantCulture), this);
200+
public override DataValue ConvertFrom(DataValue value) => new(value.Value is null ? null : Convert.ToByte(value.Value, CultureInfo.InvariantCulture), this);
201201

202202
public override int Compare(DataValue x, DataValue y) => throw new NotImplementedException();
203203

@@ -226,7 +226,7 @@ private sealed class DbInt16 : BitwiseCompatibleDataType
226226

227227
protected override int Precedence => 18;
228228

229-
public override DataValue ConvertFrom(DataValue value) => new(Convert.ToInt16(value, CultureInfo.InvariantCulture), this);
229+
public override DataValue ConvertFrom(DataValue value) => new(value.Value is null ? null : Convert.ToInt16(value.Value, CultureInfo.InvariantCulture), this);
230230

231231
public override int Compare(DataValue x, DataValue y) => throw new NotImplementedException();
232232

SqlServerSimulator/Parser/Expressions/Value.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ public Value(DoubleAtPrefixedString doubleAtPrefixedString)
3838
/// Transitional: parser tokens still produce <see cref="DataValue"/>, so a
3939
/// literal <see cref="Value"/> expression has to translate at this seam.
4040
/// When parser tokens are rewritten to produce <see cref="SqlValue"/>
41-
/// directly, this helper goes away.
41+
/// directly, this helper goes away. <c>BuiltInDbSystemName</c> isn't
42+
/// mapped: it's only used for system-table column definitions, never as a
43+
/// literal that flows through this shim.
4244
/// </remarks>
43-
private static SqlValue ToSqlValue(DataValue dv) =>
44-
dv.Type == DataType.BuiltInDbInt32
45-
? (dv.Value is null ? SqlValue.Null(SqlType.Int32) : SqlValue.FromInt32((int)dv.Value))
46-
: throw new NotSupportedException($"No SqlType mapping for literal of type {dv.Type}.");
45+
private static SqlValue ToSqlValue(DataValue dv) => dv.Type switch
46+
{
47+
var t when t == DataType.BuiltInDbInt32 => dv.Value is null ? SqlValue.Null(SqlType.Int32) : SqlValue.FromInt32((int)dv.Value),
48+
var t when t == DataType.BuiltInDbInt16 => dv.Value is null ? SqlValue.Null(SqlType.SmallInt) : SqlValue.FromInt16((short)dv.Value),
49+
var t when t == DataType.BuiltInDbByte => dv.Value is null ? SqlValue.Null(SqlType.TinyInt) : SqlValue.FromByte((byte)dv.Value),
50+
var t when t == DataType.BuiltInDbBoolean => dv.Value is null ? SqlValue.Null(SqlType.Bit) : SqlValue.FromBoolean((bool)dv.Value),
51+
var t when t == DataType.BuiltInDbAnsiString => dv.Value is null ? SqlValue.Null(SqlType.Varchar) : SqlValue.FromVarchar((string)dv.Value),
52+
var t when t == DataType.BuiltInDbString => dv.Value is null ? SqlValue.Null(SqlType.NVarchar) : SqlValue.FromNVarchar((string)dv.Value),
53+
_ => throw new NotSupportedException($"No SqlType mapping for literal of type {dv.Type}."),
54+
};
4755

4856
#if DEBUG
4957
public override string ToString() => value.Value?.ToString() ?? "null";

0 commit comments

Comments
 (0)