Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 89bf144

Browse files
authored
Add proper support for nullable in Dapper #598 (#600)
1 parent 3bb7b05 commit 89bf144

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

ClickHouse.Client.Tests/ORM/DapperTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,22 @@ public async Task ShouldWriteTwoFieldsWithTheSamePrefix()
207207
const string sql = "INSERT INTO test.dapper_prefixes (testField, testFieldWithSuffix) VALUES (@testField, @testFieldWithSuffix)";
208208
await connection.ExecuteAsync(sql, new { testField = 1, testFieldWithSuffix = 2 });
209209
}
210+
211+
[Test]
212+
[TestCase(1.0)]
213+
[TestCase(null)]
214+
public async Task ShouldWriteNullableDoubleWithTypeInference(double? expected)
215+
{
216+
await connection.ExecuteStatementAsync("TRUNCATE TABLE IF EXISTS test.dapper_nullable_double");
217+
await connection.ExecuteStatementAsync("CREATE TABLE IF NOT EXISTS test.dapper_nullable_double (balance Nullable(Float64)) ENGINE Memory");
218+
219+
var sql = @"INSERT INTO test.dapper_nullable_double (balance) VALUES (@balance)";
220+
await connection.ExecuteAsync(sql, new { balance = expected });
221+
222+
var actual = await connection.ExecuteScalarAsync("SELECT * FROM test.dapper_nullable_double");
223+
if (expected is null)
224+
Assert.That(actual, Is.InstanceOf<DBNull>());
225+
else
226+
Assert.That(actual, Is.EqualTo(expected));
227+
}
210228
}

ClickHouse.Client.Tests/Types/TypeMappingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class TypeMappingTests
5454
public Type ShouldConvertFromClickHouseType(string clickHouseType) => TypeConverter.ParseClickHouseType(clickHouseType, TypeSettings.Default).FrameworkType;
5555

5656
[Test]
57-
[TestCase(typeof(DBNull), ExpectedResult = "Nothing")]
57+
[TestCase(typeof(DBNull), ExpectedResult = "Nullable(Nothing)")]
5858

5959
[TestCase(typeof(sbyte), ExpectedResult = "Int8")]
6060
[TestCase(typeof(short), ExpectedResult = "Int16")]

ClickHouse.Client/Types/TypeConverter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ static TypeConverter()
181181
#endif
182182
ReverseMapping[typeof(DateTime)] = new DateTimeType();
183183
ReverseMapping[typeof(DateTimeOffset)] = new DateTimeType();
184+
ReverseMapping[typeof(DBNull)] = new NullableType() { UnderlyingType = new NothingType() };
184185
}
185186

186187
private static void RegisterPlainType<T>()

0 commit comments

Comments
 (0)