Synchronous code

This commit is contained in:
sunkaixuan 2025-06-23 14:59:47 +08:00
parent 5992cd11bd
commit 7f07b2d42f
8 changed files with 71 additions and 30 deletions

View File

@ -742,6 +742,10 @@ namespace SqlSugar
{ {
return false; return false;
} }
else if (dataType.EqualCase("timestamp") && properyTypeName.EqualCase("timestamptz"))
{
return false;
}
else else
{ {
return properyTypeName.ToLower() != dataType.ToLower(); return properyTypeName.ToLower() != dataType.ToLower();

View File

@ -356,14 +356,16 @@ namespace SqlSugar
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns) private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
{ {
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
var isAutoOk = false;
var old = this.context.Ado.IsDisableMasterSlaveSeparation;
var oldOk = false;
try try
{ {
Begin(datas, false); Begin(datas, false);
Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0"); Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0");
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
this.context.CurrentConnectionConfig.IsAutoCloseConnection = false; this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
var old = this.context.Ado.IsDisableMasterSlaveSeparation;
this.context.Ado.IsDisableMasterSlaveSeparation = true; this.context.Ado.IsDisableMasterSlaveSeparation = true;
DataTable dt = ToDdateTable(datas); DataTable dt = ToDdateTable(datas);
IFastBuilder buider = GetBuider(); IFastBuilder buider = GetBuider();
@ -378,8 +380,10 @@ namespace SqlSugar
this.context.DbMaintenance.DropTable(dt.TableName); this.context.DbMaintenance.DropTable(dt.TableName);
} }
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
buider.CloseDb(); isAutoOk = true;
buider.CloseDb();
this.context.Ado.IsDisableMasterSlaveSeparation = old; this.context.Ado.IsDisableMasterSlaveSeparation = old;
oldOk = true;
End(datas, false); End(datas, false);
return result; return result;
} }
@ -388,6 +392,14 @@ namespace SqlSugar
this.context.Close(); this.context.Close();
throw; throw;
} }
finally
{
if(!isAutoOk)
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
if (!oldOk)
this.context.Ado.IsDisableMasterSlaveSeparation = old;
}
} }
private void ActionIgnoreColums(string[] whereColumns, string[] updateColumns, DataTable dt,bool IsActionUpdateColumns) private void ActionIgnoreColums(string[] whereColumns, string[] updateColumns, DataTable dt,bool IsActionUpdateColumns)

View File

@ -37,7 +37,7 @@ namespace SqlSugar
if (list == null) return default(T); if (list == null) return default(T);
else return list.SingleOrDefault(); else return list.SingleOrDefault();
} }
public async Task<T> SingleAsync() public virtual async Task<T> SingleAsync()
{ {
if (QueryBuilder.OrderByValue.IsNullOrEmpty()) if (QueryBuilder.OrderByValue.IsNullOrEmpty())
{ {
@ -67,19 +67,19 @@ namespace SqlSugar
return result.SingleOrDefault(); return result.SingleOrDefault();
} }
} }
public async Task<T> SingleAsync(Expression<Func<T, bool>> expression) public virtual async Task<T> SingleAsync(Expression<Func<T, bool>> expression)
{ {
_Where(expression); _Where(expression);
var result = await SingleAsync(); var result = await SingleAsync();
this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last()); this.QueryBuilder.WhereInfos.Remove(this.QueryBuilder.WhereInfos.Last());
return result; return result;
} }
public Task<T> FirstAsync(CancellationToken token) public virtual Task<T> FirstAsync(CancellationToken token)
{ {
this.Context.Ado.CancellationToken = token; this.Context.Ado.CancellationToken = token;
return FirstAsync(); return FirstAsync();
} }
public async Task<T> FirstAsync() public virtual async Task<T> FirstAsync()
{ {
if (QueryBuilder.OrderByValue.IsNullOrEmpty()) if (QueryBuilder.OrderByValue.IsNullOrEmpty())
{ {
@ -102,12 +102,12 @@ namespace SqlSugar
return default(T); return default(T);
} }
} }
public Task<T> FirstAsync(Expression<Func<T, bool>> expression, CancellationToken token) public virtual Task<T> FirstAsync(Expression<Func<T, bool>> expression, CancellationToken token)
{ {
this.Context.Ado.CancellationToken = token; this.Context.Ado.CancellationToken = token;
return FirstAsync(expression); return FirstAsync(expression);
} }
public async Task<T> FirstAsync(Expression<Func<T, bool>> expression) public virtual async Task<T> FirstAsync(Expression<Func<T, bool>> expression)
{ {
_Where(expression); _Where(expression);
var result = await FirstAsync(); var result = await FirstAsync();
@ -115,7 +115,7 @@ namespace SqlSugar
return result; return result;
} }
public async Task<bool> AnyAsync(Expression<Func<T, bool>> expression) public virtual async Task<bool> AnyAsync(Expression<Func<T, bool>> expression)
{ {
_Where(expression); _Where(expression);
var result = await AnyAsync(); var result = await AnyAsync();
@ -123,23 +123,23 @@ namespace SqlSugar
return result; return result;
} }
public Task<bool> AnyAsync(Expression<Func<T, bool>> expression, CancellationToken token) public virtual Task<bool> AnyAsync(Expression<Func<T, bool>> expression, CancellationToken token)
{ {
this.Context.Ado.CancellationToken = token; this.Context.Ado.CancellationToken = token;
return AnyAsync(expression); return AnyAsync(expression);
} }
public async Task<bool> AnyAsync() public virtual async Task<bool> AnyAsync()
{ {
return (await this.Clone().Take(1).Select("1").ToListAsync()).Count() > 0; ; return (await this.Clone().Take(1).Select("1").ToListAsync()).Count() > 0; ;
} }
public Task<int> CountAsync(CancellationToken token) public virtual Task<int> CountAsync(CancellationToken token)
{ {
this.Context.Ado.CancellationToken = token; this.Context.Ado.CancellationToken = token;
return CountAsync(); return CountAsync();
} }
public async Task<int> CountAsync() public virtual async Task<int> CountAsync()
{ {
if (this.QueryBuilder.Skip == null && if (this.QueryBuilder.Skip == null &&
this.QueryBuilder.Take == null && this.QueryBuilder.Take == null &&
@ -181,7 +181,7 @@ namespace SqlSugar
return CountAsync(expression); return CountAsync(expression);
} }
public async Task<TResult> MaxAsync<TResult>(string maxField) public virtual async Task<TResult> MaxAsync<TResult>(string maxField)
{ {
this.Select(string.Format(QueryBuilder.MaxTemplate, maxField)); this.Select(string.Format(QueryBuilder.MaxTemplate, maxField));
var list = await this._ToListAsync<TResult>(); var list = await this._ToListAsync<TResult>();
@ -189,55 +189,55 @@ namespace SqlSugar
return result; return result;
} }
public Task<TResult> MaxAsync<TResult>(string maxField, CancellationToken token) public virtual Task<TResult> MaxAsync<TResult>(string maxField, CancellationToken token)
{ {
this.Context.Ado.CancellationToken= token; this.Context.Ado.CancellationToken= token;
return MaxAsync<TResult>(maxField); return MaxAsync<TResult>(maxField);
} }
public Task<TResult> MaxAsync<TResult>(Expression<Func<T, TResult>> expression) public virtual Task<TResult> MaxAsync<TResult>(Expression<Func<T, TResult>> expression)
{ {
return _MaxAsync<TResult>(expression); return _MaxAsync<TResult>(expression);
} }
public Task<TResult> MaxAsync<TResult>(Expression<Func<T, TResult>> expression, CancellationToken token) public virtual Task<TResult> MaxAsync<TResult>(Expression<Func<T, TResult>> expression, CancellationToken token)
{ {
this.Context.Ado.CancellationToken = token; this.Context.Ado.CancellationToken = token;
return MaxAsync(expression); return MaxAsync(expression);
} }
public async Task<TResult> MinAsync<TResult>(string minField) public virtual async Task<TResult> MinAsync<TResult>(string minField)
{ {
this.Select(string.Format(QueryBuilder.MinTemplate, minField)); this.Select(string.Format(QueryBuilder.MinTemplate, minField));
var list = await this._ToListAsync<TResult>(); var list = await this._ToListAsync<TResult>();
var result = list.SingleOrDefault(); var result = list.SingleOrDefault();
return result; return result;
} }
public Task<TResult> MinAsync<TResult>(Expression<Func<T, TResult>> expression) public virtual Task<TResult> MinAsync<TResult>(Expression<Func<T, TResult>> expression)
{ {
return _MinAsync<TResult>(expression); return _MinAsync<TResult>(expression);
} }
public async Task<TResult> SumAsync<TResult>(string sumField) public virtual async Task<TResult> SumAsync<TResult>(string sumField)
{ {
this.Select(string.Format(QueryBuilder.SumTemplate, sumField)); this.Select(string.Format(QueryBuilder.SumTemplate, sumField));
var list = await this._ToListAsync<TResult>(); var list = await this._ToListAsync<TResult>();
var result = list.SingleOrDefault(); var result = list.SingleOrDefault();
return result; return result;
} }
public Task<TResult> SumAsync<TResult>(Expression<Func<T, TResult>> expression) public virtual Task<TResult> SumAsync<TResult>(Expression<Func<T, TResult>> expression)
{ {
return _SumAsync<TResult>(expression); return _SumAsync<TResult>(expression);
} }
public async Task<TResult> AvgAsync<TResult>(string avgField) public virtual async Task<TResult> AvgAsync<TResult>(string avgField)
{ {
this.Select(string.Format(QueryBuilder.AvgTemplate, avgField)); this.Select(string.Format(QueryBuilder.AvgTemplate, avgField));
var list = await this._ToListAsync<TResult>(); var list = await this._ToListAsync<TResult>();
var result = list.SingleOrDefault(); var result = list.SingleOrDefault();
return result; return result;
} }
public Task<TResult> AvgAsync<TResult>(Expression<Func<T, TResult>> expression) public virtual Task<TResult> AvgAsync<TResult>(Expression<Func<T, TResult>> expression)
{ {
return _AvgAsync<TResult>(expression); return _AvgAsync<TResult>(expression);
} }

View File

@ -401,10 +401,14 @@ namespace SqlSugar
} }
builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "=", parameterName); builder.AppendFormat(temp, type, item.FieldName.ToSqlFilter(), "=", parameterName);
var p = new SugarParameter(parameterName, GetFieldValue(item)); var p = new SugarParameter(parameterName, GetFieldValue(item));
if (item.CSharpTypeName == "DateOnly") if (item.CSharpTypeName .EqualCase("DateOnly"))
{ {
p.DbType = System.Data.DbType.Date; p.DbType = System.Data.DbType.Date;
} }
if (item.CSharpTypeName.EqualCase("Char"))
{
p.DbType = System.Data.DbType.StringFixedLength;
}
parameters.Add(p); parameters.Add(p);
} }
} }

View File

@ -9,7 +9,7 @@ namespace SqlSugar
public class ConfigQuery public class ConfigQuery
{ {
public SqlSugarProvider Context { get; set; } public SqlSugarProvider Context { get; set; }
public void SetTable<T>(Expression<Func<T, object>> keyExpression, Expression<Func<T, object>> valueTextExpression, string uniqueCode = null, Expression<Func<T, object>> whereExpression=null) public void SetTable<T>(Expression<Func<T, object>> keyExpression, Expression<Func<T, object>> valueTextExpression, string uniqueCode = null, Expression<Func<T, object>> whereExpression=null,string asTableName=null)
{ {
lock (SqlFuncExtendsion.TableInfos) lock (SqlFuncExtendsion.TableInfos)
{ {
@ -29,13 +29,13 @@ namespace SqlSugar
SqlFuncExtendsion.TableInfos.Add(new ConfigTableInfo() SqlFuncExtendsion.TableInfos.Add(new ConfigTableInfo()
{ {
Type = typeof(T), Type = typeof(T),
TableName = entity.DbTableName, TableName =asTableName??entity.DbTableName,
Key = keyValue, Key = keyValue,
Value = ValueValue, Value = ValueValue,
Where = where, Where = where,
Parameter = query.Parameters, Parameter = query.Parameters,
Code = uniqueCode Code = uniqueCode
}); });
} }
else else
{ {

View File

@ -38,5 +38,6 @@ namespace SqlSugar
public bool DisableQueryWhereColumnRemoveTrim { get; set; } public bool DisableQueryWhereColumnRemoveTrim { get; set; }
public DbType? DatabaseModel { get;set; } public DbType? DatabaseModel { get;set; }
public bool ClickHouseEnableFinal { get; set; } public bool ClickHouseEnableFinal { get; set; }
public bool EnableJsonb { get; set; }
} }
} }

View File

@ -43,7 +43,26 @@ namespace SqlSugar
public string GetValue(Expression expression = null) public string GetValue(Expression expression = null)
{ {
var exp = expression as MethodCallExpression; var exp = expression as MethodCallExpression;
return "AVG(" + SubTools.GetMethodValue(this.Context, exp.Arguments[0], ResolveExpressType.FieldSingle) + ")"; var argExp = exp.Arguments[0];
var parametres = (argExp as LambdaExpression).Parameters;
if ((argExp as LambdaExpression).Body is UnaryExpression)
{
argExp = ((argExp as LambdaExpression).Body as UnaryExpression).Operand;
}
var argLambda = argExp as LambdaExpression;
if (this.Context.InitMappingInfo != null && argLambda != null && argLambda.Parameters.Count > 0)
{
foreach (var item in argLambda.Parameters)
{
this.Context.InitMappingInfo(item.Type);
}
this.Context.RefreshMapping();
}
var result = "AVG(" + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple) + ")";
var selfParameterName = Context.GetTranslationColumnName(parametres.First().Name) + UtilConstants.Dot;
if (this.Context.JoinIndex == 0)
result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
return result;
} }
} }
} }

View File

@ -848,7 +848,8 @@ namespace SqlSugar
DatabaseModel=it.MoreSettings.DatabaseModel, DatabaseModel=it.MoreSettings.DatabaseModel,
EnableILike=it.MoreSettings.EnableILike, EnableILike=it.MoreSettings.EnableILike,
ClickHouseEnableFinal=it.MoreSettings.ClickHouseEnableFinal, ClickHouseEnableFinal=it.MoreSettings.ClickHouseEnableFinal,
PgSqlIsAutoToLowerSchema=it.MoreSettings.PgSqlIsAutoToLowerSchema PgSqlIsAutoToLowerSchema=it.MoreSettings.PgSqlIsAutoToLowerSchema,
EnableJsonb=it.MoreSettings.EnableJsonb
}, },
SqlMiddle = it.SqlMiddle == null ? null : new SqlMiddle SqlMiddle = it.SqlMiddle == null ? null : new SqlMiddle