Skip to content

Commit 75e17e6

Browse files
fixed YdbDataReader: GetValue() returns DbNull.Value if field is null (#193)
* fixed YdbDataReader: GetValue() returns DbNull.Value if field is null * fix linter
1 parent 007a40a commit 75e17e6

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Fixed YdbDataReader: `GetValue()` returns `DbNull.Value` if field is null
12
- YdbOperationInProgressException extends YdbException
23

34
## v0.7.1

src/Ydb.Sdk/src/Ado/YdbDataReader.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,16 @@ public string GetJsonDocument(int ordinal)
303303

304304
public override object GetValue(int ordinal)
305305
{
306-
var ydbValue = GetFieldYdbValue(ordinal);
306+
EnsureOrdinal(ordinal);
307+
308+
var ydbValue = CurrentRow[ordinal];
307309

308310
// ReSharper disable once InvertIf
309311
if (ydbValue.TypeId == YdbTypeId.OptionalType)
310312
{
311313
if (ydbValue.GetOptional() == null)
312314
{
313-
return (typeof(DBNull), DBNull.Value);
315+
return DBNull.Value;
314316
}
315317

316318
ydbValue = ydbValue.GetOptional()!;

src/Ydb.Sdk/tests/Dapper/DapperIntegrationTests.cs

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,73 @@ INSERT INTO {Tables.Episodes}
201201
await connection.ExecuteAsync(Tables.DeleteTables);
202202
}
203203

204+
[Fact]
205+
public async Task NullableFieldSupported()
206+
{
207+
var tableName = "DapperNullableTypes_" + Random.Shared.Next();
208+
209+
await using var connection = new YdbConnection();
210+
await connection.ExecuteAsync(@$"
211+
CREATE TABLE {tableName} (
212+
Id INT32,
213+
BoolColumn BOOL,
214+
LongColumn INT64,
215+
ShortColumn INT16,
216+
SbyteColumn INT8,
217+
FloatColumn FLOAT,
218+
DoubleColumn DOUBLE,
219+
DecimalColumn DECIMAL(22,9),
220+
ByteColumn UINT8,
221+
UshortColumn UINT16,
222+
UintColumn UINT32,
223+
UlongColumn UINT64,
224+
TextColumn TEXT,
225+
BytesColumn BYTES,
226+
TimestampColumn TIMESTAMP,
227+
PRIMARY KEY (Id)
228+
)
229+
");
230+
231+
var entity = new NullableFields();
232+
SqlMapper.AddTypeMap(typeof(DateTime), DbType.DateTime2);
233+
234+
await connection.ExecuteAsync($@"
235+
INSERT INTO {tableName} (Id, BoolColumn, LongColumn, ShortColumn, SbyteColumn, FloatColumn, DoubleColumn, DecimalColumn,
236+
ByteColumn, UshortColumn, UintColumn, UlongColumn, TextColumn, BytesColumn, TimestampColumn)
237+
VALUES (@Id, @BoolColumn, @LongColumn, @ShortColumn, @SbyteColumn,
238+
@FloatColumn, @DoubleColumn, @DecimalColumn,
239+
@ByteColumn, @UshortColumn, @UintColumn,
240+
@UlongColumn, @TextColumn, @BytesColumn, @TimestampColumn)", entity);
241+
242+
Assert.Equal(entity,
243+
await connection.QuerySingleAsync<NullableFields>($"SELECT * FROM {tableName} WHERE Id IS NULL"));
244+
}
245+
246+
private record NullableFields
247+
{
248+
public int? Id { get; init; }
249+
public bool? BoolColumn { get; init; }
250+
public long? LongColumn { get; init; }
251+
public short? ShortColumn { get; init; }
252+
public sbyte? SbyteColumn { get; init; }
253+
public float? FloatColumn { get; init; }
254+
public double? DoubleColumn { get; init; }
255+
public decimal? DecimalColumn { get; init; }
256+
public byte? ByteColumn { get; init; }
257+
public ushort? UshortColumn { get; init; }
258+
public uint? UintColumn { get; init; }
259+
public ulong? UlongColumn { get; init; }
260+
public string? TextColumn { get; init; }
261+
public byte[]? BytesColumn { get; init; }
262+
public DateTime? TimestampColumn { get; init; }
263+
}
264+
204265
private record Episode
205266
{
206-
[Column("series_id")] public uint SeriesId { get; set; }
207-
[Column("season_id")] public uint SeasonId { get; set; }
208-
[Column("episode_id")] public uint EpisodeId { get; set; }
209-
[Column("title")] public string Title { get; set; } = null!;
210-
[Column("air_date")] public DateTime AirDate { get; set; }
267+
[Column("series_id")] public uint SeriesId { get; init; }
268+
[Column("season_id")] public uint SeasonId { get; init; }
269+
[Column("episode_id")] public uint EpisodeId { get; init; }
270+
[Column("title")] public string Title { get; init; } = null!;
271+
[Column("air_date")] public DateTime AirDate { get; init; }
211272
}
212273
}

0 commit comments

Comments
 (0)