Skip to content

Commit ebd9cbe

Browse files
committed
- 修复 ClickHouse 数组类型 hasAny 与 In 解析冲突问题;#1699
1 parent 1864f9c commit ebd9cbe

File tree

10 files changed

+72
-56
lines changed

10 files changed

+72
-56
lines changed

Examples/base_entity/Entities/User.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class User1 : BaseEntity<User1, Guid>
4242
public int GroupId { get; set; }
4343
public UserGroup Group { get; set; }
4444

45+
public string[] Tags { get; set; }
4546
public virtual List<Role> Roles { get; set; }
4647

4748
/// <summary>

Examples/base_entity/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ static void Main(string[] args)
568568
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
569569
//.UseQuoteSqlName(false)
570570

571-
.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")
571+
// .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")
572572

573573
.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
574574
//.UseAdoConnectionPool(false)
@@ -596,6 +596,7 @@ static void Main(string[] args)
596596
//.UseConnectionString(DataType.QuestDb, "host=localhost;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;")
597597

598598
//.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")
599+
.UseConnectionFactory(DataType.ClickHouse, () => null)
599600
.UseMonitorCommand(cmd =>
600601
{
601602
Console.WriteLine(cmd.CommandText + "\r\n");
@@ -609,6 +610,13 @@ static void Main(string[] args)
609610
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
610611
#endregion
611612

613+
var clickhouseSql1 = fsql.Select<User1>().Where(a => new[] { 1, 2, 3 }.Contains(a.GroupId)).ToSql();
614+
var clickhouseVal1 = new[] { 1, 2, 3 };
615+
var clickhouseSql2 = fsql.Select<User1>().Where(a => clickhouseVal1.Contains(a.GroupId)).ToSql();
616+
var clickhouseSql3 = fsql.Select<User1>().Where(a => a.Tags.Contains("tag1")).ToSql();
617+
var clickhouseVal2 = "tag2";
618+
var clickhouseSql4 = fsql.Select<User1>().Where(a => a.Tags.Contains(clickhouseVal2)).ToSql();
619+
612620
fsql.Update<User1>()
613621
.Where(t => t.GroupId == 1)
614622
.ExecuteUpdated();

Examples/base_entity/base_entity.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="..\..\Extensions\FreeSql.Extensions.Linq\FreeSql.Extensions.Linq.csproj" />
2929
<ProjectReference Include="..\..\FreeSql.Repository\FreeSql.Repository.csproj" />
3030
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
31+
<ProjectReference Include="..\..\Providers\FreeSql.Provider.ClickHouse\FreeSql.Provider.ClickHouse.csproj" />
3132
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Dameng\FreeSql.Provider.Dameng.csproj" />
3233
<ProjectReference Include="..\..\Providers\FreeSql.Provider.Firebird\FreeSql.Provider.Firebird.csproj" />
3334
<ProjectReference Include="..\..\Providers\FreeSql.Provider.MySqlConnector\FreeSql.Provider.MySqlConnector.csproj" />

Providers/FreeSql.Provider.ClickHouse/ClickHouseExpression.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,23 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
152152
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
153153
return $"(case when {left} is null then 0 else length({left}) end > 0)";
154154
case "Contains":
155-
tsc.SetMapColumnTmp(null);
156-
var args1 = getExp(callExp.Arguments[argIndex]);
157-
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
158-
var oldDbParams = objExp?.NodeType == ExpressionType.MemberAccess ? tsc.SetDbParamsReturnOld(null) : null; //#900 UseGenerateCommandParameterWithLambda(true) 子查询 bug、以及 #1173 参数化 bug
159-
tsc.isNotSetMapColumnTmp = true;
160-
left = objExp == null ? null : getExp(objExp);
161-
tsc.isNotSetMapColumnTmp = false;
162-
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
163-
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
164-
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
165-
return $"(hasAny({left}, [{args1}]))";
155+
tsc.SetMapColumnTmp(null);
156+
var args1 = getExp(callExp.Arguments[argIndex]);
157+
var oldMapType = tsc.SetMapTypeReturnOld(tsc.mapTypeTmp);
158+
var oldDbParams = objExp?.NodeType == ExpressionType.MemberAccess ? tsc.SetDbParamsReturnOld(null) : null; //#900 UseGenerateCommandParameterWithLambda(true) 子查询 bug、以及 #1173 参数化 bug
159+
tsc.isNotSetMapColumnTmp = true;
160+
left = objExp == null ? null : getExp(objExp);
161+
tsc.isNotSetMapColumnTmp = false;
162+
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
163+
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
164+
//判断 in 或 hasAny
165+
if (left.StartsWith("array[") && left.EndsWith("]"))
166+
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
167+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
168+
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
169+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"[{args1.TrimStart('(').TrimEnd(')')}]";
170+
else args1 = $"[{args1}]";
171+
return $"(hasAny({left}, {args1}))";
166172
case "Concat":
167173
left = objExp == null ? null : getExp(objExp);
168174
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";

Providers/FreeSql.Provider.Custom/PostgreSQL/CustomPostgreSQLExpression.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
166166
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
167167
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
168168
//判断 in 或 array @> array
169-
if (left.StartsWith("array[") || left.EndsWith("]"))
169+
if (left.StartsWith("array[") && left.EndsWith("]"))
170170
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171-
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
171+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172172
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173-
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174-
args1 = $"array[{args1}]";
173+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174+
else args1 = $"array[{args1}]";
175175
if (objExp != null)
176176
{
177177
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);

Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
165165
tsc.isNotSetMapColumnTmp = false;
166166
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
167167
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
168-
//判断 in 或 array @> array
169-
if (left.StartsWith("array[") || left.EndsWith("]"))
170-
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171-
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172-
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173-
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174-
args1 = $"array[{args1}]";
175-
if (objExp != null)
168+
//判断 in 或 array @> array
169+
if (left.StartsWith("array[") && left.EndsWith("]"))
170+
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172+
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174+
else args1 = $"array[{args1}]";
175+
if (objExp != null)
176176
{
177177
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
178178
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESExpression.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
165165
tsc.isNotSetMapColumnTmp = false;
166166
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
167167
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
168-
//判断 in 或 array @> array
169-
if (left.StartsWith("array[") || left.EndsWith("]"))
170-
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171-
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172-
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173-
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174-
args1 = $"array[{args1}]";
175-
if (objExp != null)
168+
//判断 in 或 array @> array
169+
if (left.StartsWith("array[") && left.EndsWith("]"))
170+
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172+
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174+
else args1 = $"array[{args1}]";
175+
if (objExp != null)
176176
{
177177
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
178178
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLExpression.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
165165
tsc.isNotSetMapColumnTmp = false;
166166
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
167167
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
168-
//判断 in 或 array @> array
169-
if (left.StartsWith("array[") || left.EndsWith("]"))
170-
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171-
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172-
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173-
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174-
args1 = $"array[{args1}]";
175-
if (objExp != null)
168+
//判断 in 或 array @> array
169+
if (left.StartsWith("array[") && left.EndsWith("]"))
170+
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
171+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
172+
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
173+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
174+
else args1 = $"array[{args1}]";
175+
if (objExp != null)
176176
{
177177
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
178178
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

Providers/FreeSql.Provider.PostgreSQL/PostgreSQLExpression.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
196196
tsc.isNotSetMapColumnTmp = false;
197197
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
198198
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
199-
//判断 in 或 array @> array
200-
if (left.StartsWith("array[") || left.EndsWith("]"))
201-
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
202-
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
203-
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
204-
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
205-
args1 = $"array[{args1}]";
206-
if (objExp != null)
199+
//判断 in 或 array @> array
200+
if (left.StartsWith("array[") && left.EndsWith("]"))
201+
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
202+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
203+
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
204+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
205+
else args1 = $"array[{args1}]";
206+
if (objExp != null)
207207
{
208208
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
209209
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

Providers/FreeSql.Provider.ShenTong/ShenTongExpression.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
147147
tsc.isNotSetMapColumnTmp = false;
148148
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
149149
if (oldDbParams != null) tsc.SetDbParamsReturnOld(oldDbParams);
150-
//判断 in 或 array @> array
151-
if (left.StartsWith("array[") || left.EndsWith("]"))
152-
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
153-
if (left.StartsWith("(") || left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
154-
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
155-
if (args1.StartsWith("(") || args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
156-
args1 = $"array[{args1}]";
157-
if (objExp != null)
150+
//判断 in 或 array @> array
151+
if (left.StartsWith("array[") && left.EndsWith("]"))
152+
return $"({args1}) in ({left.Substring(6, left.Length - 7)})";
153+
if (left.StartsWith("(") && left.EndsWith(")")) //在各大 Provider AdoProvider 中已约定,500元素分割, 3空格\r\n4空格
154+
return $"(({args1}) in {left.Replace(", \r\n \r\n", $") \r\n OR ({args1}) in (")})";
155+
if (args1.StartsWith("(") && args1.EndsWith(")")) args1 = $"array[{args1.TrimStart('(').TrimEnd(')')}]";
156+
else args1 = $"array[{args1}]";
157+
if (objExp != null)
158158
{
159159
var dbinfo = _common._orm.CodeFirst.GetDbInfo(objExp.Type);
160160
if (dbinfo != null) args1 = $"{args1}::{dbinfo.dbtype}";

0 commit comments

Comments
 (0)