Merge pull request #40 from 2881099/dev_type_mapping

- 补充 Expression IEnumerable<T>.Contains 的支持,之前只能数组或IList<T>;
This commit is contained in:
2881099 2019-04-25 17:42:55 +08:00 committed by GitHub
commit e26a1246bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 64 additions and 55 deletions

View File

@ -21,6 +21,9 @@ namespace FreeSql.Tests.MySqlExpression {
Assert.Throws<MySqlException>(() => { select.Where(a => nullarr.Contains(a.testFieldInt)).ToList(); }); Assert.Throws<MySqlException>(() => { select.Where(a => nullarr.Contains(a.testFieldInt)).ToList(); });
Assert.Throws<MySqlException>(() => { select.Where(a => new int[0].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 //in not in
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToList(); 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(); var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();

View File

@ -15,6 +15,9 @@ namespace FreeSql.Tests.OracleExpression {
[Fact] [Fact]
public void Array() { 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 //in not in
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList(); 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(); //var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int) == false).ToList();

View File

@ -23,6 +23,8 @@ namespace FreeSql.Tests.PostgreSQLExpression {
[Fact] [Fact]
public void Array() { 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 sql1 = select.Where(a => a.testFieldIntArray.Contains(1)).ToList();
var sql2 = select.Where(a => a.testFieldIntArray.Contains(1) == false).ToList(); var sql2 = select.Where(a => a.testFieldIntArray.Contains(1) == false).ToList();

View File

@ -20,6 +20,9 @@ namespace FreeSql.Tests.SqlServerExpression {
[Fact] [Fact]
public void Array() { 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 //in not in
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToList(); 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(); //var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt) == false).ToList();

View File

@ -15,6 +15,9 @@ namespace FreeSql.Tests.SqliteExpression {
[Fact] [Fact]
public void Array() { 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 //in not in
var sql111 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int)).ToList(); 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(); //var sql112 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.Int) == false).ToList();

View File

@ -42,5 +42,10 @@ namespace FreeSql.DataAnnotations {
/// 数据库默认值 /// 数据库默认值
/// </summary> /// </summary>
internal object DbDefautValue { get; set; } internal object DbDefautValue { get; set; }
/// <summary>
/// 类型映射,比如:可将 enum 属性映射成 typeof(string)
/// </summary>
public Type Mapping { get; set; }
} }
} }

View File

@ -90,14 +90,12 @@ namespace FreeSql.MySql {
argIndex++; argIndex++;
} }
if (objType == null) objType = callExp.Method.DeclaringType; 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); var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) { switch (callExp.Method.Name) {
switch (callExp.Method.Name) { case "Contains":
case "Contains": //判断 in
//判断 in return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
} }
} }
break; break;

View File

@ -90,14 +90,12 @@ namespace FreeSql.Oracle {
argIndex++; argIndex++;
} }
if (objType == null) objType = callExp.Method.DeclaringType; 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); var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) { switch (callExp.Method.Name) {
switch (callExp.Method.Name) { case "Contains":
case "Contains": //判断 in
//判断 in return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
} }
} }
break; break;

View File

@ -95,7 +95,7 @@ namespace FreeSql.PostgreSQL {
argIndex++; argIndex++;
} }
if (objType == null) objType = callExp.Method.DeclaringType; 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); var left = objExp == null ? null : getExp(objExp);
switch (objType.FullName) { switch (objType.FullName) {
case "Newtonsoft.Json.Linq.JToken": case "Newtonsoft.Json.Linq.JToken":
@ -134,32 +134,30 @@ namespace FreeSql.PostgreSQL {
case "Values": return $"avals({left})"; case "Values": return $"avals({left})";
} }
} }
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) { switch (callExp.Method.Name) {
switch (callExp.Method.Name) { case "Any":
case "Any": if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
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)";
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)"; case "Contains":
case "Contains": //判断 in 或 array @> array
//判断 in 或 array @> array var right1 = getExp(callExp.Arguments[argIndex]);
var right1 = getExp(callExp.Arguments[argIndex]); if (left.StartsWith("array[") || left.EndsWith("]"))
if (left.StartsWith("array[") || left.EndsWith("]")) return $"{right1} in ({left.Substring(6, left.Length - 7)})";
return $"{right1} in ({left.Substring(6, left.Length - 7)})"; if (left.StartsWith("(") || left.EndsWith(")"))
if (left.StartsWith("(") || left.EndsWith(")")) return $"{right1} in {left}";
return $"{right1} in {left}"; if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]";
if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]"; return $"({left} @> array[{right1}])";
return $"({left} @> array[{right1}])"; case "Concat":
case "Concat": if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; var right2 = getExp(callExp.Arguments[argIndex]);
var right2 = getExp(callExp.Arguments[argIndex]); if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]";
if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]"; return $"({left} || {right2})";
return $"({left} || {right2})"; case "GetLength":
case "GetLength": case "GetLongLength":
case "GetLongLength": case "Length":
case "Length": case "Count":
case "Count": if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; return $"case when {left} is null then 0 else array_length({left},1) end";
return $"case when {left} is null then 0 else array_length({left},1) end";
}
} }
} }
break; break;

View File

@ -93,14 +93,12 @@ namespace FreeSql.SqlServer {
argIndex++; argIndex++;
} }
if (objType == null) objType = callExp.Method.DeclaringType; 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); var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) { switch (callExp.Method.Name) {
switch (callExp.Method.Name) { case "Contains":
case "Contains": //判断 in
//判断 in return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
} }
} }
break; break;

View File

@ -90,14 +90,12 @@ namespace FreeSql.Sqlite {
argIndex++; argIndex++;
} }
if (objType == null) objType = callExp.Method.DeclaringType; 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); var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) { switch (callExp.Method.Name) {
switch (callExp.Method.Name) { case "Contains":
case "Contains": //判断 in
//判断 in return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
return $"({getExp(callExp.Arguments[argIndex])}) in {left}";
}
} }
} }
break; break;