Skip to content

Commit eabe1de

Browse files
committed
- 修复 UseCommandParameterWithLambda IN 参数化判断 的逻辑 bug;#1137
1 parent 105947c commit eabe1de

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace FreeSql.Tests.Issues
5+
{
6+
public class _1137
7+
{
8+
[Fact]
9+
public void ListContains()
10+
{
11+
using (var fsql = new FreeSqlBuilder()
12+
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
13+
.UseGenerateCommandParameterWithLambda(true)
14+
.Build())
15+
{
16+
fsql.Aop.ConfigEntityProperty += (s, e) =>
17+
{
18+
if (e.Property.PropertyType.IsEnum)
19+
e.ModifyResult.MapType = typeof(int);
20+
};
21+
var listEnum = new List<UserType> { UserType.Client };
22+
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
23+
Assert.Equal(@"SELECT a.""Type"" as1
24+
FROM ""User"" a
25+
WHERE (((a.""Type"") in (1)))", sql);
26+
}
27+
28+
using (var fsql = new FreeSqlBuilder()
29+
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
30+
.UseGenerateCommandParameterWithLambda(true)
31+
.Build())
32+
{
33+
fsql.CodeFirst.ConfigEntity<User>(a => { });
34+
fsql.Aop.ConfigEntityProperty += (s, e) =>
35+
{
36+
if (e.Property.PropertyType.IsEnum)
37+
e.ModifyResult.MapType = typeof(string);
38+
};
39+
var listEnum = new List<UserType> { UserType.Client };
40+
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
41+
Assert.Equal(@"SELECT a.""Type"" as1
42+
FROM ""User"" a
43+
WHERE (((a.""Type"") in ('Client')))", sql);
44+
}
45+
46+
using (var fsql = new FreeSqlBuilder()
47+
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=5;Allow User Variables=True")
48+
.UseGenerateCommandParameterWithLambda(true)
49+
.Build())
50+
{
51+
fsql.CodeFirst.Entity<User>(a => a.ToTable("issues1137_user"));
52+
var listEnum = new List<UserType> { UserType.Client };
53+
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
54+
Assert.Equal(@"SELECT a.`Type` as1
55+
FROM `issues1137_user` a
56+
WHERE (((a.`Type`) in ('Client')))", sql);
57+
}
58+
59+
using (var fsql = new FreeSqlBuilder()
60+
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=5;Allow User Variables=True")
61+
.UseGenerateCommandParameterWithLambda(true)
62+
.Build())
63+
{
64+
fsql.CodeFirst.Entity<User>(a => a.ToTable("issues1137_user"));
65+
fsql.Aop.ConfigEntityProperty += (s, e) =>
66+
{
67+
if (e.Property.PropertyType.IsEnum)
68+
e.ModifyResult.MapType = typeof(int);
69+
};
70+
var listEnum = new List<UserType> { UserType.Client };
71+
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
72+
Assert.Equal(@"SELECT a.`Type` as1
73+
FROM `issues1137_user` a
74+
WHERE (((a.`Type`) in (1)))", sql);
75+
}
76+
77+
using (var fsql = new FreeSqlBuilder()
78+
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=5;Allow User Variables=True")
79+
.UseGenerateCommandParameterWithLambda(true)
80+
.Build())
81+
{
82+
fsql.CodeFirst.Entity<User>(a => a.ToTable("issues1137_user"));
83+
fsql.Aop.ConfigEntityProperty += (s, e) =>
84+
{
85+
if (e.Property.PropertyType.IsEnum)
86+
e.ModifyResult.MapType = typeof(string);
87+
};
88+
var listEnum = new List<UserType> { UserType.Client };
89+
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
90+
Assert.Equal(@"SELECT a.`Type` as1
91+
FROM `issues1137_user` a
92+
WHERE (((a.`Type`) in ('Client')))", sql);
93+
}
94+
}
95+
96+
public enum UserType
97+
{
98+
Client = 1,
99+
Internal = 2
100+
}
101+
public class User
102+
{
103+
public UserType Type { get; set; }
104+
}
105+
106+
107+
}
108+
109+
}

FreeSql/Internal/CommonExpression.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,14 @@ public string formatSql(object obj, Type mapType, ColumnInfo mapColumn, List<DbP
20582058
if (_common.CodeFirst.IsGenerateCommandParameterWithLambda && dbParams != null)
20592059
{
20602060
if (obj == null) return "NULL";
2061+
if (mapColumn != null)
2062+
{
2063+
var objType = obj.GetType();
2064+
if (obj is ICollection && objType.GetGenericArguments().FirstOrDefault()?.NullableTypeOrThis() == mapColumn.CsType?.NullableTypeOrThis())
2065+
return string.Format(CultureInfo.InvariantCulture, "{0}", _ado.AddslashesProcessParam(obj, mapType, mapColumn));
2066+
if (obj is Array && objType.GetElementType()?.NullableTypeOrThis() == mapColumn.CsType?.NullableTypeOrThis())
2067+
return string.Format(CultureInfo.InvariantCulture, "{0}", _ado.AddslashesProcessParam(obj, mapType, mapColumn));
2068+
}
20612069
var type = mapType ?? mapColumn?.Attribute.MapType ?? obj?.GetType();
20622070
if (_common.CodeFirst.GetDbInfo(type) != null)
20632071
{

0 commit comments

Comments
 (0)