Skip to content

Commit 1b56941

Browse files
feat: support YdbDbType.List (#554)
- **Breaking Change**: Renamed `YdbDbType` enum members to match the [ydb.tech](https://ydb.tech/docs/en/yql/reference/types/primitive) naming: - `UInt8` -> `Uint8`. - `UInt16` -> `Uint16`. - `UInt32` -> `Uint32`. - `Uint64` -> `Uint64`. - `DateTime` -> `Datetime`. - **Breaking Change**: Removed unused methods `GetJson`, `GetJsonDocument`, and `GetYson` from YdbDataReader. - Feat ADO.NET: Add support for reading `Yson` from `YdbDataReader.GetBytes`. - Feat ADO.NET: Add support for reading `Json` and `JsonDocument` from `YdbDataReader.GetString`. - Feat ADO.NET: Added type checking in the parameter list in sql statement `IN (@id1, @id2)`.
1 parent 1fdaf57 commit 1b56941

File tree

14 files changed

+1217
-738
lines changed

14 files changed

+1217
-738
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ jobs:
9696
EmptyStatement,
9797
ObjectCreationAsStatement,
9898
ParameterOnlyUsedForPreconditionCheck.Local
99+
BitwiseOperatorOnEnumWithoutFlags

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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`.
7+
- **Breaking Change**: Removed unused methods `GetJson`, `GetJsonDocument`, and `GetYson` from YdbDataReader.
8+
- Feat ADO.NET: Add support for reading `Yson` from `YdbDataReader.GetBytes`.
9+
- Feat ADO.NET: Add support for reading `Json` and `JsonDocument` from `YdbDataReader.GetString`.
10+
- Feat ADO.NET: Added type checking in the parameter list in sql statement `IN (@id1, @id2)`.
111
- **Breaking Change**: `Ydb.Sdk.Services.Topic` moved to `Ydb.Sdk.Topic`.
212

313
## v0.24.0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ListPrimitiveParam(IReadOnlyList<string> paramNames, int globalNumber)
3131
public bool IsNative => false;
3232

3333
public TypedValue YdbValueFetch(Dictionary<string, TypedValue> ydbParameters) =>
34-
_paramNames.Select(ydbParameters.Get).ToArray().List();
34+
_paramNames.Select(ydbParameters.Get).List();
3535
}
3636

3737
internal static class YdbParametersExtension
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
namespace Ydb.Sdk.Ado.Internal;
2+
3+
using static YdbValueExtensions;
4+
5+
internal class YdbPrimitiveTypeInfo
6+
{
7+
internal static readonly YdbPrimitiveTypeInfo
8+
Bool = new(Type.Types.PrimitiveTypeId.Bool, TryPack<bool>(PackBool)),
9+
Int8 = new(Type.Types.PrimitiveTypeId.Int8, TryPack<sbyte>(PackInt8)),
10+
Int16 = new(Type.Types.PrimitiveTypeId.Int16, TryPackInt16),
11+
Int32 = new(Type.Types.PrimitiveTypeId.Int32, TryPackInt32),
12+
Int64 = new(Type.Types.PrimitiveTypeId.Int64, TryPackInt64),
13+
Uint8 = new(Type.Types.PrimitiveTypeId.Uint8, TryPack<byte>(PackUint8)),
14+
Uint16 = new(Type.Types.PrimitiveTypeId.Uint16, TryPackUint16),
15+
Uint32 = new(Type.Types.PrimitiveTypeId.Uint32, TryPackUint32),
16+
Uint64 = new(Type.Types.PrimitiveTypeId.Uint64, TryPackUint64),
17+
Float = new(Type.Types.PrimitiveTypeId.Float, TryPack<float>(PackFloat)),
18+
Double = new(Type.Types.PrimitiveTypeId.Double, TryPackDouble),
19+
Bytes = new(Type.Types.PrimitiveTypeId.String, TryPackBytes),
20+
Text = new(Type.Types.PrimitiveTypeId.Utf8, TryPack<string>(PackText)),
21+
Json = new(Type.Types.PrimitiveTypeId.Json, TryPack<string>(PackText)),
22+
JsonDocument = new(Type.Types.PrimitiveTypeId.JsonDocument, TryPack<string>(PackText)),
23+
Yson = new(Type.Types.PrimitiveTypeId.Yson, TryPackBytes),
24+
Uuid = new(Type.Types.PrimitiveTypeId.Uuid, TryPack<Guid>(PackUuid)),
25+
Date = new(Type.Types.PrimitiveTypeId.Date, TryPackDate),
26+
Date32 = new(Type.Types.PrimitiveTypeId.Date32, TryPackDate32),
27+
Datetime = new(Type.Types.PrimitiveTypeId.Datetime, TryPack<DateTime>(PackDatetime)),
28+
Datetime64 = new(Type.Types.PrimitiveTypeId.Datetime64, TryPack<DateTime>(PackDatetime64)),
29+
Timestamp = new(Type.Types.PrimitiveTypeId.Timestamp, TryPack<DateTime>(PackTimestamp)),
30+
Timestamp64 = new(Type.Types.PrimitiveTypeId.Timestamp64, TryPack<DateTime>(PackTimestamp64)),
31+
Interval = new(Type.Types.PrimitiveTypeId.Interval, TryPack<TimeSpan>(PackInterval)),
32+
Interval64 = new(Type.Types.PrimitiveTypeId.Interval64, TryPack<TimeSpan>(PackInterval64));
33+
34+
private YdbPrimitiveTypeInfo(Type.Types.PrimitiveTypeId primitiveTypeId, Func<object, Ydb.Value?> pack)
35+
{
36+
YdbType = new Type { TypeId = primitiveTypeId };
37+
NullValue = new TypedValue
38+
{ Type = new Type { OptionalType = new OptionalType { Item = YdbType } }, Value = YdbValueNull };
39+
Pack = pack;
40+
}
41+
42+
internal Type YdbType { get; }
43+
internal TypedValue NullValue { get; }
44+
internal Func<object, Ydb.Value?> Pack { get; }
45+
46+
internal static YdbPrimitiveTypeInfo? TryResolve(System.Type type)
47+
{
48+
if (type == typeof(bool)) return Bool;
49+
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;
54+
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;
59+
60+
if (type == typeof(float)) return Float;
61+
if (type == typeof(double)) return Double;
62+
63+
if (type == typeof(byte[]) || type == typeof(MemoryStream)) return Bytes;
64+
if (type == typeof(string)) return Text;
65+
if (type == typeof(Guid)) return Uuid;
66+
67+
if (type == typeof(DateOnly)) return Date;
68+
if (type == typeof(DateTime)) return Timestamp;
69+
if (type == typeof(TimeSpan)) return Interval;
70+
71+
return null;
72+
}
73+
74+
private static Func<object, Ydb.Value?> TryPack<T>(Func<T, Ydb.Value> pack) =>
75+
value => value is T valueT ? pack(valueT) : null;
76+
77+
private static Ydb.Value? TryPackInt16(object value) => value switch
78+
{
79+
short shortValue => PackInt16(shortValue),
80+
sbyte sbyteValue => PackInt16(sbyteValue),
81+
byte byteValue => PackInt16(byteValue),
82+
_ => null
83+
};
84+
85+
private static Ydb.Value? TryPackInt32(object value) => value switch
86+
{
87+
int intValue => PackInt32(intValue),
88+
sbyte sbyteValue => PackInt32(sbyteValue),
89+
byte byteValue => PackInt32(byteValue),
90+
short shortValue => PackInt32(shortValue),
91+
ushort ushortValue => PackInt32(ushortValue),
92+
_ => null
93+
};
94+
95+
private static Ydb.Value? TryPackInt64(object value) => value switch
96+
{
97+
long longValue => PackInt64(longValue),
98+
sbyte sbyteValue => PackInt64(sbyteValue),
99+
byte byteValue => PackInt64(byteValue),
100+
short shortValue => PackInt64(shortValue),
101+
ushort ushortValue => PackInt64(ushortValue),
102+
int intValue => PackInt64(intValue),
103+
uint uintValue => PackInt64(uintValue),
104+
_ => null
105+
};
106+
107+
private static Ydb.Value? TryPackUint16(object value) => value switch
108+
{
109+
ushort shortValue => PackUint16(shortValue),
110+
byte byteValue => PackUint16(byteValue),
111+
_ => null
112+
};
113+
114+
private static Ydb.Value? TryPackUint32(object value) => value switch
115+
{
116+
uint intValue => PackUint32(intValue),
117+
byte byteValue => PackUint32(byteValue),
118+
ushort ushortValue => PackUint32(ushortValue),
119+
_ => null
120+
};
121+
122+
private static Ydb.Value? TryPackUint64(object value) => value switch
123+
{
124+
ulong longValue => PackUint64(longValue),
125+
byte byteValue => PackUint64(byteValue),
126+
ushort ushortValue => PackUint64(ushortValue),
127+
uint uintValue => PackUint64(uintValue),
128+
_ => null
129+
};
130+
131+
private static Ydb.Value? TryPackDouble(object value) => value switch
132+
{
133+
double doubleValue => PackDouble(doubleValue),
134+
float floatValue => PackDouble(floatValue),
135+
_ => null
136+
};
137+
138+
private static Ydb.Value? TryPackBytes(object value) => value switch
139+
{
140+
byte[] bytesValue => PackBytes(bytesValue),
141+
MemoryStream memoryStream => PackBytes(memoryStream.ToArray()),
142+
_ => null
143+
};
144+
145+
private static Ydb.Value? TryPackDate(object value) => value switch
146+
{
147+
DateTime dateTimeValue => PackDate(dateTimeValue),
148+
DateOnly dateOnlyValue => PackDate(dateOnlyValue.ToDateTime(TimeOnly.MinValue)),
149+
_ => null
150+
};
151+
152+
private static Ydb.Value? TryPackDate32(object value) => value switch
153+
{
154+
DateTime dateTimeValue => PackDate32(dateTimeValue),
155+
DateOnly dateOnlyValue => PackDate32(dateOnlyValue.ToDateTime(TimeOnly.MinValue)),
156+
_ => null
157+
};
158+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Ydb.Sdk.Ado.Internal;
2+
3+
internal static class YdbTypeExtensions
4+
{
5+
internal const byte DefaultDecimalPrecision = 22;
6+
internal const byte DefaultDecimalScale = 9;
7+
8+
private static readonly Type DefaultDecimalType = DecimalType(DefaultDecimalPrecision, DefaultDecimalScale);
9+
10+
internal static Type DecimalType(byte precision, byte scale) => precision == 0 && scale == 0
11+
? DefaultDecimalType
12+
: new Type { DecimalType = new DecimalType { Precision = precision, Scale = scale } };
13+
14+
internal static Type ListType(this Type type) => new() { ListType = new ListType { Item = type } };
15+
16+
internal static Type OptionalType(this Type type) => new() { OptionalType = new OptionalType { Item = type } };
17+
}

0 commit comments

Comments
 (0)