-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathQueryBinder.SingleValueFunctionCall.cs
More file actions
783 lines (643 loc) · 36.2 KB
/
QueryBinder.SingleValueFunctionCall.cs
File metadata and controls
783 lines (643 loc) · 36.2 KB
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
//-----------------------------------------------------------------------------
// <copyright file="QueryBinder.SingleValueFunctionCall.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.OData.Common;
using Microsoft.AspNetCore.OData.Edm;
using Microsoft.OData;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using Microsoft.OData.UriParser;
namespace Microsoft.AspNetCore.OData.Query.Expressions
{
/// <summary>
/// The base class for all expression binders.
/// </summary>
public abstract partial class QueryBinder
{
/// <summary>
/// Binds a <see cref="SingleValueFunctionCallNode"/> to create a LINQ <see cref="Expression"/> that
/// represents the semantics of the <see cref="SingleValueFunctionCallNode"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
public virtual Expression BindSingleValueFunctionCallNode(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context);
switch (node.Name)
{
case ClrCanonicalFunctions.StartswithFunctionName:
return BindStartsWith(node, context);
case ClrCanonicalFunctions.EndswithFunctionName:
return BindEndsWith(node, context);
case ClrCanonicalFunctions.ContainsFunctionName:
return BindContains(node, context);
case ClrCanonicalFunctions.SubstringFunctionName:
return BindSubstring(node, context);
case ClrCanonicalFunctions.LengthFunctionName:
return BindLength(node, context);
case ClrCanonicalFunctions.IndexofFunctionName:
return BindIndexOf(node, context);
case ClrCanonicalFunctions.TolowerFunctionName:
return BindToLower(node, context);
case ClrCanonicalFunctions.ToupperFunctionName:
return BindToUpper(node, context);
case ClrCanonicalFunctions.TrimFunctionName:
return BindTrim(node, context);
case ClrCanonicalFunctions.ConcatFunctionName:
return BindConcat(node, context);
case ClrCanonicalFunctions.MatchesPatternFunctionName:
return BindMatchesPattern(node, context);
case ClrCanonicalFunctions.YearFunctionName:
case ClrCanonicalFunctions.MonthFunctionName:
case ClrCanonicalFunctions.DayFunctionName:
return BindDateRelatedProperty(node, context); // Date & DateTime & DateTimeOffset
case ClrCanonicalFunctions.HourFunctionName:
case ClrCanonicalFunctions.MinuteFunctionName:
case ClrCanonicalFunctions.SecondFunctionName:
return BindTimeRelatedProperty(node, context); // TimeOfDay & DateTime & DateTimeOffset
case ClrCanonicalFunctions.FractionalSecondsFunctionName:
return BindFractionalSeconds(node, context);
case ClrCanonicalFunctions.RoundFunctionName:
return BindRound(node, context);
case ClrCanonicalFunctions.FloorFunctionName:
return BindFloor(node, context);
case ClrCanonicalFunctions.CeilingFunctionName:
return BindCeiling(node, context);
case ClrCanonicalFunctions.CastFunctionName:
return BindCastSingleValue(node, context);
case ClrCanonicalFunctions.IsofFunctionName:
return BindIsOf(node, context);
case ClrCanonicalFunctions.DateFunctionName:
return BindDate(node, context);
case ClrCanonicalFunctions.TimeFunctionName:
return BindTime(node, context);
case ClrCanonicalFunctions.NowFunctionName:
return BindNow(node, context);
default:
// Get Expression of custom binded method.
Expression expression = BindCustomMethodExpressionOrNull(node, context);
if (expression != null)
{
return expression;
}
throw new NotImplementedException(Error.Format(SRResources.ODataFunctionNotSupported, node.Name));
}
}
/// <summary>
/// Binds a 'startswith' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindStartsWith(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "startswith");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 2 && arguments[0].Type == typeof(string) && arguments[1].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.StartsWith, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'endswith' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindEndsWith(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "endswith");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 2 && arguments[0].Type == typeof(string) && arguments[1].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.EndsWith, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'contains' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindContains(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "contains");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 2 && arguments[0].Type == typeof(string) && arguments[1].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.Contains, context.QuerySettings, arguments[0], arguments[1]);
}
/// <summary>
/// Binds a 'substring' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindSubstring(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "substring");
Expression[] arguments = BindArguments(node.Parameters, context);
if (arguments[0].Type != typeof(string))
{
throw new ODataException(Error.Format(SRResources.FunctionNotSupportedOnEnum, node.Name));
}
ODataQuerySettings querySettings = context.QuerySettings;
Expression functionCall;
if (arguments.Length == 2)
{
Contract.Assert(ExpressionBinderHelper.IsInteger(arguments[1].Type));
// When null propagation is allowed, we use a safe version of String.Substring(int).
// But for providers that would not recognize custom expressions like this, we map
// directly to String.Substring(int)
if (context.QuerySettings.HandleNullPropagation == HandleNullPropagationOption.True)
{
// Safe function is static and takes string "this" as first argument
functionCall = ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.SubstringStartNoThrow, querySettings, arguments);
}
else
{
functionCall = ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.SubstringStart, querySettings, arguments);
}
}
else
{
// arguments.Length == 3 implies String.Substring(int, int)
Contract.Assert(arguments.Length == 3 && ExpressionBinderHelper.IsInteger(arguments[1].Type) && ExpressionBinderHelper.IsInteger(arguments[2].Type));
// When null propagation is allowed, we use a safe version of String.Substring(int, int).
// But for providers that would not recognize custom expressions like this, we map
// directly to String.Substring(int, int)
if (querySettings.HandleNullPropagation == HandleNullPropagationOption.True)
{
// Safe function is static and takes string "this" as first argument
functionCall = ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.SubstringStartAndLengthNoThrow, querySettings, arguments);
}
else
{
functionCall = ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.SubstringStartAndLength, querySettings, arguments);
}
}
return functionCall;
}
/// <summary>
/// Binds a 'length' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindLength(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "length");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 1 && arguments[0].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.Length, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'indexof' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindIndexOf(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "indexof");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 2 && arguments[0].Type == typeof(string) && arguments[1].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.IndexOf, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'tolower' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindToLower(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "tolower");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 1 && arguments[0].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.ToLower, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'toupper' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindToUpper(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "toupper");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 1 && arguments[0].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.ToUpper, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'trim' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindTrim(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "trim");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 1 && arguments[0].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.Trim, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'concat' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindConcat(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "concat");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 2 && arguments[0].Type == typeof(string) && arguments[1].Type == typeof(string));
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.Concat, context.QuerySettings, arguments);
}
/// <summary>
/// Binds a 'matchesPattern' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindMatchesPattern(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "matchesPattern");
Expression[] arguments = BindArguments(node.Parameters, context);
ValidateAllStringArguments(node.Name, arguments);
Contract.Assert(arguments.Length == 2 && arguments[0].Type == typeof(string) && arguments[1].Type == typeof(string));
//add argument that must be ECMAScript compatible regex
arguments = new[] { arguments[0], arguments[1], Expression.Constant(RegexOptions.ECMAScript) };
return ExpressionBinderHelper.MakeFunctionCall(ClrCanonicalFunctions.MatchesMattern, context.QuerySettings, arguments);
}
/// <summary>
/// Binds date related functions to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindDateRelatedProperty(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context);
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsDateRelated(arguments[0].Type));
// We should support DateTime & DateTimeOffset even though DateTime is not part of OData v4 Spec.
Expression parameter = arguments[0];
PropertyInfo property;
if (ExpressionBinderHelper.IsDate(parameter.Type))
{
Contract.Assert(ClrCanonicalFunctions.DateProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.DateProperties[node.Name];
}
#if NET6_0
else if (parameter.Type.IsDateOnly())
{
Contract.Assert(ClrCanonicalFunctions.DateOnlyProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.DateOnlyProperties[node.Name];
}
#endif
else if (ExpressionBinderHelper.IsDateTime(parameter.Type))
{
Contract.Assert(ClrCanonicalFunctions.DateTimeProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.DateTimeProperties[node.Name];
}
else
{
Contract.Assert(ClrCanonicalFunctions.DateTimeOffsetProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.DateTimeOffsetProperties[node.Name];
}
return ExpressionBinderHelper.MakeFunctionCall(property, context.QuerySettings, parameter);
}
/// <summary>
/// Binds time related functions to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindTimeRelatedProperty(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context);
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsTimeRelated(arguments[0].Type));
// We should support DateTime & DateTimeOffset even though DateTime is not part of OData v4 Spec.
Expression parameter = arguments[0];
PropertyInfo property;
if (ExpressionBinderHelper.IsTimeOfDay(parameter.Type))
{
Contract.Assert(ClrCanonicalFunctions.TimeOfDayProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.TimeOfDayProperties[node.Name];
}
#if NET6_0
else if (parameter.Type.IsTimeOnly())
{
Contract.Assert(ClrCanonicalFunctions.TimeOnlyProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.TimeOnlyProperties[node.Name];
}
#endif
else if (ExpressionBinderHelper.IsDateTime(parameter.Type))
{
Contract.Assert(ClrCanonicalFunctions.DateTimeProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.DateTimeProperties[node.Name];
}
else if (ExpressionBinderHelper.IsTimeSpan(parameter.Type))
{
Contract.Assert(ClrCanonicalFunctions.TimeSpanProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.TimeSpanProperties[node.Name];
}
else
{
Contract.Assert(ClrCanonicalFunctions.DateTimeOffsetProperties.ContainsKey(node.Name));
property = ClrCanonicalFunctions.DateTimeOffsetProperties[node.Name];
}
return ExpressionBinderHelper.MakeFunctionCall(property, context.QuerySettings, parameter);
}
/// <summary>
/// Binds 'fractionalseconds' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindFractionalSeconds(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "fractionalseconds");
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 && (ExpressionBinderHelper.IsTimeRelated(arguments[0].Type)));
// We should support DateTime & DateTimeOffset even though DateTime is not part of OData v4 Spec.
Expression parameter = arguments[0];
PropertyInfo property;
if (ExpressionBinderHelper.IsTimeOfDay(parameter.Type))
{
property = ClrCanonicalFunctions.TimeOfDayProperties[ClrCanonicalFunctions.MillisecondFunctionName];
}
else if (ExpressionBinderHelper.IsDateTime(parameter.Type))
{
property = ClrCanonicalFunctions.DateTimeProperties[ClrCanonicalFunctions.MillisecondFunctionName];
}
else if (ExpressionBinderHelper.IsTimeSpan(parameter.Type))
{
property = ClrCanonicalFunctions.TimeSpanProperties[ClrCanonicalFunctions.MillisecondFunctionName];
}
else
{
property = ClrCanonicalFunctions.DateTimeOffsetProperties[ClrCanonicalFunctions.MillisecondFunctionName];
}
// Millisecond
Expression milliSecond = ExpressionBinderHelper.MakePropertyAccess(property, parameter, context.QuerySettings);
Expression decimalMilliSecond = Expression.Convert(milliSecond, typeof(decimal));
Expression fractionalSeconds = Expression.Divide(decimalMilliSecond, Expression.Constant(1000m, typeof(decimal)));
return ExpressionBinderHelper.CreateFunctionCallWithNullPropagation(fractionalSeconds, arguments, context.QuerySettings);
}
/// <summary>
/// Binds 'round' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindRound(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "round");
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsDoubleOrDecimal(arguments[0].Type));
MethodInfo round = ExpressionBinderHelper.IsType<double>(arguments[0].Type)
? ClrCanonicalFunctions.RoundOfDouble
: ClrCanonicalFunctions.RoundOfDecimal;
return ExpressionBinderHelper.MakeFunctionCall(round, context.QuerySettings, arguments);
}
/// <summary>
/// Binds 'floor' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindFloor(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "floor");
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsDoubleOrDecimal(arguments[0].Type));
MethodInfo floor = ExpressionBinderHelper.IsType<double>(arguments[0].Type)
? ClrCanonicalFunctions.FloorOfDouble
: ClrCanonicalFunctions.FloorOfDecimal;
return ExpressionBinderHelper.MakeFunctionCall(floor, context.QuerySettings, arguments);
}
/// <summary>
/// Binds 'ceiling' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindCeiling(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "ceiling");
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsDoubleOrDecimal(arguments[0].Type));
MethodInfo ceiling = ExpressionBinderHelper.IsType<double>(arguments[0].Type)
? ClrCanonicalFunctions.CeilingOfDouble
: ClrCanonicalFunctions.CeilingOfDecimal;
return ExpressionBinderHelper.MakeFunctionCall(ceiling, context.QuerySettings, arguments);
}
/// <summary>
/// Binds 'cast' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindCastSingleValue(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "cast");
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 1 || arguments.Length == 2);
Expression source = arguments.Length == 1 ? context.CurrentParameter : arguments[0];
string targetTypeName = (string)((ConstantNode)node.Parameters.Last()).Value;
IEdmType targetEdmType = context.Model.FindType(targetTypeName);
Type targetClrType = null;
if (targetEdmType != null)
{
IEdmTypeReference targetEdmTypeReference = targetEdmType.ToEdmTypeReference(false);
targetClrType = context.Model.GetClrType(targetEdmTypeReference);
if (source != NullConstant)
{
if (source.Type == targetClrType)
{
return source;
}
if ((!targetEdmTypeReference.IsPrimitive() && !targetEdmTypeReference.IsEnum()) ||
(context.Model.GetEdmPrimitiveTypeReference(source.Type) == null && !TypeHelper.IsEnum(source.Type)))
{
// Cast fails and return null.
return NullConstant;
}
}
}
if (targetClrType == null || source == NullConstant)
{
return NullConstant;
}
if (targetClrType == typeof(string))
{
return ExpressionBinderHelper.BindCastToStringType(source);
}
else if (TypeHelper.IsEnum(targetClrType))
{
return BindCastToEnumType(source.Type, targetClrType, node.Parameters.First(), arguments.Length, context);
}
else
{
if (TypeHelper.IsNullable(source.Type) && !TypeHelper.IsNullable(targetClrType))
{
// Make the target Clr type nullable to avoid failure while casting
// nullable source, whose value may be null, to a non-nullable type.
// For example: cast(NullableInt32Property,Edm.Int64)
// The target Clr type should be Nullable<Int64> rather than Int64.
targetClrType = typeof(Nullable<>).MakeGenericType(targetClrType);
}
try
{
return Expression.Convert(source, targetClrType);
}
catch (InvalidOperationException)
{
// Cast fails and return null.
return NullConstant;
}
}
}
/// <summary>
/// Binds a 'isof' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindIsOf(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "isof");
Expression[] arguments = BindArguments(node.Parameters, context);
// Edm.Boolean isof(type) or
// Edm.Boolean isof(expression,type)
Contract.Assert(arguments.Length == 1 || arguments.Length == 2);
Expression source = arguments.Length == 1 ? context.CurrentParameter : arguments[0];
if (source == NullConstant)
{
return FalseConstant;
}
string typeName = (string)((ConstantNode)node.Parameters.Last()).Value;
IEdmType edmType = context.Model.FindType(typeName);
Type clrType = null;
if (edmType != null)
{
// bool nullable = source.Type.IsNullable();
IEdmTypeReference edmTypeReference = edmType.ToEdmTypeReference(false);
clrType = context.Model.GetClrType(edmTypeReference);
}
if (clrType == null)
{
return FalseConstant;
}
bool isSourcePrimitiveOrEnum = context.Model.GetEdmPrimitiveTypeReference(source.Type) != null ||
TypeHelper.IsEnum(source.Type);
bool isTargetPrimitiveOrEnum = context.Model.GetEdmPrimitiveTypeReference(clrType) != null ||
TypeHelper.IsEnum(clrType);
if (isSourcePrimitiveOrEnum && isTargetPrimitiveOrEnum)
{
if (TypeHelper.IsNullable(source.Type))
{
clrType = TypeHelper.ToNullable(clrType);
}
}
// Be caution: Type method of LINQ to Entities only supports entity type.
return Expression.Condition(Expression.TypeIs(source, clrType), TrueConstant, FalseConstant);
}
/// <summary>
/// Binds a 'date' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindDate(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "date");
Expression[] arguments = BindArguments(node.Parameters, context);
// We should support DateTime & DateTimeOffset even though DateTime is not part of OData v4 Spec.
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsDateOrOffset(arguments[0].Type));
// EF doesn't support new Date(int, int, int), also doesn't support other property access, for example DateTime.Date.
// Therefore, we just return the source (DateTime or DateTimeOffset).
return arguments[0];
}
/// <summary>
/// Binds a 'time' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindTime(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "time");
Expression[] arguments = BindArguments(node.Parameters, context);
// We should support DateTime & DateTimeOffset even though DateTime is not part of OData v4 Spec.
Contract.Assert(arguments.Length == 1 && ExpressionBinderHelper.IsDateOrOffset(arguments[0].Type));
// EF doesn't support new TimeOfDay(int, int, int, int), also doesn't support other property access, for example DateTimeOffset.DateTime.
// Therefore, we just return the source (DateTime or DateTimeOffset).
return arguments[0];
}
/// <summary>
/// Binds a 'now' function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindNow(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context, "now");
// Function Now() does not take any arguments.
Expression[] arguments = BindArguments(node.Parameters, context);
Contract.Assert(arguments.Length == 0);
return Expression.Property(null, typeof(DateTimeOffset), "UtcNow");
}
/// <summary>
/// Binds customized function to create a LINQ <see cref="Expression"/>.
/// </summary>
/// <param name="node">The query node to bind.</param>
/// <param name="context">The query binder context.</param>
/// <returns>The LINQ <see cref="Expression"/> created.</returns>
protected virtual Expression BindCustomMethodExpressionOrNull(SingleValueFunctionCallNode node, QueryBinderContext context)
{
CheckArgumentNull(node, context);
Expression[] arguments = BindArguments(node.Parameters, context);
IEnumerable<Type> methodArgumentsType = arguments.Select(argument => argument.Type);
// Search for custom method info that are binded to the node name
MethodInfo methodInfo;
if (UriFunctionsBinder.TryGetMethodInfo(node.Name, methodArgumentsType, out methodInfo))
{
return ExpressionBinderHelper.MakeCustomFunctionCall(methodInfo, arguments);
}
return null;
}
private static void CheckArgumentNull(SingleValueFunctionCallNode node, QueryBinderContext context, string nodeName)
{
if (node == null || node.Name != nodeName)
{
throw Error.ArgumentNull(nameof(node));
}
if (context == null)
{
throw Error.ArgumentNull(nameof(context));
}
}
}
}