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); } }