VEU.WathBody/3-app/1-lib/Kit.Helpers/SqlHelperAbstract/DataRecordSpecified.cs

129 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
namespace Kit.Helpers.SqlHelperAbstract
{
public interface IDataRecordSpecified
{
IDataReader Src { get; }
Npgsql.INpgsqlNameTranslator NameTranslator { get; }
TValue Convert<TValue>(object value);
}
internal class DataRecordSpecified : IDataRecordSpecified
{
public IDataReader Src { get; }
public Npgsql.INpgsqlNameTranslator NameTranslator { get; }
public virtual TValue Convert<TValue>(object value)
{
return (TValue)value;
}
public DataRecordSpecified(IDataReader dataReader, Npgsql.INpgsqlNameTranslator nameTranslator)
{
Src = dataReader;
NameTranslator = nameTranslator;
}
}
public static class DataRecordSpecifiedExtensions
{
/// <summary>
/// Проверка на наличие столбца в запросе
/// </summary>
/// <param name="reader"></param>
/// <param name="name"></param>
/// <returns></returns>
public static bool HasColumn(this IDataRecordSpecified reader, string name)
{
string nameTranslated = reader.NameTranslator.TranslateMemberName(name).ToLower();
for (int n = 0; n < reader.Src.FieldCount; n++)
{
string fieldNameTranslated = reader.NameTranslator.TranslateMemberName(reader.Src.GetName(n)).ToLower();
if (fieldNameTranslated == nameTranslated) return true;
}
return false;
}
private static string GetTranslatedName(this IDataRecordSpecified record, string fieldName) => record.NameTranslator.TranslateMemberName(fieldName);
#if NETFRAMEWORK
private static TValue TryGet<TValue>(this IDataRecordSpecified record, string fieldName, bool isStrict)
#elif NETCOREAPP
private static TValue? TryGet<TValue>(this IDataRecordSpecified record, string fieldName, bool isStrict)
#endif
{
fieldName = GetTranslatedName(record, fieldName);
object value = record.Src[fieldName];
if (value == DBNull.Value)
{
if (isStrict)
throw new InvalidCastException(string.Format("{0} {1} нельзя привести к {2}", fieldName, typeof(DBNull).FullName, typeof(TValue).FullName));
return default;
}
try
{
return record.Convert<TValue>(value);
}
catch (InvalidCastException ex)
{
throw new InvalidCastException(string.Format("{0} {1} нельзя привести к {2}", fieldName, value.GetType().FullName, typeof(TValue).FullName), ex);
}
}
public static bool IsDbNull(this IDataRecordSpecified record, string fieldName) => record.Src[fieldName] == DBNull.Value;
public static bool IsNotDbNull(this IDataRecordSpecified record, string fieldName) => !record.IsDbNull(fieldName);
public static TValue Get<TValue>(this IDataRecordSpecified record, string fieldName) => record.TryGet<TValue>(fieldName, isStrict: true);
#if NETFRAMEWORK
public static TValue GetN<TValue>(this IDataRecordSpecified record, string fieldName)
#elif NETCOREAPP
public static TValue? GetN<TValue>(this IDataRecordSpecified record, string fieldName)
#endif
=> record.TryGet<TValue>(fieldName, isStrict: false);
internal static void ProcessExtraSelect<TEntity>(this IDataRecordSpecified readerSpecified, IList<TEntity> selectResult, SelectType extraSelectType = SelectType.Default)
{
switch (extraSelectType)
{
case SelectType.Page:
{
var result = (IEnumerableWithPage<TEntity>)selectResult;
// считывание данных постраничного вывода
while (readerSpecified.Src.Read())
{
if (readerSpecified.HasColumn("FilteredRows")) result.FilterRow = readerSpecified.Get<int>("filteredRows");
if (readerSpecified.HasColumn("TotalRows")) result.TotalRows = readerSpecified.Get<int>("totalRows");
if (readerSpecified.HasColumn("PageNo")) result.PageNo = readerSpecified.Get<int>("pageNo");
if (readerSpecified.HasColumn("PageSize")) result.PageSize = readerSpecified.Get<int>("pageSize");
if (readerSpecified.HasColumn("Sort")) result.Sort = readerSpecified.Get<string>("sort");
//if (readerSpecified.HasColumn("Dir")) result.Dir = readerSpecified.Get<string>("dir");
}
}
break;
case SelectType.Offer:
{
var result = (IEnumerableWithOffer<TEntity>)selectResult;
// считывание данных постраничного вывода
while (readerSpecified.Src.Read())
{
if (readerSpecified.HasColumn("FilteredRows")) result.FilterRow = readerSpecified.Get<int>("FilteredRows");
if (readerSpecified.HasColumn("TotalRows")) result.TotalRows = readerSpecified.Get<int>("TotalRows");
if (readerSpecified.HasColumn("Start")) result.Start = readerSpecified.Get<int>("Start");
if (readerSpecified.HasColumn("Length")) result.Lenght = readerSpecified.Get<int>("Length");
if (readerSpecified.HasColumn("Sort")) result.Sort = readerSpecified.Get<string>("Sort");
//if (readerSpecified.HasColumn("Dir")) result.Dir = readerSpecified.Get<string>("Dir");
}
}
break;
case SelectType.Default:
default:
break;
}
}
}
}