2023-08-01 22:12:54 -04:00
|
|
|
using System;
|
2022-12-23 12:38:51 -05:00
|
|
|
using System.Data;
|
2024-05-03 18:08:35 -04:00
|
|
|
using System.Data.Common;
|
|
|
|
using System.Threading.Tasks;
|
2022-03-31 20:34:17 -04:00
|
|
|
|
2023-08-01 22:12:54 -04:00
|
|
|
namespace FruityFoundation.Base.Structures;
|
2022-03-31 20:34:17 -04:00
|
|
|
|
|
|
|
public static class DataReaderExtensions
|
|
|
|
{
|
|
|
|
public static Maybe<bool> TryGetBoolean(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetBoolean);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<bool>> TryGetBooleanAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetBoolean);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<byte> TryGetByte(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetByte);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<byte>> TryGetByteAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetByte);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<long> TryGetBytes(this IDataReader reader, int ord, long fieldOffset, byte[]? buffer, int bufferOffset, int length) =>
|
|
|
|
TryGet(reader, ord, _ => reader.GetBytes(ord, fieldOffset, buffer, bufferOffset, length));
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<long>> 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<char> TryGetChar(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetChar);
|
|
|
|
|
|
|
|
public static async Task<Maybe<char>> TryGetCharAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetChar);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<long> TryGetChars(this IDataReader reader, int ord, long fieldOffset, char[]? buffer, int bufferOffset, int length) =>
|
|
|
|
TryGet(reader, ord, _ => reader.GetChars(ord, fieldOffset, buffer, bufferOffset, length));
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<long>> 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));
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<DateTime> TryGetDateTime(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetDateTime);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<DateTime>> TryGetDateTimeAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetDateTime);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<decimal> TryGetDecimal(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetDecimal);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<decimal>> TryGetDecimalAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetDecimal);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<float> TryGetFloat(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetFloat);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<float>> TryGetFloatAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetFloat);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<Guid> TryGetGuid(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetGuid);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<Guid>> TryGetGuidAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetGuid);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<short> TryGetInt16(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetInt16);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<short>> TryGetInt16Async(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetInt16);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<int> TryGetInt32(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetInt32);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<int>> TryGetInt32Async(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetInt32);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<long> TryGetInt64(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetInt64);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<long>> TryGetInt64Async(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetInt64);
|
|
|
|
|
2022-03-31 20:34:17 -04:00
|
|
|
public static Maybe<string> TryGetString(this IDataReader reader, int ord) =>
|
|
|
|
TryGet(reader, ord, reader.GetString);
|
|
|
|
|
2024-05-03 18:08:35 -04:00
|
|
|
public static async Task<Maybe<string>> TryGetStringAsync(this DbDataReader reader, int ord) =>
|
|
|
|
await TryGetAsync(reader, ord, reader.GetString);
|
|
|
|
|
2023-08-01 22:12:54 -04:00
|
|
|
private static Maybe<T> TryGet<T>(IDataRecord reader, int ord, Func<int, T> valueGetter) =>
|
|
|
|
reader.IsDBNull(ord) ? Maybe.Empty<T>() : valueGetter(ord);
|
2024-05-03 18:08:35 -04:00
|
|
|
|
|
|
|
private static async Task<Maybe<T>> TryGetAsync<T>(DbDataReader reader, int ord, Func<int, T> valueGetter)
|
|
|
|
{
|
|
|
|
if (await reader.IsDBNullAsync(ord))
|
|
|
|
return Maybe.Empty<T>();
|
|
|
|
|
|
|
|
var value = valueGetter(ord);
|
|
|
|
|
|
|
|
return Maybe.Create(value);
|
|
|
|
}
|
2023-08-01 22:12:54 -04:00
|
|
|
}
|