Skip to content

Commit

Permalink
- 修复 AsTable 子查询未传播的 bug;#1103
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jun 9, 2022
1 parent 7e2b052 commit 38c6796
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 100 deletions.
6 changes: 3 additions & 3 deletions Extensions/FreeSql.Extensions.Linq/SelectedQueryProvider.cs
Expand Up @@ -67,7 +67,7 @@ public SelectedQueryProvider(Select0Provider select, Expression selector)
var index = -10000; //临时规则,不返回 as1

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

Expand Down Expand Up @@ -162,7 +162,7 @@ public ISelectedQuery<TOut> OrderByIf<TMember>(bool condition, Expression<Func<T
{
if (condition == false) return this;
_lambdaParameter = column?.Parameters[0];
var sql = _comonExp.ExpressionWhereLambda(null, column, this, null, null);
var sql = _comonExp.ExpressionWhereLambda(null, null, column, this, null, null);
var method = _select.GetType().GetMethod("OrderBy", new[] { typeof(string), typeof(object) });
method.Invoke(_select, new object[] { descending ? $"{sql} DESC" : sql, null });
return this;
Expand All @@ -174,7 +174,7 @@ public ISelectedQuery<TOut> WhereIf(bool condition, Expression<Func<TOut, bool>>
{
if (condition == false) return this;
_lambdaParameter = exp?.Parameters[0];
var sql = _comonExp.ExpressionWhereLambda(null, exp, this, null, null);
var sql = _comonExp.ExpressionWhereLambda(null, null, exp, this, null, null);
var method = _select.GetType().GetMethod("Where", new[] { typeof(string), typeof(object) });
method.Invoke(_select, new object[] { sql, null });
return this;
Expand Down
Expand Up @@ -12,26 +12,6 @@ namespace FreeSql.Tests.Odbc.Default
public class OdbcCodeFirstTest
{

[Fact]
public void Test_0String()
{
var fsql = g.odbc;
fsql.Delete<test_0string01>().Where("1=1").ExecuteAffrows();

Assert.Equal(1, fsql.Insert(new test_0string01 { name = @"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000" }).ExecuteAffrows());
Assert.Equal(1, fsql.Insert(new test_0string01 { name = @"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000" }).NoneParameter().ExecuteAffrows());

var list = fsql.Select<test_0string01>().ToList();
Assert.Equal(2, list.Count);
Assert.Equal(@"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000", list[0].name);
Assert.Equal(@"1.0000\0.0000\0.0000\0.0000\1.0000\0.0000", list[1].name);
}
class test_0string01
{
public Guid id { get; set; }
public string name { get; set; }
}

[Fact]
public void 中文表_字段()
{
Expand Down
18 changes: 18 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs
Expand Up @@ -1126,6 +1126,24 @@ public void As()
[Fact]
public void AsTable()
{
var fsql = g.sqlite;
var asTableSubSql = fsql.Select<TestTypeParentInfo>().AsTable((_, old) => $"{old}_01").ToSql(a => new
{
a.Id,
max = fsql.Select<TestTypeInfo>().Max(b => b.Guid),
any = a.Types.Any(b => b.Name == "xx"),
any2 = a.Types.AsSelect().Any(b => b.Name == "xx"),
sub = fsql.Select<TestTypeInfo>().Where(b => b.Guid == a.Id).ToList()
});
Assert.Equal(@"SELECT a.""Id"" as1, ifnull((SELECT max(a.""Guid"")
FROM ""TestTypeInfo_01"" a), 0) as2, exists(SELECT 1
FROM ""TestTypeInfo_01"" b
WHERE (b.""ParentId"" = a.""Id"") AND (b.""Name"" = 'xx')
limit 0,1) as3, exists(SELECT 1
FROM ""TestTypeInfo_01"" b
WHERE (b.""Name"" = 'xx') AND (b.""ParentId"" = a.""Id"")
limit 0,1) as4
FROM ""TestTypeParentInfo_01"" a", asTableSubSql);

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

Expand Down
6 changes: 3 additions & 3 deletions FreeSql/Extensions/FreeSqlGlobalExtensions.cs
Expand Up @@ -696,10 +696,10 @@ WHERE find_in_set({select._commonUtils.QuoteSqlName(tbref.RefColumns[0].Attribut
case DataType.OdbcSqlServer:
case DataType.Firebird:
case DataType.ClickHouse:
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, Expression.Call(typeof(Convert).GetMethod("ToString", new Type[] { typeof(string) }), pathSelector?.Body), null, null, null);
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, select._tableRule, Expression.Call(typeof(Convert).GetMethod("ToString", new Type[] { typeof(string) }), pathSelector?.Body), null, null, null);
break;
default:
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, pathSelector?.Body, null, null, null);
sql1ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, select._tableRule, pathSelector?.Body, null, null, null);
break;
}
sql1ctePath = $"{sql1ctePath} as cte_path, ";
Expand All @@ -717,7 +717,7 @@ WHERE find_in_set({select._commonUtils.QuoteSqlName(tbref.RefColumns[0].Attribut
if (pathSelector != null)
{
select._tables[0].Parameter = pathSelector?.Parameters[0];
var wct2ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, pathSelector?.Body, null, null, null);
var wct2ctePath = select._commonExpression.ExpressionWhereLambda(select._tables, select._tableRule, pathSelector?.Body, null, null, null);
sql2ctePath = select._commonUtils.StringConcat(
new string[] {
up == false ? "wct1.cte_path" : wct2ctePath,
Expand Down
9 changes: 9 additions & 0 deletions FreeSql/FreeSql.csproj
Expand Up @@ -37,6 +37,11 @@
</ItemGroup>

<ItemGroup>
<Compile Update="Internal\CommonProvider\SelectProvider\Select1Provider2`16.cs">
<DependentUpon>Select1Provider2`16.tt</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
<Compile Update="Properties\CoreStrings.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand All @@ -45,6 +50,10 @@
</ItemGroup>

<ItemGroup>
<None Update="Internal\CommonProvider\SelectProvider\T4Temp\Select1Provider2`16.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Select1Provider2`16.cs</LastGenOutput>
</None>
<None Update="Properties\CoreStrings.Designer.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>CoreStrings.Designer.cs</LastGenOutput>
Expand Down
8 changes: 5 additions & 3 deletions FreeSql/Internal/CommonExpression.cs
Expand Up @@ -993,6 +993,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
break;
}
object fsql = null;
Select0Provider fsqlSelect0 = null;
List<SelectTableInfo> fsqltables = null;
var fsqltable1SetAlias = false;
Type fsqlType = null;
Expand Down Expand Up @@ -1071,9 +1072,7 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
if (fsql == null) fsql = Expression.Lambda(exp3tmp).Compile().DynamicInvoke();
fsqlType = fsql?.GetType();
if (fsqlType == null) break;
var fsqlSelect0 = fsql as Select0Provider;
if (tsc._tableRule != null)
fsqlSelect0._tableRules.Add(tsc._tableRule);
fsqlSelect0 = fsql as Select0Provider;
switch (exp3.Method.Name)
{
case "Any": //exists
Expand Down Expand Up @@ -1196,6 +1195,9 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
}
if (fsql != null)
{
if (fsqlSelect0 != null && tsc._tableRule != null && fsqlSelect0._tableRules.Any() == false)
fsqlSelect0._tableRules.Add(tsc._tableRule);

if (asSelectParentExp != null)
{
//执行 AsSelect() 的关联,OneToMany,ManyToMany,PgArrayToMany
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/Internal/CommonProvider/DeleteProvider.cs
Expand Up @@ -102,7 +102,7 @@ public int ExecuteAffrows()
public IDelete<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp)
{
if (condition == false || exp == null) return this;
return this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp?.Body, null, _params));
return this.Where(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, null, _table, null, exp?.Body, null, _params));
}
public IDelete<T1> Where(string sql, object parms = null) => WhereIf(true, sql, parms);
public IDelete<T1> WhereIf(bool condition, string sql, object parms = null)
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/Internal/CommonProvider/InsertOrUpdateProvider.cs
Expand Up @@ -67,7 +67,7 @@ public IInsertOrUpdate<T1> CommandTimeout(int timeout)
return this;
}

public IInsertOrUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns) => UpdateColumns(_commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, columns?.Body, false, null));
public IInsertOrUpdate<T1> UpdateColumns(Expression<Func<T1, object>> columns) => UpdateColumns(_commonExpression.ExpressionSelectColumns_MemberAccess_New_NewArrayInit(null, null, columns?.Body, false, null));
public IInsertOrUpdate<T1> UpdateColumns(string[] columns)
{
var cols = columns.Distinct().ToDictionary(a => a);
Expand Down

0 comments on commit 38c6796

Please sign in to comment.