2017-01-07 21:54:51 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Data;
|
|
|
|
|
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
|
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
|
|
|
|
}
|
2019-05-23 20:37:04 +08:00
|
|
|
|
public static object GetValue(this IDataRecord dr, int i)
|
2019-05-23 19:25:51 +08:00
|
|
|
|
{
|
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var result = dr.GetValue(i);
|
|
|
|
|
if (result.GetType().Name == "SqlHierarchyId")
|
|
|
|
|
{
|
|
|
|
|
result = result.ObjToString();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = (DateTimeOffset)dr.GetValue(i);
|
|
|
|
|
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);
|
|
|
|
|
}
|
2018-01-31 16:41:30 +08:00
|
|
|
|
var result = (DateTimeOffset)dr.GetValue(i);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (value.GetType() == UtilConstants.DecType)
|
|
|
|
|
{
|
|
|
|
|
value = Convert.ToUInt32(value);
|
|
|
|
|
}
|
|
|
|
|
else if (value.GetType() == UtilConstants.StringType)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
2017-07-09 21:11:32 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Sqlite Extensions
|
2017-07-09 19:12:41 +08:00
|
|
|
|
public static Nullable<T> GetSqliteTypeNull<T>(this IDataReader dr, int i) where T : struct
|
|
|
|
|
{
|
2017-08-25 22:25:29 +08:00
|
|
|
|
var type = UtilMethods.GetUnderType(typeof(T));
|
2017-07-09 21:11:32 +08:00
|
|
|
|
if (dr.IsDBNull(i))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return SqliteTypeConvert<T>(dr, i, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T GetSqliteType<T>(this IDataReader dr, int i) where T : struct
|
|
|
|
|
{
|
|
|
|
|
var type = typeof(T);
|
|
|
|
|
return SqliteTypeConvert<T>(dr, i, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static T SqliteTypeConvert<T>(IDataReader dr, int i, Type type) where T : struct
|
|
|
|
|
{
|
2017-08-25 22:22:12 +08:00
|
|
|
|
if (type.IsIn(UtilConstants.IntType))
|
2017-07-09 19:12:41 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)((object)(dr.GetInt32(i)));
|
|
|
|
|
}
|
2017-08-25 22:22:12 +08:00
|
|
|
|
else if (type == UtilConstants.DateType)
|
2017-07-09 19:12:41 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(Convert.ToDateTime(dr.GetString(i)), type);
|
|
|
|
|
}
|
2017-08-25 22:22:12 +08:00
|
|
|
|
else if (type == UtilConstants.DecType)
|
2017-07-09 19:12:41 +08:00
|
|
|
|
{
|
2017-07-09 21:11:32 +08:00
|
|
|
|
return (T)Convert.ChangeType(dr.GetDecimal(i), type);
|
2017-07-09 19:12:41 +08:00
|
|
|
|
}
|
2017-08-25 22:22:12 +08:00
|
|
|
|
else if (type == UtilConstants.DobType)
|
2017-07-09 19:12:41 +08:00
|
|
|
|
{
|
2017-07-09 21:11:32 +08:00
|
|
|
|
return (T)Convert.ChangeType(dr.GetDouble(i), type);
|
|
|
|
|
}
|
2017-08-25 22:22:12 +08:00
|
|
|
|
else if (type == UtilConstants.BoolType)
|
2017-07-09 21:11:32 +08:00
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(dr.GetBoolean(i), type);
|
|
|
|
|
}
|
2017-10-05 10:59:22 +08:00
|
|
|
|
else if (type == UtilConstants.LongType)
|
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType(dr.GetInt64(i), type);
|
|
|
|
|
}
|
2017-08-25 22:22:12 +08:00
|
|
|
|
else if (type == UtilConstants.GuidType)
|
2017-07-09 21:11:32 +08:00
|
|
|
|
{
|
|
|
|
|
string guidString = dr.GetString(i);
|
|
|
|
|
string changeValue = guidString.IsNullOrEmpty() ? Guid.Empty.ToString() : guidString;
|
|
|
|
|
return (T)Convert.ChangeType(Guid.Parse(changeValue), type);
|
2017-07-09 19:12:41 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (T)Convert.ChangeType((dr.GetString(i)), type);
|
|
|
|
|
}
|
2019-05-16 20:25:11 +08:00
|
|
|
|
}
|
2017-07-09 21:11:32 +08:00
|
|
|
|
#endregion
|
2017-01-07 21:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|