using System; using System.Data; using System.Data.Common; using System.Threading.Tasks; namespace FruityFoundation.Base.Structures; 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); } }