Skip to content

Commit

Permalink
修复多表连接查询只返回单表时(如单元测试Test_Select_86所示),无法全部解析表别名bug;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Oct 31, 2020
1 parent 1861396 commit 95e8283
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 47 deletions.
14 changes: 13 additions & 1 deletion SQLBuilder.UnitTest/SelectTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SQLBuilder.Entry;
using SQLBuilder.Enums;
using SQLBuilder.Extensions;
Expand Down Expand Up @@ -1634,6 +1634,18 @@ group a by a.Id into g
Assert.AreEqual("SELECT [g].[Id] FROM [Base_UserInfo] AS [g] WHERE [g].[Id] IS NOT NULL AND [g].[Name] LIKE '%' + @p__1 + '%' ORDER BY [g].[Id] DESC GROUP BY [g].[Id]", query.Sql);
Assert.AreEqual(1, query.Parameters.Count);
}

/// <summary>
/// 查询86
/// </summary>
[TestMethod]
public void Test_Select_86()
{
var builder = SqlBuilder.Select<UserInfo, Account>((u, a) => a)
.InnerJoin<Account>((u, a) => u.Id == a.UserId);
Assert.AreEqual("SELECT [a].* FROM [Base_UserInfo] AS [u] INNER JOIN [Base_Account] AS [a] ON [u].[Id] = [a].[UserId]", builder.Sql);
Assert.AreEqual(0, builder.Parameters.Count);
}
#endregion

#region Page
Expand Down
58 changes: 12 additions & 46 deletions SQLBuilder/Entry/SqlBuilderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ public SqlBuilderCore<T> Select(Expression expression = null, string sql = null)
public SqlBuilderCore<T> Select(Expression<Func<T, object>> expression = null)
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant)
if (expr?.NodeType == ExpressionType.Constant ||
expr?.NodeType == ExpressionType.Parameter ||
expr?.NodeType == ExpressionType.New)
expr = expression;

return this.Select(expr);
Expand All @@ -300,11 +302,7 @@ public SqlBuilderCore<T> Select(Expression<Func<T, object>> expression = null)
public SqlBuilderCore<T> Select<T2>(Expression<Func<T, T2, object>> expression = null)
where T2 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -319,11 +317,7 @@ public SqlBuilderCore<T> Select<T2, T3>(Expression<Func<T, T2, T3, object>> expr
where T2 : class
where T3 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -340,11 +334,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4>(Expression<Func<T, T2, T3, T4, objec
where T3 : class
where T4 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -363,11 +353,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, T
where T4 : class
where T5 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -388,11 +374,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T
where T5 : class
where T6 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -415,11 +397,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T
where T6 : class
where T7 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -444,11 +422,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<T, T
where T7 : class
where T8 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8)));
return this.Select(expression?.Body, sql);
}

Expand All @@ -475,11 +449,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<
where T8 : class
where T9 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9)));
return this.Select(expression?.Body, sql);
}

Expand Down Expand Up @@ -508,11 +478,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<
where T9 : class
where T10 : class
{
var expr = expression?.Body;
if (expr?.NodeType == ExpressionType.Constant || expr?.NodeType == ExpressionType.New)
expr = expression;

var sql = this.Select(GetExpressionAlias(expr, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10)));
var sql = this.Select(GetExpressionAlias(expression, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10)));
return this.Select(expression?.Body, sql);
}
#endregion
Expand Down

0 comments on commit 95e8283

Please sign in to comment.