Skip to content

Commit 62bd9af

Browse files
committed
- 修复 pgsql WithTempQuery + ToList 对于 bool 类型处理导致的性能问题;#2093
1 parent ab5a6a3 commit 62bd9af

File tree

7 files changed

+375
-18
lines changed

7 files changed

+375
-18
lines changed

FreeSql/Internal/CommonExpression.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
344344
case ExpressionType.Equal:
345345
case ExpressionType.Not:
346346
initExpArg = Expression.Condition(initExpArg, Expression.Constant(true, typeof(bool)), Expression.Constant(false, typeof(bool)));
347+
switch (_ado.DataType)
348+
{
349+
case DataType.PostgreSQL: //#2093
350+
case DataType.OdbcPostgreSQL:
351+
case DataType.CustomPostgreSQL:
352+
case DataType.KingbaseES:
353+
case DataType.ShenTong:
354+
case DataType.Xugu:
355+
initExpArg = Expression.Convert(initExpArg, typeof(bool));
356+
break;
357+
}
347358
break;
348359
}
349360
var child = new ReadAnonymousTypeInfo
@@ -456,6 +467,17 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
456467
case ExpressionType.Equal:
457468
case ExpressionType.Not:
458469
initExpArg = Expression.Condition(initExpArg, Expression.Constant(true, typeof(bool)), Expression.Constant(false, typeof(bool)));
470+
switch (_ado.DataType)
471+
{
472+
case DataType.PostgreSQL: //#2093
473+
case DataType.OdbcPostgreSQL:
474+
case DataType.CustomPostgreSQL:
475+
case DataType.KingbaseES:
476+
case DataType.ShenTong:
477+
case DataType.Xugu:
478+
initExpArg = Expression.Convert(initExpArg, typeof(bool));
479+
break;
480+
}
459481
break;
460482
}
461483
var child = new ReadAnonymousTypeInfo
@@ -507,6 +529,17 @@ void LocalSetFieldAlias(ref int localIndex, bool isdiymemexp)
507529
case ExpressionType.Equal:
508530
case ExpressionType.Not:
509531
initExpArg = Expression.Condition(initExpArg, Expression.Constant(true, typeof(bool)), Expression.Constant(false, typeof(bool)));
532+
switch (_ado.DataType)
533+
{
534+
case DataType.PostgreSQL: //#2093
535+
case DataType.OdbcPostgreSQL:
536+
case DataType.CustomPostgreSQL:
537+
case DataType.KingbaseES:
538+
case DataType.ShenTong:
539+
case DataType.Xugu:
540+
initExpArg = Expression.Convert(initExpArg, typeof(bool));
541+
break;
542+
}
510543
break;
511544
}
512545
var csname = newExp.Members != null ? newExp.Members[a].Name : (initExpArg as MemberExpression)?.Member.Name;

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

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,25 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
3232
{
3333
switch (exp.Type.NullableTypeOrThis().ToString())
3434
{
35-
case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))";
35+
case "System.Boolean":
36+
var boolstr = getExp(operandExp);
37+
switch (boolstr)
38+
{
39+
case "0":
40+
case "'0'":
41+
case "false":
42+
case "'f'":
43+
case "'no'":
44+
return "'f'::bool";
45+
case "1":
46+
case "'1'":
47+
case "true":
48+
case "'t'":
49+
case "'yes'":
50+
return "'t'::bool";
51+
default:
52+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
53+
}
3654
case "System.Byte": return $"({getExp(operandExp)})::int2";
3755
case "System.Char": return $"substr(({getExp(operandExp)})::char, 1, 1)";
3856
case "System.DateTime": return ExpressionConstDateTime(operandExp) ?? $"({getExp(operandExp)})::timestamp";
@@ -60,7 +78,25 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
6078
case "TryParse":
6179
switch (callExp.Method.DeclaringType.NullableTypeOrThis().ToString())
6280
{
63-
case "System.Boolean": return $"(({getExp(callExp.Arguments[0])})::varchar not in ('0','false','f','no'))";
81+
case "System.Boolean":
82+
var boolstr = getExp(callExp.Arguments[0]);
83+
switch (boolstr)
84+
{
85+
case "0":
86+
case "'0'":
87+
case "false":
88+
case "'f'":
89+
case "'no'":
90+
return "'f'::bool";
91+
case "1":
92+
case "'1'":
93+
case "true":
94+
case "'t'":
95+
case "'yes'":
96+
return "'t'::bool";
97+
default:
98+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
99+
}
64100
case "System.Byte": return $"({getExp(callExp.Arguments[0])})::int2";
65101
case "System.Char": return $"substr(({getExp(callExp.Arguments[0])})::char, 1, 1)";
66102
case "System.DateTime": return ExpressionConstDateTime(callExp.Arguments[0]) ?? $"({getExp(callExp.Arguments[0])})::timestamp";
@@ -587,7 +623,25 @@ public override string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp
587623
{
588624
switch (exp.Method.Name)
589625
{
590-
case "ToBoolean": return $"(({getExp(exp.Arguments[0])})::varchar not in ('0','false','f','no'))";
626+
case "ToBoolean":
627+
var boolstr = getExp(exp.Arguments[0]);
628+
switch (boolstr)
629+
{
630+
case "0":
631+
case "'0'":
632+
case "false":
633+
case "'f'":
634+
case "'no'":
635+
return "'f'::bool";
636+
case "1":
637+
case "'1'":
638+
case "true":
639+
case "'t'":
640+
case "'yes'":
641+
return "'t'::bool";
642+
default:
643+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
644+
}
591645
case "ToByte": return $"({getExp(exp.Arguments[0])})::int2";
592646
case "ToChar": return $"substr(({getExp(exp.Arguments[0])})::char, 1, 1)";
593647
case "ToDateTime": return ExpressionConstDateTime(exp.Arguments[0]) ?? $"({getExp(exp.Arguments[0])})::timestamp";

Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,25 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
3333
{
3434
switch (exp.Type.NullableTypeOrThis().ToString())
3535
{
36-
case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))";
36+
case "System.Boolean":
37+
var boolstr = getExp(operandExp);
38+
switch (boolstr)
39+
{
40+
case "0":
41+
case "'0'":
42+
case "false":
43+
case "'f'":
44+
case "'no'":
45+
return "'f'::bool";
46+
case "1":
47+
case "'1'":
48+
case "true":
49+
case "'t'":
50+
case "'yes'":
51+
return "'t'::bool";
52+
default:
53+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
54+
}
3755
case "System.Byte": return $"({getExp(operandExp)})::int2";
3856
case "System.Char": return $"substr(({getExp(operandExp)})::char, 1, 1)";
3957
case "System.DateTime": return ExpressionConstDateTime(operandExp) ?? $"({getExp(operandExp)})::timestamp";
@@ -61,7 +79,25 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
6179
case "TryParse":
6280
switch (callExp.Method.DeclaringType.NullableTypeOrThis().ToString())
6381
{
64-
case "System.Boolean": return $"(({getExp(callExp.Arguments[0])})::varchar not in ('0','false','f','no'))";
82+
case "System.Boolean":
83+
var boolstr = getExp(callExp.Arguments[0]);
84+
switch (boolstr)
85+
{
86+
case "0":
87+
case "'0'":
88+
case "false":
89+
case "'f'":
90+
case "'no'":
91+
return "'f'::bool";
92+
case "1":
93+
case "'1'":
94+
case "true":
95+
case "'t'":
96+
case "'yes'":
97+
return "'t'::bool";
98+
default:
99+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
100+
}
65101
case "System.Byte": return $"({getExp(callExp.Arguments[0])})::int2";
66102
case "System.Char": return $"substr(({getExp(callExp.Arguments[0])})::char, 1, 1)";
67103
case "System.DateTime": return ExpressionConstDateTime(callExp.Arguments[0]) ?? $"({getExp(callExp.Arguments[0])})::timestamp";
@@ -620,7 +656,25 @@ public override string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp
620656
{
621657
switch (exp.Method.Name)
622658
{
623-
case "ToBoolean": return $"(({getExp(exp.Arguments[0])})::varchar not in ('0','false','f','no'))";
659+
case "ToBoolean":
660+
var boolstr = getExp(exp.Arguments[0]);
661+
switch (boolstr)
662+
{
663+
case "0":
664+
case "'0'":
665+
case "false":
666+
case "'f'":
667+
case "'no'":
668+
return "'f'::bool";
669+
case "1":
670+
case "'1'":
671+
case "true":
672+
case "'t'":
673+
case "'yes'":
674+
return "'t'::bool";
675+
default:
676+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
677+
}
624678
case "ToByte": return $"({getExp(exp.Arguments[0])})::int2";
625679
case "ToChar": return $"substr(({getExp(exp.Arguments[0])})::char, 1, 1)";
626680
case "ToDateTime": return ExpressionConstDateTime(exp.Arguments[0]) ?? $"({getExp(exp.Arguments[0])})::timestamp";

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

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,25 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
3232
{
3333
switch (exp.Type.NullableTypeOrThis().ToString())
3434
{
35-
case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))";
35+
case "System.Boolean":
36+
var boolstr = getExp(operandExp);
37+
switch (boolstr)
38+
{
39+
case "0":
40+
case "'0'":
41+
case "false":
42+
case "'f'":
43+
case "'no'":
44+
return "'f'::bool";
45+
case "1":
46+
case "'1'":
47+
case "true":
48+
case "'t'":
49+
case "'yes'":
50+
return "'t'::bool";
51+
default:
52+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
53+
}
3654
case "System.Byte": return $"({getExp(operandExp)})::int2";
3755
case "System.Char": return $"substr(({getExp(operandExp)})::char, 1, 1)";
3856
case "System.DateTime": return ExpressionConstDateTime(operandExp) ?? $"({getExp(operandExp)})::timestamp";
@@ -60,7 +78,25 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
6078
case "TryParse":
6179
switch (callExp.Method.DeclaringType.NullableTypeOrThis().ToString())
6280
{
63-
case "System.Boolean": return $"(({getExp(callExp.Arguments[0])})::varchar not in ('0','false','f','no'))";
81+
case "System.Boolean":
82+
var boolstr = getExp(callExp.Arguments[0]);
83+
switch (boolstr)
84+
{
85+
case "0":
86+
case "'0'":
87+
case "false":
88+
case "'f'":
89+
case "'no'":
90+
return "'f'::bool";
91+
case "1":
92+
case "'1'":
93+
case "true":
94+
case "'t'":
95+
case "'yes'":
96+
return "'t'::bool";
97+
default:
98+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
99+
}
64100
case "System.Byte": return $"({getExp(callExp.Arguments[0])})::int2";
65101
case "System.Char": return $"substr(({getExp(callExp.Arguments[0])})::char, 1, 1)";
66102
case "System.DateTime": return ExpressionConstDateTime(callExp.Arguments[0]) ?? $"({getExp(callExp.Arguments[0])})::timestamp";
@@ -587,7 +623,25 @@ public override string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp
587623
{
588624
switch (exp.Method.Name)
589625
{
590-
case "ToBoolean": return $"(({getExp(exp.Arguments[0])})::varchar not in ('0','false','f','no'))";
626+
case "ToBoolean":
627+
var boolstr = getExp(exp.Arguments[0]);
628+
switch (boolstr)
629+
{
630+
case "0":
631+
case "'0'":
632+
case "false":
633+
case "'f'":
634+
case "'no'":
635+
return "'f'::bool";
636+
case "1":
637+
case "'1'":
638+
case "true":
639+
case "'t'":
640+
case "'yes'":
641+
return "'t'::bool";
642+
default:
643+
return $"(({boolstr})::varchar not in ('0','false','f','no'))";
644+
}
591645
case "ToByte": return $"({getExp(exp.Arguments[0])})::int2";
592646
case "ToChar": return $"substr(({getExp(exp.Arguments[0])})::char, 1, 1)";
593647
case "ToDateTime": return ExpressionConstDateTime(exp.Arguments[0]) ?? $"({getExp(exp.Arguments[0])})::timestamp";

0 commit comments

Comments
 (0)