Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
de4935ddf8
  1. 61
      SqlSugar/Abstract/DbProvider/DbBindProvider/IDataReaderEntityBuilder.cs

61
SqlSugar/Abstract/DbProvider/DbBindProvider/IDataReaderEntityBuilder.cs

@ -12,10 +12,12 @@ namespace SqlSugar
private SqlSugarClient Context = null; private SqlSugarClient Context = null;
private IDataReaderEntityBuilder<T> DynamicBuilder; private IDataReaderEntityBuilder<T> DynamicBuilder;
private IDataRecord DataRecord; private IDataRecord DataRecord;
private IDataReaderEntityBuilder() { private IDataReaderEntityBuilder()
{
} }
public IDataReaderEntityBuilder(SqlSugarClient context, IDataRecord dataRecord) { public IDataReaderEntityBuilder(SqlSugarClient context, IDataRecord dataRecord)
{
this.Context = context; this.Context = context;
this.DataRecord = dataRecord; this.DataRecord = dataRecord;
this.DynamicBuilder = new IDataReaderEntityBuilder<T>(); this.DynamicBuilder = new IDataReaderEntityBuilder<T>();
@ -49,12 +51,11 @@ namespace SqlSugar
private static readonly MethodInfo getConvertEnum_Null = typeof(IDataRecordExtensions).GetMethod("GetConvertEnum_Null"); private static readonly MethodInfo getConvertEnum_Null = typeof(IDataRecordExtensions).GetMethod("GetConvertEnum_Null");
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull"); private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther"); private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
private static readonly MethodInfo getEntity = typeof(IDataRecordExtensions).GetMethod("getEntity"); private static readonly MethodInfo getEntity = typeof(IDataRecordExtensions).GetMethod("getEntity", new Type[] { typeof(SqlSugarClient) });
private delegate T Load(IDataRecord dataRecord); private delegate T Load(IDataRecord dataRecord);
private Load handler; private Load handler;
#endregion #endregion
#region functions
public T Build(IDataRecord dataRecord) public T Build(IDataRecord dataRecord)
{ {
return handler(dataRecord); return handler(dataRecord);
@ -62,7 +63,7 @@ namespace SqlSugar
public IDataReaderEntityBuilder<T> CreateBuilder(Type type) public IDataReaderEntityBuilder<T> CreateBuilder(Type type)
{ {
DynamicMethod method = new DynamicMethod("DynamicCreateEntity", type, DynamicMethod method = new DynamicMethod("SqlSugarEntity", type,
new Type[] { typeof(IDataRecord) }, type, true); new Type[] { typeof(IDataRecord) }, type, true);
ILGenerator generator = method.GetILGenerator(); ILGenerator generator = method.GetILGenerator();
LocalBuilder result = generator.DeclareLocal(type); LocalBuilder result = generator.DeclareLocal(type);
@ -103,30 +104,6 @@ namespace SqlSugar
DynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); DynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));
return DynamicBuilder; return DynamicBuilder;
} }
private void CreateBuilder_Part(ILGenerator generator, LocalBuilder result, PropertyInfo propertyInfo, List<MappingColumn> mappingColumns)
{
foreach (var prop in propertyInfo.PropertyType.GetProperties())
{
string entityName = propertyInfo.PropertyType.Name;
string entityProptertyName = prop.Name;
string fileName = entityName + "." + entityProptertyName;
if (Context.IgnoreComumns != null && Context.IgnoreComumns.Any(it => it.EntityPropertyName.Equals(entityProptertyName, StringComparison.CurrentCultureIgnoreCase)
&& it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase)))
{
continue;
}
if (mappingColumns != null)
{
var mappInfo = mappingColumns.SingleOrDefault(it => it.EntityName.Equals(propertyInfo.Name));
if (mappInfo != null)
{
fileName = mappInfo.DbColumnName;
}
}
BindField(generator, result, prop, fileName);
}
}
private void BindClass(ILGenerator generator, LocalBuilder result, PropertyInfo propertyInfo, string fileName) private void BindClass(ILGenerator generator, LocalBuilder result, PropertyInfo propertyInfo, string fileName)
{ {
int i = DataRecord.GetOrdinal(fileName); int i = DataRecord.GetOrdinal(fileName);
@ -147,7 +124,6 @@ namespace SqlSugar
private void BindField(ILGenerator generator, LocalBuilder result, PropertyInfo propertyInfo, string fileName) private void BindField(ILGenerator generator, LocalBuilder result, PropertyInfo propertyInfo, string fileName)
{ {
int i = DataRecord.GetOrdinal(fileName); int i = DataRecord.GetOrdinal(fileName);
bool isNullable = false;
Label endIfLabel = generator.DefineLabel(); Label endIfLabel = generator.DefineLabel();
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Ldc_I4, i);
@ -156,23 +132,11 @@ namespace SqlSugar
generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldloc, result);
generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Ldc_I4, i);
GeneratorCallMethod(generator,propertyInfo,i); BindMethod(generator, propertyInfo, i);
generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod()); generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
generator.MarkLabel(endIfLabel); generator.MarkLabel(endIfLabel);
} }
#endregion private void BindMethod(ILGenerator generator, PropertyInfo bindProperty, int ordinal)
#region helpers
private void CheckType(List<string> errorTypes, string objType, string dbType, string field)
{
var isAny = errorTypes.Contains(objType);
if (isAny)
{
throw new SqlSugarException(string.Format("{0} can't convert {1} to {2}", field, dbType, objType));
}
}
private void GeneratorCallMethod(ILGenerator generator,PropertyInfo bindProperty,int ordinal)
{ {
var isNullableType = false; var isNullableType = false;
var bindType = PubMethod.GetUnderType(bindProperty, ref isNullableType); var bindType = PubMethod.GetUnderType(bindProperty, ref isNullableType);
@ -328,6 +292,13 @@ namespace SqlSugar
} }
} }
} }
#endregion private void CheckType(List<string> errorTypes, string objType, string dbType, string field)
{
var isAny = errorTypes.Contains(objType);
if (isAny)
{
throw new SqlSugarException(string.Format("{0} can't convert {1} to {2}", field, dbType, objType));
}
}
} }
} }

Loading…
Cancel
Save