Skip to content

Commit

Permalink
repair bug: ILIst.Contains Expression #16
Browse files Browse the repository at this point in the history
  • Loading branch information
28810 authored and 28810 committed Mar 16, 2019
1 parent c7394b7 commit 4667698
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 10 deletions.
15 changes: 15 additions & 0 deletions FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand All @@ -24,6 +25,20 @@ public void Array() {
var sql1111 = select.Where(a => inarray.Contains(a.testFieldInt)).ToList();
var sql1122 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList();
var sql1133 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList();

//in not in
var sql11111 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();
var sql11222 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();
var sql11333 = select.Where(a => !new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();

var sql11111a = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt)).ToList();
var sql11222b = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt) == false).ToList();
var sql11333c = select.Where(a => !new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt)).ToList();

var inarray2 = new List<int>() { 1, 2, 3 };
var sql111111 = select.Where(a => inarray.Contains(a.testFieldInt)).ToList();
var sql112222 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList();
var sql113333 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList();
}

[Table(Name = "tb_alltype")]
Expand Down
16 changes: 16 additions & 0 deletions FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand All @@ -23,6 +24,21 @@ public void Array() {
var sql1111 = select.Where(a => inarray.Contains(a.Int)).ToList();
//var sql1122 = select.Where(a => inarray.Contains(a.Int) == false).ToList();
var sql1133 = select.Where(a => !inarray.Contains(a.Int)).ToList();


//in not in
var sql11111 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.Int)).ToList();
//var sql11222 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.Int) == false).ToList();
var sql11333 = select.Where(a => !new List<int>() { 1, 2, 3 }.Contains(a.Int)).ToList();

var sql11111a = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.Int)).ToList();
//var sql11222b = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.Int) == false).ToList();
var sql11333c = select.Where(a => !new List<int>(new[] { 1, 2, 3 }).Contains(a.Int)).ToList();

var inarray2 = new List<int>() { 1, 2, 3 };
var sql111111 = select.Where(a => inarray.Contains(a.Int)).ToList();
//var sql112222 = select.Where(a => inarray.Contains(a.Int) == false).ToList();
var sql113333 = select.Where(a => !inarray.Contains(a.Int)).ToList();
}

[Table(Name = "tb_alltype")]
Expand Down
15 changes: 15 additions & 0 deletions FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public void Array() {
var sql1122 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToSql();
var sql1133 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToSql();

//in not in
var sql11111 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();
var sql11222 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();
var sql11333 = select.Where(a => !new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();

var sql11111a = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt)).ToList();
var sql11222b = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt) == false).ToList();
var sql11333c = select.Where(a => !new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt)).ToList();

var inarray2 = new List<int>() { 1, 2, 3 };
var sql111111 = select.Where(a => inarray.Contains(a.testFieldInt)).ToList();
var sql112222 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList();
var sql113333 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList();


var sql3 = select.Where(a => a.testFieldIntArray.Any()).ToList();
var sql4 = select.Where(a => a.testFieldIntArray.Any() == false).ToList();

Expand Down
15 changes: 15 additions & 0 deletions FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql.DataAnnotations;
using FreeSql.Tests.DataContext.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -28,6 +29,20 @@ public void Array() {
var sql1111 = select.Where(a => inarray.Contains(a.testFieldInt)).ToList();
//var sql1122 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList();
var sql1133 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList();

//in not in
var sql11111 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();
//var sql11222 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();
var sql11333 = select.Where(a => !new List<int>() { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();

var sql11111a = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt)).ToList();
//var sql11222b = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt) == false).ToList();
var sql11333c = select.Where(a => !new List<int>(new[] { 1, 2, 3 }).Contains(a.testFieldInt)).ToList();

var inarray2 = new List<int>() { 1, 2, 3 };
var sql111111 = select.Where(a => inarray.Contains(a.testFieldInt)).ToList();
//var sql112222 = select.Where(a => inarray.Contains(a.testFieldInt) == false).ToList();
var sql113333 = select.Where(a => !inarray.Contains(a.testFieldInt)).ToList();
}

[Table(Name = "tb_alltype")]
Expand Down
15 changes: 15 additions & 0 deletions FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

Expand All @@ -23,6 +24,20 @@ public void Array() {
var sql1111 = select.Where(a => inarray.Contains(a.Int)).ToList();
//var sql1122 = select.Where(a => inarray.Contains(a.Int) == false).ToList();
var sql1133 = select.Where(a => !inarray.Contains(a.Int)).ToList();

//in not in
var sql11111 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.Int)).ToList();
//var sql11222 = select.Where(a => new List<int>() { 1, 2, 3 }.Contains(a.Int) == false).ToList();
var sql11333 = select.Where(a => !new List<int>() { 1, 2, 3 }.Contains(a.Int)).ToList();

var sql11111a = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.Int)).ToList();
//var sql11222b = select.Where(a => new List<int>(new[] { 1, 2, 3 }).Contains(a.Int) == false).ToList();
var sql11333c = select.Where(a => !new List<int>(new[] { 1, 2, 3 }).Contains(a.Int)).ToList();

var inarray2 = new List<int>() { 1, 2, 3 };
var sql111111 = select.Where(a => inarray.Contains(a.Int)).ToList();
//var sql112222 = select.Where(a => inarray.Contains(a.Int) == false).ToList();
var sql113333 = select.Where(a => !inarray.Contains(a.Int)).ToList();
}

[Table(Name = "tb_alltype")]
Expand Down
25 changes: 23 additions & 2 deletions FreeSql/MySql/MySqlExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -21,15 +22,15 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (objType?.FullName == "System.Byte[]") return null;

var argIndex = 0;
if (objType == null && callExp.Method.DeclaringType.FullName == typeof(Enumerable).FullName) {
if (objType == null && callExp.Method.DeclaringType == typeof(Enumerable)) {
objExp = callExp.Arguments.FirstOrDefault();
objType = objExp?.Type;
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray == true) {
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
Expand All @@ -46,7 +47,27 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (a > 0) arrSb.Append(",");
arrSb.Append(getExp(arrExp.Expressions[a]));
}
if (arrSb.Length == 1) arrSb.Append("NULL");
return arrSb.Append(")").ToString();
case ExpressionType.ListInit:
var listExp = exp as ListInitExpression;
var listSb = new StringBuilder();
listSb.Append("(");
for (var a = 0; a < listExp.Initializers.Count; a++) {
if (listExp.Initializers[a].Arguments.Any() == false) continue;
if (a > 0) listSb.Append(",");
listSb.Append(getExp(listExp.Initializers[a].Arguments.FirstOrDefault()));
}
if (listSb.Length == 1) listSb.Append("NULL");
return listSb.Append(")").ToString();
case ExpressionType.New:
var newExp = exp as NewExpression;
if (typeof(IList).IsAssignableFrom(newExp.Type)) {
if (newExp.Arguments.Count == 0) return "(NULL)";
if (typeof(IEnumerable).IsAssignableFrom(newExp.Arguments[0].Type) == false) return "(NULL)";
return getExp(newExp.Arguments[0]);
}
return null;
}
return null;
}
Expand Down
25 changes: 23 additions & 2 deletions FreeSql/Oracle/OracleExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -21,15 +22,15 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (objType?.FullName == "System.Byte[]") return null;

var argIndex = 0;
if (objType == null && callExp.Method.DeclaringType.FullName == typeof(Enumerable).FullName) {
if (objType == null && callExp.Method.DeclaringType == typeof(Enumerable)) {
objExp = callExp.Arguments.FirstOrDefault();
objType = objExp?.Type;
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray == true) {
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
Expand All @@ -46,7 +47,27 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (a > 0) arrSb.Append(",");
arrSb.Append(getExp(arrExp.Expressions[a]));
}
if (arrSb.Length == 1) arrSb.Append("NULL");
return arrSb.Append(")").ToString();
case ExpressionType.ListInit:
var listExp = exp as ListInitExpression;
var listSb = new StringBuilder();
listSb.Append("(");
for (var a = 0; a < listExp.Initializers.Count; a++) {
if (listExp.Initializers[a].Arguments.Any() == false) continue;
if (a > 0) listSb.Append(",");
listSb.Append(getExp(listExp.Initializers[a].Arguments.FirstOrDefault()));
}
if (listSb.Length == 1) listSb.Append("NULL");
return listSb.Append(")").ToString();
case ExpressionType.New:
var newExp = exp as NewExpression;
if (typeof(IList).IsAssignableFrom(newExp.Type)) {
if (newExp.Arguments.Count == 0) return "(NULL)";
if (typeof(IEnumerable).IsAssignableFrom(newExp.Arguments[0].Type) == false) return "(NULL)";
return getExp(newExp.Arguments[0]);
}
return null;
}
return null;
}
Expand Down
25 changes: 23 additions & 2 deletions FreeSql/PostgreSQL/PostgreSQLExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FreeSql.Internal.Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -26,15 +27,15 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (objType?.FullName == "System.Byte[]") return null;

var argIndex = 0;
if (objType == null && callExp.Method.DeclaringType.FullName == typeof(Enumerable).FullName) {
if (objType == null && callExp.Method.DeclaringType == typeof(Enumerable)) {
objExp = callExp.Arguments.FirstOrDefault();
objType = objExp?.Type;
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray == true) {
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Any":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
Expand Down Expand Up @@ -141,7 +142,27 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (a > 0) arrSb.Append(",");
arrSb.Append(getExp(arrExp.Expressions[a]));
}
if (arrSb.Length == 1) arrSb.Append("NULL");
return arrSb.Append("]").ToString();
case ExpressionType.ListInit:
var listExp = exp as ListInitExpression;
var listSb = new StringBuilder();
listSb.Append("(");
for (var a = 0; a < listExp.Initializers.Count; a++) {
if (listExp.Initializers[a].Arguments.Any() == false) continue;
if (a > 0) listSb.Append(",");
listSb.Append(getExp(listExp.Initializers[a].Arguments.FirstOrDefault()));
}
if (listSb.Length == 1) listSb.Append("NULL");
return listSb.Append(")").ToString();
case ExpressionType.New:
var newExp = exp as NewExpression;
if (typeof(IList).IsAssignableFrom(newExp.Type)) {
if (newExp.Arguments.Count == 0) return "(NULL)";
if (typeof(IEnumerable).IsAssignableFrom(newExp.Arguments[0].Type) == false) return "(NULL)";
return getExp(newExp.Arguments[0]);
}
return null;
}
return null;
}
Expand Down
25 changes: 23 additions & 2 deletions FreeSql/SqlServer/SqlServerExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -21,15 +22,15 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (objType?.FullName == "System.Byte[]") return null;

var argIndex = 0;
if (objType == null && callExp.Method.DeclaringType.FullName == typeof(Enumerable).FullName) {
if (objType == null && callExp.Method.DeclaringType == typeof(Enumerable)) {
objExp = callExp.Arguments.FirstOrDefault();
objType = objExp?.Type;
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray == true) {
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
Expand All @@ -46,7 +47,27 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
if (a > 0) arrSb.Append(",");
arrSb.Append(getExp(arrExp.Expressions[a]));
}
if (arrSb.Length == 1) arrSb.Append("NULL");
return arrSb.Append(")").ToString();
case ExpressionType.ListInit:
var listExp = exp as ListInitExpression;
var listSb = new StringBuilder();
listSb.Append("(");
for (var a = 0; a < listExp.Initializers.Count; a++) {
if (listExp.Initializers[a].Arguments.Any() == false) continue;
if (a > 0) listSb.Append(",");
listSb.Append(getExp(listExp.Initializers[a].Arguments.FirstOrDefault()));
}
if (listSb.Length == 1) listSb.Append("NULL");
return listSb.Append(")").ToString();
case ExpressionType.New:
var newExp = exp as NewExpression;
if (typeof(IList).IsAssignableFrom(newExp.Type)) {
if (newExp.Arguments.Count == 0) return "(NULL)";
if (typeof(IEnumerable).IsAssignableFrom(newExp.Arguments[0].Type) == false) return "(NULL)";
return getExp(newExp.Arguments[0]);
}
return null;
}
return null;
}
Expand Down
Loading

0 comments on commit 4667698

Please sign in to comment.