Skip to content

Commit

Permalink
Merge pull request #40 from 2881099/dev_type_mapping
Browse files Browse the repository at this point in the history
- 补充 Expression IEnumerable<T>.Contains 的支持,之前只能数组或IList<T>;
  • Loading branch information
2881099 authored Apr 25, 2019
2 parents 169cf59 + 205421f commit e26a124
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 55 deletions.
3 changes: 3 additions & 0 deletions FreeSql.Tests/MySql/MySqlExpression/OtherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public void Array() {
Assert.Throws<MySqlException>(() => { select.Where(a => nullarr.Contains(a.testFieldInt)).ToList(); });
Assert.Throws<MySqlException>(() => { select.Where(a => new int[0].Contains(a.testFieldInt)).ToList(); });

IEnumerable<int> testlinqlist = new List<int>(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();
Expand Down
3 changes: 3 additions & 0 deletions FreeSql.Tests/Oracle/OracleExpression/OtherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public OtherTest() {

[Fact]
public void Array() {
IEnumerable<int> testlinqlist = new List<int>(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();
Expand Down
2 changes: 2 additions & 0 deletions FreeSql.Tests/PostgreSQL/PostgreSQLExpression/OtherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public OtherTest() {

[Fact]
public void Array() {
IEnumerable<int> testlinqlist = new List<int>(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();
Expand Down
3 changes: 3 additions & 0 deletions FreeSql.Tests/SqlServer/SqlServerExpression/OtherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public OtherTest(SqlServerFixture sqlserverFixture)

[Fact]
public void Array() {
IEnumerable<int> testlinqlist = new List<int>(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();
Expand Down
3 changes: 3 additions & 0 deletions FreeSql.Tests/Sqlite/SqliteExpression/OtherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public OtherTest() {

[Fact]
public void Array() {
IEnumerable<int> testlinqlist = new List<int>(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();
Expand Down
5 changes: 5 additions & 0 deletions FreeSql/DataAnnotations/ColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ public class ColumnAttribute : Attribute {
/// 数据库默认值
/// </summary>
internal object DbDefautValue { get; set; }

/// <summary>
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
/// </summary>
public Type Mapping { get; set; }
}
}
12 changes: 5 additions & 7 deletions FreeSql/MySql/MySqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
}
break;
Expand Down
12 changes: 5 additions & 7 deletions FreeSql/Oracle/OracleExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
}
break;
Expand Down
52 changes: 25 additions & 27 deletions FreeSql/PostgreSQL/PostgreSQLExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
var left = objExp == null ? null : getExp(objExp);
switch (objType.FullName) {
case "Newtonsoft.Json.Linq.JToken":
Expand Down Expand Up @@ -134,32 +134,30 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
case "Values": return $"avals({left})";
}
}
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(')')}]";
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";
}
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;
Expand Down
12 changes: 5 additions & 7 deletions FreeSql/SqlServer/SqlServerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
}
break;
Expand Down
12 changes: 5 additions & 7 deletions FreeSql/Sqlite/SqliteExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, List<SelectT
argIndex++;
}
if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) {
if (objType != null || objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
switch (callExp.Method.Name) {
case "Contains":
//判断 in
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
}
break;
Expand Down

0 comments on commit e26a124

Please sign in to comment.