Update Sqlite

This commit is contained in:
sunkaixuan
2017-07-09 19:12:41 +08:00
parent 08a02cf258
commit 642f403ed8
4 changed files with 66 additions and 13 deletions

View File

@@ -56,6 +56,8 @@ namespace SqlSugar
private static readonly MethodInfo getConvertEnum_Null = typeof(IDataRecordExtensions).GetMethod("GetConvertEnum_Null");
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
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(SqlSugarClient) });
private delegate T Load(IDataRecord dataRecord);
private Load handler;
@@ -101,7 +103,7 @@ namespace SqlSugar
string fileName = propertyInfo.Name;
if (mappingColumns != null)
{
var mappInfo = mappingColumns.SingleOrDefault(it => it.EntityName==type.Name&&it.PropertyName.Equals(propertyInfo.Name));
var mappInfo = mappingColumns.SingleOrDefault(it => it.EntityName == type.Name && it.PropertyName.Equals(propertyInfo.Name));
if (mappInfo != null)
{
fileName = mappInfo.DbColumnName;
@@ -161,13 +163,25 @@ namespace SqlSugar
MethodInfo method = null;
Type bindPropertyType = PubMethod.GetUnderType(bindProperty, ref isNullableType);
string dbTypeName = DataRecord.GetDataTypeName(ordinal);
if (Regex.IsMatch(dbTypeName, @"\(.+\)")) {
dbTypeName = Regex.Replace(dbTypeName,@"\(.+\)", "");
if (Regex.IsMatch(dbTypeName, @"\(.+\)"))
{
dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", "");
}
string propertyName = bindProperty.Name;
string validPropertyName = bind.GetPropertyTypeName(dbTypeName);
validPropertyName = validPropertyName == "byte[]" ? "byteArray" : validPropertyName;
CSharpDataType validPropertyType = (CSharpDataType)Enum.Parse(typeof(CSharpDataType), validPropertyName);
#region Sqlite Logic
if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite)
{
method = isNullableType ? getSqliteTypeNull.MakeGenericMethod(bindPropertyType) : getSqliteType.MakeGenericMethod(bindPropertyType);
generator.Emit(OpCodes.Call, method);
return;
};
#endregion
#region Common Database Logic
string bindProperyTypeName = bindPropertyType.Name.ToLower();
bool isEnum = bindPropertyType.IsEnum();
if (isEnum) { validPropertyType = CSharpDataType.@enum; }
@@ -175,7 +189,7 @@ namespace SqlSugar
{
case CSharpDataType.@int:
CheckType(bind.IntThrow, bindProperyTypeName, validPropertyName, propertyName);
if (bindProperyTypeName.IsContainsIn("int","int32","int64"))
if (bindProperyTypeName.IsContainsIn("int", "int32", "int64"))
method = isNullableType ? getConvertInt32 : getInt32;
break;
case CSharpDataType.@bool:
@@ -185,8 +199,9 @@ namespace SqlSugar
case CSharpDataType.@string:
CheckType(bind.StringThrow, bindProperyTypeName, validPropertyName, propertyName);
method = getString;
if (bindProperyTypeName == "guid") {
method = getConvertStringGuid ;
if (bindProperyTypeName == "guid")
{
method = getConvertStringGuid;
}
break;
case CSharpDataType.DateTime:
@@ -229,16 +244,18 @@ namespace SqlSugar
method = getValueMethod;
break;
}
if (method == null&&bindPropertyType == PubConst.StringType) {
if (method == null && bindPropertyType == PubConst.StringType)
{
method = getConvertString;
}
if (method == null)
method =isNullableType? getOtherNull.MakeGenericMethod(bindPropertyType):getOther.MakeGenericMethod(bindPropertyType);
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
generator.Emit(OpCodes.Call, method);
if (method == getValueMethod)
{
generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType);
}
#endregion
}
private void CheckType(List<string> invalidTypes, string bindProperyTypeName, string validPropertyType, string propertyName)

View File

@@ -10,7 +10,7 @@ namespace SqlSugar
public static Guid GetStringGuid(this IDataRecord dr, int i)
{
var reval =Guid.Parse(dr.GetValue(i).ToString());
var reval = Guid.Parse(dr.GetValue(i).ToString());
return reval;
}
@@ -182,5 +182,35 @@ namespace SqlSugar
{
return null;
}
public static Nullable<T> GetSqliteTypeNull<T>(this IDataReader dr, int i) where T : struct
{
var type = PubMethod.GetUnderType(typeof(T));
if (type.IsIn(PubConst.IntType))
{
return (T)((object)(dr.GetInt32(i)));
}
else if (type == PubConst.DateType)
{
return (T)Convert.ChangeType(Convert.ToDateTime(dr.GetString(i)), type);
}
else
{
return (T)Convert.ChangeType((dr.GetString(i)), type);
}
}
public static T GetSqliteType<T>(this IDataReader dr, int i)
{
var type = typeof(T);
if (type.IsIn(PubConst.IntType))
{
return (T)((object)(dr.GetInt32(i)));
}
else
{
return (T)Convert.ChangeType((dr.GetString(i)), type);
}
}
}
}

View File

@@ -9,6 +9,12 @@ namespace SqlSugar
{
public class PubMethod
{
internal static Type GetUnderType(Type oldType)
{
Type type = Nullable.GetUnderlyingType(oldType);
return type==null ? oldType : type;
}
internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable)
{
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
@@ -27,11 +33,11 @@ namespace SqlSugar
internal static bool IsNullable(PropertyInfo propertyInfo)
{
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
return unType!=null;
return unType != null;
}
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
{
if (returnObj.IsNullOrEmpty())
{
@@ -40,7 +46,7 @@ namespace SqlSugar
return returnObj;
}
internal static T ChangeType<T>(T obj,Type type)
internal static T ChangeType<T>(T obj, Type type)
{
return (T)Convert.ChangeType(obj, type);
}