Skip to content

Commit e26a124

Browse files
authored
Merge pull request #40 from 2881099/dev_type_mapping
- 补充 Expression IEnumerable<T>.Contains 的支持,之前只能数组或IList<T>;
2 parents 169cf59 + 205421f commit e26a124

File tree

11 files changed

+64
-55
lines changed

11 files changed

+64
-55
lines changed

FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public void Array() {
2121
Assert.Throws<MySqlException>(() => { select.Where(a => nullarr.Contains(a.testFieldInt)).ToList(); });
2222
Assert.Throws<MySqlException>(() => { select.Where(a => new int[0].Contains(a.testFieldInt)).ToList(); });
2323

24+
IEnumerable<int> testlinqlist = new List<int>(new[] { 1, 2, 3 });
25+
var testlinq = select.Where(a => testlinqlist.Contains(a.testFieldInt)).ToList();
26+
2427
//in not in
2528
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();
2629
var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();

FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public OtherTest() {
1515

1616
[Fact]
1717
public void Array() {
18+
IEnumerable<int> testlinqlist = new List<int>(new[] { 1, 2, 3 });
19+
var testlinq = select.Where(a => testlinqlist.Contains(a.Int)).ToList();
20+
1821
//in not in
1922
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList();
2023
//var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int) == false).ToList();

FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public OtherTest() {
2323

2424
[Fact]
2525
public void Array() {
26+
IEnumerable<int> testlinqlist = new List<int>(new[] { 1, 2, 3 });
27+
var testlinq = select.Where(a => testlinqlist.Contains(a.testFieldInt)).ToList();
2628

2729
var sql1 = select.Where(a => a.testFieldIntArray.Contains(1)).ToList();
2830
var sql2 = select.Where(a => a.testFieldIntArray.Contains(1) == false).ToList();

FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public OtherTest(SqlServerFixture sqlserverFixture)
2020

2121
[Fact]
2222
public void Array() {
23+
IEnumerable<int> testlinqlist = new List<int>(new[] { 1, 2, 3 });
24+
var testlinq = select.Where(a => testlinqlist.Contains(a.testFieldInt)).ToList();
25+
2326
//in not in
2427
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToList();
2528
//var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();

FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public OtherTest() {
1515

1616
[Fact]
1717
public void Array() {
18+
IEnumerable<int> testlinqlist = new List<int>(new[] { 1, 2, 3 });
19+
var testlinq = select.Where(a => testlinqlist.Contains(a.Int)).ToList();
20+
1821
//in not in
1922
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList();
2023
//var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int) == false).ToList();

FreeSql/DataAnnotations/ColumnAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ public class ColumnAttribute : Attribute {
4242
/// 数据库默认值
4343
/// </summary>
4444
internal object DbDefautValue { get; set; }
45+
46+
/// <summary>
47+
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
48+
/// </summary>
49+
public Type Mapping { get; set; }
4550
}
4651
}

FreeSql/MySql/MySqlExpression.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
9090
argIndex++;
9191
}
9292
if (objType == null) objType = callExp.Method.DeclaringType;
93-
if (objType != null) {
93+
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
9494
var left = objExp == null ? null : getExp(objExp);
95-
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
96-
switch (callExp.Method.Name) {
97-
case "Contains":
98-
//判断 in
99-
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
100-
}
95+
switch (callExp.Method.Name) {
96+
case "Contains":
97+
//判断 in
98+
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
10199
}
102100
}
103101
break;

FreeSql/Oracle/OracleExpression.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
9090
argIndex++;
9191
}
9292
if (objType == null) objType = callExp.Method.DeclaringType;
93-
if (objType != null) {
93+
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
9494
var left = objExp == null ? null : getExp(objExp);
95-
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
96-
switch (callExp.Method.Name) {
97-
case "Contains":
98-
//判断 in
99-
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
100-
}
95+
switch (callExp.Method.Name) {
96+
case "Contains":
97+
//判断 in
98+
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
10199
}
102100
}
103101
break;

FreeSql/PostgreSQL/PostgreSQLExpression.cs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
9595
argIndex++;
9696
}
9797
if (objType == null) objType = callExp.Method.DeclaringType;
98-
if (objType != null) {
98+
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
9999
var left = objExp == null ? null : getExp(objExp);
100100
switch (objType.FullName) {
101101
case "Newtonsoft.Json.Linq.JToken":
@@ -134,32 +134,30 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
134134
case "Values": return $"avals({left})";
135135
}
136136
}
137-
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
138-
switch (callExp.Method.Name) {
139-
case "Any":
140-
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
141-
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)";
142-
case "Contains":
143-
//判断 in 或 array @> array
144-
var right1 = getExp(callExp.Arguments[argIndex]);
145-
if (left.StartsWith("array[") || left.EndsWith("]"))
146-
return $"{right1} in ({left.Substring(6, left.Length - 7)})";
147-
if (left.StartsWith("(") || left.EndsWith(")"))
148-
return $"{right1} in {left}";
149-
if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]";
150-
return $"({left} @> array[{right1}])";
151-
case "Concat":
152-
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
153-
var right2 = getExp(callExp.Arguments[argIndex]);
154-
if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]";
155-
return $"({left} || {right2})";
156-
case "GetLength":
157-
case "GetLongLength":
158-
case "Length":
159-
case "Count":
160-
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
161-
return $"case when {left} is null then 0 else array_length({left},1) end";
162-
}
137+
switch (callExp.Method.Name) {
138+
case "Any":
139+
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
140+
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)";
141+
case "Contains":
142+
//判断 in 或 array @> array
143+
var right1 = getExp(callExp.Arguments[argIndex]);
144+
if (left.StartsWith("array[") || left.EndsWith("]"))
145+
return $"{right1} in ({left.Substring(6, left.Length - 7)})";
146+
if (left.StartsWith("(") || left.EndsWith(")"))
147+
return $"{right1} in {left}";
148+
if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]";
149+
return $"({left} @> array[{right1}])";
150+
case "Concat":
151+
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
152+
var right2 = getExp(callExp.Arguments[argIndex]);
153+
if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]";
154+
return $"({left} || {right2})";
155+
case "GetLength":
156+
case "GetLongLength":
157+
case "Length":
158+
case "Count":
159+
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
160+
return $"case when {left} is null then 0 else array_length({left},1) end";
163161
}
164162
}
165163
break;

FreeSql/SqlServer/SqlServerExpression.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
9393
argIndex++;
9494
}
9595
if (objType == null) objType = callExp.Method.DeclaringType;
96-
if (objType != null) {
96+
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
9797
var left = objExp == null ? null : getExp(objExp);
98-
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
99-
switch (callExp.Method.Name) {
100-
case "Contains":
101-
//判断 in
102-
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
103-
}
98+
switch (callExp.Method.Name) {
99+
case "Contains":
100+
//判断 in
101+
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
104102
}
105103
}
106104
break;

0 commit comments

Comments
 (0)