Skip to content

Commit

Permalink
- 修复 UseCommandParameterWithLambda IN 参数化判断 的逻辑 bug;#1137
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jun 1, 2022
1 parent 105947c commit eabe1de
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
109 changes: 109 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/Issues/1137.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Collections.Generic;
using Xunit;

namespace FreeSql.Tests.Issues
{
public class _1137
{
[Fact]
public void ListContains()
{
using (var fsql = new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
.UseGenerateCommandParameterWithLambda(true)
.Build())
{
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(int);
};
var listEnum = new List<UserType> { UserType.Client };
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
Assert.Equal(@"SELECT a.""Type"" as1
FROM ""User"" a
WHERE (((a.""Type"") in (1)))", sql);
}

using (var fsql = new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, "data source=:memory:")
.UseGenerateCommandParameterWithLambda(true)
.Build())
{
fsql.CodeFirst.ConfigEntity<User>(a => { });
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(string);
};
var listEnum = new List<UserType> { UserType.Client };
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
Assert.Equal(@"SELECT a.""Type"" as1
FROM ""User"" a
WHERE (((a.""Type"") in ('Client')))", sql);
}

using (var fsql = new FreeSqlBuilder()
.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")
.UseGenerateCommandParameterWithLambda(true)
.Build())
{
fsql.CodeFirst.Entity<User>(a => a.ToTable("issues1137_user"));
var listEnum = new List<UserType> { UserType.Client };
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
Assert.Equal(@"SELECT a.`Type` as1
FROM `issues1137_user` a
WHERE (((a.`Type`) in ('Client')))", sql);
}

using (var fsql = new FreeSqlBuilder()
.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")
.UseGenerateCommandParameterWithLambda(true)
.Build())
{
fsql.CodeFirst.Entity<User>(a => a.ToTable("issues1137_user"));
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(int);
};
var listEnum = new List<UserType> { UserType.Client };
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
Assert.Equal(@"SELECT a.`Type` as1
FROM `issues1137_user` a
WHERE (((a.`Type`) in (1)))", sql);
}

using (var fsql = new FreeSqlBuilder()
.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")
.UseGenerateCommandParameterWithLambda(true)
.Build())
{
fsql.CodeFirst.Entity<User>(a => a.ToTable("issues1137_user"));
fsql.Aop.ConfigEntityProperty += (s, e) =>
{
if (e.Property.PropertyType.IsEnum)
e.ModifyResult.MapType = typeof(string);
};
var listEnum = new List<UserType> { UserType.Client };
var sql = fsql.Select<User>().Where(a => listEnum.Contains(a.Type)).ToSql(a => a);
Assert.Equal(@"SELECT a.`Type` as1
FROM `issues1137_user` a
WHERE (((a.`Type`) in ('Client')))", sql);
}
}

public enum UserType
{
Client = 1,
Internal = 2
}
public class User
{
public UserType Type { get; set; }
}


}

}
8 changes: 8 additions & 0 deletions FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,14 @@ public string formatSql(object obj, Type mapType, ColumnInfo mapColumn, List<DbP
if (_common.CodeFirst.IsGenerateCommandParameterWithLambda && dbParams != null)
{
if (obj == null) return "NULL";
if (mapColumn != null)
{
var objType = obj.GetType();
if (obj is ICollection && objType.GetGenericArguments().FirstOrDefault()?.NullableTypeOrThis() == mapColumn.CsType?.NullableTypeOrThis())
return string.Format(CultureInfo.InvariantCulture, "{0}", _ado.AddslashesProcessParam(obj, mapType, mapColumn));
if (obj is Array && objType.GetElementType()?.NullableTypeOrThis() == mapColumn.CsType?.NullableTypeOrThis())
return string.Format(CultureInfo.InvariantCulture, "{0}", _ado.AddslashesProcessParam(obj, mapType, mapColumn));
}
var type = mapType ?? mapColumn?.Attribute.MapType ?? obj?.GetType();
if (_common.CodeFirst.GetDbInfo(type) != null)
{
Expand Down

0 comments on commit eabe1de

Please sign in to comment.