Skip to content

Commit 2629b33

Browse files
authored
Add support for UInt128 in SqliteValueBinder (#37492)
- Bind UInt128 parameter as TEXT in SQLite. - Add provider-level tests to verify correct binding. - This change only affects parameter binding; reading values back still returns string.
1 parent 2e74fff commit 2629b33

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ public virtual void Bind()
238238
var value1 = (long)(ushort)value;
239239
BindInt64(value1);
240240
}
241+
#if NET7_0_OR_GREATER
242+
else if (type == typeof(UInt128))
243+
{
244+
BindText(((UInt128)value).ToString("D39", CultureInfo.InvariantCulture));
245+
}
246+
#endif
241247
else
242248
{
243249
throw new InvalidOperationException(Resources.UnknownDataType(type));
@@ -259,7 +265,11 @@ public virtual void Bind()
259265
{ typeof(DateOnly), SqliteType.Text },
260266
{ typeof(TimeOnly), SqliteType.Text },
261267
#endif
268+
262269
{ typeof(DBNull), SqliteType.Text },
270+
#if NET7_0_OR_GREATER
271+
{ typeof(UInt128), SqliteType.Text },
272+
#endif
263273
{ typeof(decimal), SqliteType.Text },
264274
{ typeof(double), SqliteType.Real },
265275
{ typeof(float), SqliteType.Real },

test/Microsoft.Data.Sqlite.Tests/SqliteParameterTest.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Data;
77
using System.Data.Common;
8+
using System.Numerics;
89
using Microsoft.Data.Sqlite.Properties;
910
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;
1011
using Xunit;
@@ -588,6 +589,86 @@ public void Add_range_of_parameters_using_DbCommand_base_class()
588589
}
589590
}
590591

592+
#if NET7_0_OR_GREATER
593+
[Fact]
594+
public void Bind_UInt128_zero_as_text()
595+
{
596+
using (var connection = new SqliteConnection("Data Source=:memory:"))
597+
{
598+
var command = connection.CreateCommand();
599+
command.CommandText = "SELECT @Parameter;";
600+
var value = (UInt128)0;
601+
command.Parameters.AddWithValue("@Parameter", value);
602+
connection.Open();
603+
var result = (string)command.ExecuteScalar()!;
604+
Assert.Equal("000000000000000000000000000000000000000", result);
605+
}
606+
}
607+
608+
[Fact]
609+
public void Bind_UInt128_max_value_as_text()
610+
{
611+
using (var connection = new SqliteConnection("Data Source=:memory:"))
612+
{
613+
var command = connection.CreateCommand();
614+
command.CommandText = "SELECT @Parameter;";
615+
var value = UInt128.MaxValue;
616+
command.Parameters.AddWithValue("@Parameter", value);
617+
connection.Open();
618+
var result = (string)command.ExecuteScalar()!;
619+
Assert.Equal("340282366920938463463374607431768211455", result);
620+
}
621+
}
622+
623+
[Fact]
624+
public void Bind_UInt128_ordering_works()
625+
{
626+
using (var connection = new SqliteConnection("Data Source=:memory:"))
627+
{
628+
connection.Open();
629+
630+
var command = connection.CreateCommand();
631+
command.CommandText = """
632+
CREATE TABLE TestOrdering (Value TEXT);
633+
INSERT INTO TestOrdering VALUES (@A);
634+
INSERT INTO TestOrdering VALUES (@B);
635+
INSERT INTO TestOrdering VALUES (@C);
636+
SELECT Value FROM TestOrdering ORDER BY Value;
637+
""";
638+
command.Parameters.AddWithValue("@A", (UInt128)500);
639+
command.Parameters.AddWithValue("@B", UInt128.MaxValue);
640+
command.Parameters.AddWithValue("@C", (UInt128)1);
641+
642+
var results = new List<string>();
643+
using var reader = command.ExecuteReader();
644+
while (reader.Read())
645+
{
646+
results.Add(reader.GetString(0));
647+
}
648+
649+
Assert.Equal(3, results.Count);
650+
Assert.Equal("000000000000000000000000000000000000001", results[0]);
651+
Assert.Equal("000000000000000000000000000000000000500", results[1]);
652+
Assert.Equal("340282366920938463463374607431768211455", results[2]);
653+
}
654+
}
655+
656+
[Fact]
657+
public void Bind_UInt128_concatenation_works()
658+
{
659+
using (var connection = new SqliteConnection("Data Source=:memory:"))
660+
{
661+
var command = connection.CreateCommand();
662+
command.CommandText = "SELECT @Parameter || '_suffix';";
663+
var value = (UInt128)42;
664+
command.Parameters.AddWithValue("@Parameter", value);
665+
connection.Open();
666+
var result = (string)command.ExecuteScalar()!;
667+
Assert.Equal("000000000000000000000000000000000000042_suffix", result);
668+
}
669+
}
670+
#endif
671+
591672
public static IEnumerable<object[]> TypesData
592673
=> new List<object[]>
593674
{

0 commit comments

Comments
 (0)