Skip to content

Commit df759e4

Browse files
last changes
1 parent 55bdbb0 commit df759e4

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
- **Breaking Change**: Renamed `YdbDbType` enum members to match the [ydb.tech](https://ydb.tech/docs/en/yql/reference/types/primitive) naming:
2+
- `UInt8` -> `Uint8`.
3+
- `UInt16` -> `Uint16`.
4+
- `UInt32` -> `Uint32`.
5+
- `Uint64` -> `Uint64`.
6+
- `DateTime` -> `Datetime`.
17
- **Breaking Change**: Removed unused methods `GetJson`, `GetJsonDocument`, and `GetYson` from YdbDataReader.
28
- Feat ADO.NET: Add support for reading `Yson` from `YdbDataReader.GetBytes`.
39
- Feat ADO.NET: Add support for reading `Json` and `JsonDocument` from `YdbDataReader.GetString`.

src/Ydb.Sdk/src/Ado/Internal/YdbPrimitiveTypeInfo.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@ private YdbPrimitiveTypeInfo(Type.Types.PrimitiveTypeId primitiveTypeId, Func<ob
4545

4646
internal static YdbPrimitiveTypeInfo? TryResolve(System.Type type)
4747
{
48-
if (type.IsAssignableFrom(typeof(bool))) return Bool;
48+
if (type == typeof(bool)) return Bool;
4949

50-
if (type.IsAssignableFrom(typeof(sbyte))) return Int8;
51-
if (type.IsAssignableFrom(typeof(short))) return Int16;
52-
if (type.IsAssignableFrom(typeof(int))) return Int32;
53-
if (type.IsAssignableFrom(typeof(long))) return Int64;
50+
if (type == typeof(sbyte)) return Int8;
51+
if (type == typeof(short)) return Int16;
52+
if (type == typeof(int)) return Int32;
53+
if (type == typeof(long)) return Int64;
5454

55-
if (type.IsAssignableFrom(typeof(byte))) return Uint8;
56-
if (type.IsAssignableFrom(typeof(ushort))) return Uint16;
57-
if (type.IsAssignableFrom(typeof(uint))) return Uint32;
58-
if (type.IsAssignableFrom(typeof(ulong))) return Uint64;
55+
if (type == typeof(byte)) return Uint8;
56+
if (type == typeof(ushort)) return Uint16;
57+
if (type == typeof(uint)) return Uint32;
58+
if (type == typeof(ulong)) return Uint64;
5959

60-
if (type.IsAssignableFrom(typeof(float))) return Float;
61-
if (type.IsAssignableFrom(typeof(double))) return Double;
60+
if (type == typeof(float)) return Float;
61+
if (type == typeof(double)) return Double;
6262

63-
if (type.IsAssignableFrom(typeof(byte[])) || type.IsAssignableFrom(typeof(MemoryStream))) return Bytes;
64-
if (type.IsAssignableFrom(typeof(string))) return Text;
65-
if (type.IsAssignableFrom(typeof(Guid))) return Uuid;
63+
if (type == typeof(byte[]) || type == typeof(MemoryStream)) return Bytes;
64+
if (type == typeof(string)) return Text;
65+
if (type == typeof(Guid)) return Uuid;
6666

67-
if (type.IsAssignableFrom(typeof(DateOnly))) return Date;
68-
if (type.IsAssignableFrom(typeof(DateTime))) return Timestamp;
69-
if (type.IsAssignableFrom(typeof(TimeSpan))) return Interval;
67+
if (type == typeof(DateOnly)) return Date;
68+
if (type == typeof(DateTime)) return Timestamp;
69+
if (type == typeof(TimeSpan)) return Interval;
7070

7171
return null;
7272
}

src/Ydb.Sdk/src/Ado/YdbParameter.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ _ when YdbDbType.HasFlag(YdbDbType.List) && value is IList itemsValue =>
285285
private TypedValue PackList(IList items, YdbDbType ydbDbType = YdbDbType.Unspecified)
286286
{
287287
var elementType = GetElementType(items) ?? throw ValueTypeNotSupportedException;
288+
elementType = Nullable.GetUnderlyingType(elementType) ?? elementType;
288289
var primitiveTypeInfo = ydbDbType.PrimitiveTypeInfo() ?? YdbPrimitiveTypeInfo.TryResolve(elementType);
289290

290291
if (primitiveTypeInfo != null)
@@ -314,7 +315,7 @@ private TypedValue PackList(IList items, YdbDbType ydbDbType = YdbDbType.Unspeci
314315
return new TypedValue { Type = type.ListType(), Value = value };
315316
}
316317

317-
if (ydbDbType == YdbDbType.Decimal || elementType.IsAssignableFrom(typeof(decimal)))
318+
if (ydbDbType == YdbDbType.Decimal || elementType == typeof(decimal))
318319
{
319320
var value = new Ydb.Value();
320321
var isOptional = false;
@@ -343,8 +344,11 @@ private TypedValue PackList(IList items, YdbDbType ydbDbType = YdbDbType.Unspeci
343344
return new TypedValue { Type = type.ListType(), Value = value };
344345
}
345346

346-
return (from object? item in items select PackObject(item ?? throw ValueTypeNotSupportedException)).ToArray()
347-
.List();
347+
return (from object? item in items
348+
select PackObject(item ?? throw new ArgumentException(
349+
$"Collection of type '{items.GetType()}' contains null. " +
350+
$"Specify YdbDbType (e.g. YdbDbType.List | YdbDbType.<T>) or use a strongly-typed collection (e.g., List<T?>)."))
351+
).ToArray().List();
348352
}
349353

350354
private InvalidOperationException ValueTypeNotSupportedException =>

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/YdbParameterTests.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ PRIMARY KEY (Int32Column)
554554
YdbDbType.Interval,
555555
new List<TimeSpan?> { TimeSpan.FromDays(1), TimeSpan.FromDays(2), TimeSpan.FromDays(3), null }
556556
},
557-
{ YdbDbType.Interval, (TimeSpan?[])[TimeSpan.FromDays(1), TimeSpan.FromDays(2), TimeSpan.FromDays(3), null] }
557+
{ YdbDbType.Interval, (TimeSpan?[])[TimeSpan.FromDays(1), TimeSpan.FromDays(2), TimeSpan.FromDays(3), null] },
558+
{ YdbDbType.Int64, new List<object> { 1, 2u, (byte)3 } },
559+
{ YdbDbType.Int64, new object[] { 1, 2u, (byte)3 } } // only not null objects
558560
};
559561

560562
public static TheoryData<YdbDbType, IList> ExtraParams = new()
@@ -740,13 +742,37 @@ public async Task YdbParameter_Value_WithYdbDbTypeList_ProducesListOfSpecifiedTy
740742
public void YdbParameter_SetValue_ListOrArray_InvalidInputs_Throws()
741743
{
742744
Assert.Equal("Writing value of 'System.Object[]' is not supported for parameters having YdbDbType 'List<Bool>'",
743-
Assert.Throws<InvalidOperationException>(() =>
744-
new YdbParameter("list", YdbDbType.List | YdbDbType.Bool, new object[] { true, false, "string" })
745-
.TypedValue).Message);
745+
Assert.Throws<InvalidOperationException>(() => new YdbParameter("list",
746+
YdbDbType.List | YdbDbType.Bool, new object[] { true, false, "string" }).TypedValue).Message);
746747

747748
Assert.Equal(
748749
"Writing value of 'System.Object[]' is not supported for parameters having YdbDbType 'List<Decimal>'",
749750
Assert.Throws<InvalidOperationException>(() => new YdbParameter("list",
750751
YdbDbType.List | YdbDbType.Decimal, new object[] { 1.0m, false, 2.0m }).TypedValue).Message);
752+
753+
Assert.Equal("All elements in the list must have the same type. Expected: { \"typeId\": \"INT32\" }, " +
754+
"actual: { \"typeId\": \"UINT32\" }", Assert.Throws<ArgumentException>(() =>
755+
new YdbParameter("list", new List<object> { 1, 2u, (byte)3 }).TypedValue).Message);
756+
757+
Assert.Equal("All elements in the list must have the same type. Expected: { \"typeId\": \"INT32\" }, " +
758+
"actual: { \"typeId\": \"UINT32\" }", Assert.Throws<ArgumentException>(() =>
759+
new YdbParameter("list", new object[] { 1, 2u, (byte)3 }).TypedValue).Message);
760+
761+
Assert.Equal("Collection of type 'System.Collections.Generic.List`1[System.Object]' contains null. " +
762+
"Specify YdbDbType (e.g. YdbDbType.List | YdbDbType.<T>) " +
763+
"or use a strongly-typed collection (e.g., List<T?>).", Assert.Throws<ArgumentException>(() =>
764+
new YdbParameter("list", new List<object?> { 1, null }).TypedValue).Message);
765+
766+
Assert.Equal("Collection of type 'System.Object[]' contains null. " +
767+
"Specify YdbDbType (e.g. YdbDbType.List | YdbDbType.<T>) " +
768+
"or use a strongly-typed collection (e.g., List<T?>).", Assert.Throws<ArgumentException>(() =>
769+
new YdbParameter("list", new object?[] { 1, null }).TypedValue).Message);
751770
}
771+
772+
[Fact]
773+
public void YdbParameter_SetYdbDbTypeList_Throws() =>
774+
Assert.Equal("Cannot set YdbDbType to just List. " +
775+
"Use Binary-Or with the element type (e.g. Array of dates is YdbDbType.List | YdbDbType.Date). " +
776+
"(Parameter 'value')",
777+
Assert.Throws<ArgumentOutOfRangeException>(() => new YdbParameter("list", YdbDbType.List)).Message);
752778
}

0 commit comments

Comments
 (0)