Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion ClickHouse.Driver.Tests/SQL/SqlParameterizedSelectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ SELECT 1
FROM (SELECT tuple(1, 'a', NULL) AS res)
WHERE res.1 = tupleElement({var:Tuple(Int32, String, Nullable(Int32))}, 1)
AND res.2 = tupleElement({var:Tuple(Int32, String, Nullable(Int32))}, 2)
AND res.3 is NULL
AND res.3 is NULL
AND tupleElement({var:Tuple(Int32, String, Nullable(Int32))}, 3) is NULL";
using var command = connection.CreateCommand();
command.CommandText = sql;
Expand Down Expand Up @@ -130,5 +130,43 @@ SELECT 1
result.GetEnsureSingleRow();
}

[Test]
public async Task ShouldExecuteSelectWithValueTupleParameter()
{
var sql = @"
SELECT 1
FROM (SELECT tuple(1, 'a', NULL) AS res)
WHERE res.1 = tupleElement({var:Tuple(Int32, String, Nullable(Int32))}, 1)
AND res.2 = tupleElement({var:Tuple(Int32, String, Nullable(Int32))}, 2)
AND res.3 is NULL
AND tupleElement({var:Tuple(Int32, String, Nullable(Int32))}, 3) is NULL";
using var command = connection.CreateCommand();
command.CommandText = sql;

command.AddParameter("var", ValueTuple.Create<int, string, int?>(1, "a", null));

var result = await command.ExecuteReaderAsync();
result.GetEnsureSingleRow();
}

[Test]
public async Task ShouldExecuteSelectWithUnderlyingValueTupleParameter()
{
var sql = @"
SELECT 1
FROM (SELECT tuple(123, tuple(5, 'a', 7)) AS res)
WHERE res.1 = tupleElement({var:Tuple(Int32, Tuple(UInt8, String, Nullable(Int32)))}, 1)
AND res.2.1 = tupleElement(tupleElement({var:Tuple(Int32, Tuple(UInt8, String, Nullable(Int32)))}, 2), 1)
AND res.2.2 = tupleElement(tupleElement({var:Tuple(Int32, Tuple(UInt8, String, Nullable(Int32)))}, 2), 2)
AND res.2.3 = tupleElement(tupleElement({var:Tuple(Int32, Tuple(UInt8, String, Nullable(Int32)))}, 2), 3)";
using var command = connection.CreateCommand();
command.CommandText = sql;

command.AddParameter("var", ValueTuple.Create(123, ValueTuple.Create((byte)5, "a", 7)));

var result = await command.ExecuteReaderAsync();
result.GetEnsureSingleRow();
}

public void Dispose() => connection?.Dispose();
}
1 change: 1 addition & 0 deletions ClickHouse.Driver.Tests/Types/TypeMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class TypeMappingTests
[TestCase(typeof(DateOnly), ExpectedResult = "Date")]
#endif
[TestCase(typeof(Tuple<int, byte, float?, string[]>), ExpectedResult = "Tuple(Int32,UInt8,Nullable(Float32),Array(String))")]
[TestCase(typeof(ValueTuple<int, byte, float?, string[]>), ExpectedResult = "Tuple(Int32,UInt8,Nullable(Float32),Array(String))")]
public string ShouldConvertToClickHouseType(Type type) => TypeConverter.ToClickHouseType(type).ToString();

[Test, Explicit]
Expand Down
5 changes: 5 additions & 0 deletions ClickHouse.Driver/Types/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ public static ClickHouseType ToClickHouseType(Type type)
return new TupleType { UnderlyingTypes = type.GetGenericArguments().Select(ToClickHouseType).ToArray() };
}

if (type.IsGenericType && type.GetGenericTypeDefinition().FullName.StartsWith("System.ValueTuple", StringComparison.InvariantCulture))
{
return new TupleType { UnderlyingTypes = type.GetGenericArguments().Select(ToClickHouseType).ToArray() };
}

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
var types = type.GetGenericArguments().Select(ToClickHouseType).ToArray();
Expand Down