Synchronization code

This commit is contained in:
sunkaixuan 2025-03-10 13:31:08 +08:00
parent c292512649
commit 30d9ba390c
3 changed files with 38 additions and 10 deletions

View File

@ -1731,6 +1731,8 @@ namespace SqlSugar
}
protected ISugarQueryable<T> _GroupBy(Expression expression)
{
var oldParameterNames = this.QueryBuilder.Parameters?.Select(it => it.ParameterName)
?.ToList();
QueryBuilder.CheckExpression(expression, "GroupBy");
LambdaExpression lambda = expression as LambdaExpression;
expression = lambda.Body;
@ -1761,7 +1763,19 @@ namespace SqlSugar
lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
result = lamResult.GetResultString();
}
GroupBy(result);
if (oldParameterNames != null && this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer)
{
var newParas = this.QueryBuilder.Parameters.Where(it => !oldParameterNames.Contains(it.ParameterName)).ToList();
this.QueryBuilder.GroupParameters = newParas;
var groupBySql = UtilMethods.GetSqlString(DbType.SqlServer, result, newParas.ToArray());
this.QueryBuilder.GroupBySql = groupBySql;
this.QueryBuilder.GroupBySqlOld = result;
GroupBy(result);
}
else
{
GroupBy(result);
}
return this;
}
protected ISugarQueryable<T> _As(string tableName, string entityName)
@ -2142,6 +2156,9 @@ namespace SqlSugar
asyncQueryableBuilder.Hints = this.QueryBuilder.Hints;
asyncQueryableBuilder.MasterDbTableName = this.QueryBuilder.MasterDbTableName;
asyncQueryableBuilder.IsParameterizedConstructor = this.QueryBuilder.IsParameterizedConstructor;
asyncQueryableBuilder.GroupParameters = this.QueryBuilder.GroupParameters;
asyncQueryableBuilder.GroupBySql = this.QueryBuilder.GroupBySql;
asyncQueryableBuilder.GroupBySqlOld = this.QueryBuilder.GroupBySqlOld;
if (this.QueryBuilder.AppendNavInfo != null)
{
asyncQueryableBuilder.AppendNavInfo = new AppendNavInfo()
@ -2336,12 +2353,6 @@ namespace SqlSugar
new SugarParameter("@p",re.Value)
};
var value = UtilMethods.GetSqlString(config.DbType, "@p", p, true);
if (this.Context.CurrentConnectionConfig?.DbType == DbType.SqlServer&&
this.Context.CurrentConnectionConfig?.MoreSettings?.DisableNvarchar!=true&&
p?.FirstOrDefault()?.DbType == System.Data.DbType.String)
{
value = "N" + value;
}
sql = sql.Replace(re.Name, value);
}

View File

@ -34,6 +34,9 @@ namespace SqlSugar
#endregion
#region Splicing basic
public List<SugarParameter> GroupParameters { get; set; }
public string GroupBySql { get; set; }
public string GroupBySqlOld { get; set; }
public Type AsType { get; set; }
public bool IsParameterizedConstructor { get; set; }
public string Hints { get; set; }
@ -283,7 +286,7 @@ namespace SqlSugar
};
resolveExpress.Resolve(expression, resolveType);
this.Parameters.AddRange(resolveExpress.Parameters.Select(it => new SugarParameter(it.ParameterName, it.Value, it.DbType) { Size=it.Size,TypeName=it.TypeName, IsNvarchar2=it.IsNvarchar2}));
var result = resolveExpress.Result;
var result = resolveExpress.Result;
var isSingleTableHasSubquery = IsSingle() && resolveExpress.SingleTableNameSubqueryShortName.HasValue();
if (isSingleTableHasSubquery)
{
@ -919,7 +922,16 @@ namespace SqlSugar
{
result = result + " AS columnName";
}
this.SelectCacheKey = result;
if (this.GroupParameters?.Any()==true && this.GroupBySql.HasValue())
{
var selectSql = UtilMethods.GetSqlString(DbType.SqlServer, result, UtilMethods.CopySugarParameters(this.Parameters).ToArray());
if (selectSql.Contains(this.GroupBySql))
{
result = selectSql;
this.GroupByIsReplace = true;
}
}
this.SelectCacheKey = result;
return result;
}
}
@ -1085,12 +1097,13 @@ namespace SqlSugar
#region NoCopy
internal bool GroupByIsReplace { get; set; }
internal List<QueryableFormat> QueryableFormats { get; set; }
internal bool IsClone { get; set; }
public bool NoCheckInclude { get; set; }
public virtual bool IsSelectNoAll { get; set; } = false;
public List<string> AutoAppendedColumns { get; set; }
public Dictionary<string, string> MappingKeys { get; set; }
public Dictionary<string, string> MappingKeys { get; set; }
#endregion
private string GetTableName(string entityName)

View File

@ -108,6 +108,10 @@ namespace SqlSugar
{
result = result.Replace("TOP 1 DISTINCT", "TOP 1 ");
}
if (this.GroupParameters?.Any() == true&&this.GroupByIsReplace)
{
result = result.Replace(this.GroupBySqlOld, this.GroupBySql);
}
return result;
}