Update Core

This commit is contained in:
skx 2020-12-29 23:56:46 +08:00
parent ae0a67f89e
commit 3b535d7ff2
25 changed files with 564 additions and 54 deletions

View File

@ -304,7 +304,8 @@ namespace SqlSugar
}
else
{
result.DataType = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
var name = GetType(propertyType.Name);
result.DataType = this.Context.Ado.DbBind.GetDbTypeName(name);
}
}
@ -322,7 +323,8 @@ namespace SqlSugar
}
else
{
properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(propertyType.Name);
var name = GetType(propertyType.Name);
properyTypeName = this.Context.Ado.DbBind.GetDbTypeName(name);
}
var dataType = dc.DataType;
if (properyTypeName == "boolean" && dataType == "bool")
@ -331,6 +333,16 @@ namespace SqlSugar
}
return properyTypeName != dataType;
}
private static string GetType(string name)
{
if (name.IsContainsIn("UInt32", "UInt16", "UInt64"))
{
name = name.TrimStart('U');
}
return name;
}
#endregion
}
}

View File

@ -239,6 +239,7 @@ namespace SqlSugar
result.InsertBuilder = this.InsertBuilder;
result.Builder = this.SqlBuilder;
result.Context = this.Context;
result.Inserts=this.InsertObjs;
return result;
}

View File

@ -128,6 +128,11 @@ namespace SqlSugar
}
public virtual ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, TObject>> mapperObject, Expression<Func<T, object>> mapperField)
{
if (CallContext.MapperExpression.Value == null)
{
CallContext.MapperExpression.Value = new List<MapperExpression>();
}
CallContext.MapperExpression.Value.Add(new MapperExpression() { SqlBuilder= SqlBuilder, QueryBuilder = this.QueryBuilder, Type=MapperExpressionType.oneToOne, FillExpression=mapperObject, MappingField1Expression= mapperField,Context=this.Context });
return _Mapper<TObject>(mapperObject, mapperField);
}

View File

@ -69,6 +69,10 @@ namespace SqlSugar
public virtual string GetTranslationColumnName(string propertyName)
{
if (propertyName.Contains(SqlTranslationLeft)) return propertyName;
if (propertyName.Contains("."))
{
return string.Join(".", propertyName.Split('.').Select(it => SqlTranslationLeft + it + SqlTranslationRight));
}
else
return SqlTranslationLeft + propertyName + SqlTranslationRight;
}

View File

@ -17,6 +17,10 @@ namespace SqlSugar
public static void RemoveCache(ICacheService cacheService, string tableName)
{
if (cacheService == null)
{
return;
}
var keys = cacheService.GetAllKey<string>();
if (keys.HasValue())
{

View File

@ -251,13 +251,24 @@ namespace SqlSugar
public static object DynamicInvoke(Expression expression, MemberExpression memberExpression = null)
{
object value = Expression.Lambda(expression).Compile().DynamicInvoke();
if (value != null && value.GetType().IsClass() && value.GetType() != UtilConstants.StringType && memberExpression != null)
try
{
value = Expression.Lambda(memberExpression).Compile().DynamicInvoke();
}
object value = Expression.Lambda(expression).Compile().DynamicInvoke();
if (value != null && value.GetType().IsClass() && value.GetType() != UtilConstants.StringType && memberExpression != null)
{
value = Expression.Lambda(memberExpression).Compile().DynamicInvoke();
}
return value;
return value;
}
catch (InvalidOperationException ex)
{
return new MapperExpressionResolve(expression,ex).GetSql(); ;
}
catch (Exception ex)
{
throw new Exception("No support "+expression.ToString()+" "+ex.Message);
}
}
public static Type GetPropertyOrFieldType(MemberInfo propertyOrField)

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class MapperExpression
{
public MapperExpressionType Type { get; set; }
public Expression FillExpression { get; set; }
public Expression MappingField1Expression { get; set; }
public Expression MappingField2Expression { get; set; }
public SqlSugarProvider Context { get; set; }
public QueryBuilder QueryBuilder { get; set; }
public ISqlBuilder SqlBuilder { get; set; }
}
public enum MapperExpressionType
{
oneToOne=1,
oneToN=2
}
}

View File

@ -129,7 +129,7 @@ namespace SqlSugar
if (this.Value != null)
this.Value = this.Value.ToString();
}
else if (type.IsEnum())
else if (type!=null&&type.IsEnum())
{
this.DbType = System.Data.DbType.Int64;
}

View File

@ -36,6 +36,7 @@ namespace SqlSugar
}
}
public int SubQueryIndex { get; set; }
public int JoinIndex { get; set; }
public int Index { get; set; }
public int ParameterIndex { get; set; }
public string SingleTableNameSubqueryShortName{ get; set; }

View File

@ -133,7 +133,7 @@ namespace SqlSugar
if (parameter.BaseExpression is BinaryExpression || parameter.BaseExpression == null)
{
var oppoSiteExpression = isLeft == true ? parameter.BaseParameter.RightExpression : parameter.BaseParameter.LeftExpression;
if (parameter.CurrentExpression is MethodCallExpression||parameter.CurrentExpression is ConditionalExpression||parameter.CurrentExpression.NodeType==ExpressionType.Coalesce)
if (parameter.CurrentExpression is MethodCallExpression || parameter.CurrentExpression is ConditionalExpression || parameter.CurrentExpression.NodeType == ExpressionType.Coalesce)
{
var appendValue = value;
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
@ -146,6 +146,22 @@ namespace SqlSugar
}
this.AppendOpreator(parameter, isLeft);
}
else if (value is MapperSql)
{
var sql = ((MapperSql)value).Sql;
if (isLeft == true)
{
sql += ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index;
}
if (this.Context.Result.Contains(ExpressionConst.FormatSymbol))
{
this.Context.Result.Replace(ExpressionConst.FormatSymbol, sql);
}
else
{
this.Context.Result.Append(sql);
}
}
else if (oppoSiteExpression is MemberExpression)
{
string appendValue = Context.SqlParameterKeyWord
@ -174,7 +190,8 @@ namespace SqlSugar
this.Context.Result.Append(appendValue);
}
}
else if ((oppoSiteExpression is UnaryExpression && (oppoSiteExpression as UnaryExpression).Operand is MemberExpression)) {
else if ((oppoSiteExpression is UnaryExpression && (oppoSiteExpression as UnaryExpression).Operand is MemberExpression))
{
string appendValue = Context.SqlParameterKeyWord
+ ((MemberExpression)(oppoSiteExpression as UnaryExpression).Operand).Member.Name
+ Context.ParameterIndex;

View File

@ -0,0 +1,224 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace SqlSugar
{
public class MapperExpressionResolve
{
private Expression expression;
private List<MapperExpression> mappers;
private InvalidOperationException ex;
private SqlSugarProvider context;
private QueryBuilder querybuiler;
private ISqlBuilder sqlBuilder;
private string sql;
public MapperExpressionResolve(Expression expression, InvalidOperationException ex)
{
this.expression = expression;
this.ex = ex;
this.mappers = CallContext.MapperExpression.Value;
Error01();
var isMember = expression is MemberExpression;
if (isMember)
{
ResolveMember();
}
else
{
ResolveList();
}
}
private void ResolveList()
{
throw new NotImplementedException();
}
private void ResolveMember()
{
var exp = expression as MemberExpression;
ThrowTrue(exp.Expression == null);
var childExpression = exp.Expression;
MapperExpression mapper = GetMapper(exp);
var fillInfo=GetFillInfo(childExpression, mapper);
var mappingFild1Info = GetMappingFild1Info(childExpression, mapper);
var mappingFild1Info2 = GetMappingFild2Info(childExpression, mapper);
var SelectInfo = GetSelectInfo(expression);
var entity = this.context.EntityMaintenance.GetEntityInfo(childExpression.Type);
var isExMapper = mappingFild1Info2!=null;
var isFillFild1SameType = fillInfo.Type == mappingFild1Info.Type;
var isSameProperty = false;
if (isExMapper)
{
ExtMapper();
}
else if (isSameProperty)
{
}
else if (isFillFild1SameType)
{
oneToMany();
}
else
{
oneToOne(fillInfo, mappingFild1Info, mappingFild1Info2,SelectInfo);
}
}
private void oneToOne(MapperExpressionInfo fillInfo, MapperExpressionInfo mappingFild1Info, MapperExpressionInfo mappingFild1Info2, MapperExpressionInfo selectInfo)
{
var pkColumn = selectInfo.EntityInfo.Columns.Where(it => it.IsPrimarykey == true).FirstOrDefault();
if (pkColumn == null)
{
pkColumn = selectInfo.EntityInfo.Columns.First();
}
var tableName = sqlBuilder.GetTranslationTableName(fillInfo.EntityInfo.DbTableName);
var whereLeft = sqlBuilder.GetTranslationColumnName(pkColumn.DbColumnName);
var whereRight = sqlBuilder.GetTranslationColumnName(mappingFild1Info.FieldString);
this.sql = this.context.Queryable<object>()
.AS(tableName)
.Where(string.Format(" {0}={1} ",whereLeft , whereRight))
.Select(sqlBuilder.GetTranslationColumnName(selectInfo.FieldName)).ToSql().Key;
}
private void oneToMany()
{
throw new NotImplementedException();
}
private MapperExpressionInfo GetSelectInfo(Expression expression)
{
var field = expression;
if (field is UnaryExpression)
{
field = (field as UnaryExpression).Operand;
}
var type = ((field as MemberExpression).Expression).Type;
this.context.InitMappingInfo(type);
var name = (field as MemberExpression).Member.Name;
var entity = this.context.EntityMaintenance.GetEntityInfo(type);
var fieldName = entity.Columns.First(it => it.PropertyName == name).DbColumnName;
return new MapperExpressionInfo()
{
Type = type,
FieldName = fieldName,
EntityInfo= entity
};
}
private MapperExpressionInfo GetMappingFild2Info(Expression childExpression, MapperExpression mapper)
{
if (mapper.MappingField2Expression == null)
return null;
var exp = mapper.MappingField2Expression;
var field = (exp as LambdaExpression).Body;
if (field is UnaryExpression)
{
field = (field as UnaryExpression).Operand;
}
var type = ((field as MemberExpression).Expression).Type;
this.context.InitMappingInfo(type);
var name = (field as MemberExpression).Member.Name;
var entity = this.context.EntityMaintenance.GetEntityInfo(type);
var fieldName = entity.Columns.First(it => it.PropertyName == name).DbColumnName;
return new MapperExpressionInfo()
{
Type = type,
FieldName = fieldName
};
}
private MapperExpressionInfo GetMappingFild1Info(Expression childExpression, MapperExpression mapper)
{
var exp = mapper.MappingField1Expression;
var field = (exp as LambdaExpression).Body;
if (field is UnaryExpression)
{
field = (field as UnaryExpression).Operand;
}
var type = ((field as MemberExpression).Expression).Type;
this.context.InitMappingInfo(type);
var name =(field as MemberExpression).Member.Name;
var entity = this.context.EntityMaintenance.GetEntityInfo(type);
var fieldName=entity.Columns.First(it => it.PropertyName == name).DbColumnName;
var array = (field as MemberExpression).ToString().Split('.').ToList();
array[array.Count()-1] = fieldName;
var filedString = string.Join(".", array);
return new MapperExpressionInfo()
{
Type=type,
FieldName = fieldName,
FieldString= filedString,
EntityInfo= entity
};
}
private MapperExpressionInfo GetFillInfo(Expression childExpression, MapperExpression mapper)
{
this.querybuiler = mapper.QueryBuilder;
this.context = mapper.Context;
this.sqlBuilder = mapper.SqlBuilder;
if (this.querybuiler.TableShortName.IsNullOrEmpty())
{
this.querybuiler.TableShortName = (childExpression as MemberExpression).Expression.ToString();
}
this.context.InitMappingInfo(childExpression.Type);
return new MapperExpressionInfo() {
EntityInfo=this.context.EntityMaintenance.GetEntityInfo(childExpression.Type)
};
}
private MapperExpression GetMapper(MemberExpression exp)
{
var mapper= mappers.Where(it => it.Type == MapperExpressionType.oneToOne)
.Reverse()
.Where(it => (it.FillExpression as LambdaExpression).Body.ToString() == exp.Expression.ToString()).FirstOrDefault();
ThrowTrue(mapper == null);
return mapper;
}
public string GetMemberName(MemberExpression memberExpression)
{
return "";
}
private void ExtMapper()
{
throw new NotImplementedException();
}
public MapperSql GetSql()
{
return new MapperSql() { Sql = " (" + this.sql + ") " };
}
void Error01()
{
Check.Exception(mappers == null, ErrorMessage.GetThrowMessage(expression.ToString() + "no support", "当前表达式" + expression.ToString() + "必须在Mapper之后使用"));
}
void ThrowTrue(bool isError)
{
Check.Exception(isError, ErrorMessage.GetThrowMessage(expression.ToString() + "no support", "不支持表达式" + expression.ToString()+ " 1.检查当前表达式中的别名是否与Mapper中的一致 2.目前只支持 1对1 Mapper下的 Where "));
}
}
public class MapperSql
{
public string Sql { get; set; }
}
public class MapperExpressionInfo
{
public Type Type { get; set; }
public EntityInfo EntityInfo { get; set; }
public string FieldName { get; set; }
public string FieldString { get; set; }
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SubInnerJoin : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get { return "InnerJoin"; }
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 301;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
var argExp = exp.Arguments[0];
var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name);
var parameter = (argExp as LambdaExpression).Parameters[1];
Context.InitMappingInfo(parameter.Type);
var tableName= Context.GetTranslationTableName(parameter.Type.Name, true);
var joinString =string.Format(" {2} INNER JOIN {1} {0} ",
this.Context.GetTranslationColumnName(parameter.Name),
tableName,
this.Context.JoinIndex==0?name:"");
var result = joinString+ "ON " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
//var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
this.Context.JoinIndex++;
//result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SubLeftJoin : ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get { return "LeftJoin"; }
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 301;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
var argExp = exp.Arguments[0];
var name =this.Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters[0].Name);
var parameter = (argExp as LambdaExpression).Parameters[1];
Context.InitMappingInfo(parameter.Type);
var tableName= Context.GetTranslationTableName(parameter.Type.Name, true);
var joinString =string.Format(" {2} LEFT JOIN {1} {0} ",
this.Context.GetTranslationColumnName(parameter.Name),
tableName,
this.Context.JoinIndex==0?name:"");
var result = joinString+ "ON " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
//var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
this.Context.JoinIndex++;
//result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}
}

View File

@ -7,7 +7,7 @@ using System.Text.RegularExpressions;
namespace SqlSugar
{
public class SubWhere: ISubOperation
public class SubWhere : ISubOperation
{
public bool HasWhere
{
@ -34,14 +34,14 @@ namespace SqlSugar
public ExpressionContext Context
{
get;set;
get; set;
}
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
var argExp= exp.Arguments[0];
var result= "WHERE "+SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
var argExp = exp.Arguments[0];
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
var regex = @"^WHERE (\@Const\d+) $";
@ -59,8 +59,9 @@ namespace SqlSugar
return result;
}
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name)+UtilConstants.Dot;
result = result.Replace(selfParameterName,SubTools.GetSubReplace(this.Context));
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
if (this.Context.JoinIndex == 0)
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

View File

@ -55,7 +55,8 @@ namespace SqlSugar
var argExp = exp.Arguments[1];
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple); ;
var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
if (this.Context.JoinIndex == 0)
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
}
}

View File

@ -16,6 +16,8 @@ namespace SqlSugar
new SubSelect() { Context=Context },
new SubWhere(){ Context=Context },
new SubWhereIF(){ Context=Context },
new SubLeftJoin(){ Context=Context },
new SubInnerJoin(){ Context=Context },
new SubAnd(){ Context=Context },
new SubAndIF(){ Context=Context },
new SubAny(){ Context=Context },

View File

@ -9,6 +9,16 @@ namespace SqlSugar
public class Subqueryable<T> where T : class, new()
{
public Subqueryable<T> InnerJoin<JoinType>(Func<T, JoinType, bool> expression)
{
return this;
}
public Subqueryable<T> LeftJoin<JoinType>(Func<T, JoinType, bool> expression)
{
return this;
}
public Subqueryable<T> Where(string where)
{
return this;

View File

@ -98,6 +98,9 @@ namespace SqlSugar
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.Single),
new KeyValuePair<string, SqlSugar.CSharpDataType>("double precision",CSharpDataType.@double),
new KeyValuePair<string, SqlSugar.CSharpDataType>("binary", CSharpDataType.@byteArray),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@Guid),
new KeyValuePair<string, CSharpDataType>("varchar2",CSharpDataType.@string),

View File

@ -433,7 +433,15 @@ namespace SqlSugar
this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
return pks;
});
return comments.HasValue() ? comments.First(it => it.DbColumnName.Equals(filedName, StringComparison.CurrentCultureIgnoreCase)).ColumnDescription : "";
if (comments.HasValue())
{
var comment = comments.FirstOrDefault(it => it.DbColumnName.Equals(filedName, StringComparison.CurrentCultureIgnoreCase));
return comment?.ColumnDescription;
}
else
{
return "";
}
}

View File

@ -400,7 +400,7 @@ namespace SqlSugar
string sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName);
this.Context.Ado.ExecuteCommand(sql);
return true;
}
}
#endregion
}
}

View File

@ -14,13 +14,19 @@ namespace SqlSugar
internal SqlSugarProvider Context { get; set; }
internal ISqlBuilder Builder { get; set; }
internal InsertBuilder InsertBuilder { get; set; }
internal object[] Inserts { get; set; }
public int ExecuteBlueCopy()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
DataTable dt;
SqlBulkCopy bulkCopy;
SetCopyData(out dt, out bulkCopy);
if (Inserts.First().GetType() == typeof(DataTable))
{
return WriteToServer();
}
DataTable dt = GetCopyData();
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
try
{
bulkCopy.WriteToServer(dt);
@ -30,19 +36,21 @@ namespace SqlSugar
this.Context.Ado.Connection.Close();
throw ex;
}
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
CloseDb();
return DbColumnInfoList.Count;
}
public async Task<int> ExecuteBlueCopyAsync()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
DataTable dt;
SqlBulkCopy bulkCopy;
SetCopyData(out dt, out bulkCopy);
if (Inserts.First().GetType() == typeof(DataTable))
{
return WriteToServer();
}
DataTable dt=GetCopyData();
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
try
{
await bulkCopy.WriteToServerAsync(dt);
@ -59,9 +67,62 @@ namespace SqlSugar
return DbColumnInfoList.Count;
}
private void SetCopyData(out DataTable dt, out SqlBulkCopy bulkCopy)
private int WriteToServer()
{
dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
var dt = this.Inserts.First() as DataTable;
if (dt == null)
return 0;
Check.Exception(dt.TableName == "Table", "dt.TableName can't be null ");
dt = GetCopyWriteDataTable(dt);
SqlBulkCopy copy = GetBulkCopyInstance();
copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
copy.WriteToServer(dt);
CloseDb();
return dt.Rows.Count;
}
private DataTable GetCopyWriteDataTable(DataTable dt)
{
var result = this.Context.Ado.GetDataTable("select top 0 * from " + this.Builder.GetTranslationColumnName(dt.TableName));
foreach (DataRow item in dt.Rows)
{
DataRow dr= result.NewRow();
foreach (DataColumn column in result.Columns)
{
if (dt.Columns.Cast<DataColumn>().Select(it => it.ColumnName.ToLower()).Contains(column.ColumnName.ToLower()))
{
dr[column.ColumnName] = item[column.ColumnName];
if (dr[column.ColumnName] == null)
{
dr[column.ColumnName] = DBNull.Value;
}
}
}
result.Rows.Add(dr);
}
result.TableName = dt.TableName;
return result;
}
private SqlBulkCopy GetBulkCopyInstance()
{
SqlBulkCopy copy;
if (this.Context.Ado.Transaction == null)
{
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection);
}
else
{
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction);
}
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
{
this.Context.Ado.Connection.Open();
}
return copy;
}
private DataTable GetCopyData()
{
var dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
foreach (var rowInfos in DbColumnInfoList)
{
var dr = dt.NewRow();
@ -81,35 +142,23 @@ namespace SqlSugar
value.Value = Convert.ToDateTime("1753/01/01");
}
}
if (value.Value == null)
{
value.Value = DBNull.Value;
}
dr[item.ColumnName] = value.Value;
}
}
dt.Rows.Add(dr);
}
bulkCopy = null;
if (this.Context.Ado.Transaction != null)
{
var sqlTran = this.Context.Ado.Transaction as SqlTransaction;
bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection, SqlBulkCopyOptions.CheckConstraints, sqlTran);
}
else
{
bulkCopy = new SqlBulkCopy(this.Context.Ado.Connection as SqlConnection);
}
//获取目标表的名称
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
//写入DataReader对象
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
{
this.Context.Ado.Connection.Open();
}
return dt;
}
private object AddParameter(int i,string dbColumnName, object value)
private void CloseDb()
{
var name =Builder.SqlParameterKeyWord+dbColumnName+i;
InsertBuilder.Parameters.Add(new SugarParameter(name,value));
return name;
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
}
}
}

View File

@ -269,7 +269,7 @@ namespace SqlSugar
}
public virtual async Task<bool> DeleteByIdAsync(dynamic id)
{
return await this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
return await this.Context.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
}
public virtual async Task<bool> DeleteByIdsAsync(dynamic[] ids)
{

View File

@ -553,6 +553,8 @@ namespace SqlSugar
if (this._IsOpen)
this.Open();
_Context.Ado.IsEnableLogEvent = isLog;
if (_CurrentConnectionConfig.AopEvents==null)
_CurrentConnectionConfig.AopEvents = new AopEvents();
}
public void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression)
{
@ -565,6 +567,8 @@ namespace SqlSugar
if (this._IsOpen)
this.Open();
_Context.Ado.IsEnableLogEvent = isLog;
if (_CurrentConnectionConfig.AopEvents == null)
_CurrentConnectionConfig.AopEvents = new AopEvents();
}
public void BeginTran()
{

View File

@ -11,5 +11,6 @@ namespace SqlSugar
internal class CallContext
{
public static ThreadLocal<List<SqlSugarProvider>> ContextList = new ThreadLocal<List<SqlSugarProvider>>();
public static ThreadLocal<List<MapperExpression>> MapperExpression = new ThreadLocal<List<MapperExpression>>();
}
}

View File

@ -93,6 +93,8 @@ namespace SqlSugar
itemSql = Regex.Replace(itemSql, string.Format(@"{0}\,", "\\" + itemParameter.ParameterName), newName + ",", RegexOptions.IgnoreCase);
itemSql = Regex.Replace(itemSql, string.Format(@"{0}$", "\\" + itemParameter.ParameterName), newName, RegexOptions.IgnoreCase);
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0}\+", "\\" + itemParameter.ParameterName), "+" + newName + "+", RegexOptions.IgnoreCase);
itemSql = Regex.Replace(itemSql, string.Format(@"\+{0} ", "\\" + itemParameter.ParameterName), "+" + newName +" ", RegexOptions.IgnoreCase);
itemSql = Regex.Replace(itemSql, string.Format(@" {0}\+", "\\" + itemParameter.ParameterName)," "+ newName + "+", RegexOptions.IgnoreCase);
itemSql = Regex.Replace(itemSql, string.Format(@"\|\|{0}\|\|", "\\" + itemParameter.ParameterName), "+" + newName + "+", RegexOptions.IgnoreCase);
return itemSql;
}