- 修复 UseCommandParameterWithLambda IN 参数化判断 的逻辑 bug;#1137

This commit is contained in:
2881099 2022-06-01 15:27:35 +08:00
parent 105947c2ed
commit eabe1decb8
2 changed files with 117 additions and 0 deletions

View File

@ -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; }
}
}
}

View File

@ -2058,6 +2058,14 @@ namespace FreeSql.Internal
if (_common.CodeFirst.IsGenerateCommandParameterWithLambda && dbParams != null) if (_common.CodeFirst.IsGenerateCommandParameterWithLambda && dbParams != null)
{ {
if (obj == null) return "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(); var type = mapType ?? mapColumn?.Attribute.MapType ?? obj?.GetType();
if (_common.CodeFirst.GetDbInfo(type) != null) if (_common.CodeFirst.GetDbInfo(type) != null)
{ {