Skip to content

Commit

Permalink
add DateOnly and TimeOnly reader extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Aug 9, 2024
1 parent 4fe021d commit f3c4cb4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/FluentCommand/DataMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ static DataMapping()
{DbType.Boolean, typeof(bool)},
{DbType.Byte, typeof(byte)},
{DbType.Currency, typeof(decimal)},
{DbType.Date, typeof(DateTime)},
{DbType.DateTime, typeof(DateTime)},
{DbType.Decimal, typeof(decimal)},
{DbType.Double, typeof(double)},
Expand All @@ -62,13 +61,20 @@ static DataMapping()
{DbType.Single, typeof(float)},
{DbType.String, typeof(string)},
{DbType.StringFixedLength, typeof(string)},
{DbType.Time, typeof(TimeSpan)},
{DbType.UInt16, typeof(ushort)},
{DbType.UInt32, typeof(uint)},
{DbType.UInt64, typeof(ulong)},
{DbType.VarNumeric, typeof(decimal)},
{DbType.DateTime2, typeof(DateTime)},
{DbType.DateTimeOffset, typeof(DateTimeOffset)},
#if NET6_0_OR_GREATER
{DbType.Date, typeof(DateOnly)},
{DbType.Time, typeof(TimeOnly)},
#else
{DbType.Date, typeof(DateTime)},
{DbType.Time, typeof(TimeSpan)},
#endif

};
}

Expand Down
71 changes: 71 additions & 0 deletions src/FluentCommand/Extensions/DataRecordExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using System.Data.Common;

Expand Down Expand Up @@ -428,6 +429,76 @@ public static string GetStringNull(this IDataRecord dataRecord, string name)
return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetString(ordinal);
}

#if NET6_0_OR_GREATER
/// <summary>Gets the <see cref="DateOnly"/> value of the specified field.</summary>
/// <param name="dataRecord">The data record.</param>
/// <param name="name">The <paramref name="name"/> of the field to find.</param>
/// <returns>The <see cref="DateOnly"/> value of the specified field.</returns>
public static DateOnly GetDateOnly(this IDataRecord dataRecord, string name)
{
int ordinal = dataRecord.GetOrdinal(name);
if (dataRecord.IsDBNull(ordinal))
return default;

if (dataRecord is DbDataReader dataReader)
return dataReader.GetFieldValue<DateOnly>(ordinal);

var dateTime = dataRecord.GetDateTime(ordinal);
return DateOnly.FromDateTime(dateTime);
}

/// <summary>Gets the <see cref="DateOnly"/> value of the specified field.</summary>
/// <param name="dataRecord">The data record.</param>
/// <param name="name">The <paramref name="name"/> of the field to find.</param>
/// <returns>The <see cref="DateOnly"/> value of the specified field.</returns>
public static DateOnly? GetDateOnlyNull(this IDataRecord dataRecord, string name)
{
int ordinal = dataRecord.GetOrdinal(name);
if (dataRecord.IsDBNull(ordinal))
return null;

if (dataRecord is DbDataReader dataReader)
return dataReader.GetFieldValue<DateOnly>(ordinal);

var dateTime = dataRecord.GetDateTime(ordinal);
return DateOnly.FromDateTime(dateTime);
}

/// <summary>Gets the <see cref="TimeOnly"/> value of the specified field.</summary>
/// <param name="dataRecord">The data record.</param>
/// <param name="name">The <paramref name="name"/> of the field to find.</param>
/// <returns>The <see cref="TimeOnly"/> value of the specified field.</returns>
public static TimeOnly GetTimeOnly(this IDataRecord dataRecord, string name)
{
int ordinal = dataRecord.GetOrdinal(name);
if (dataRecord.IsDBNull(ordinal))
return default;

if (dataRecord is DbDataReader dataReader)
return dataReader.GetFieldValue<TimeOnly>(ordinal);

var dateTime = dataRecord.GetDateTime(ordinal);
return TimeOnly.FromDateTime(dateTime);
}

/// <summary>Gets the <see cref="TimeOnly"/> value of the specified field.</summary>
/// <param name="dataRecord">The data record.</param>
/// <param name="name">The <paramref name="name"/> of the field to find.</param>
/// <returns>The <see cref="TimeOnly"/> value of the specified field.</returns>
public static TimeOnly? GetTimeOnlyNull(this IDataRecord dataRecord, string name)
{
int ordinal = dataRecord.GetOrdinal(name);
if (dataRecord.IsDBNull(ordinal))
return null;

if (dataRecord is DbDataReader dataReader)
return dataReader.GetFieldValue<TimeOnly>(ordinal);

var dateTime = dataRecord.GetDateTime(ordinal);
return TimeOnly.FromDateTime(dateTime);
}
#endif

/// <summary>Gets the value of the specified field.</summary>
/// <param name="dataRecord">The data record.</param>
/// <param name="name">The <paramref name="name"/> of the field to find.</param>
Expand Down

0 comments on commit f3c4cb4

Please sign in to comment.