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) protected ISugarQueryable<T> _GroupBy(Expression expression)
{ {
var oldParameterNames = this.QueryBuilder.Parameters?.Select(it => it.ParameterName)
?.ToList();
QueryBuilder.CheckExpression(expression, "GroupBy"); QueryBuilder.CheckExpression(expression, "GroupBy");
LambdaExpression lambda = expression as LambdaExpression; LambdaExpression lambda = expression as LambdaExpression;
expression = lambda.Body; expression = lambda.Body;
@@ -1761,7 +1763,19 @@ namespace SqlSugar
lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
result = lamResult.GetResultString(); 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; return this;
} }
protected ISugarQueryable<T> _As(string tableName, string entityName) protected ISugarQueryable<T> _As(string tableName, string entityName)
@@ -2142,6 +2156,9 @@ namespace SqlSugar
asyncQueryableBuilder.Hints = this.QueryBuilder.Hints; asyncQueryableBuilder.Hints = this.QueryBuilder.Hints;
asyncQueryableBuilder.MasterDbTableName = this.QueryBuilder.MasterDbTableName; asyncQueryableBuilder.MasterDbTableName = this.QueryBuilder.MasterDbTableName;
asyncQueryableBuilder.IsParameterizedConstructor = this.QueryBuilder.IsParameterizedConstructor; 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) if (this.QueryBuilder.AppendNavInfo != null)
{ {
asyncQueryableBuilder.AppendNavInfo = new AppendNavInfo() asyncQueryableBuilder.AppendNavInfo = new AppendNavInfo()
@@ -2336,12 +2353,6 @@ namespace SqlSugar
new SugarParameter("@p",re.Value) new SugarParameter("@p",re.Value)
}; };
var value = UtilMethods.GetSqlString(config.DbType, "@p", p, true); 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); sql = sql.Replace(re.Name, value);
} }

View File

@@ -34,6 +34,9 @@ namespace SqlSugar
#endregion #endregion
#region Splicing basic #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 Type AsType { get; set; }
public bool IsParameterizedConstructor { get; set; } public bool IsParameterizedConstructor { get; set; }
public string Hints { get; set; } public string Hints { get; set; }
@@ -919,6 +922,15 @@ namespace SqlSugar
{ {
result = result + " AS columnName"; result = result + " AS columnName";
} }
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; this.SelectCacheKey = result;
return result; return result;
} }
@@ -1085,6 +1097,7 @@ namespace SqlSugar
#region NoCopy #region NoCopy
internal bool GroupByIsReplace { get; set; }
internal List<QueryableFormat> QueryableFormats { get; set; } internal List<QueryableFormat> QueryableFormats { get; set; }
internal bool IsClone { get; set; } internal bool IsClone { get; set; }
public bool NoCheckInclude { get; set; } public bool NoCheckInclude { get; set; }

View File

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