Code optimization

This commit is contained in:
sunkaixuan 2019-05-24 19:06:36 +08:00
parent 6296a7829d
commit 771265e250
4 changed files with 45 additions and 86 deletions

View File

@ -151,10 +151,6 @@ namespace SqlSugar
{
return "long";
}
else if (dbTypeName.Contains("hierarchyid"))
{
return "object";
}
else if (dbTypeName == "int16")
{
return "short";
@ -173,8 +169,7 @@ namespace SqlSugar
}
else if (propertyTypes == null || propertyTypes.Count() == 0)
{
Check.ThrowNotSupportedException(string.Format(" \"{0}\" Type NotSupported, DbBindProvider.GetPropertyTypeName error.", dbTypeName));
return null;
return "object";
}
else if (propertyTypes.First().Value == CSharpDataType.byteArray)
{

View File

@ -37,7 +37,7 @@ namespace SqlSugar
private static readonly MethodInfo getInt32 = typeof(IDataRecord).GetMethod("GetInt32", new Type[] { typeof(int) });
private static readonly MethodInfo getInt64 = typeof(IDataRecord).GetMethod("GetInt64", new Type[] { typeof(int) });
private static readonly MethodInfo getString = typeof(IDataRecord).GetMethod("GetString", new Type[] { typeof(int) });
private static readonly MethodInfo getValueMethod = typeof(IDataRecordExtensions).GetMethod("GetValue");
private static readonly MethodInfo getConvertValueMethod = typeof(IDataRecordExtensions).GetMethod("GetConvertValue");
private static readonly MethodInfo getdatetimeoffset = typeof(IDataRecordExtensions).GetMethod("Getdatetimeoffset");
private static readonly MethodInfo getdatetimeoffsetDate = typeof(IDataRecordExtensions).GetMethod("GetdatetimeoffsetDate");
private static readonly MethodInfo getStringGuid = typeof(IDataRecordExtensions).GetMethod("GetStringGuid");
@ -64,8 +64,6 @@ namespace SqlSugar
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
private static readonly MethodInfo getJson = typeof(IDataRecordExtensions).GetMethod("GetJson");
private static readonly MethodInfo getSqliteTypeNull = typeof(IDataRecordExtensions).GetMethod("GetSqliteTypeNull");
private static readonly MethodInfo getSqliteType = typeof(IDataRecordExtensions).GetMethod("GetSqliteType");
private static readonly MethodInfo getEntity = typeof(IDataRecordExtensions).GetMethod("GetEntity", new Type[] { typeof(SqlSugarProvider) });
private delegate T Load(IDataRecord dataRecord);
@ -205,16 +203,9 @@ namespace SqlSugar
{
method = getString;
}
else if (bindPropertyType == UtilConstants.ByteArrayType)
{
method = getValueMethod;
generator.Emit(OpCodes.Call, method);
generator.Emit(OpCodes.Unbox_Any, columnInfo.PropertyInfo.PropertyType);
return;
}
else
{
method = isNullableType ? getSqliteTypeNull.MakeGenericMethod(bindPropertyType) : getSqliteType.MakeGenericMethod(bindPropertyType);
method = getConvertValueMethod.MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
}
generator.Emit(OpCodes.Call, method);
return;
@ -300,7 +291,7 @@ namespace SqlSugar
method = isNullableType ? getConvertdatetimeoffsetDate : getdatetimeoffsetDate;
break;
default:
method = getValueMethod;
method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
break;
}
if (method == null && bindPropertyType == UtilConstants.StringType)
@ -309,15 +300,12 @@ namespace SqlSugar
}
if (bindPropertyType == UtilConstants.ObjType)
{
method = getValueMethod;
method = getConvertValueMethod.MakeGenericMethod(bindPropertyType);
}
if (method == null)
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
generator.Emit(OpCodes.Call, method);
if (method == getValueMethod)
{
generator.Emit(OpCodes.Unbox_Any, columnInfo.PropertyInfo.PropertyType);
}
#endregion
}

View File

@ -136,19 +136,16 @@ namespace SqlSugar
var result = dr.GetInt32(i);
return result;
}
public static object GetValue(this IDataRecord dr, int i)
public static T GetConvertValue<T>(this IDataRecord dr, int i)
{
if (dr.IsDBNull(i))
{
return null;
return default(T);
}
var result = dr.GetValue(i);
if (result.GetType().Name == "SqlHierarchyId")
{
result = result.ObjToString();
}
return result;
return UtilMethods.To<T>(result);
}
public static long? GetConvetInt64(this IDataRecord dr, int i)
{
if (dr.IsDBNull(i))
@ -281,61 +278,5 @@ namespace SqlSugar
}
#endregion
#region Sqlite Extensions
public static Nullable<T> GetSqliteTypeNull<T>(this IDataReader dr, int i) where T : struct
{
var type = UtilMethods.GetUnderType(typeof(T));
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
{
if (type.IsIn(UtilConstants.IntType))
{
return (T)((object)(dr.GetInt32(i)));
}
else if (type == UtilConstants.DateType)
{
return (T)Convert.ChangeType(Convert.ToDateTime(dr.GetString(i)), type);
}
else if (type == UtilConstants.DecType)
{
return (T)Convert.ChangeType(dr.GetDecimal(i), type);
}
else if (type == UtilConstants.DobType)
{
return (T)Convert.ChangeType(dr.GetDouble(i), type);
}
else if (type == UtilConstants.BoolType)
{
return (T)Convert.ChangeType(dr.GetBoolean(i), type);
}
else if (type == UtilConstants.LongType)
{
return (T)Convert.ChangeType(dr.GetInt64(i), type);
}
else if (type == UtilConstants.GuidType)
{
string guidString = dr.GetString(i);
string changeValue = guidString.IsNullOrEmpty() ? Guid.Empty.ToString() : guidString;
return (T)Convert.ChangeType(Guid.Parse(changeValue), type);
}
else
{
return (T)Convert.ChangeType((dr.GetString(i)), type);
}
}
#endregion
}
}

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
@ -12,6 +14,39 @@ namespace SqlSugar
{
public class UtilMethods
{
internal static object To(object value, Type destinationType)
{
return To(value, destinationType, CultureInfo.InvariantCulture);
}
internal static object To(object value, Type destinationType, CultureInfo culture)
{
if (value != null)
{
var sourceType = value.GetType();
var destinationConverter = TypeDescriptor.GetConverter(destinationType);
if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
return destinationConverter.ConvertFrom(null, culture, value);
var sourceConverter = TypeDescriptor.GetConverter(sourceType);
if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
return sourceConverter.ConvertTo(null, culture, value, destinationType);
if (destinationType.IsEnum && value is int)
return Enum.ToObject(destinationType, (int)value);
if (!destinationType.IsInstanceOfType(value))
return Convert.ChangeType(value, destinationType, culture);
}
return value;
}
internal static T To<T>(object value)
{
return (T)To(value, typeof(T));
}
internal static Type GetUnderType(Type oldType)
{
Type type = Nullable.GetUnderlyingType(oldType);