diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 1710d4136..9648fe245 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -25,8 +25,7 @@ public abstract class BaseDiyMemberExpression public ParameterExpression _lambdaParameter; public ReadAnonymousTypeInfo _map; public string _field; - public ReadAnonymousTypeInfo ParseExpMapResult { get; internal protected set; } - public ColumnInfo ParseExpColumnResult { get; internal protected set; } + public ReadAnonymousTypeInfo ParseExpMapResult { get; protected set; } public abstract string ParseExp(Expression[] members); } @@ -532,7 +531,7 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp) parent.Childs.Add(child); if (dtTb.Parameter != null) ReadAnonymousField(_tables, _tableRule, field, child, ref index, Expression.Property( - dtTb.Parameter.Type == dtTb.Table.Type ? (Expression)dtTb.Parameter : Expression.TypeAs(dtTb.Parameter, dtTb.Table.Type), + dtTb.Parameter.Type == dtTb.Table.Type ? (Expression)dtTb.Parameter : Expression.TypeAs(dtTb.Parameter, dtTb.Table.Type), dtTb.Table.Properties[trydtocol.CsName]), select, diymemexp, whereGlobalFilter, findIncludeMany, findSubSelectMany, isAllDtoMap); else { @@ -825,7 +824,7 @@ rightExp is UnaryExpression rightExpUexp && var rightBool = ExpressionLambdaToSql(rightExp, tsc); var rightColumn = SearchColumnByField(tsc._tables, tsc.currentTable, rightBool); - if (rightColumn != null) rightBool = $"{rightBool} = {formatSql(true, rightColumn.Attribute.MapType, null, null)}"; + if (rightColumn != null) rightBool = $"{rightBool} = {formatSql(true, rightColumn.Attribute.MapType, null, null)}"; else rightBool = GetBoolString(rightBool); if (_common._orm?.Ado?.DataType == DataType.QuestDb) return $"(({leftBool}) {oper} ({rightBool}))"; return $"({leftBool} {oper} {rightBool})"; @@ -860,20 +859,17 @@ rightExp is UnaryExpression rightExpUexp && } Type oldMapType = null; - if (tsc.diymemexp != null) tsc.diymemexp.ParseExpColumnResult = null; var left = ExpressionLambdaToSql(leftExp, tsc); - var leftMapColumn = SearchColumnByField(tsc._tables, tsc.currentTable, left) ?? - tsc.diymemexp?.ParseExpColumnResult; //group by emum -> MapType(string) #1727 + var leftMapColumn = SearchColumnByField(tsc._tables, tsc.currentTable, left); var isLeftMapType = leftMapColumn != null && new[] { "AND", "OR", "*", "/", "+", "-" }.Contains(oper) == false && (leftMapColumn.Attribute.MapType != rightExp.Type || leftMapColumn.CsType != rightExp.Type); ColumnInfo rightMapColumn = null; var isRightMapType = false; if (isLeftMapType) oldMapType = tsc.SetMapTypeReturnOld(leftMapColumn.Attribute.MapType); - var right = (leftMapColumn != null && + var right = (leftMapColumn != null && (leftMapColumn.Table.AsTableColumn == leftMapColumn && rightExp.IsParameter() == false)) ? //自动分表 formatSql(Expression.Lambda(rightExp).Compile().DynamicInvoke(), leftMapColumn.Attribute.MapType, leftMapColumn, tsc.dbParams) : ExpressionLambdaToSql(rightExp, tsc); - if (isLeftMapType) tsc.SetMapTypeReturnOld(leftMapColumn.Attribute.MapType); if (right != "NULL" && isLeftMapType && //判断参数化后的bug !(right.Contains('@') || right.Contains('?') || right.Contains(':')) && @@ -896,11 +892,10 @@ rightExp is UnaryExpression rightExpUexp && if (isRightMapType) { oldMapType = tsc.SetMapTypeReturnOld(rightMapColumn.Attribute.MapType); - left = (rightMapColumn != null && + left = (rightMapColumn != null && (rightMapColumn.Table.AsTableColumn == rightMapColumn && leftExp.IsParameter() == false)) ? //自动分表 - formatSql(Expression.Lambda(leftExp).Compile().DynamicInvoke(), rightMapColumn.Attribute.MapType, rightMapColumn, tsc.dbParams) : + formatSql(Expression.Lambda(leftExp).Compile().DynamicInvoke(), rightMapColumn.Attribute.MapType, rightMapColumn, tsc.dbParams) : ExpressionLambdaToSql(leftExp, tsc); - if (isRightMapType) tsc.SetMapTypeReturnOld(rightMapColumn.Attribute.MapType); if (left != "NULL" && isRightMapType && //判断参数化后的bug !(left.Contains('@') || left.Contains('?') || left.Contains(':')) && @@ -990,7 +985,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) { case ExpressionType.Not: var notExp = (exp as UnaryExpression)?.Operand; - if (notExp.Type.IsNumberType())return _common.BitNot(ExpressionLambdaToSql(notExp, tsc)); //位操作 + if (notExp.Type.IsNumberType()) return _common.BitNot(ExpressionLambdaToSql(notExp, tsc)); //位操作 if (notExp.NodeType == ExpressionType.MemberAccess) { var notBody = ExpressionLambdaToSql(notExp, tsc); @@ -1006,7 +1001,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) case ExpressionType.Invoke: //#1378 var invokeExp = exp as InvocationExpression; var invokeReplaceExp = invokeExp.Expression; - var invokeLambdaExp = invokeReplaceExp as LambdaExpression; + var invokeLambdaExp = invokeReplaceExp as LambdaExpression; if (invokeLambdaExp == null) return formatSql(Expression.Lambda(exp).Compile().DynamicInvoke(), tsc.mapType, tsc.mapColumnTmp, tsc.dbParams); var invokeReplaceVistor = new FreeSql.Internal.CommonExpression.ReplaceVisitor(); var len = Math.Min(invokeExp.Arguments.Count, invokeLambdaExp.Parameters.Count); @@ -1842,7 +1837,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) } if (callExp != null) return ExpressionLambdaToSql(callExp, tsc); var diymemexps = new[] { tsc.diymemexp, tsc.subSelect001?._diymemexpWithTempQuery }; - if (_subSelectParentDiyMemExps.Value?.Any() == true) + if (_subSelectParentDiyMemExps.Value?.Any() == true) diymemexps = diymemexps.Concat(_subSelectParentDiyMemExps.Value).ToArray(); foreach (var diymemexp in diymemexps) { @@ -2425,7 +2420,7 @@ public string GetWhereCascadeSql(SelectTableInfo tb, IEnumerable(new[] { tb }) : null, _selectColumnMap = null, @@ -2507,8 +2502,8 @@ public class ReplaceHzyTupleToMultiParam : ExpressionVisitor public LambdaExpression Modify(LambdaExpression lambda, List tables) { this.tables = tables.Where(a => a.Type != SelectTableInfoType.Parent).ToList(); - parameters = this.tables.Select(a => a.Parameter ?? - Expression.Parameter(a.Table.Type, + parameters = this.tables.Select(a => a.Parameter ?? + Expression.Parameter(a.Table.Type, a.Alias.StartsWith("SP10") ? a.Alias.Replace("SP10", "ht") : a.Alias)).ToArray(); lambdaHzyParameter = lambda.Parameters.FirstOrDefault(); var exp = Visit(lambda.Body); @@ -2596,7 +2591,7 @@ Expression LocalMatch(Type type, Expression memExp) { if (matchIgnores.ContainsKey(typeProp)) continue; matchIgnores.Add(typeProp, true); - var nextExp = Expression.MakeMemberAccess(memExp, typeProp); + var nextExp = Expression.MakeMemberAccess(memExp, typeProp); var ret = LocalMatch(typeProp.PropertyType, nextExp); if (ret != null) { @@ -2765,8 +2760,8 @@ void LocalInitSelectProvider() Type = SelectTableInfoType.Parent, Parameter = a.Parameter })); - if (dbParams != null) select.WithParameters(dbParams); - if (tableRule != null) select._tableRules.Add(tableRule); + if (dbParams != null) select.WithParameters(dbParams); + if (tableRule != null) select._tableRules.Add(tableRule); if (whereGlobalFilter != null) { select._whereGlobalFilter.Clear(); diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs index d20306cd2..178b49d35 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs @@ -36,11 +36,9 @@ public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymou public override string ParseExp(Expression[] members) { ParseExpMapResult = null; - ParseExpColumnResult = null; if (members.Any() == false) { ParseExpMapResult = _map; - ParseExpColumnResult = ParseExpMapResult.GetColumn(); return _map.DbField; } var firstMember = ((members.FirstOrDefault() as MemberExpression)?.Expression as MemberExpression); @@ -55,7 +53,6 @@ public override string ParseExp(Expression[] members) if (read == null) return null; } ParseExpMapResult = read; - ParseExpColumnResult = ParseExpMapResult.GetColumn(); if (!_addFieldAlias) return read.DbField; if (_flagNestedFieldAlias) return read.DbField; if (_comonExp.EndsWithDbNestedField(read.DbField, read.DbNestedField) == false) @@ -85,7 +82,6 @@ public override string ParseExp(Expression[] members) members[a] = replaceVistor.Modify(members[a], replaceMember, curtable.Parameter); var ret = _select._diymemexpWithTempQuery.ParseExp(members); ParseExpMapResult = _select._diymemexpWithTempQuery.ParseExpMapResult; - ParseExpColumnResult = ParseExpMapResult.GetColumn(); return ret; } } @@ -134,10 +130,7 @@ void LocalValueInitData() return null; } } - var tsc = new CommonExpression.ExpTSC { _tables = _tables, _tableRule = _select._tableRule, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = true, style = CommonExpression.ExpressionStyle.Where }; - var result = _comonExp.ExpressionLambdaToSql(retExp, tsc); - ParseExpColumnResult = tsc.mapColumnTmp; - return result; + return _comonExp.ExpressionLambdaToSql(retExp, new CommonExpression.ExpTSC { _tables = _tables, _tableRule = _select._tableRule, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = true, style = CommonExpression.ExpressionStyle.Where }); } return null; } @@ -238,7 +231,7 @@ public string InternalToSql(string field) public class SelectGroupingProvider : SelectGroupingProvider, ISelectGrouping { public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymousTypeInfo map, string field, CommonExpression comonExp, List tables) - :base(orm, select, map, field, comonExp, tables) { } + : base(orm, select, map, field, comonExp, tables) { } public string ToSql(Expression, TReturn>> select, FieldAliasOptions fieldAlias = FieldAliasOptions.AsIndex) { @@ -262,10 +255,10 @@ public ISelect WithTempQuery(Expression new { p.ID, ShopType=l.ShopType??0 }).WithTempQuery(a => new { Money = a.Sum(a.Value.Item1.Amount)* a.Key.ShopType }) - var old_field = _field; + var old_field = _field; var fieldsb = new StringBuilder(); if (_map.Childs.Any() == false) fieldsb.Append(", ").Append(_map.DbField).Append(_comonExp.EndsWithDbNestedField(_map.DbField, _map.DbNestedField) ? "" : _comonExp._common.FieldAsAlias(_map.DbNestedField)); - foreach (var child in _map.GetAllChilds()) + foreach (var child in _map.GetAllChilds()) fieldsb.Append(", ").Append(child.DbField).Append(_comonExp.EndsWithDbNestedField(child.DbField, child.DbNestedField) ? "" : _comonExp._common.FieldAsAlias(child.DbNestedField)); _field = fieldsb.ToString(); fieldsb.Clear(); @@ -279,7 +272,7 @@ public ISelect WithTempQuery(Expression