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

Commit 0306072

Browse files
authored
Fix for Dapper decimal support (#452)
1 parent fae0f8c commit 0306072

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

ClickHouse.Client.Tests/ORM/DapperTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,20 @@ public async Task ShouldExecuteSelectReturningDecimal()
175175
Assert.IsInstanceOf<decimal>(result);
176176
Assert.AreEqual(0.0001m, result);
177177
}
178+
179+
[Test]
180+
public async Task ShouldInsertParameterizedFloat64Array()
181+
{
182+
const decimal expected = 123.456m;
183+
184+
await connection.ExecuteStatementAsync("TRUNCATE TABLE IF EXISTS test.dapper_decimal");
185+
await connection.ExecuteStatementAsync("CREATE TABLE IF NOT EXISTS test.dapper_decimal (balance Decimal128(4)) ENGINE Memory");
186+
187+
188+
var sql = @"INSERT INTO test.dapper_decimal (balance) VALUES (@balance)";
189+
await connection.ExecuteAsync(sql, new { balance = expected });
190+
191+
var actual = (ClickHouseDecimal) await connection.ExecuteScalarAsync("SELECT * FROM test.dapper_decimal");
192+
Assert.AreEqual(expected, actual.ToDecimal(CultureInfo.InvariantCulture));
193+
}
178194
}

ClickHouse.Client.Tests/Types/TypeMappingTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public class TypeMappingTests
6767

6868
[TestCase(typeof(float), ExpectedResult = "Float32")]
6969
[TestCase(typeof(double), ExpectedResult = "Float64")]
70-
[TestCase(typeof(decimal), ExpectedResult = "Decimal128(0)")]
7170

7271
[TestCase(typeof(string), ExpectedResult = "String")]
7372

ClickHouse.Client/ADO/Parameters/ClickHouseDbParameter.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data;
33
using System.Data.Common;
4+
using ClickHouse.Client.Numerics;
45
using ClickHouse.Client.Types;
56

67
namespace ClickHouse.Client.ADO.Parameters;
@@ -33,7 +34,17 @@ public string QueryForm
3334
{
3435
get
3536
{
36-
var chType = ClickHouseType ?? TypeConverter.ToClickHouseType(Value?.GetType() ?? typeof(DBNull)).ToString();
37+
if (ClickHouseType != null)
38+
return $"{{{ParameterName}:{ClickHouseType}}}";
39+
40+
if (Value is decimal d)
41+
{
42+
var parts = decimal.GetBits(d);
43+
int scale = (parts[3] >> 16) & 0x7F;
44+
return $"{{{ParameterName}:Decimal(22,{scale})}}";
45+
}
46+
47+
var chType = TypeConverter.ToClickHouseType(Value?.GetType() ?? typeof(DBNull)).ToString();
3748
return $"{{{ParameterName}:{chType}}}";
3849
}
3950
}

0 commit comments

Comments
 (0)