using System; using System.Data; namespace FruityFoundation.Base.Structures; public static class DataReaderExtensions { public static Maybe TryGetBoolean(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetBoolean); public static Maybe TryGetByte(this IDataReader reader, int ord) => TryGet(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 Maybe TryGetChar(this IDataReader reader, int ord) => TryGet(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 Maybe TryGetDateTime(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetDateTime); public static Maybe TryGetDecimal(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetDecimal); public static Maybe TryGetFloat(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetFloat); public static Maybe TryGetGuid(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetGuid); public static Maybe TryGetInt16(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetInt16); public static Maybe TryGetInt32(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetInt32); public static Maybe TryGetInt64(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetInt64); public static Maybe TryGetString(this IDataReader reader, int ord) => TryGet(reader, ord, reader.GetString); private static Maybe TryGet(IDataRecord reader, int ord, Func valueGetter) => reader.IsDBNull(ord) ? Maybe.Empty() : valueGetter(ord); }