feat: add cancellation token support to DbDataReaderExtensions

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

View File

@ -8,7 +8,7 @@ using NUnit.Framework;
namespace Base.Tests.Structures; namespace Base.Tests.Structures;
public class DataReaderAsyncMaybeExtensionTests public class DbDataReaderAsyncMaybeExtensionTests
{ {
[Test] [Test]
public async Task DbDataReader_TryGetBooleanAsync_WithDbNull_ReturnsEmptyMaybe() public async Task DbDataReader_TryGetBooleanAsync_WithDbNull_ReturnsEmptyMaybe()

View File

@ -1,7 +1,5 @@
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;
@ -10,91 +8,42 @@ 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) => public static Maybe<char> TryGetChar(this IDataReader reader, int ord) =>
TryGet(reader, ord, reader.GetChar); 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);
}
} }

View File

@ -0,0 +1,58 @@
using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
namespace FruityFoundation.Base.Structures;
public static class DbDataReaderMaybeExtensions
{
public static async Task<Maybe<bool>> TryGetBooleanAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetBoolean, cancellationToken);
public static async Task<Maybe<byte>> TryGetByteAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetByte, cancellationToken);
public static async Task<Maybe<long>> TryGetBytesAsync(this DbDataReader reader, int ord, long fieldOffset, byte[]? buffer, int bufferOffset, int length, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, _ => reader.GetBytes(ord, fieldOffset, buffer, bufferOffset, length), cancellationToken);
public static async Task<Maybe<char>> TryGetCharAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetChar, cancellationToken);
public static async Task<Maybe<long>> TryGetCharsAsync(this DbDataReader reader, int ord, long fieldOffset, char[]? buffer, int bufferOffset, int length, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, _ => reader.GetChars(ord, fieldOffset, buffer, bufferOffset, length), cancellationToken);
public static async Task<Maybe<DateTime>> TryGetDateTimeAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetDateTime, cancellationToken);
public static async Task<Maybe<decimal>> TryGetDecimalAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetDecimal, cancellationToken);
public static async Task<Maybe<float>> TryGetFloatAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetFloat, cancellationToken);
public static async Task<Maybe<Guid>> TryGetGuidAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetGuid, cancellationToken);
public static async Task<Maybe<short>> TryGetInt16Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetInt16, cancellationToken);
public static async Task<Maybe<int>> TryGetInt32Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetInt32, cancellationToken);
public static async Task<Maybe<long>> TryGetInt64Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetInt64, cancellationToken);
public static async Task<Maybe<string>> TryGetStringAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) =>
await TryGetAsync(reader, ord, reader.GetString, cancellationToken);
private static async Task<Maybe<T>> TryGetAsync<T>(DbDataReader reader, int ord, Func<int, T> valueGetter, CancellationToken cancellationToken)
{
if (await reader.IsDBNullAsync(ord, cancellationToken))
return Maybe.Empty<T>();
var value = valueGetter(ord);
return Maybe.Create(value);
}
}