Skip to content

Commit

Permalink
#253 Support CTE
Browse files Browse the repository at this point in the history
  • Loading branch information
mk3008 committed Oct 30, 2023
1 parent 93942c3 commit 1559cc0
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 13 deletions.
7 changes: 1 addition & 6 deletions src/QueryBuilderByLinq/SelectQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ private SelectQuery BuildRootQuery(MethodCallExpression expression)
// from, relation pattern
return BuildRootQuery(expression, root, nest, select, where);
}
else if (nestBody.Method.Name == nameof(Sql.CommonTable2))
{
// CTE, CTE pattern
return BuildCteQuery(expression, root, nest, select);
}
}

throw new NotSupportedException();
Expand Down Expand Up @@ -145,7 +140,7 @@ private SelectQuery BuildCteQuery(MethodCallExpression expression, ConstantExpre

if (from.Body is MethodCallExpression fromBody && Queryable.TryParse(fromBody, out var cte2))
{
sq.With(cte2.ToQueryAsPostgres()).As(joinAlias.Name!);
sq.With(cte2.ToQueryAsPostgres()).As(joinAlias!.Name!);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilderByLinq/SelectQueryExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static bool TryParse(MethodCallExpression method, out IQueryable query)

if (!method.Arguments.Any()) return false;

if (!(method.Method.Name == nameof(Sql.InnerJoinTable) || method.Method.Name == nameof(Sql.LeftJoinTable) || method.Method.Name == nameof(Sql.CrossJoinTable) || method.Method.Name == nameof(Sql.CommonTable2)))
if (!(method.Method.Name == nameof(Sql.InnerJoinTable) || method.Method.Name == nameof(Sql.LeftJoinTable) || method.Method.Name == nameof(Sql.CrossJoinTable) || method.Method.Name == nameof(Sql.CommonTable)))
{
return false;
}
Expand Down
5 changes: 0 additions & 5 deletions src/QueryBuilderByLinq/Sql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public static IQueryable<T> CommonTable<T>(IQueryable<T> subquery)
return new Table<T>(subquery);
}

public static IQueryable<T> CommonTable2<T>(IQueryable<T> subquery)
{
return subquery;
}

public static IQueryable<T> FromTable<T>()
{
return Enumerable.Empty<T>().AsQueryable();
Expand Down
2 changes: 1 addition & 1 deletion test/QueryBuilderByLinq.Test/CommonTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void CTEsTest()
var sub_a2 = from a in FromTable<table_a>() select new { a.a_id, a.value };

var query = from cte1 in CommonTable(sub_a1)
from cte2 in CommonTable2(sub_a2)
from cte2 in CommonTable(sub_a2)
from b in FromTable<table_a>(nameof(cte1))
from c in InnerJoinTable<table_a>(nameof(cte2), x => b.a_id == x.a_id)
where b.a_id == 1
Expand Down

0 comments on commit 1559cc0

Please sign in to comment.