From 205421f7e0991b512e7cda6d08b9958308624af2 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Thu, 25 Apr 2019 17:28:25 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E8=A1=A5=E5=85=85=20Expression=20IEnumerab?= =?UTF-8?q?le.Contains=20=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E5=8F=AA=E8=83=BD=E6=95=B0=E7=BB=84=E6=88=96IList?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MySql/MySqlExpression/OtherTest.cs | 3 ++ .../Oracle/OracleExpression/OtherTest.cs | 3 ++ .../PostgreSQLExpression/OtherTest.cs | 2 + .../SqlServerExpression/OtherTest.cs | 3 ++ .../Sqlite/SqliteExpression/OtherTest.cs | 3 ++ FreeSql/DataAnnotations/ColumnAttribute.cs | 5 ++ FreeSql/MySql/MySqlExpression.cs | 12 ++--- FreeSql/Oracle/OracleExpression.cs | 12 ++--- FreeSql/PostgreSQL/PostgreSQLExpression.cs | 52 +++++++++---------- FreeSql/SqlServer/SqlServerExpression.cs | 12 ++--- FreeSql/Sqlite/SqliteExpression.cs | 12 ++--- 11 files changed, 64 insertions(+), 55 deletions(-) diff --git a/FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs b/FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs index ece900da7..bee6336f8 100644 --- a/FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs +++ b/FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs @@ -21,6 +21,9 @@ public void Array() { Assert.Throws(() => { select.Where(a => nullarr.Contains(a.testFieldInt)).ToList(); }); Assert.Throws(() => { select.Where(a => new int[0].Contains(a.testFieldInt)).ToList(); }); + IEnumerable testlinqlist = new List(new[] { 1, 2, 3 }); + var testlinq = select.Where(a => testlinqlist.Contains(a.testFieldInt)).ToList(); + //in not in var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToList(); var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList(); diff --git a/FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs b/FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs index a9c428be7..67acf44bd 100644 --- a/FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs +++ b/FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs @@ -15,6 +15,9 @@ public OtherTest() { [Fact] public void Array() { + IEnumerable testlinqlist = new List(new[] { 1, 2, 3 }); + var testlinq = select.Where(a => testlinqlist.Contains(a.Int)).ToList(); + //in not in var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList(); //var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int) == false).ToList(); diff --git a/FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs b/FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs index c8c99168b..14e95e546 100644 --- a/FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs +++ b/FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs @@ -23,6 +23,8 @@ public OtherTest() { [Fact] public void Array() { + IEnumerable testlinqlist = new List(new[] { 1, 2, 3 }); + var testlinq = select.Where(a => testlinqlist.Contains(a.testFieldInt)).ToList(); var sql1 = select.Where(a => a.testFieldIntArray.Contains(1)).ToList(); var sql2 = select.Where(a => a.testFieldIntArray.Contains(1) == false).ToList(); diff --git a/FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs b/FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs index aeb695f05..2bad405c0 100644 --- a/FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs +++ b/FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs @@ -20,6 +20,9 @@ public OtherTest(SqlServerFixture sqlserverFixture) [Fact] public void Array() { + IEnumerable testlinqlist = new List(new[] { 1, 2, 3 }); + var testlinq = select.Where(a => testlinqlist.Contains(a.testFieldInt)).ToList(); + //in not in var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToList(); //var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList(); diff --git a/FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs b/FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs index c348ef366..2d962bbd6 100644 --- a/FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs +++ b/FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs @@ -15,6 +15,9 @@ public OtherTest() { [Fact] public void Array() { + IEnumerable testlinqlist = new List(new[] { 1, 2, 3 }); + var testlinq = select.Where(a => testlinqlist.Contains(a.Int)).ToList(); + //in not in var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList(); //var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int) == false).ToList(); diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs index d58046d29..a46238d10 100644 --- a/FreeSql/DataAnnotations/ColumnAttribute.cs +++ b/FreeSql/DataAnnotations/ColumnAttribute.cs @@ -42,5 +42,10 @@ public class ColumnAttribute : Attribute { /// 数据库默认值 /// internal object DbDefautValue { get; set; } + + /// + /// 类型映射,比如:可将 enum 属性映射成 typeof(string) + /// + public Type Mapping { get; set; } } } diff --git a/FreeSql/MySql/MySqlExpression.cs b/FreeSql/MySql/MySqlExpression.cs index 129b900cd..974c4b690 100644 --- a/FreeSql/MySql/MySqlExpression.cs +++ b/FreeSql/MySql/MySqlExpression.cs @@ -90,14 +90,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List 0)"; - case "Contains": - //判断 in 或 array @> array - var right1 = getExp(callExp.Arguments[argIndex]); - if (left.StartsWith("array[") || left.EndsWith("]")) - return $"{right1} in ({left.Substring(6, left.Length - 7)})"; - if (left.StartsWith("(") || left.EndsWith(")")) - return $"{right1} in {left}"; - if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]"; - return $"({left} @> array[{right1}])"; - case "Concat": - if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; - var right2 = getExp(callExp.Arguments[argIndex]); - if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]"; - return $"({left} || {right2})"; - case "GetLength": - case "GetLongLength": - case "Length": - case "Count": - if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; - return $"case when {left} is null then 0 else array_length({left},1) end"; - } + switch (callExp.Method.Name) { + case "Any": + if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; + return $"(case when {left} is null then 0 else array_length({left},1) end > 0)"; + case "Contains": + //判断 in 或 array @> array + var right1 = getExp(callExp.Arguments[argIndex]); + if (left.StartsWith("array[") || left.EndsWith("]")) + return $"{right1} in ({left.Substring(6, left.Length - 7)})"; + if (left.StartsWith("(") || left.EndsWith(")")) + return $"{right1} in {left}"; + if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]"; + return $"({left} @> array[{right1}])"; + case "Concat": + if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; + var right2 = getExp(callExp.Arguments[argIndex]); + if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]"; + return $"({left} || {right2})"; + case "GetLength": + case "GetLongLength": + case "Length": + case "Count": + if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; + return $"case when {left} is null then 0 else array_length({left},1) end"; } } break; diff --git a/FreeSql/SqlServer/SqlServerExpression.cs b/FreeSql/SqlServer/SqlServerExpression.cs index bfcad652d..99366be12 100644 --- a/FreeSql/SqlServer/SqlServerExpression.cs +++ b/FreeSql/SqlServer/SqlServerExpression.cs @@ -93,14 +93,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List