Skip to content

Commit 51494c3

Browse files
2881028810
authored andcommitted
- 修复 ISelect.AsTable union all 查询对 count/max/min/avg/sum 的别名 bug;#157
1 parent fe7b7e5 commit 51494c3

File tree

15 files changed

+357
-199
lines changed

15 files changed

+357
-199
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

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

FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/Curd/MySqlSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,21 @@ public void AsTable()
954954
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", new { bname = "xxx" }).AsTable(tableRule);
955955
sql = query.ToSql().Replace("\r\n", "");
956956
Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", sql);
957+
958+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
959+
sql = query.ToSql().Replace("\r\n", "");
960+
Assert.Equal("SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a) ftb UNION ALLSELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a) ftb", sql);
961+
query.ToList();
962+
963+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
964+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
965+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM `tb_topic` a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM `tb_topic` a) ftb", sql);
966+
query.Count();
967+
968+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
969+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
970+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
971+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
957972
}
958973

959974
public class TestInclude_OneToManyModel1

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/Dameng/Curd/DamengSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,21 @@ public void AsTable()
881881
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" }).AsTable(tableRule);
882882
sql = query.ToSql().Replace("\r\n", "");
883883
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22_T1\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
884+
885+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
886+
sql = query.ToSql().Replace("\r\n", "");
887+
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALLSELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
888+
query.ToList();
889+
890+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
891+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
892+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
893+
query.Count();
894+
895+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
896+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
897+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
898+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
884899
}
885900

886901
public class TiOtmModel1

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/Default/Curd/OdbcSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,21 @@ public void AsTable()
850850
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", new { bname = "xxx" }).AsTable(tableRule);
851851
sql = query.ToSql().Replace("\r\n", "");
852852
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22AsTable1] a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
853+
854+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
855+
sql = query.ToSql().Replace("\r\n", "");
856+
Assert.Equal("SELECT * from (SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a) ftb UNION ALLSELECT * from (SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a) ftb", sql);
857+
query.ToList();
858+
859+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
860+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
861+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM [tb_topic22] a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM [tb_topic22] a) ftb", sql);
862+
query.Count();
863+
864+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
865+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
866+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
867+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
853868
}
854869

855870
public class TestInclude_OneToManyModel1

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/MySql/Curd/MySqlSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,21 @@ public void AsTable()
965965
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", new { bname = "xxx" }).AsTable(tableRule);
966966
sql = query.ToSql().Replace("\r\n", "");
967967
Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", sql);
968+
969+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
970+
sql = query.ToSql().Replace("\r\n", "");
971+
Assert.Equal("SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a) ftb UNION ALLSELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a) ftb", sql);
972+
query.ToList();
973+
974+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
975+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
976+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM `tb_topic` a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM `tb_topic` a) ftb", sql);
977+
query.Count();
978+
979+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
980+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
981+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
982+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
968983
}
969984

970985
public class TestInclude_OneToManyModel1

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/Oracle/Curd/OracleSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,21 @@ public void AsTable()
881881
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" }).AsTable(tableRule);
882882
sql = query.ToSql().Replace("\r\n", "");
883883
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22_T1\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
884+
885+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
886+
sql = query.ToSql().Replace("\r\n", "");
887+
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALLSELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
888+
query.ToList();
889+
890+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
891+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
892+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
893+
query.Count();
894+
895+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
896+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
897+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
898+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
884899
}
885900

886901
public class TiOtmModel1

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/PostgreSQL/Curd/PostgreSQLSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,21 @@ public void AsTable()
937937
query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", new { bname = "xxx" }).AsTable(tableRule);
938938
sql = query.ToSql().Replace("\r\n", "");
939939
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topicastable1\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", sql);
940+
941+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
942+
sql = query.ToSql().Replace("\r\n", "");
943+
Assert.Equal("SELECT * from (SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a) ftb UNION ALLSELECT * from (SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a) ftb", sql);
944+
query.ToList();
945+
946+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
947+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
948+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"tb_topic\" a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM \"tb_topic\" a) ftb", sql);
949+
query.Count();
950+
951+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
952+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
953+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
954+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
940955
}
941956

942957
public class TestInclude_OneToManyModel1

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/SqlServer/Curd/SqlServerSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,21 @@ public void AsTable()
831831
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", new { bname = "xxx" }).AsTable(tableRule);
832832
sql = query.ToSql().Replace("\r\n", "");
833833
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22AsTable1] a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
834+
835+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
836+
sql = query.ToSql().Replace("\r\n", "");
837+
Assert.Equal("SELECT * from (SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a) ftb UNION ALLSELECT * from (SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a) ftb", sql);
838+
query.ToList();
839+
840+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
841+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
842+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM [tb_topic22] a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM [tb_topic22] a) ftb", sql);
843+
query.Count();
844+
845+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
846+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
847+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
848+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
834849
}
835850

836851
public class TestInclude_OneToManyModel1

FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,21 @@ public void AsTable()
996996
query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", new { bname = "xxx" }).AsTable(tableRule);
997997
sql = query.ToSql().Replace("\r\n", "");
998998
Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", sql);
999+
1000+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
1001+
sql = query.ToSql().Replace("\r\n", "");
1002+
Assert.Equal("SELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a) ftb UNION ALLSELECT * from (SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topic` a) ftb", sql);
1003+
query.ToList();
1004+
1005+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
1006+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
1007+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM `tb_topic` a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM `tb_topic` a) ftb", sql);
1008+
query.Count();
1009+
1010+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
1011+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
1012+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
1013+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
9991014
}
10001015

10011016
public class TestInclude_OneToManyModel1

FreeSql.Tests/FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,21 @@ public void AsTable()
881881
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" }).AsTable(tableRule);
882882
sql = query.ToSql().Replace("\r\n", "");
883883
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22_T1\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
884+
885+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
886+
sql = query.ToSql().Replace("\r\n", "");
887+
Assert.Equal("SELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb UNION ALLSELECT * from (SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a) ftb", sql);
888+
query.ToList();
889+
890+
query = select.AsTable((_, old) => old).AsTable((_, old) => old);
891+
sql = query.ToSql("count(1) as1").Replace("\r\n", "");
892+
Assert.Equal("SELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb UNION ALLSELECT * from (SELECT count(1) as1 FROM \"TB_TOPIC22\" a) ftb", sql);
893+
query.Count();
894+
895+
select.AsTable((_, old) => old).AsTable((_, old) => old).Max(a => a.Id);
896+
select.AsTable((_, old) => old).AsTable((_, old) => old).Min(a => a.Id);
897+
select.AsTable((_, old) => old).AsTable((_, old) => old).Sum(a => a.Id);
898+
select.AsTable((_, old) => old).AsTable((_, old) => old).Avg(a => a.Id);
884899
}
885900

886901
public class TiOtmModel1

0 commit comments

Comments
 (0)