From 04f94c2441fc5f589c8a7ec7f4565f144e2d2d73 Mon Sep 17 00:00:00 2001 From: Kyle Ratti Date: Fri, 3 May 2024 18:16:00 -0400 Subject: [PATCH] feat: add cancellation token support to DbDataReaderExtensions --- ...> DbDataReaderAsyncMaybeExtensionTests.cs} | 2 +- Base/Structures/DataReaderMaybeExtensions.cs | 51 ---------------- .../Structures/DbDataReaderMaybeExtensions.cs | 58 +++++++++++++++++++ 3 files changed, 59 insertions(+), 52 deletions(-) rename Base.Tests/Structures/{DataReaderAsyncMaybeExtensionTests.cs => DbDataReaderAsyncMaybeExtensionTests.cs} (99%) create mode 100644 Base/Structures/DbDataReaderMaybeExtensions.cs diff --git a/Base.Tests/Structures/DataReaderAsyncMaybeExtensionTests.cs b/Base.Tests/Structures/DbDataReaderAsyncMaybeExtensionTests.cs similarity index 99% rename from Base.Tests/Structures/DataReaderAsyncMaybeExtensionTests.cs rename to Base.Tests/Structures/DbDataReaderAsyncMaybeExtensionTests.cs index 23f5386..bf70487 100644 --- a/Base.Tests/Structures/DataReaderAsyncMaybeExtensionTests.cs +++ b/Base.Tests/Structures/DbDataReaderAsyncMaybeExtensionTests.cs @@ -8,7 +8,7 @@ using NUnit.Framework; namespace Base.Tests.Structures; -public class DataReaderAsyncMaybeExtensionTests +public class DbDataReaderAsyncMaybeExtensionTests { [Test] public async Task DbDataReader_TryGetBooleanAsync_WithDbNull_ReturnsEmptyMaybe() diff --git a/Base/Structures/DataReaderMaybeExtensions.cs b/Base/Structures/DataReaderMaybeExtensions.cs index 252b2ba..8a9261e 100644 --- a/Base/Structures/DataReaderMaybeExtensions.cs +++ b/Base/Structures/DataReaderMaybeExtensions.cs @@ -1,7 +1,5 @@ using System; using System.Data; -using System.Data.Common; -using System.Threading.Tasks; namespace FruityFoundation.Base.Structures; @@ -10,91 +8,42 @@ public static class DataReaderExtensions public static Maybe TryGetBoolean(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetBoolean); - public static async Task> TryGetBooleanAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetBoolean); - public static Maybe TryGetByte(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetByte); - public static async Task> TryGetByteAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetByte); - public static Maybe TryGetBytes(this IDataReader reader, int ord, long fieldOffset, byte[]? buffer, int bufferOffset, int length) => TryGet(reader, ord, _ => reader.GetBytes(ord, fieldOffset, buffer, bufferOffset, length)); - public static async Task> 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 TryGetChar(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetChar); - public static async Task> TryGetCharAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetChar); - public static Maybe TryGetChars(this IDataReader reader, int ord, long fieldOffset, char[]? buffer, int bufferOffset, int length) => TryGet(reader, ord, _ => reader.GetChars(ord, fieldOffset, buffer, bufferOffset, length)); - public static async Task> 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 TryGetDateTime(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetDateTime); - public static async Task> TryGetDateTimeAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetDateTime); - public static Maybe TryGetDecimal(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetDecimal); - public static async Task> TryGetDecimalAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetDecimal); - public static Maybe TryGetFloat(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetFloat); - public static async Task> TryGetFloatAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetFloat); - public static Maybe TryGetGuid(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetGuid); - public static async Task> TryGetGuidAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetGuid); - public static Maybe TryGetInt16(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetInt16); - public static async Task> TryGetInt16Async(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetInt16); - public static Maybe TryGetInt32(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetInt32); - public static async Task> TryGetInt32Async(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetInt32); - public static Maybe TryGetInt64(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetInt64); - public static async Task> TryGetInt64Async(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetInt64); - public static Maybe TryGetString(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetString); - public static async Task> TryGetStringAsync(this DbDataReader reader, int ord) => - await TryGetAsync(reader, ord, reader.GetString); - private static Maybe TryGet(IDataRecord reader, int ord, Func valueGetter) => reader.IsDBNull(ord) ? Maybe.Empty() : valueGetter(ord); - - private static async Task> TryGetAsync(DbDataReader reader, int ord, Func valueGetter) - { - if (await reader.IsDBNullAsync(ord)) - return Maybe.Empty(); - - var value = valueGetter(ord); - - return Maybe.Create(value); - } } diff --git a/Base/Structures/DbDataReaderMaybeExtensions.cs b/Base/Structures/DbDataReaderMaybeExtensions.cs new file mode 100644 index 0000000..342d893 --- /dev/null +++ b/Base/Structures/DbDataReaderMaybeExtensions.cs @@ -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> TryGetBooleanAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetBoolean, cancellationToken); + + public static async Task> TryGetByteAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetByte, cancellationToken); + + public static async Task> 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> TryGetCharAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetChar, cancellationToken); + + public static async Task> 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> TryGetDateTimeAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetDateTime, cancellationToken); + + public static async Task> TryGetDecimalAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetDecimal, cancellationToken); + + public static async Task> TryGetFloatAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetFloat, cancellationToken); + + public static async Task> TryGetGuidAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetGuid, cancellationToken); + + public static async Task> TryGetInt16Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetInt16, cancellationToken); + + public static async Task> TryGetInt32Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetInt32, cancellationToken); + + public static async Task> TryGetInt64Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetInt64, cancellationToken); + + public static async Task> TryGetStringAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => + await TryGetAsync(reader, ord, reader.GetString, cancellationToken); + + private static async Task> TryGetAsync(DbDataReader reader, int ord, Func valueGetter, CancellationToken cancellationToken) + { + if (await reader.IsDBNullAsync(ord, cancellationToken)) + return Maybe.Empty(); + + var value = valueGetter(ord); + + return Maybe.Create(value); + } +}