- 修复 ClickHouse 数组类型 hasAny 与 In 解析冲突问题;#1699

This commit is contained in:
2881099 2023-12-21 17:06:16 +08:00
parent 1864f9cc2f
commit ebd9cbed0f
10 changed files with 72 additions and 56 deletions

View File

@ -42,6 +42,7 @@ public class User1 : BaseEntity<User1, Guid>
public int GroupId { get; set; } public int GroupId { get; set; }
public UserGroup Group { get; set; } public UserGroup Group { get; set; }
public string[] Tags { get; set; }
public virtual List<Role> Roles { get; set; } public virtual List<Role> Roles { get; set; }
/// <summary> /// <summary>

View File

@ -568,7 +568,7 @@ namespace base_entity
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5") //.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
//.UseQuoteSqlName(false) //.UseQuoteSqlName(false)
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true") // .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true") .UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
//.UseAdoConnectionPool(false) //.UseAdoConnectionPool(false)
@ -596,6 +596,7 @@ namespace base_entity
//.UseConnectionString(DataType.QuestDb, "host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;") //.UseConnectionString(DataType.QuestDb, "host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;")
//.UseConnectionString(DataType.ClickHouse, "DataCompress=False;BufferSize=32768;SocketTimeout=10000;CheckCompressedHash=False;Encrypt=False;Compressor=lz4;Host=192.168.0.121;Port=8125;Database=PersonnelLocation;Username=root;Password=123") //.UseConnectionString(DataType.ClickHouse, "DataCompress=False;BufferSize=32768;SocketTimeout=10000;CheckCompressedHash=False;Encrypt=False;Compressor=lz4;Host=192.168.0.121;Port=8125;Database=PersonnelLocation;Username=root;Password=123")
.UseConnectionFactory(DataType.ClickHouse, () => null)
.UseMonitorCommand(cmd => .UseMonitorCommand(cmd =>
{ {
Console.WriteLine(cmd.CommandText + "\r\n"); Console.WriteLine(cmd.CommandText + "\r\n");
@ -609,6 +610,13 @@ namespace base_entity
BaseEntity.Initialization(fsql, () => _asyncUow.Value); BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion #endregion
var clickhouseSql1 = fsql.Select<User1>().Where(a => new[] { 1, 2, 3 }.Contains(a.GroupId)).ToSql();
var clickhouseVal1 = new[] { 1, 2, 3 };
var clickhouseSql2 = fsql.Select<User1>().Where(a => clickhouseVal1.Contains(a.GroupId)).ToSql();
var clickhouseSql3 = fsql.Select<User1>().Where(a => a.Tags.Contains("tag1")).ToSql();
var clickhouseVal2 = "tag2";
var clickhouseSql4 = fsql.Select<User1>().Where(a => a.Tags.Contains(clickhouseVal2)).ToSql();
fsql.Update<User1>() fsql.Update<User1>()
.Where(t => t.GroupId == 1) .Where(t => t.GroupId == 1)
.ExecuteUpdated(); .ExecuteUpdated();

View File

@ -28,6 +28,7 @@
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.Linq\FreeSql.Extensions.Linq.csproj" /> <ProjectReference Include="..\..\Extensions\FreeSql.Extensions.Linq\FreeSql.Extensions.Linq.csproj" />
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" /> <ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" /> <ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.ClickHouse\FreeSql.Provider.ClickHouse.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Dameng\FreeSql.Provider.Dameng.csproj" /> <ProjectReference Include="..\..\Providers\FreeSql.Provider.Dameng\FreeSql.Provider.Dameng.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Firebird\FreeSql.Provider.Firebird.csproj" /> <ProjectReference Include="..\..\Providers\FreeSql.Provider.Firebird\FreeSql.Provider.Firebird.csproj" />
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySqlConnector\FreeSql.Provider.MySqlConnector.csproj" /> <ProjectReference Include="..\..\Providers\FreeSql.Provider.MySqlConnector\FreeSql.Provider.MySqlConnector.csproj" />

View File

@ -152,17 +152,23 @@ namespace FreeSql.ClickHouse
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 length({left}) end > 0)"; return $"(case when {left} is null then 0 else length({left}) end > 0)";
case "Contains": case "Contains":
tsc.SetMapColumnTmp(null); tsc.SetMapColumnTmp(null);
var args1 = getExp(callExp.Arguments[argIndex]); var args1 = getExp(callExp.Arguments[argIndex]);
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp); var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
var oldDbParams = objExp?.NodeType == ExpressionType.MemberAccess ? tsc.SetDbParamsReturnOld(null) : null; //#900 UseGenerateCommandParameterWithLambda(true) 子查询 bug、以及 #1173 参数化 bug var oldDbParams = objExp?.NodeType == ExpressionType.MemberAccess ? tsc.SetDbParamsReturnOld(null) : null; //#900 UseGenerateCommandParameterWithLambda(true) 子查询 bug、以及 #1173 参数化 bug
tsc.isNotSetMapColumnTmp = true; tsc.isNotSetMapColumnTmp = true;
left = objExp == null ? null : getExp(objExp); left = objExp == null ? null : getExp(objExp);
tsc.isNotSetMapColumnTmp = false; tsc.isNotSetMapColumnTmp = false;
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; //判断 in 或 hasAny
return $"(hasAny({left}, [{args1}]))"; if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"[{args1.TrimStart('(').TrimEnd(')')}]";
else args1 = $"[{args1}]";
return $"(hasAny({left}, {args1}))";
case "Concat": case "Concat":
left = objExp == null ? null : getExp(objExp); left = objExp == null ? null : getExp(objExp);
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]"; if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";

View File

@ -166,12 +166,12 @@ namespace FreeSql.Custom.PostgreSQL
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
//判断 in 或 array @> array //判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]")) if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})"; return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格 if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})"; return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]"; if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
args1 = $"array[{args1}]"; else args1 = $"array[{args1}]";
if (objExp != null) if (objExp != null)
{ {
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type); var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);

View File

@ -165,14 +165,14 @@ namespace FreeSql.KingbaseES
tsc.isNotSetMapColumnTmp = false; tsc.isNotSetMapColumnTmp = false;
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
//判断 in 或 array @> array //判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]")) if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})"; return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格 if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})"; return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]"; if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
args1 = $"array[{args1}]"; else args1 = $"array[{args1}]";
if (objExp != null) if (objExp != null)
{ {
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type); var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}"; if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

View File

@ -165,14 +165,14 @@ namespace FreeSql.Odbc.KingbaseES
tsc.isNotSetMapColumnTmp = false; tsc.isNotSetMapColumnTmp = false;
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
//判断 in 或 array @> array //判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]")) if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})"; return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格 if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})"; return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]"; if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
args1 = $"array[{args1}]"; else args1 = $"array[{args1}]";
if (objExp != null) if (objExp != null)
{ {
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type); var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}"; if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

View File

@ -165,14 +165,14 @@ namespace FreeSql.Odbc.PostgreSQL
tsc.isNotSetMapColumnTmp = false; tsc.isNotSetMapColumnTmp = false;
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
//判断 in 或 array @> array //判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]")) if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})"; return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格 if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})"; return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]"; if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
args1 = $"array[{args1}]"; else args1 = $"array[{args1}]";
if (objExp != null) if (objExp != null)
{ {
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type); var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}"; if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

View File

@ -196,14 +196,14 @@ namespace FreeSql.PostgreSQL
tsc.isNotSetMapColumnTmp = false; tsc.isNotSetMapColumnTmp = false;
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
//判断 in 或 array @> array //判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]")) if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})"; return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格 if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})"; return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]"; if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
args1 = $"array[{args1}]"; else args1 = $"array[{args1}]";
if (objExp != null) if (objExp != null)
{ {
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type); var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}"; if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

View File

@ -147,14 +147,14 @@ namespace FreeSql.ShenTong
tsc.isNotSetMapColumnTmp = false; tsc.isNotSetMapColumnTmp = false;
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType); tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams); if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
//判断 in 或 array @> array //判断 in 或 array @> array
if (left.StartsWith("array[") || left.EndsWith("]")) if (left.StartsWith("array[") && left.EndsWith("]"))
return $"({args1}) in ({left.Substring(6, left.Length - 7)})"; return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格 if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定500元素分割, 3空格\r\n4空格
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})"; return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]"; if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
args1 = $"array[{args1}]"; else args1 = $"array[{args1}]";
if (objExp != null) if (objExp != null)
{ {
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type); var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}"; if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";