/// <summary> /// 设置Sql类型 /// </summary> /// <param name="prop"></param> /// <param name="par"></param> internal static void SetSqlDbType(PropertyInfo prop, SqlParameter par) { var isNullable = false; var isByteArray = SqlSugarTool.GetUnderType(prop, ref isNullable) == typeof(byte[]); if (isByteArray) { par.SqlDbType = SqlDbType.VarBinary; } }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="dataRecord"></param> /// <returns></returns> public static IDataReaderEntityBuilder <T> CreateBuilder(Type type, IDataRecord dataRecord) { IDataReaderEntityBuilder <T> dynamicBuilder = new IDataReaderEntityBuilder <T>(); DynamicMethod method = new DynamicMethod("DynamicCreateEntity", type, new Type[] { typeof(IDataRecord) }, type, true); ILGenerator generator = method.GetILGenerator(); LocalBuilder result = generator.DeclareLocal(type); generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); string cacheKey = "SqlSugarClient.InitAttributes"; var cm = CacheManager <List <KeyValue> > .GetInstance(); var tFieldNames = typeof(T).GetProperties().Select(it => it.Name).ToList(); for (int i = 0; i < dataRecord.FieldCount; i++) { string dbFieldName = dataRecord.GetName(i); if (cm.ContainsKey(cacheKey) && cm[cacheKey].Any(it => it.Value == dbFieldName)) { var classFieldName = cm[cacheKey].Single(it => it.Value == dbFieldName).Key; if (tFieldNames.Any(it => it == classFieldName)) //T包含映射属性 { dbFieldName = classFieldName; } } //实体类的字段名与数据库大小写不一致可以映射 if (!tFieldNames.Any(it => it == dbFieldName) && tFieldNames.Any(it => it.ToLower() == dbFieldName.ToLower())) { dbFieldName = tFieldNames.Single(it => it.ToLower() == dbFieldName.ToLower()); } PropertyInfo propertyInfo = type.GetProperty(dbFieldName); Label endIfLabel = generator.DefineLabel(); if (propertyInfo != null && propertyInfo.GetSetMethod() != null) { bool isNullable = false; var underType = SqlSugarTool.GetUnderType(propertyInfo, ref isNullable); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, isDBNullMethod); generator.Emit(OpCodes.Brtrue, endIfLabel); generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); GeneratorCallMethod(generator, underType, isNullable, propertyInfo, dataRecord.GetDataTypeName(i), dbFieldName); generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod()); generator.MarkLabel(endIfLabel); } } generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ret); dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); return(dynamicBuilder); }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="dataRecord"></param> /// <returns></returns> public static IDataReaderEntityBuilder <T> CreateBuilder(Type type, IDataRecord dataRecord) { { IDataReaderEntityBuilder <T> dynamicBuilder = new IDataReaderEntityBuilder <T>(); DynamicMethod method = new DynamicMethod("DynamicCreateEntity", type, new Type[] { typeof(IDataRecord) }, type, true); ILGenerator generator = method.GetILGenerator(); LocalBuilder result = generator.DeclareLocal(type); generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); for (int i = 0; i < dataRecord.FieldCount; i++) { string fieldName = dataRecord.GetName(i); PropertyInfo propertyInfo = type.GetProperty(fieldName); Label endIfLabel = generator.DefineLabel(); if (propertyInfo != null && propertyInfo.GetSetMethod() != null) { bool isNullable = false; var underType = SqlSugarTool.GetUnderType(propertyInfo, ref isNullable); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, isDBNullMethod); generator.Emit(OpCodes.Brtrue, endIfLabel); generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); GeneratorCallMethod(generator, underType, isNullable, propertyInfo, dataRecord.GetDataTypeName(i), fieldName); generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod()); generator.MarkLabel(endIfLabel); } } generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ret); dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); return(dynamicBuilder); } }
private bool SqlBulkReplace <T>(IEnumerable <T> entities) where T : class { if (entities == null) { return(false); } ; StringBuilder sbSql = new StringBuilder(""); Type type = typeof(T); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); string pkName = SqlSugarTool.GetPrimaryKeyByTableName(this, typeName); Check.Exception(pkName.IsNullOrEmpty(), "没有找到主键。"); var identityNames = SqlSugarTool.GetIdentitiesKeyByTableName(this, typeName); var isIdentity = identityNames != null && identityNames.Count > 0; var columnNames = SqlSugarTool.GetColumnsByTableName(this, typeName); if (isIdentity) { columnNames = columnNames.Where(c => !identityNames.Any(it => it.Value == c)).ToList();//去掉自添列 } //属性缓存 string cachePropertiesKey = "db." + type.FullName + ".GetProperties"; var cachePropertiesManager = CacheManager <PropertyInfo[]> .GetInstance(); PropertyInfo[] props = null; if (cachePropertiesManager.ContainsKey(cachePropertiesKey)) { props = cachePropertiesManager[cachePropertiesKey]; } else { props = type.GetProperties(); cachePropertiesManager.Add(cachePropertiesKey, props, cachePropertiesManager.Day); } foreach (var entity in entities) { string pkValue = string.Empty; sbSql.Append(" UPDATE "); sbSql.Append(typeName); sbSql.Append(" SET "); pkValue = props.Single(it => it.Name.ToLower() == pkName.ToLower()).GetValue(entity, null).ToString(); foreach (var name in columnNames) { var isPk = pkName != null && pkName.ToLower() == name.ToLower(); var isDisableUpdateColumns = DisableUpdateColumns != null && DisableUpdateColumns.Any(it => it.ToLower() == name.ToLower()); var isLastName = name == columnNames.Last(); var prop = props.Single(it => it.Name == name); var objValue = prop.GetValue(entity, null); if (this.IsIgnoreErrorColumns) { if (!SqlSugarTool.GetColumnsByTableName(this, typeName).Any(it => it.ToLower() == name.ToLower())) { continue; } } if (isPk || isDisableUpdateColumns) { continue; } bool isNullable = false; var underType = SqlSugarTool.GetUnderType(prop, ref isNullable); if (objValue == null) { objValue = "NULL"; } else if (underType == SqlSugarTool.DateType) { objValue = "'" + objValue.ToString() + "'"; } else if (underType == SqlSugarTool.BoolType) { objValue = Convert.ToBoolean(objValue) ? 1 : 0; } else if (underType == SqlSugarTool.StringType) { //string参数需要处理注入 (因为SqlParameter参数上限为2100所以无法使用参数化) objValue = "'" + objValue.ToString().ToSqlFilter() + "'"; } else { objValue = "'" + objValue.ToString() + "'"; } sbSql.AppendFormat(" [{0}]={1}{2} ", name, objValue, ","); } sbSql.Remove(sbSql.ToString().LastIndexOf(","), 1); sbSql.AppendFormat("WHERE [{0}]='{1}' ", pkName, pkValue.ToSuperSqlFilter()); } var reval = base.ExecuteCommand(sbSql.ToString()); sbSql = null; return(reval > 0); }
private bool SqlBulkCopy <T>(IEnumerable <T> entities) where T : class { if (entities == null) { return(false); } ; Type type = typeof(T); string typeName = type.Name; typeName = GetTableNameByClassType(typeName); string pkName = SqlSugarTool.GetPrimaryKeyByTableName(this, typeName); var identityNames = SqlSugarTool.GetIdentitiesKeyByTableName(this, typeName); var isIdentity = identityNames != null && identityNames.Count > 0; var columnNames = SqlSugarTool.GetColumnsByTableName(this, typeName); if (isIdentity) { columnNames = columnNames.Where(c => !identityNames.Any(it => it.Value == c)).ToList();//去掉自添列 } StringBuilder sbSql = new StringBuilder("INSERT INTO "); sbSql.AppendLine(typeName); sbSql.AppendFormat("({0})", string.Join(",", columnNames.Select(it => "[" + it + "]"))); //属性缓存 string cachePropertiesKey = "db." + type.FullName + ".GetProperties"; var cachePropertiesManager = CacheManager <PropertyInfo[]> .GetInstance(); PropertyInfo[] props = null; if (cachePropertiesManager.ContainsKey(cachePropertiesKey)) { props = cachePropertiesManager[cachePropertiesKey]; } else { props = type.GetProperties(); cachePropertiesManager.Add(cachePropertiesKey, props, cachePropertiesManager.Day); } foreach (var entity in entities) { sbSql.AppendLine("SELECT "); foreach (var name in columnNames) { var isLastName = name == columnNames.Last(); var prop = props.Single(it => it.Name == name); var objValue = prop.GetValue(entity, null); bool isNullable = false; var underType = SqlSugarTool.GetUnderType(prop, ref isNullable); if (objValue == null) { objValue = "NULL"; } else if (underType == SqlSugarTool.DateType) { objValue = "'" + objValue.ToString() + "'"; } else if (underType == SqlSugarTool.BoolType) { objValue = Convert.ToBoolean(objValue) ? 1 : 0; } else if (underType == SqlSugarTool.StringType) { //string参数需要处理注入 (因为SqlParameter参数上限为2100所以无法使用参数化) objValue = "'" + objValue.ToString().ToSqlFilter() + "'"; } else { objValue = "'" + objValue.ToString() + "'"; } sbSql.Append(objValue + (isLastName ? "" : ",")); } var isLastEntity = entities.Last() == entity; if (!isLastEntity) { sbSql.AppendLine(" UNION ALL "); } } var reval = base.ExecuteCommand(sbSql.ToString()); sbSql = null; return(reval > 0); }