feat: add async extensions support for DbDataReader

This commit is contained in:
Kyle Ratti 2024-05-03 18:08:35 -04:00
parent c8f7538677
commit 35d1742f00
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9
3 changed files with 644 additions and 0 deletions

View File

@ -0,0 +1,555 @@
using System;
using System.Collections;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using FruityFoundation.Base.Structures;
using NUnit.Framework;
namespace Base.Tests.Structures;
public class DataReaderAsyncMaybeExtensionTests
{
[Test]
public async Task DbDataReader_TryGetBooleanAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetBooleanAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<bool>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetBooleanAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithBoolean(true);
// Act
var result = await fakeDataReader.TryGetBooleanAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<bool>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(true));
}
[Test]
public async Task DbDataReader_TryGetByteAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetByteAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<byte>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetByteAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithByte(25);
// Act
var result = await fakeDataReader.TryGetByteAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<byte>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo((byte)25));
}
[Test]
public async Task DbDataReader_TryGetBytesAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetBytesAsync(0, 0, null, 0, 0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<long>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetBytesAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithBytes(25);
// Act
var result = await fakeDataReader.TryGetBytesAsync(0, 0, [], 0, 0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<long>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
[Test]
public async Task DbDataReader_TryGetCharAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetCharAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<char>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetCharAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithChar('b');
// Act
var result = await fakeDataReader.TryGetCharAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<char>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo('b'));
}
[Test]
public async Task DbDataReader_TryGetCharsAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetCharsAsync(0, 0, null, 0, 0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<long>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetCharsAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithChars(25);
// Act
var result = await fakeDataReader.TryGetCharsAsync(0, 0, [], 0, 0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<long>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
[Test]
public async Task DbDataReader_TryGetDateTimeAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetDateTimeAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<DateTime>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetDateTimeAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithDateTime(new DateTime(2024, 05, 03, 17, 21, 0, DateTimeKind.Utc));
// Act
var result = await fakeDataReader.TryGetDateTimeAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<DateTime>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(new DateTime(2024, 05, 03, 17, 21, 0, DateTimeKind.Utc)));
}
[Test]
public async Task DbDataReader_TryGetDecimalAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetDecimalAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<decimal>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetDecimalAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithDecimal(25.0m);
// Act
var result = await fakeDataReader.TryGetDecimalAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<decimal>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25.0m));
}
[Test]
public async Task DbDataReader_TryGetFloatAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetFloatAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<float>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetFloatAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithFloat(25.0f);
// Act
var result = await fakeDataReader.TryGetFloatAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<float>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25.0f));
}
[Test]
public async Task DbDataReader_TryGetGuidAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetGuidAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<Guid>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetGuidAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithGuid(new Guid("b3e7f4d3-3b7b-4b1b-8e0e-3b1b7b4b3e7f"));
// Act
var result = await fakeDataReader.TryGetGuidAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<Guid>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(new Guid("b3e7f4d3-3b7b-4b1b-8e0e-3b1b7b4b3e7f")));
}
[Test]
public async Task DbDataReader_TryGetInt16Async_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetInt16Async(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<short>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetInt16Async_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithInt16(25);
// Act
var result = await fakeDataReader.TryGetInt16Async(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<short>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo((short)25));
}
[Test]
public async Task DbDataReader_TryGetInt32Async_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetInt32Async(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetInt32Async_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithInt32(25);
// Act
var result = await fakeDataReader.TryGetInt32Async(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
[Test]
public async Task DbDataReader_TryGetInt64Async_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetInt64Async(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<long>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetInt64Async_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithInt64(25L);
// Act
var result = await fakeDataReader.TryGetInt64Async(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<long>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
[Test]
public async Task DbDataReader_TryGetStringAsync_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = MockDbDataReader.Empty;
// Act
var result = await fakeDataReader.TryGetStringAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<string>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public async Task DbDataReader_TryGetStringAsync_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = MockDbDataReader.WithString("banana");
// Act
var result = await fakeDataReader.TryGetStringAsync(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<string>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo("banana"));
}
private class MockDbDataReader : DbDataReader
{
private readonly bool _isNull;
private readonly Maybe<bool> _boolValue;
private readonly Maybe<byte> _byteValue;
private readonly Maybe<long> _bytesValue;
private readonly Maybe<char> _charValue;
private readonly Maybe<long> _charsValue;
private readonly Maybe<DateTime> _dateTimeValue;
private readonly Maybe<decimal> _decimalValue;
private readonly Maybe<float> _floatValue;
private readonly Maybe<Guid> _guidValue;
private readonly Maybe<short> _int16Value;
private readonly Maybe<int> _int32Value;
private readonly Maybe<long> _int64Value;
private readonly Maybe<string> _stringValue;
private MockDbDataReader(
bool isNull,
Maybe<bool> boolValue = default,
Maybe<byte> byteValue = default,
Maybe<long> bytesValue = default,
Maybe<char> charValue = default,
Maybe<long> charsValue = default,
Maybe<DateTime> dateTimeValue = default,
Maybe<decimal> decimalValue = default,
Maybe<float> floatValue = default,
Maybe<Guid> guidValue = default,
Maybe<short> int16Value = default,
Maybe<int> int32Value = default,
Maybe<long> int64Value = default,
Maybe<string> stringValue = default
)
{
_isNull = isNull;
_boolValue = boolValue;
_byteValue = byteValue;
_bytesValue = bytesValue;
_charValue = charValue;
_charsValue = charsValue;
_dateTimeValue = dateTimeValue;
_decimalValue = decimalValue;
_floatValue = floatValue;
_guidValue = guidValue;
_int16Value = int16Value;
_int32Value = int32Value;
_int64Value = int64Value;
_stringValue = stringValue;
//
}
public static MockDbDataReader Empty => new(isNull: true);
public static MockDbDataReader WithBoolean(bool value) => new(isNull: false, boolValue: Maybe.Create(value));
public static MockDbDataReader WithByte(byte value) => new(isNull: false, byteValue: Maybe.Create(value));
public static MockDbDataReader WithBytes(long value) => new(isNull: false, bytesValue: Maybe.Create(value));
public static MockDbDataReader WithChar(char value) => new(isNull: false, charValue: Maybe.Create(value));
public static MockDbDataReader WithChars(long value) => new(isNull: false, charsValue: Maybe.Create(value));
public static MockDbDataReader WithDateTime(DateTime value) => new(isNull: false, dateTimeValue: Maybe.Create(value));
public static MockDbDataReader WithDecimal(decimal value) => new(isNull: false, decimalValue: Maybe.Create(value));
public static MockDbDataReader WithFloat(float value) => new(isNull: false, floatValue: Maybe.Create(value));
public static MockDbDataReader WithGuid(Guid value) => new(isNull: false, guidValue: Maybe.Create(value));
public static MockDbDataReader WithInt16(short value) => new(isNull: false, int16Value: Maybe.Create(value));
public static MockDbDataReader WithInt32(int value) => new(isNull: false, int32Value: Maybe.Create(value));
public static MockDbDataReader WithInt64(long value) => new(isNull: false, int64Value: Maybe.Create(value));
public static MockDbDataReader WithString(string value) => new(isNull: false, stringValue: Maybe.Create(value));
/// <inheritdoc />
public override bool GetBoolean(int ordinal) => _boolValue.Value;
/// <inheritdoc />
public override byte GetByte(int ordinal) => _byteValue.Value;
/// <inheritdoc />
public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length) =>
_bytesValue.Value;
/// <inheritdoc />
public override char GetChar(int ordinal) => _charValue.Value;
/// <inheritdoc />
public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length) =>
_charsValue.Value;
/// <inheritdoc />
public override string GetDataTypeName(int ordinal) => throw new NotImplementedException();
/// <inheritdoc />
public override DateTime GetDateTime(int ordinal) => _dateTimeValue.Value;
/// <inheritdoc />
public override decimal GetDecimal(int ordinal) => _decimalValue.Value;
/// <inheritdoc />
public override double GetDouble(int ordinal) => throw new NotImplementedException();
/// <inheritdoc />
public override Type GetFieldType(int ordinal) => throw new NotImplementedException();
/// <inheritdoc />
public override float GetFloat(int ordinal) => _floatValue.Value;
/// <inheritdoc />
public override Guid GetGuid(int ordinal) => _guidValue.Value;
/// <inheritdoc />
public override short GetInt16(int ordinal) => _int16Value.Value;
/// <inheritdoc />
public override int GetInt32(int ordinal) => _int32Value.Value;
/// <inheritdoc />
public override long GetInt64(int ordinal) => _int64Value.Value;
/// <inheritdoc />
public override string GetName(int ordinal) => throw new NotImplementedException();
/// <inheritdoc />
public override int GetOrdinal(string name) => throw new NotImplementedException();
/// <inheritdoc />
public override string GetString(int ordinal) => _stringValue.Value;
/// <inheritdoc />
public override object GetValue(int ordinal) => throw new NotImplementedException();
/// <inheritdoc />
public override int GetValues(object[] values) => throw new NotImplementedException();
/// <inheritdoc />
public override bool IsDBNull(int ordinal) => throw new NotImplementedException();
/// <inheritdoc />
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken) =>
Task.FromResult(_isNull);
/// <inheritdoc />
public override int FieldCount => throw new NotImplementedException();
/// <inheritdoc />
public override object this[int ordinal] => throw new NotImplementedException();
/// <inheritdoc />
public override object this[string name] => throw new NotImplementedException();
/// <inheritdoc />
public override int RecordsAffected => throw new NotImplementedException();
/// <inheritdoc />
public override bool HasRows => throw new NotImplementedException();
/// <inheritdoc />
public override bool IsClosed => throw new NotImplementedException();
/// <inheritdoc />
public override bool NextResult() => throw new NotImplementedException();
/// <inheritdoc />
public override bool Read() => throw new NotImplementedException();
/// <inheritdoc />
public override int Depth => throw new NotImplementedException();
/// <inheritdoc />
public override IEnumerator GetEnumerator() => throw new NotImplementedException();
}
}

View File

@ -113,6 +113,41 @@ public class DataReaderMaybeExtensionTests
Assert.That(result.Value, Is.EqualTo(25)); Assert.That(result.Value, Is.EqualTo(25));
} }
[Test]
public void DataReader_TryGetChar_WithDbNull_ReturnsEmptyMaybe()
{
// Arrange
var fakeDataReader = A.Fake<IDataReader>();
A.CallTo(() => fakeDataReader.IsDBNull(0))
.Returns(true);
// Act
var result = fakeDataReader.TryGetChar(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<char>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public void DataReader_TryGetChar_WithValue_ReturnsMaybeWithValue()
{
// Arrange
var fakeDataReader = A.Fake<IDataReader>();
A.CallTo(() => fakeDataReader.IsDBNull(0))
.Returns(false);
A.CallTo(() => fakeDataReader.GetChar(0))
.Returns('b');
// Act
var result = fakeDataReader.TryGetChar(0);
// Assert
Assert.That(result, Is.InstanceOf<Maybe<char>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo('b'));
}
[Test] [Test]
public void DataReader_TryGetChars_WithDbNull_ReturnsEmptyMaybe() public void DataReader_TryGetChars_WithDbNull_ReturnsEmptyMaybe()
{ {

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Data; using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
namespace FruityFoundation.Base.Structures; namespace FruityFoundation.Base.Structures;
@ -8,39 +10,91 @@ public static class DataReaderExtensions
public static Maybe<bool> TryGetBoolean(this IDataReader reader, int ord) => public static Maybe<bool> TryGetBoolean(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetBoolean); TryGet(reader, ord, reader.GetBoolean);
public static async Task<Maybe<bool>> TryGetBooleanAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetBoolean);
public static Maybe<byte> TryGetByte(this IDataReader reader, int ord) => public static Maybe<byte> TryGetByte(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetByte); TryGet(reader, ord, reader.GetByte);
public static async Task<Maybe<byte>> TryGetByteAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetByte);
public static Maybe<long> TryGetBytes(this IDataReader reader, int ord, long fieldOffset, byte[]? buffer, int bufferOffset, int length) => public static Maybe<long> TryGetBytes(this IDataReader reader, int ord, long fieldOffset, byte[]? buffer, int bufferOffset, int length) =>
TryGet(reader, ord, _ => reader.GetBytes(ord, fieldOffset, buffer, bufferOffset, length)); TryGet(reader, ord, _ => reader.GetBytes(ord, fieldOffset, buffer, bufferOffset, length));
public static async Task<Maybe<long>> TryGetBytesAsync(this DbDataReader reader, int ord, long fieldOffset, byte[]? buffer, int bufferOffset, int length) =>
await TryGetAsync(reader, ord, _ => reader.GetBytes(ord, fieldOffset, buffer, bufferOffset, length));
public static Maybe<char> TryGetChar(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetChar);
public static async Task<Maybe<char>> TryGetCharAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetChar);
public static Maybe<long> TryGetChars(this IDataReader reader, int ord, long fieldOffset, char[]? buffer, int bufferOffset, int length) => public static Maybe<long> TryGetChars(this IDataReader reader, int ord, long fieldOffset, char[]? buffer, int bufferOffset, int length) =>
TryGet(reader, ord, _ => reader.GetChars(ord, fieldOffset, buffer, bufferOffset, length)); TryGet(reader, ord, _ => reader.GetChars(ord, fieldOffset, buffer, bufferOffset, length));
public static async Task<Maybe<long>> TryGetCharsAsync(this DbDataReader reader, int ord, long fieldOffset, char[]? buffer, int bufferOffset, int length) =>
await TryGetAsync(reader, ord, _ => reader.GetChars(ord, fieldOffset, buffer, bufferOffset, length));
public static Maybe<DateTime> TryGetDateTime(this IDataReader reader, int ord) => public static Maybe<DateTime> TryGetDateTime(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetDateTime); TryGet(reader, ord, reader.GetDateTime);
public static async Task<Maybe<DateTime>> TryGetDateTimeAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetDateTime);
public static Maybe<decimal> TryGetDecimal(this IDataReader reader, int ord) => public static Maybe<decimal> TryGetDecimal(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetDecimal); TryGet(reader, ord, reader.GetDecimal);
public static async Task<Maybe<decimal>> TryGetDecimalAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetDecimal);
public static Maybe<float> TryGetFloat(this IDataReader reader, int ord) => public static Maybe<float> TryGetFloat(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetFloat); TryGet(reader, ord, reader.GetFloat);
public static async Task<Maybe<float>> TryGetFloatAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetFloat);
public static Maybe<Guid> TryGetGuid(this IDataReader reader, int ord) => public static Maybe<Guid> TryGetGuid(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetGuid); TryGet(reader, ord, reader.GetGuid);
public static async Task<Maybe<Guid>> TryGetGuidAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetGuid);
public static Maybe<short> TryGetInt16(this IDataReader reader, int ord) => public static Maybe<short> TryGetInt16(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetInt16); TryGet(reader, ord, reader.GetInt16);
public static async Task<Maybe<short>> TryGetInt16Async(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetInt16);
public static Maybe<int> TryGetInt32(this IDataReader reader, int ord) => public static Maybe<int> TryGetInt32(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetInt32); TryGet(reader, ord, reader.GetInt32);
public static async Task<Maybe<int>> TryGetInt32Async(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetInt32);
public static Maybe<long> TryGetInt64(this IDataReader reader, int ord) => public static Maybe<long> TryGetInt64(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetInt64); TryGet(reader, ord, reader.GetInt64);
public static async Task<Maybe<long>> TryGetInt64Async(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetInt64);
public static Maybe<string> TryGetString(this IDataReader reader, int ord) => public static Maybe<string> TryGetString(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetString); TryGet(reader, ord, reader.GetString);
public static async Task<Maybe<string>> TryGetStringAsync(this DbDataReader reader, int ord) =>
await TryGetAsync(reader, ord, reader.GetString);
private static Maybe<T> TryGet<T>(IDataRecord reader, int ord, Func<int, T> valueGetter) => private static Maybe<T> TryGet<T>(IDataRecord reader, int ord, Func<int, T> valueGetter) =>
reader.IsDBNull(ord) ? Maybe.Empty<T>() : valueGetter(ord); reader.IsDBNull(ord) ? Maybe.Empty<T>() : valueGetter(ord);
private static async Task<Maybe<T>> TryGetAsync<T>(DbDataReader reader, int ord, Func<int, T> valueGetter)
{
if (await reader.IsDBNullAsync(ord))
return Maybe.Empty<T>();
var value = valueGetter(ord);
return Maybe.Create(value);
}
} }