Skip to content

Commit

Permalink
补充 表达式解析 Equals 为 = #28
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Apr 4, 2019
1 parent 8b49dd8 commit b1578f8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
10 changes: 10 additions & 0 deletions FreeSql.Tests/MySql/MySqlExpression/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class TestTypeParentInfo {

public List<TestTypeInfo> Types { get; set; }
}
class TestEqualsGuid {
public Guid id { get; set; }
}

[Fact]
public void Equals__() {
var list = new List<object>();
list.Add(select.Where(a => a.Title.Equals("aaa")).ToList());
list.Add(g.sqlite.Select<TestEqualsGuid>().Where(a => a.id.Equals(Guid.Empty)).ToList());
}

[Fact]
public void Empty() {
Expand Down
11 changes: 11 additions & 0 deletions FreeSql.Tests/Oracle/OracleExpression/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class TestTypeParentInfo {

public List<TestTypeInfo> Types { get; set; }
}
class TestEqualsGuid {
public Guid id { get; set; }
}

[Fact]
public void Equals__() {
var list = new List<object>();
list.Add(select.Where(a => a.Title.Equals("aaa")).ToList());
list.Add(g.oracle.Select<TestEqualsGuid>().Where(a => a.id.Equals(Guid.Empty)).ToList());
}


[Fact]
public void Empty() {
Expand Down
11 changes: 11 additions & 0 deletions FreeSql.Tests/PostgreSQL/PostgreSQLExpression/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class TestTypeParentInfo {

public List<TestTypeInfo> Types { get; set; }
}
class TestEqualsGuid {
public Guid id { get; set; }
}

[Fact]
public void Equals__() {
var list = new List<object>();
list.Add(select.Where(a => a.Title.Equals("aaa")).ToList());
list.Add(g.pgsql.Select<TestEqualsGuid>().Where(a => a.id.Equals(Guid.Empty)).ToList());
}


[Fact]
public void Empty() {
Expand Down
10 changes: 10 additions & 0 deletions FreeSql.Tests/SqlServer/SqlServerExpression/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class TestTypeParentInfo {

public List<TestTypeInfo> Types { get; set; }
}
class TestEqualsGuid {
public Guid id { get; set; }
}

[Fact]
public void Equals__() {
var list = new List<object>();
list.Add(select.Where(a => a.Title.Equals("aaa")).ToList());
list.Add(_sqlserverFixture.SqlServer.Select<TestEqualsGuid>().Where(a => a.id.Equals(Guid.Empty)).ToList());
}

[Fact]
public void Empty() {
Expand Down
10 changes: 10 additions & 0 deletions FreeSql.Tests/Sqlite/SqliteExpression/StringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class TestTypeParentInfo {

public List<TestTypeInfo> Types { get; set; }
}
class TestEqualsGuid {
public Guid id { get; set; }
}

[Fact]
public void Equals__() {
var list = new List<object>();
list.Add(select.Where(a => a.Title.Equals("aaa")).ToList());
list.Add(g.sqlite.Select<TestEqualsGuid>().Where(a => a.id.Equals(Guid.Empty)).ToList());
}

[Fact]
public void Empty() {
Expand Down
4 changes: 2 additions & 2 deletions FreeSql.Tests/g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class g {
Console.WriteLine(traceLog);
}) //监听SQL命令对象,在执行后
.Build());
public static IFreeSql oracle = oracleLazy.Value;
public static IFreeSql oracle => oracleLazy.Value;

static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=10")
Expand All @@ -64,5 +64,5 @@ public class g {
Console.WriteLine(traceLog);
}) //监听SQL命令对象,在执行后
.Build());
public static IFreeSql sqlite = sqliteLazy.Value;
public static IFreeSql sqlite => sqliteLazy.Value;
}
12 changes: 12 additions & 0 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ internal string ExpressionLambdaToSql(Expression exp, List<SelectTableInfo> _tab
return $"case when {ExpressionLambdaToSql(condExp.Test, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, isDisableDiyParse, style)} then {ExpressionLambdaToSql(condExp.IfTrue, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, isDisableDiyParse, style)} else {ExpressionLambdaToSql(condExp.IfFalse, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, isDisableDiyParse, style)} end";
case ExpressionType.Call:
var exp3 = exp as MethodCallExpression;
if (exp3.Method.Name == "Equals" && exp3.Object != null && exp3.Arguments.Count > 0) {
var tmptryoper = "=";
var tmpleft = ExpressionLambdaToSql(exp3.Object, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, isDisableDiyParse, style);
var tmpright = ExpressionLambdaToSql(exp3.Arguments[0], _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, isDisableDiyParse, style);
if (tmpleft == "NULL") {
var tmp33 = tmpright;
tmpright = tmpleft;
tmpleft = tmp33;
}
if (tmpright == "NULL") tmptryoper = " IS ";
return $"{tmpleft}{tmptryoper}{tmpright}";
}
var callType = exp3.Object?.Type ?? exp3.Method.DeclaringType;
switch (callType.FullName) {
case "System.String": return ExpressionLambdaToSqlCallString(exp3, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName, isDisableDiyParse, style);
Expand Down

0 comments on commit b1578f8

Please sign in to comment.