-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathSearchConditionConvertingExpressionVisitor.cs
841 lines (729 loc) · 41.2 KB
/
SearchConditionConvertingExpressionVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class SearchConditionConvertingExpressionVisitor : SqlExpressionVisitor
{
private bool _isSearchCondition;
private readonly ISqlExpressionFactory _sqlExpressionFactory;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SearchConditionConvertingExpressionVisitor(
ISqlExpressionFactory sqlExpressionFactory)
{
_sqlExpressionFactory = sqlExpressionFactory;
}
private SqlExpression ApplyConversion(SqlExpression sqlExpression, bool condition)
=> _isSearchCondition
? ConvertToSearchCondition(sqlExpression, condition)
: ConvertToValue(sqlExpression, condition);
private SqlExpression ConvertToSearchCondition(SqlExpression sqlExpression, bool condition)
=> condition
? sqlExpression
: BuildCompareToExpression(sqlExpression);
private SqlExpression ConvertToValue(SqlExpression sqlExpression, bool condition)
=> condition
? _sqlExpressionFactory.Case(
new[]
{
new CaseWhenClause(
SimplifyNegatedBinary(sqlExpression),
_sqlExpressionFactory.ApplyDefaultTypeMapping(_sqlExpressionFactory.Constant(true)))
},
_sqlExpressionFactory.Constant(false))
: sqlExpression;
private SqlExpression BuildCompareToExpression(SqlExpression sqlExpression)
=> sqlExpression is SqlConstantExpression { Value: bool boolValue }
? _sqlExpressionFactory.Equal(
boolValue
? _sqlExpressionFactory.Constant(1)
: _sqlExpressionFactory.Constant(0),
_sqlExpressionFactory.Constant(1))
: _sqlExpressionFactory.Equal(
sqlExpression,
_sqlExpressionFactory.Constant(true));
private SqlExpression SimplifyNegatedBinary(SqlExpression sqlExpression)
{
if (sqlExpression is SqlUnaryExpression { OperatorType: ExpressionType.Not } sqlUnaryExpression
&& sqlUnaryExpression.Type == typeof(bool)
&& sqlUnaryExpression.Operand is SqlBinaryExpression
{
OperatorType: ExpressionType.Equal
} sqlBinaryOperand)
{
if (sqlBinaryOperand.Left.Type == typeof(bool)
&& sqlBinaryOperand.Right.Type == typeof(bool)
&& (sqlBinaryOperand.Left is SqlConstantExpression
|| sqlBinaryOperand.Right is SqlConstantExpression))
{
var constant = sqlBinaryOperand.Left as SqlConstantExpression ?? (SqlConstantExpression)sqlBinaryOperand.Right;
if (sqlBinaryOperand.Left is SqlConstantExpression)
{
return _sqlExpressionFactory.MakeBinary(
ExpressionType.Equal,
_sqlExpressionFactory.Constant(!(bool)constant.Value!, constant.TypeMapping),
sqlBinaryOperand.Right,
sqlBinaryOperand.TypeMapping)!;
}
return _sqlExpressionFactory.MakeBinary(
ExpressionType.Equal,
sqlBinaryOperand.Left,
_sqlExpressionFactory.Constant(!(bool)constant.Value!, constant.TypeMapping),
sqlBinaryOperand.TypeMapping)!;
}
return _sqlExpressionFactory.MakeBinary(
sqlBinaryOperand.OperatorType == ExpressionType.Equal
? ExpressionType.NotEqual
: ExpressionType.Equal,
sqlBinaryOperand.Left,
sqlBinaryOperand.Right,
sqlBinaryOperand.TypeMapping)!;
}
return sqlExpression;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitCase(CaseExpression caseExpression)
{
var parentSearchCondition = _isSearchCondition;
var testIsCondition = caseExpression.Operand == null;
_isSearchCondition = false;
var operand = (SqlExpression?)Visit(caseExpression.Operand);
var whenClauses = new List<CaseWhenClause>();
foreach (var whenClause in caseExpression.WhenClauses)
{
_isSearchCondition = testIsCondition;
var test = (SqlExpression)Visit(whenClause.Test);
_isSearchCondition = false;
var result = (SqlExpression)Visit(whenClause.Result);
whenClauses.Add(new CaseWhenClause(test, result));
}
_isSearchCondition = false;
var elseResult = (SqlExpression?)Visit(caseExpression.ElseResult);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(caseExpression.Update(operand, whenClauses, elseResult), condition: false);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitCollate(CollateExpression collateExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var operand = (SqlExpression)Visit(collateExpression.Operand);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(collateExpression.Update(operand), condition: false);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitColumn(ColumnExpression columnExpression)
=> ApplyConversion(columnExpression, condition: false);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitDelete(DeleteExpression deleteExpression)
=> deleteExpression.Update(deleteExpression.Table, (SelectExpression)Visit(deleteExpression.SelectExpression));
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitDistinct(DistinctExpression distinctExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var operand = (SqlExpression)Visit(distinctExpression.Operand);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(distinctExpression.Update(operand), condition: false);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitExists(ExistsExpression existsExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var subquery = (SelectExpression)Visit(existsExpression.Subquery);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(existsExpression.Update(subquery), condition: true);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitFromSql(FromSqlExpression fromSqlExpression)
=> fromSqlExpression;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitIn(InExpression inExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var item = (SqlExpression)Visit(inExpression.Item);
var subquery = (SelectExpression?)Visit(inExpression.Subquery);
var values = inExpression.Values;
SqlExpression[]? newValues = null;
if (values is not null)
{
for (var i = 0; i < values.Count; i++)
{
var value = values[i];
var newValue = (SqlExpression)Visit(value);
if (newValue != value && newValues is null)
{
newValues = new SqlExpression[values.Count];
for (var j = 0; j < i; j++)
{
newValues[j] = values[j];
}
}
if (newValues is not null)
{
newValues[i] = newValue;
}
}
}
var valuesParameter = (SqlParameterExpression?)Visit(inExpression.ValuesParameter);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(inExpression.Update(item, subquery, newValues ?? values, valuesParameter), condition: true);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitLike(LikeExpression likeExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var match = (SqlExpression)Visit(likeExpression.Match);
var pattern = (SqlExpression)Visit(likeExpression.Pattern);
var escapeChar = (SqlExpression?)Visit(likeExpression.EscapeChar);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(likeExpression.Update(match, pattern, escapeChar), condition: true);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSelect(SelectExpression selectExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var projections = this.VisitAndConvert(selectExpression.Projection);
var tables = this.VisitAndConvert(selectExpression.Tables);
var groupBy = this.VisitAndConvert(selectExpression.GroupBy);
var orderings = this.VisitAndConvert(selectExpression.Orderings);
var offset = (SqlExpression?)Visit(selectExpression.Offset);
var limit = (SqlExpression?)Visit(selectExpression.Limit);
_isSearchCondition = true;
var predicate = (SqlExpression?)Visit(selectExpression.Predicate);
var havingExpression = (SqlExpression?)Visit(selectExpression.Having);
_isSearchCondition = parentSearchCondition;
return selectExpression.Update(tables, predicate, groupBy, havingExpression, projections, orderings, offset, limit);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitAtTimeZone(AtTimeZoneExpression atTimeZoneExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var operand = (SqlExpression)Visit(atTimeZoneExpression.Operand);
var timeZone = (SqlExpression)Visit(atTimeZoneExpression.TimeZone);
_isSearchCondition = parentSearchCondition;
return atTimeZoneExpression.Update(operand, timeZone);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlBinary(SqlBinaryExpression sqlBinaryExpression)
{
var parentIsSearchCondition = _isSearchCondition;
switch (sqlBinaryExpression.OperatorType)
{
// Only logical operations need conditions on both sides
case ExpressionType.AndAlso:
case ExpressionType.OrElse:
_isSearchCondition = true;
break;
default:
_isSearchCondition = false;
break;
}
var newLeft = (SqlExpression)Visit(sqlBinaryExpression.Left);
var newRight = (SqlExpression)Visit(sqlBinaryExpression.Right);
_isSearchCondition = parentIsSearchCondition;
if (!parentIsSearchCondition
&& (newLeft.Type == typeof(bool) || newLeft.Type.IsEnum || newLeft.Type.IsInteger())
&& (newRight.Type == typeof(bool) || newRight.Type.IsEnum || newRight.Type.IsInteger())
&& sqlBinaryExpression.OperatorType is ExpressionType.NotEqual or ExpressionType.Equal)
{
// "lhs != rhs" is the same as "CAST(lhs ^ rhs AS BIT)", except that
// the first is a boolean, the second is a BIT
var result = _sqlExpressionFactory.MakeBinary(
ExpressionType.ExclusiveOr,
newLeft,
newRight,
null)!;
if (result.Type != typeof(bool))
{
result = _sqlExpressionFactory.Convert(result, typeof(bool), sqlBinaryExpression.TypeMapping);
}
// "lhs == rhs" is the same as "NOT(lhs == rhs)" aka "lhs ^ rhs ^ 1"
if (sqlBinaryExpression.OperatorType is ExpressionType.Equal)
{
result = _sqlExpressionFactory.MakeBinary(
ExpressionType.ExclusiveOr,
result,
_sqlExpressionFactory.Constant(true, result.TypeMapping),
result.TypeMapping
)!;
}
return result;
}
sqlBinaryExpression = sqlBinaryExpression.Update(newLeft, newRight);
var condition = sqlBinaryExpression.OperatorType is ExpressionType.AndAlso
or ExpressionType.OrElse
or ExpressionType.Equal
or ExpressionType.NotEqual
or ExpressionType.GreaterThan
or ExpressionType.GreaterThanOrEqual
or ExpressionType.LessThan
or ExpressionType.LessThanOrEqual;
return ApplyConversion(sqlBinaryExpression, condition);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpression)
{
var parentSearchCondition = _isSearchCondition;
bool resultCondition;
switch (sqlUnaryExpression.OperatorType)
{
case ExpressionType.Not
when sqlUnaryExpression.Type == typeof(bool):
{
// when possible, avoid converting to/from predicate form
if (!_isSearchCondition && sqlUnaryExpression.Operand is not (ExistsExpression or InExpression or LikeExpression))
{
var negatedOperand = (SqlExpression)Visit(sqlUnaryExpression.Operand);
return _sqlExpressionFactory.MakeBinary(
ExpressionType.ExclusiveOr,
negatedOperand,
_sqlExpressionFactory.Constant(true, negatedOperand.TypeMapping),
negatedOperand.TypeMapping
)!;
}
_isSearchCondition = true;
resultCondition = true;
break;
}
case ExpressionType.Not:
_isSearchCondition = false;
resultCondition = false;
break;
case ExpressionType.Convert:
case ExpressionType.Negate:
_isSearchCondition = false;
resultCondition = false;
break;
case ExpressionType.Equal:
case ExpressionType.NotEqual:
_isSearchCondition = false;
resultCondition = true;
break;
default:
throw new InvalidOperationException(
RelationalStrings.UnsupportedOperatorForSqlExpression(
sqlUnaryExpression.OperatorType, typeof(SqlUnaryExpression)));
}
var operand = (SqlExpression)Visit(sqlUnaryExpression.Operand);
_isSearchCondition = parentSearchCondition;
return SimplifyNegatedBinary(
ApplyConversion(
sqlUnaryExpression.Update(operand),
condition: resultCondition));
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlConstant(SqlConstantExpression sqlConstantExpression)
=> ApplyConversion(sqlConstantExpression, condition: false);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlFragment(SqlFragmentExpression sqlFragmentExpression)
=> sqlFragmentExpression;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlFunction(SqlFunctionExpression sqlFunctionExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var instance = (SqlExpression?)Visit(sqlFunctionExpression.Instance);
SqlExpression[]? arguments = default;
if (!sqlFunctionExpression.IsNiladic)
{
arguments = new SqlExpression[sqlFunctionExpression.Arguments.Count];
for (var i = 0; i < arguments.Length; i++)
{
arguments[i] = (SqlExpression)Visit(sqlFunctionExpression.Arguments[i]);
}
}
_isSearchCondition = parentSearchCondition;
var newFunction = sqlFunctionExpression.Update(instance, arguments);
var condition = sqlFunctionExpression.Name is "FREETEXT" or "CONTAINS";
return ApplyConversion(newFunction, condition);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitTableValuedFunction(TableValuedFunctionExpression tableValuedFunctionExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var arguments = new SqlExpression[tableValuedFunctionExpression.Arguments.Count];
for (var i = 0; i < arguments.Length; i++)
{
arguments[i] = (SqlExpression)Visit(tableValuedFunctionExpression.Arguments[i]);
}
_isSearchCondition = parentSearchCondition;
return tableValuedFunctionExpression.Update(arguments);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitSqlParameter(SqlParameterExpression sqlParameterExpression)
=> ApplyConversion(sqlParameterExpression, condition: false);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitTable(TableExpression tableExpression)
=> tableExpression;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitProjection(ProjectionExpression projectionExpression)
{
var expression = (SqlExpression)Visit(projectionExpression.Expression);
return projectionExpression.Update(expression);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitOrdering(OrderingExpression orderingExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var expression = (SqlExpression)Visit(orderingExpression.Expression);
_isSearchCondition = parentSearchCondition;
return orderingExpression.Update(expression);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitCrossJoin(CrossJoinExpression crossJoinExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var table = (TableExpressionBase)Visit(crossJoinExpression.Table);
_isSearchCondition = parentSearchCondition;
return crossJoinExpression.Update(table);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitCrossApply(CrossApplyExpression crossApplyExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var table = (TableExpressionBase)Visit(crossApplyExpression.Table);
_isSearchCondition = parentSearchCondition;
return crossApplyExpression.Update(table);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitOuterApply(OuterApplyExpression outerApplyExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var table = (TableExpressionBase)Visit(outerApplyExpression.Table);
_isSearchCondition = parentSearchCondition;
return outerApplyExpression.Update(table);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitInnerJoin(InnerJoinExpression innerJoinExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var table = (TableExpressionBase)Visit(innerJoinExpression.Table);
_isSearchCondition = true;
var joinPredicate = (SqlExpression)Visit(innerJoinExpression.JoinPredicate);
_isSearchCondition = parentSearchCondition;
return innerJoinExpression.Update(table, joinPredicate);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitLeftJoin(LeftJoinExpression leftJoinExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var table = (TableExpressionBase)Visit(leftJoinExpression.Table);
_isSearchCondition = true;
var joinPredicate = (SqlExpression)Visit(leftJoinExpression.JoinPredicate);
_isSearchCondition = parentSearchCondition;
return leftJoinExpression.Update(table, joinPredicate);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitScalarSubquery(ScalarSubqueryExpression scalarSubqueryExpression)
{
var parentSearchCondition = _isSearchCondition;
var subquery = (SelectExpression)Visit(scalarSubqueryExpression.Subquery);
_isSearchCondition = parentSearchCondition;
return ApplyConversion(scalarSubqueryExpression.Update(subquery), condition: false);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitRowNumber(RowNumberExpression rowNumberExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var partitions = new List<SqlExpression>();
foreach (var partition in rowNumberExpression.Partitions)
{
var newPartition = (SqlExpression)Visit(partition);
partitions.Add(newPartition);
}
var orderings = new List<OrderingExpression>();
foreach (var ordering in rowNumberExpression.Orderings)
{
var newOrdering = (OrderingExpression)Visit(ordering);
orderings.Add(newOrdering);
}
_isSearchCondition = parentSearchCondition;
return ApplyConversion(rowNumberExpression.Update(partitions, orderings), condition: false);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitRowValue(RowValueExpression rowValueExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var values = new SqlExpression[rowValueExpression.Values.Count];
for (var i = 0; i < values.Length; i++)
{
values[i] = (SqlExpression)Visit(rowValueExpression.Values[i]);
}
_isSearchCondition = parentSearchCondition;
return rowValueExpression.Update(values);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitExcept(ExceptExpression exceptExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var source1 = (SelectExpression)Visit(exceptExpression.Source1);
var source2 = (SelectExpression)Visit(exceptExpression.Source2);
_isSearchCondition = parentSearchCondition;
return exceptExpression.Update(source1, source2);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitIntersect(IntersectExpression intersectExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var source1 = (SelectExpression)Visit(intersectExpression.Source1);
var source2 = (SelectExpression)Visit(intersectExpression.Source2);
_isSearchCondition = parentSearchCondition;
return intersectExpression.Update(source1, source2);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitUnion(UnionExpression unionExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var source1 = (SelectExpression)Visit(unionExpression.Source1);
var source2 = (SelectExpression)Visit(unionExpression.Source2);
_isSearchCondition = parentSearchCondition;
return unionExpression.Update(source1, source2);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitUpdate(UpdateExpression updateExpression)
{
var selectExpression = (SelectExpression)Visit(updateExpression.SelectExpression);
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
List<ColumnValueSetter>? columnValueSetters = null;
for (var (i, n) = (0, updateExpression.ColumnValueSetters.Count); i < n; i++)
{
var columnValueSetter = updateExpression.ColumnValueSetters[i];
var newValue = (SqlExpression)Visit(columnValueSetter.Value);
if (columnValueSetters != null)
{
columnValueSetters.Add(new ColumnValueSetter(columnValueSetter.Column, newValue));
}
else if (!ReferenceEquals(newValue, columnValueSetter.Value))
{
columnValueSetters = [];
for (var j = 0; j < i; j++)
{
columnValueSetters.Add(updateExpression.ColumnValueSetters[j]);
}
columnValueSetters.Add(new ColumnValueSetter(columnValueSetter.Column, newValue));
}
}
_isSearchCondition = parentSearchCondition;
return updateExpression.Update(selectExpression, columnValueSetters ?? updateExpression.ColumnValueSetters);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitJsonScalar(JsonScalarExpression jsonScalarExpression)
=> ApplyConversion(jsonScalarExpression, condition: false);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitValues(ValuesExpression valuesExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
var rowValues = new RowValueExpression[valuesExpression.RowValues.Count];
for (var i = 0; i < rowValues.Length; i++)
{
rowValues[i] = (RowValueExpression)Visit(valuesExpression.RowValues[i]);
}
_isSearchCondition = parentSearchCondition;
return valuesExpression.Update(rowValues);
}
}