Skip to content

Commit 38c6796

Browse files
committed
- 修复 AsTable 子查询未传播的 bug;#1103
1 parent 7e2b052 commit 38c6796

File tree

14 files changed

+109
-100
lines changed

14 files changed

+109
-100
lines changed

Extensions/FreeSql.Extensions.Linq/SelectedQueryProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public SelectedQueryProvider(Select0Provider select, Expression selector)
6767
var index = -10000; //临时规则,不返回 as1
6868

6969
if (selector != null)
70-
_comonExp.ReadAnonymousField(_select._tables, field, _map, ref index, selector, null, null, _select._whereGlobalFilter, null, null, false); //不走 DTO 映射,不处理 IncludeMany
70+
_comonExp.ReadAnonymousField(_select._tables, _select._tableRule, field, _map, ref index, selector, null, null, _select._whereGlobalFilter, null, null, false); //不走 DTO 映射,不处理 IncludeMany
7171
_field = field.ToString();
7272
}
7373

@@ -162,7 +162,7 @@ public ISelectedQuery<TOut> OrderByIf<TMember>(bool condition, Expression<Func<T
162162
{
163163
if (condition == false) return this;
164164
_lambdaParameter = column?.Parameters[0];
165-
var sql = _comonExp.ExpressionWhereLambda(null, column, this, null, null);
165+
var sql = _comonExp.ExpressionWhereLambda(null, null, column, this, null, null);
166166
var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) });
167167
method.Invoke(_select, new object[] { descending ? $"{sql} DESC" : sql, null });
168168
return this;
@@ -174,7 +174,7 @@ public ISelectedQuery<TOut> WhereIf(bool condition, Expression<Func<TOut, bool>>
174174
{
175175
if (condition == false) return this;
176176
_lambdaParameter = exp?.Parameters[0];
177-
var sql = _comonExp.ExpressionWhereLambda(null, exp, this, null, null);
177+
var sql = _comonExp.ExpressionWhereLambda(null, null, exp, this, null, null);
178178
var method = _select.GetType().GetMethod("Where", new[] { typeof(string), typeof(object) });
179179
method.Invoke(_select, new object[] { sql, null });
180180
return this;

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/Default/OdbcCodeFirstTest.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,6 @@ namespace FreeSql.Tests.Odbc.Default
1212
public class OdbcCodeFirstTest
1313
{
1414

15-
[Fact]
16-
public void Test_0String()
17-
{
18-
var fsql = g.odbc;
19-
fsql.Delete<test_0string01>().Where("1=1").ExecuteAffrows();
20-
21-
Assert.Equal(1, fsql.Insert(new test_0string01 { name = @"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000" }).ExecuteAffrows());
22-
Assert.Equal(1, fsql.Insert(new test_0string01 { name = @"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000" }).NoneParameter().ExecuteAffrows());
23-
24-
var list = fsql.Select<test_0string01>().ToList();
25-
Assert.Equal(2, list.Count);
26-
Assert.Equal(@"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000", list[0].name);
27-
Assert.Equal(@"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000", list[1].name);
28-
}
29-
class test_0string01
30-
{
31-
public Guid id { get; set; }
32-
public string name { get; set; }
33-
}
34-
3515
[Fact]
3616
public void 中文表_字段()
3717
{

FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,24 @@ public void As()
11261126
[Fact]
11271127
public void AsTable()
11281128
{
1129+
var fsql = g.sqlite;
1130+
var asTableSubSql = fsql.Select<TestTypeParentInfo>().AsTable((_, old) => $"{old}_01").ToSql(a => new
1131+
{
1132+
a.Id,
1133+
max = fsql.Select<TestTypeInfo>().Max(b => b.Guid),
1134+
any = a.Types.Any(b => b.Name == "xx"),
1135+
any2 = a.Types.AsSelect().Any(b => b.Name == "xx"),
1136+
sub = fsql.Select<TestTypeInfo>().Where(b => b.Guid == a.Id).ToList()
1137+
});
1138+
Assert.Equal(@"SELECT a.""Id"" as1, ifnull((SELECT max(a.""Guid"")
1139+
FROM ""TestTypeInfo_01"" a), 0) as2, exists(SELECT 1
1140+
FROM ""TestTypeInfo_01"" b
1141+
WHERE (b.""ParentId"" = a.""Id"") AND (b.""Name"" = 'xx')
1142+
limit 0,1) as3, exists(SELECT 1
1143+
FROM ""TestTypeInfo_01"" b
1144+
WHERE (b.""Name"" = 'xx') AND (b.""ParentId"" = a.""Id"")
1145+
limit 0,1) as4
1146+
FROM ""TestTypeParentInfo_01"" a", asTableSubSql);
11291147

11301148
var listt = select.AsTable((a, b) => "(select * from tb_topic where clicks > 10)").Page(1, 10).ToList();
11311149

FreeSql/Extensions/FreeSqlGlobalExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,10 @@ WHERE @cte_ids IS NOT NULL
696696
case DataType.OdbcSqlServer:
697697
case DataType.Firebird:
698698
case DataType.ClickHouse:
699-
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, Expression.Call(typeof(Convert).GetMethod("ToString", new Type[] { typeof(string) }), pathSelector?.Body), null, null, null);
699+
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, select._tableRule, Expression.Call(typeof(Convert).GetMethod("ToString", new Type[] { typeof(string) }), pathSelector?.Body), null, null, null);
700700
break;
701701
default:
702-
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, pathSelector?.Body, null, null, null);
702+
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, select._tableRule, pathSelector?.Body, null, null, null);
703703
break;
704704
}
705705
sql1ctePath = $"{sql1ctePath} as cte_path, ";
@@ -717,7 +717,7 @@ WHERE @cte_ids IS NOT NULL
717717
if (pathSelector != null)
718718
{
719719
select._tables[0].Parameter = pathSelector?.Parameters[0];
720-
var wct2ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, pathSelector?.Body, null, null, null);
720+
var wct2ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, select._tableRule, pathSelector?.Body, null, null, null);
721721
sql2ctePath = select._commonUtils.StringConcat(
722722
new string[] {
723723
up == false ? "wct1.cte_path" : wct2ctePath,

FreeSql/FreeSql.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
</ItemGroup>
3838

3939
<ItemGroup>
40+
<Compile Update="Internal\CommonProvider\SelectProvider\Select1Provider2`16.cs">
41+
<DependentUpon>Select1Provider2`16.tt</DependentUpon>
42+
<DesignTime>True</DesignTime>
43+
<AutoGen>True</AutoGen>
44+
</Compile>
4045
<Compile Update="Properties\CoreStrings.Designer.cs">
4146
<DesignTime>True</DesignTime>
4247
<AutoGen>True</AutoGen>
@@ -45,6 +50,10 @@
4550
</ItemGroup>
4651

4752
<ItemGroup>
53+
<None Update="Internal\CommonProvider\SelectProvider\T4Temp\Select1Provider2`16.tt">
54+
<Generator>TextTemplatingFileGenerator</Generator>
55+
<LastGenOutput>Select1Provider2`16.cs</LastGenOutput>
56+
</None>
4857
<None Update="Properties\CoreStrings.Designer.tt">
4958
<Generator>TextTemplatingFileGenerator</Generator>
5059
<LastGenOutput>CoreStrings.Designer.cs</LastGenOutput>

FreeSql/Internal/CommonExpression.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
993993
break;
994994
}
995995
object fsql = null;
996+
Select0Provider fsqlSelect0 = null;
996997
List<SelectTableInfo> fsqltables = null;
997998
var fsqltable1SetAlias = false;
998999
Type fsqlType = null;
@@ -1071,9 +1072,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
10711072
if (fsql == null) fsql = Expression.Lambda(exp3tmp).Compile().DynamicInvoke();
10721073
fsqlType = fsql?.GetType();
10731074
if (fsqlType == null) break;
1074-
var fsqlSelect0 = fsql as Select0Provider;
1075-
if (tsc._tableRule != null)
1076-
fsqlSelect0._tableRules.Add(tsc._tableRule);
1075+
fsqlSelect0 = fsql as Select0Provider;
10771076
switch (exp3.Method.Name)
10781077
{
10791078
case "Any": //exists
@@ -1196,6 +1195,9 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
11961195
}
11971196
if (fsql != null)
11981197
{
1198+
if (fsqlSelect0 != null && tsc._tableRule != null && fsqlSelect0._tableRules.Any() == false)
1199+
fsqlSelect0._tableRules.Add(tsc._tableRule);
1200+
11991201
if (asSelectParentExp != null)
12001202
{
12011203
//执行 AsSelect() 的关联,OneToMany,ManyToMany,PgArrayToMany

FreeSql/Internal/CommonProvider/DeleteProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public int ExecuteAffrows()
102102
public IDelete<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp)
103103
{
104104
if (condition == false || exp == null) return this;
105-
return this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp?.Body, null, _params));
105+
return this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, null, _table, null, exp?.Body, null, _params));
106106
}
107107
public IDelete<T1> Where(string sql, object parms = null) => WhereIf(true, sql, parms);
108108
public IDelete<T1> WhereIf(bool condition, string sql, object parms = null)

FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public IInsertOrUpdate<T1> CommandTimeout(int timeout)
6767
return this;
6868
}
6969

70-
public IInsertOrUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns) => UpdateColumns(_commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, columns?.Body, false, null));
70+
public IInsertOrUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns) => UpdateColumns(_commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, columns?.Body, false, null));
7171
public IInsertOrUpdate<T1> UpdateColumns(string[] columns)
7272
{
7373
var cols = columns.Distinct().ToDictionary(a => a);

0 commit comments

Comments
 (0)