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).ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetByteAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetByte, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); 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).ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetCharAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetChar, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); 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).ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetDateTimeAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetDateTime, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetDecimalAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetDecimal, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetFloatAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetFloat, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetGuidAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetGuid, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetInt16Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetInt16, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetInt32Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetInt32, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetInt64Async(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetInt64, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); public static async Task> TryGetStringAsync(this DbDataReader reader, int ord, CancellationToken cancellationToken = default) => await TryGetAsync(reader, ord, reader.GetString, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); private static async Task> TryGetAsync(DbDataReader reader, int ord, Func valueGetter, CancellationToken cancellationToken) { if (await reader.IsDBNullAsync(ord, cancellationToken).ConfigureAwait(continueOnCapturedContext: false)) return Maybe.Empty(); var value = valueGetter(ord); return Maybe.Create(value); } }