Skip to content

Commit

Permalink
修复Select解析强类型实体映射情形时sql解析错误bug;新增对应的单元测试用例;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Jun 6, 2021
1 parent 9347e51 commit ce32cd2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions SQLBuilder.UnitTest/Models/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SQLBuilder.UnitTest
[Table("Base_Class")]
public class Class
{
public int? Id { get; set; }
[Column(Insert = false)]
public int CityId { get; set; }
[Column(Update = false)]
Expand Down
8 changes: 8 additions & 0 deletions SQLBuilder.UnitTest/Models/Teacher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Teacher

public int? Age { get; set; }

public int ClassId { get; set; }

public TeacherType? Type { get; set; }
}

Expand All @@ -17,4 +19,10 @@ public enum TeacherType : int
A = 1,
B = 2
}

public class TeacherResponse
{
public string TeacherName { get; set; }
public string ClassName { get; set; }
}
}
19 changes: 19 additions & 0 deletions SQLBuilder.UnitTest/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,25 @@ public void Test_Select_112()
Assert.AreEqual("SELECT x1.*,y.Name AS AccountName FROM Base_UserInfo AS x1 INNER JOIN Base_Account AS y ON y.UserId = x1.Id INNER JOIN Base_UserInfo AS x2 ON x1.Id = x2.Sex AND x1.Name = y.Name WHERE x1.Id IS NOT NULL", builder.Sql);
Assert.AreEqual(0, builder.Parameters.Count);
}

/// <summary>
/// 查询113
/// </summary>
[TestMethod]
public void Test_Select_113()
{
var teacherType = TeacherType.A;
var builder = SqlBuilder
.Select<Teacher, Class>((x, y) =>
new TeacherResponse { TeacherName = x.Name, ClassName = y.Name })
.InnerJoin<Class>((x, y) =>
x.ClassId == y.Id)
.Where(x =>
x.Type == teacherType &&
x.Name != null);
Assert.AreEqual("SELECT x.Name AS TeacherName,y.Name AS ClassName FROM Base_Teacher AS x INNER JOIN Base_Class AS y ON x.ClassId = y.Id WHERE x.Type = @p__1 AND x.Name IS NOT NULL", builder.Sql);
Assert.AreEqual(1, builder.Parameters.Count);
}
#endregion

#region Page
Expand Down
11 changes: 4 additions & 7 deletions SQLBuilder/Expressions/MemberInitExpressionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,18 @@ public override SqlWrapper Select(MemberInitExpression expression, SqlWrapper sq
{
foreach (MemberAssignment memberAssignment in expression.Bindings)
{
var type = expression.Type != memberAssignment.Member.DeclaringType ?
expression.Type :
memberAssignment.Member.DeclaringType;

var aliasName = memberAssignment.Member.Name;
var tableName = sqlWrapper.GetTableName(type);

if ((memberAssignment.Expression as MemberExpression)?.Expression is ParameterExpression parameterExpr)
if (memberAssignment.Expression is MemberExpression memberExpr && memberExpr.Expression is ParameterExpression parameterExpr)
{
var type = parameterExpr.Type;
var tableName = sqlWrapper.GetTableName(type);
var tableAlias = sqlWrapper.GetTableAlias(tableName, parameterExpr.Name);

if (tableAlias.IsNotNullOrEmpty())
tableAlias += ".";

var fieldName = tableAlias + sqlWrapper.GetColumnInfo(type, memberAssignment.Member).columnName;
var fieldName = tableAlias + sqlWrapper.GetColumnInfo(type, memberExpr.Member).columnName;

sqlWrapper.AddField(fieldName);
}
Expand Down

0 comments on commit ce32cd2

Please sign in to comment.