2017-01-07 21:54:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Data;
|
2022-05-14 17:23:35 +08:00
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
namespace SqlSugar
|
|
|
|
|
{
|
|
|
|
|
public static partial class IDataRecordExtensions
|
|
|
|
|
{
|
2017-06-25 23:42:53 +08:00
|
|
|
|
|
2017-07-09 21:11:32 +08:00
|
|
|
|
#region Common Extensions
|
2022-05-14 17:23:35 +08:00
|
|
|
|
public static XElement GetXelement(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
var result = XElement.Parse(dr.GetString(i).ToString());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-06-25 23:42:53 +08:00
|
|
|
|
public static Guid GetStringGuid(this IDataRecord dr, int i)
|
|
|
|
|
{
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = Guid.Parse(dr.GetValue(i).ToString());
|
|
|
|
|
return result;
|
2017-06-25 23:42:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Guid? GetConvertStringGuid(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return Guid.Empty;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = Guid.Parse(dr.GetValue(i).ToString());
|
|
|
|
|
return result;
|
2017-06-25 23:42:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public static bool? GetConvertBoolean(this IDataRecord dr, int i)
|
|
|
|
|
{
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetBoolean(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte? GetConvertByte(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetByte(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static char? GetConvertChar(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetChar(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTime? GetConvertDateTime(this IDataRecord dr, int i)
|
|
|
|
|
{
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetDateTime(i);
|
|
|
|
|
if (result == DateTime.MinValue)
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
2017-08-26 14:51:57 +08:00
|
|
|
|
return null; ;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2018-04-08 16:31:27 +08:00
|
|
|
|
public static DateTime? GetConvertTime(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
var result = dr.GetValue(i);
|
|
|
|
|
if (result == DBNull.Value)
|
|
|
|
|
{
|
|
|
|
|
return null; ;
|
|
|
|
|
}
|
|
|
|
|
return Convert.ToDateTime(result.ToString());
|
|
|
|
|
}
|
|
|
|
|
public static DateTime GetTime(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToDateTime(dr.GetValue(i).ToString());
|
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
|
|
|
|
public static decimal? GetConvertDecimal(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetDecimal(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 23:42:53 +08:00
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public static double? GetConvertDouble(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetDouble(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 00:13:18 +08:00
|
|
|
|
|
|
|
|
|
public static float? GetConvertDoubleToFloat(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var result = dr.GetDouble(i);
|
|
|
|
|
return Convert.ToSingle(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public static Guid? GetConvertGuid(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetGuid(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static short? GetConvertInt16(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetInt16(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Int32? GetConvertInt32(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetInt32(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
2021-02-04 21:47:14 +08:00
|
|
|
|
//public static T GetConvertValue<T>(this IDataRecord dr, int i)
|
|
|
|
|
//{
|
|
|
|
|
// try
|
|
|
|
|
// {
|
|
|
|
|
// if (dr.IsDBNull(i))
|
|
|
|
|
// {
|
|
|
|
|
// return default(T);
|
|
|
|
|
// }
|
|
|
|
|
// var result = dr.GetValue(i);
|
|
|
|
|
// return UtilMethods.To<T>(result);
|
|
|
|
|
// }
|
|
|
|
|
// catch (Exception ex)
|
|
|
|
|
// {
|
|
|
|
|
// return OtherException<T>(dr, i, ex);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2019-05-24 19:06:36 +08:00
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public static long? GetConvetInt64(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetInt64(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float? GetConvertFloat(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = dr.GetFloat(i);
|
|
|
|
|
return result;
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-20 12:44:10 +08:00
|
|
|
|
public static DateTime GetdatetimeoffsetDate(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return DateTime.MinValue;
|
|
|
|
|
}
|
|
|
|
|
var offsetValue = (DateTimeOffset)dr.GetValue(i);
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = offsetValue.DateTime;
|
|
|
|
|
return result;
|
2017-09-20 12:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTime? GetConvertdatetimeoffsetDate(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return DateTime.MinValue;
|
|
|
|
|
}
|
|
|
|
|
var offsetValue = (DateTimeOffset)dr.GetValue(i);
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = offsetValue.DateTime;
|
|
|
|
|
return result;
|
2017-09-20 12:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTimeOffset Getdatetimeoffset(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return default(DateTimeOffset);
|
|
|
|
|
}
|
2020-12-30 18:38:55 +08:00
|
|
|
|
var date = dr.GetValue(i);
|
|
|
|
|
if (date is DateTime)
|
|
|
|
|
{
|
|
|
|
|
return UtilMethods.GetDateTimeOffsetByDateTime((DateTime)(date));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var result = (DateTimeOffset)date;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-09-20 12:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTimeOffset? GetConvertdatetimeoffset(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return default(DateTimeOffset);
|
|
|
|
|
}
|
2020-12-30 18:38:55 +08:00
|
|
|
|
var date = dr.GetValue(i);
|
|
|
|
|
if (date is DateTime)
|
|
|
|
|
{
|
|
|
|
|
return UtilMethods.GetDateTimeOffsetByDateTime((DateTime)(date));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var result = (DateTimeOffset)date;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-09-20 12:44:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-07 14:25:33 +08:00
|
|
|
|
public static string GetConvertString(this IDataRecord dr, int i)
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = Convert.ToString(dr.GetValue(i));
|
|
|
|
|
return result;
|
2017-06-07 14:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public static Nullable<T> GetOtherNull<T>(this IDataReader dr, int i) where T : struct
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-02-04 21:47:14 +08:00
|
|
|
|
return GetOther<T>(dr,i);
|
2017-01-07 21:54:51 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 23:42:53 +08:00
|
|
|
|
public static T GetOther<T>(this IDataReader dr, int i)
|
2017-01-07 21:54:51 +08:00
|
|
|
|
{
|
2021-02-04 21:47:14 +08:00
|
|
|
|
try
|
2019-05-24 19:10:02 +08:00
|
|
|
|
{
|
2021-02-04 21:47:14 +08:00
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
var result = dr.GetValue(i);
|
|
|
|
|
return UtilMethods.To<T>(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return OtherException<T>(dr, i, ex);
|
2019-05-24 19:10:02 +08:00
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 20:25:11 +08:00
|
|
|
|
public static T GetJson<T>(this IDataReader dr, int i)
|
|
|
|
|
{
|
|
|
|
|
var obj = dr.GetValue(i);
|
|
|
|
|
if (obj == null)
|
|
|
|
|
return default(T);
|
|
|
|
|
var value = obj.ObjToString();
|
|
|
|
|
return new SerializeService().DeserializeObject<T>(value);
|
|
|
|
|
}
|
2020-10-13 18:14:16 +08:00
|
|
|
|
public static T GetArray<T>(this IDataReader dr, int i)
|
|
|
|
|
{
|
|
|
|
|
//pgsql
|
|
|
|
|
var obj = dr.GetValue(i);
|
|
|
|
|
if (obj == null)
|
|
|
|
|
return default(T);
|
|
|
|
|
return (T)obj;
|
|
|
|
|
}
|
2019-05-16 20:25:11 +08:00
|
|
|
|
|
2017-01-07 21:54:51 +08:00
|
|
|
|
public static Nullable<T> GetConvertEnum_Null<T>(this IDataReader dr, int i) where T : struct
|
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
object value = dr.GetValue(i);
|
2021-12-29 11:52:27 +08:00
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2022-07-19 09:46:57 +08:00
|
|
|
|
var valueType = value.GetType();
|
|
|
|
|
if (valueType.IsIn(UtilConstants.FloatType, UtilConstants.DecType, UtilConstants.DobType))
|
2021-12-29 11:52:27 +08:00
|
|
|
|
{
|
|
|
|
|
value = Convert.ToUInt32(value);
|
|
|
|
|
}
|
2022-07-19 09:46:57 +08:00
|
|
|
|
else if (valueType == UtilConstants.StringType)
|
2021-12-29 11:52:27 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)Enum.Parse(typeof(T), value.ObjToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
T t = (T)Enum.ToObject(typeof(T), value);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T GetEnum<T>(this IDataReader dr, int i) where T : struct
|
|
|
|
|
{
|
|
|
|
|
object value = dr.GetValue(i);
|
2019-05-17 17:15:24 +08:00
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2022-07-19 09:46:57 +08:00
|
|
|
|
var valueType = value.GetType();
|
|
|
|
|
if (valueType.IsIn(UtilConstants.FloatType, UtilConstants.DecType, UtilConstants.DobType))
|
2019-05-17 17:15:24 +08:00
|
|
|
|
{
|
|
|
|
|
value = Convert.ToUInt32(value);
|
|
|
|
|
}
|
2022-07-19 09:46:57 +08:00
|
|
|
|
else if (valueType == UtilConstants.StringType)
|
2019-05-17 17:15:24 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)Enum.Parse(typeof(T), value.ObjToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-07 21:54:51 +08:00
|
|
|
|
T t = (T)Enum.ToObject(typeof(T), value);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
2017-04-23 14:46:06 +08:00
|
|
|
|
|
2019-05-11 14:39:00 +08:00
|
|
|
|
public static object GetEntity(this IDataReader dr, SqlSugarProvider context)
|
2017-04-23 14:46:06 +08:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-07-09 19:12:41 +08:00
|
|
|
|
|
2021-02-04 21:47:14 +08:00
|
|
|
|
|
|
|
|
|
private static T OtherException<T>(IDataRecord dr, int i, Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (dr.GetFieldType(i) == UtilConstants.DateType)
|
|
|
|
|
{
|
|
|
|
|
return UtilMethods.To<T>(dr.GetConvertDouble(i));
|
|
|
|
|
}
|
|
|
|
|
if (dr.GetFieldType(i) == UtilConstants.GuidType)
|
|
|
|
|
{
|
|
|
|
|
var data = dr.GetString(i);
|
|
|
|
|
if (data.ToString() == "")
|
|
|
|
|
{
|
2021-02-04 23:17:13 +08:00
|
|
|
|
return UtilMethods.To<T>(default(T));
|
2021-02-04 21:47:14 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return UtilMethods.To<T>(Guid.Parse(data.ToString()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new Exception(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-09 21:11:32 +08:00
|
|
|
|
#endregion
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|