Skip to content

Commit 2a2be54

Browse files
committed
- 修复 Enum -> MapType(string) + GroupBy 解析问题;#1727
1 parent 2598451 commit 2a2be54

File tree

7 files changed

+20
-135
lines changed

7 files changed

+20
-135
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests/Issues/1237.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ GROUP BY replace(replace(a.""Name"", ' ', ''), ' ', '')
3636
.ToSql();
3737
Assert.Equal(@"SELECT *
3838
FROM (
39-
SELECT replace(replace(a.""Name"", ' ', ''), ' ', '') ""xm_new"", a.""CSNY"" ""csny""
39+
SELECT replace(replace(a.""Name"", ' ', ''), ' ', '') ""xm"", a.""CSNY"" ""csny""
4040
FROM ""people_issues_1237"" a
4141
GROUP BY replace(replace(a.""Name"", ' ', ''), ' ', ''), a.""CSNY""
4242
HAVING (count(1) > 1) ) a
43-
INNER JOIN ""people_issues_1237"" b ON a.""xm_new"" = replace(replace(b.""Name"", ' ', ''), ' ', '') AND a.""csny"" = b.""CSNY""
43+
INNER JOIN ""people_issues_1237"" b ON a.""xm"" = replace(replace(b.""Name"", ' ', ''), ' ', '') AND a.""csny"" = b.""CSNY""
4444
ORDER BY b.""Name"", b.""ID""", people2);
4545

4646
}

FreeSql/FreeSql.xml

Lines changed: 0 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonExpression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public abstract class BaseDiyMemberExpression
2626
public ReadAnonymousTypeInfo _map;
2727
public string _field;
2828
public ReadAnonymousTypeInfo ParseExpMapResult { get; internal protected set; }
29+
public ColumnInfo ParseExpColumnResult { get; internal protected set; }
2930
public abstract string ParseExp(Expression[] members);
3031
}
3132

@@ -859,10 +860,10 @@ rightExp is UnaryExpression rightExpUexp &&
859860
}
860861

861862
Type oldMapType = null;
862-
if (tsc.diymemexp != null) tsc.diymemexp.ParseExpMapResult = null;
863+
if (tsc.diymemexp != null) tsc.diymemexp.ParseExpColumnResult = null;
863864
var left = ExpressionLambdaToSql(leftExp, tsc);
864865
var leftMapColumn = SearchColumnByField(tsc._tables, tsc.currentTable, left) ??
865-
(tsc.diymemexp?.ParseExpMapResult?.Table?.ColumnsByCs.TryGetValue(tsc.diymemexp.ParseExpMapResult.CsName, out var dmcol) == true ? dmcol : null); //group by emum -> MapType(string) #1727
866+
tsc.diymemexp?.ParseExpColumnResult; //group by emum -> MapType(string) #1727
866867
var isLeftMapType = leftMapColumn != null && new[] { "AND", "OR", "*", "/", "+", "-" }.Contains(oper) == false && (leftMapColumn.Attribute.MapType != rightExp.Type || leftMapColumn.CsType != rightExp.Type);
867868
ColumnInfo rightMapColumn = null;
868869
var isRightMapType = false;

FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public WithTempQueryParser Append(Select0Provider select, SelectTableInfo outsid
241241
public override string ParseExp(Expression[] members)
242242
{
243243
ParseExpMapResult = null;
244+
ParseExpColumnResult = null;
244245
ParseExpMatchedTable = GetOutsideSelectTable(members.FirstOrDefault()?.GetParameter());
245246
if (ParseExpMatchedTable == null) return null;
246247

@@ -254,12 +255,14 @@ public override string ParseExp(Expression[] members)
254255
if (members.Any() == false)
255256
{
256257
ParseExpMapResult = insideData.InsideMap;
258+
ParseExpColumnResult = ParseExpMapResult.GetColumn();
257259
return $"{ParseExpMatchedTable.Alias}.{insideData.InsideMap.DbNestedField}";
258260
}
259261
var read = insideData.InsideMap;
260262
if (members.Length == 1 && members[0] == ParseExpMatchedTable.Parameter)
261263
{
262264
ParseExpMapResult = read;
265+
ParseExpColumnResult = ParseExpMapResult.GetColumn();
263266
return $"{ParseExpMatchedTable.Alias}.{read.DbNestedField}";
264267
}
265268
for (var a = members[0] == ParseExpMatchedTable.Parameter ? 1 : 0; a < members.Length; a++)
@@ -268,6 +271,7 @@ public override string ParseExp(Expression[] members)
268271
if (read == null) return null;
269272
}
270273
ParseExpMapResult = read;
274+
ParseExpColumnResult = ParseExpMapResult.GetColumn();
271275
return $"{ParseExpMatchedTable.Alias}.{read.DbNestedField}";
272276
}
273277
public SelectTableInfo GetOutsideSelectTable(ParameterExpression parameterExp)

FreeSql/Internal/CommonProvider/SelectProvider/SelectGroupingProvider.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public SelectGroupingProvider(IFreeSql orm, Select0Provider select, ReadAnonymou
3636
public override string ParseExp(Expression[] members)
3737
{
3838
ParseExpMapResult = null;
39+
ParseExpColumnResult = null;
3940
if (members.Any() == false)
4041
{
4142
ParseExpMapResult = _map;
43+
ParseExpColumnResult = ParseExpMapResult.GetColumn();
4244
return _map.DbField;
4345
}
4446
var firstMember = ((members.FirstOrDefault() as MemberExpression)?.Expression as MemberExpression);
@@ -53,6 +55,7 @@ public override string ParseExp(Expression[] members)
5355
if (read == null) return null;
5456
}
5557
ParseExpMapResult = read;
58+
ParseExpColumnResult = ParseExpMapResult.GetColumn();
5659
if (!_addFieldAlias) return read.DbField;
5760
if (_flagNestedFieldAlias) return read.DbField;
5861
if (_comonExp.EndsWithDbNestedField(read.DbField, read.DbNestedField) == false)
@@ -82,6 +85,7 @@ public override string ParseExp(Expression[] members)
8285
members[a] = replaceVistor.Modify(members[a], replaceMember, curtable.Parameter);
8386
var ret = _select._diymemexpWithTempQuery.ParseExp(members);
8487
ParseExpMapResult = _select._diymemexpWithTempQuery.ParseExpMapResult;
88+
ParseExpColumnResult = ParseExpMapResult.GetColumn();
8589
return ret;
8690
}
8791
}
@@ -132,16 +136,7 @@ void LocalValueInitData()
132136
}
133137
var tsc = new CommonExpression.ExpTSC { _tables = _tables, _tableRule = _select._tableRule, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = true, style = CommonExpression.ExpressionStyle.Where };
134138
var result = _comonExp.ExpressionLambdaToSql(retExp, tsc);
135-
//ParseExpMapResult = result
136-
if (tsc.mapColumnTmp != null)
137-
ParseExpMapResult = new ReadAnonymousTypeInfo
138-
{
139-
Property = tsc.mapColumnTmp.Table.Properties[tsc.mapColumnTmp.CsName],
140-
CsName = tsc.mapColumnTmp.CsName,
141-
CsType = tsc.mapColumnTmp.CsType, //dtoProp.PropertyType,
142-
MapType = tsc.mapColumnTmp.Attribute.MapType,
143-
Table = tsc.mapColumnTmp.Table
144-
};
139+
ParseExpColumnResult = tsc.mapColumnTmp;
145140
return result;
146141
}
147142
return null;

FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class ReadAnonymousTypeInfo
2525
public string IncludeManyKey { get; set; } //ToList(a => new { a.Childs }) 集合属性指定加载
2626
public Expression SubSelectMany { get; set; } //ToList(a => new { sublist = fsql.Select<T>().ToList() }) 子集合查询
2727

28+
public ColumnInfo GetColumn()
29+
{
30+
if (Childs.Any() == false && Table != null && CsName != null && Table.ColumnsByCs.TryGetValue(CsName, out var pcol)) return pcol;
31+
return null;
32+
}
33+
2834
public void CopyTo(ReadAnonymousTypeInfo target)
2935
{
3036
target.Property = Property;

0 commit comments

Comments
 (0)