-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathNodeToStringBuilder.cs
825 lines (738 loc) · 37.3 KB
/
NodeToStringBuilder.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
//---------------------------------------------------------------------
// <copyright file="NodeToStringBuilder.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
namespace Microsoft.OData
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
/// <summary>
/// Build QueryNode to String Representation
/// </summary>
internal sealed class NodeToStringBuilder : QueryNodeVisitor<String>
{
/// <summary>
/// whether translating search options or others
/// </summary>
private bool searchFlag;
/// <summary>
/// Stack current RangeVariable.
/// </summary>
private readonly Stack<RangeVariable> rangeVariables = new Stack<RangeVariable>();
/// <summary>
/// Translates a <see cref="AllNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(AllNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
string source = TranslateNode(node.Source);
rangeVariables.Push(node.CurrentRangeVariable);
String result = String.Concat(source, ExpressionConstants.SymbolForwardSlash, ExpressionConstants.KeywordAll, ExpressionConstants.SymbolOpenParen, node.CurrentRangeVariable.Name, ":", this.TranslateNode(node.Body), ExpressionConstants.SymbolClosedParen);
rangeVariables.Pop();
return result;
}
/// <summary>
/// Translates a <see cref="AnyNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(AnyNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
if (node.CurrentRangeVariable == null && node.Body.Kind == QueryNodeKind.Constant)
{
return String.Concat(this.TranslateNode(node.Source), ExpressionConstants.SymbolForwardSlash, ExpressionConstants.KeywordAny, ExpressionConstants.SymbolOpenParen, ExpressionConstants.SymbolClosedParen);
}
else
{
string source = TranslateNode(node.Source);
rangeVariables.Push(node.CurrentRangeVariable);
string query = String.Concat(source, ExpressionConstants.SymbolForwardSlash, ExpressionConstants.KeywordAny, ExpressionConstants.SymbolOpenParen, node.CurrentRangeVariable.Name, ":", this.TranslateNode(node.Body), ExpressionConstants.SymbolClosedParen);
rangeVariables.Pop();
return query;
}
}
/// <summary>
/// Translates a <see cref="BinaryOperatorNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(BinaryOperatorNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
var left = this.TranslateNode(node.Left);
if (node.Left.Kind == QueryNodeKind.BinaryOperator && TranslateBinaryOperatorPriority(((BinaryOperatorNode)node.Left).OperatorKind) < TranslateBinaryOperatorPriority(node.OperatorKind) ||
node.Left.Kind == QueryNodeKind.Convert && ((ConvertNode)node.Left).Source.Kind == QueryNodeKind.BinaryOperator &&
TranslateBinaryOperatorPriority(((BinaryOperatorNode)((ConvertNode)node.Left).Source).OperatorKind) < TranslateBinaryOperatorPriority(node.OperatorKind))
{
left = String.Concat(ExpressionConstants.SymbolOpenParen, left, ExpressionConstants.SymbolClosedParen);
}
var right = this.TranslateNode(node.Right);
if (node.Right.Kind == QueryNodeKind.BinaryOperator && TranslateBinaryOperatorPriority(((BinaryOperatorNode)node.Right).OperatorKind) < TranslateBinaryOperatorPriority(node.OperatorKind) ||
node.Right.Kind == QueryNodeKind.Convert && ((ConvertNode)node.Right).Source.Kind == QueryNodeKind.BinaryOperator &&
TranslateBinaryOperatorPriority(((BinaryOperatorNode)((ConvertNode)node.Right).Source).OperatorKind) < TranslateBinaryOperatorPriority(node.OperatorKind))
{
right = String.Concat(ExpressionConstants.SymbolOpenParen, right, ExpressionConstants.SymbolClosedParen);
}
return String.Concat(left, ' ', this.BinaryOperatorNodeToString(node.OperatorKind), ' ', right);
}
/// <summary>
/// Translates a <see cref="InNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(InNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
string left = this.TranslateNode(node.Left);
string right = this.TranslateNode(node.Right);
return String.Concat(left, ' ', ExpressionConstants.KeywordIn, ' ', right);
}
/// <summary>
/// Translates a <see cref="CountNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(CountNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
String source = this.TranslateNode(node.Source);
return string.Concat(source, ExpressionConstants.SymbolForwardSlash, UriQueryConstants.CountSegment);
}
/// <summary>
/// Translates a <see cref="CollectionNavigationNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(CollectionNavigationNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.NavigationProperty.Name, node.NavigationSource);
}
/// <summary>
/// Translates a <see cref="CollectionPropertyAccessNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(CollectionPropertyAccessNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.Property.Name);
}
/// <summary>
/// Translates a <see cref="CollectionComplexNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(CollectionComplexNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.Property.Name);
}
/// <summary>
/// Translates a <see cref="ConstantNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(ConstantNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
if (node.Value == null)
{
return ExpressionConstants.KeywordNull;
}
return node.LiteralText;
}
/// <summary>
/// Translates a <see cref="CollectionConstantNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(CollectionConstantNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
if (String.IsNullOrEmpty(node.LiteralText))
{
return ExpressionConstants.KeywordNull;
}
return node.LiteralText;
}
/// <summary>
/// Translates a <see cref="ConvertNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(ConvertNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslateNode(node.Source);
}
/// <summary>
/// Translates a <see cref="CollectionResourceCastNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String of CollectionResourceCastNode.</returns>
public override String Visit(CollectionResourceCastNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.ItemStructuredType.Definition.ToString());
}
/// <summary>
/// Translates a <see cref="ResourceRangeVariableReferenceNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(ResourceRangeVariableReferenceNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
if (node.Name == "$it")
{
return String.Empty;
}
else
{
return node.Name;
}
}
/// <summary>
/// Translates a <see cref="NonResourceRangeVariableReferenceNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(NonResourceRangeVariableReferenceNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return node.Name;
}
/// <summary>
/// Translates a <see cref="SingleResourceCastNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleResourceCastNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.StructuredTypeReference.Definition.ToString());
}
/// <summary>
/// Translates a <see cref="SingleNavigationNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleNavigationNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.NavigationProperty.Name, node.NavigationSource);
}
/// <summary>
/// Translates a <see cref="SingleResourceFunctionCallNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleResourceFunctionCallNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
String result = node.Name;
if (node.Source != null)
{
result = this.TranslatePropertyAccess(node.Source, result);
}
return this.TranslateFunctionCall(result, node.Parameters);
}
/// <summary>
/// Translates a <see cref="SingleValueFunctionCallNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleValueFunctionCallNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
String result = node.Name;
if (node.Source != null)
{
result = this.TranslatePropertyAccess(node.Source, result);
}
return this.TranslateFunctionCall(result, node.Parameters);
}
/// <summary>
/// Translates a <see cref="CollectionFunctionCallNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String of CollectionFunctionCallNode.</returns>
public override String Visit(CollectionFunctionCallNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
String result = node.Name;
if (node.Source != null)
{
result = this.TranslatePropertyAccess(node.Source, result);
}
return this.TranslateFunctionCall(result, node.Parameters);
}
/// <summary>
/// Translates a <see cref="CollectionResourceFunctionCallNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String of CollectionResourceFunctionCallNode.</returns>
public override String Visit(CollectionResourceFunctionCallNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
String result = node.Name;
if (node.Source != null)
{
result = this.TranslatePropertyAccess(node.Source, result);
}
return this.TranslateFunctionCall(result, node.Parameters);
}
/// <summary>
/// Translates a <see cref="SingleValueOpenPropertyAccessNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleValueOpenPropertyAccessNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.Name);
}
/// <summary>
/// Translates an <see cref="CollectionOpenPropertyAccessNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(CollectionOpenPropertyAccessNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.Name);
}
/// <summary>
/// Translates a <see cref="SingleValuePropertyAccessNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleValuePropertyAccessNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.Property.Name);
}
/// <summary>
/// Translates a <see cref="SingleComplexNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(SingleComplexNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return this.TranslatePropertyAccess(node.Source, node.Property.Name);
}
/// <summary>
/// Translates a <see cref="ParameterAliasNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(ParameterAliasNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return node.Alias;
}
/// <summary>
/// Translates a <see cref="NamedFunctionParameterNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String of NamedFunctionParameterNode.</returns>
public override string Visit(NamedFunctionParameterNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
return String.Concat(node.Name, ExpressionConstants.SymbolEqual, this.TranslateNode(node.Value));
}
/// <summary>
/// Translates a <see cref="NamedFunctionParameterNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String of SearchTermNode.</returns>
public override string Visit(SearchTermNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
if (IsValidSearchWord(node.Text) == false)
{
return String.Concat("\"", node.Text, "\"");
}
else
{
return node.Text;
}
}
/// <summary>
/// Translates a <see cref="UnaryOperatorNode"/> into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="node">The node to translate.</param>
/// <returns>The translated String.</returns>
public override String Visit(UnaryOperatorNode node)
{
ExceptionUtils.CheckArgumentNotNull(node, "node");
String result = null;
if (node.OperatorKind == UnaryOperatorKind.Negate)
{
result = ExpressionConstants.SymbolNegate;
}
// if current translated node is SearchNode, the UnaryOperator should return NOT, or return not
if (node.OperatorKind == UnaryOperatorKind.Not)
{
if (searchFlag)
{
result = ExpressionConstants.SearchKeywordNot;
}
else
{
result = ExpressionConstants.KeywordNot;
}
}
if (node.Operand.Kind == QueryNodeKind.Constant || node.Operand.Kind == QueryNodeKind.SearchTerm)
{
return String.Concat(result, ' ', this.TranslateNode(node.Operand));
}
else
{
return String.Concat(result, ExpressionConstants.SymbolOpenParen, this.TranslateNode(node.Operand), ExpressionConstants.SymbolClosedParen);
}
}
/// <summary>Translates a <see cref="LevelsClause"/> into a string.</summary>
/// <param name="levelsClause">The levels clause to translate.</param>
/// <returns>The translated String.</returns>
internal static string TranslateLevelsClause(LevelsClause levelsClause)
{
Debug.Assert(levelsClause != null, "levelsClause != null");
string levelsStr = levelsClause.IsMaxLevel
? ExpressionConstants.KeywordMax
: levelsClause.Level.ToString(CultureInfo.InvariantCulture);
return levelsStr;
}
/// <summary>
/// Main dispatching visit method for translating query-nodes into expressions.
/// </summary>
/// <param name="node">The node to visit/translate.</param>
/// <returns>The LINQ String resulting from visiting the node.</returns>
internal String TranslateNode(QueryNode node)
{
Debug.Assert(node != null, "node != null");
return node.Accept(this);
}
/// <summary>Translates a <see cref="FilterClause"/> into a string.</summary>
/// <param name="filterClause">The filter clause to translate.</param>
/// <returns>The translated String.</returns>
internal String TranslateFilterClause(FilterClause filterClause)
{
Debug.Assert(filterClause != null, "filterClause != null");
rangeVariables.Push(filterClause.RangeVariable);
string query = this.TranslateNode(filterClause.Expression);
rangeVariables.Pop();
return query;
}
/// <summary>Translates a <see cref="OrderByClause"/> into a string.</summary>
/// <param name="orderByClause">The orderBy clause to translate.</param>
/// <returns>The translated String.</returns>
internal String TranslateOrderByClause(OrderByClause orderByClause)
{
Debug.Assert(orderByClause != null, "orderByClause != null");
String expr = this.TranslateNode(orderByClause.Expression);
if (orderByClause.Direction == OrderByDirection.Descending)
{
expr = String.Concat(expr, ' ', ExpressionConstants.KeywordDescending);
}
orderByClause = orderByClause.ThenBy;
if (orderByClause == null)
{
return expr;
}
else
{
return String.Concat(expr, ExpressionConstants.SymbolComma, this.TranslateOrderByClause(orderByClause));
}
}
/// <summary>Translates a <see cref="SearchClause"/> into a string.</summary>
/// <param name="searchClause">The search clause to translate.</param>
/// <returns>The translated String.</returns>
internal String TranslateSearchClause(SearchClause searchClause)
{
Debug.Assert(searchClause != null, "searchClause != null");
searchFlag = true;
string searchStr = this.TranslateNode(searchClause.Expression);
searchFlag = false;
return searchStr;
}
/// <summary>Translates a <see cref="ComputeClause"/> into a string.</summary>
/// <param name="computeClause">The compute clause to translate.</param>
/// <returns>The translated String.</returns>
internal string TranslateComputeClause(ComputeClause computeClause)
{
Debug.Assert(computeClause != null, "computeClause != null");
bool appendComma = false;
StringBuilder sb = new StringBuilder();
foreach (var item in computeClause.ComputedItems)
{
if (appendComma)
{
sb.Append(ExpressionConstants.SymbolComma);
}
else
{
appendComma = true;
}
sb.Append(this.TranslateNode(item.Expression));
sb.Append(ExpressionConstants.SymbolEscapedSpace); // "%20"
sb.Append(ExpressionConstants.KeywordAs);
sb.Append(ExpressionConstants.SymbolEscapedSpace); // "%20"
sb.Append(item.Alias);
}
return sb.ToString();
}
/// <summary>
/// Add dictionary to url and each alias value will be URL encoded.
/// </summary>
/// <param name="dictionary">Dictionary</param>
/// <returns>The url query string of dictionary's key value pairs (URL encoded)</returns>
internal String TranslateParameterAliasNodes(IDictionary<string, SingleValueNode> dictionary)
{
String result = null;
if (dictionary != null)
{
foreach (KeyValuePair<string, SingleValueNode> keyValuePair in dictionary)
{
if (keyValuePair.Value != null)
{
String tmp = this.TranslateNode(keyValuePair.Value);
result = string.IsNullOrEmpty(tmp) ? result : string.Concat(result, String.IsNullOrEmpty(result) ? null : ExpressionConstants.SymbolQueryConcatenate, keyValuePair.Key, ExpressionConstants.SymbolEqual, Uri.EscapeDataString(tmp));
}
}
}
return result;
}
/// <summary>
/// Helper for translating an access to a metadata-defined property or navigation.
/// </summary>
/// <param name="sourceNode">The source of the property access.</param>
/// <param name="edmPropertyName">The structural or navigation property being accessed.</param>
/// <param name="navigationSource">The navigation source of the result, required for navigations.</param>
/// <returns>The translated String.</returns>
private String TranslatePropertyAccess(QueryNode sourceNode, String edmPropertyName, IEdmNavigationSource navigationSource = null)
{
ExceptionUtils.CheckArgumentNotNull(sourceNode, "sourceNode");
ExceptionUtils.CheckArgumentNotNull(edmPropertyName, "edmPropertyName");
rangeVariables.Push(GetResourceRangeVariableReferenceNode(sourceNode)?.RangeVariable);
String source = this.TranslateNode(sourceNode);
rangeVariables.Pop();
string query = String.IsNullOrEmpty(source) ? edmPropertyName : string.Concat(source, ExpressionConstants.SymbolForwardSlash, edmPropertyName);
if (IsDifferentSource(sourceNode))
{
query = ExpressionConstants.It + ExpressionConstants.SymbolForwardSlash + query;
}
return query;
}
/// <summary>
/// Translates a function call into a corresponding <see cref="String"/>.
/// </summary>
/// <param name="functionName">Name of the function.</param>
/// <param name="argumentNodes">The argument nodes.</param>
/// <returns>
/// The translated String.
/// </returns>
private String TranslateFunctionCall(string functionName, IEnumerable<QueryNode> argumentNodes)
{
ExceptionUtils.CheckArgumentNotNull(functionName, "functionName");
String result = String.Empty;
foreach (QueryNode queryNode in argumentNodes)
{
result = String.Concat(result, String.IsNullOrEmpty(result) ? null : ExpressionConstants.SymbolComma, this.TranslateNode(queryNode));
}
return String.Concat(functionName, ExpressionConstants.SymbolOpenParen, result, ExpressionConstants.SymbolClosedParen);
}
/// <summary>
/// Build BinaryOperatorNode to uri
/// </summary>
/// <param name="operatorKind">the kind of the BinaryOperatorNode</param>
/// <returns>String format of the operator</returns>
private String BinaryOperatorNodeToString(BinaryOperatorKind operatorKind)
{
switch (operatorKind)
{
case BinaryOperatorKind.Has:
return ExpressionConstants.KeywordHas;
case BinaryOperatorKind.Equal:
return ExpressionConstants.KeywordEqual;
case BinaryOperatorKind.NotEqual:
return ExpressionConstants.KeywordNotEqual;
case BinaryOperatorKind.GreaterThan:
return ExpressionConstants.KeywordGreaterThan;
case BinaryOperatorKind.GreaterThanOrEqual:
return ExpressionConstants.KeywordGreaterThanOrEqual;
case BinaryOperatorKind.LessThan:
return ExpressionConstants.KeywordLessThan;
case BinaryOperatorKind.LessThanOrEqual:
return ExpressionConstants.KeywordLessThanOrEqual;
// if current translated node is SearchNode, the BinaryOperator should return AND, OR; or return and, or.
case BinaryOperatorKind.And:
if (searchFlag)
{
return ExpressionConstants.SearchKeywordAnd;
}
return ExpressionConstants.KeywordAnd;
case BinaryOperatorKind.Or:
if (searchFlag)
{
return ExpressionConstants.SearchKeywordOr;
}
return ExpressionConstants.KeywordOr;
case BinaryOperatorKind.Add:
return ExpressionConstants.KeywordAdd;
case BinaryOperatorKind.Subtract:
return ExpressionConstants.KeywordSub;
case BinaryOperatorKind.Multiply:
return ExpressionConstants.KeywordMultiply;
case BinaryOperatorKind.Divide:
return ExpressionConstants.KeywordDivide;
case BinaryOperatorKind.Modulo:
return ExpressionConstants.KeywordModulo;
default:
return null;
}
}
/// <summary>
/// Get the priority of BinaryOperatorNode
/// This priority table is from http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part2-url-conventions.html (5.1.1.9 Operator Precedence )
/// </summary>
/// <param name="operatorKind">binary operator </param>
/// <returns>the priority value of the binary operator</returns>
private static int TranslateBinaryOperatorPriority(BinaryOperatorKind operatorKind)
{
switch (operatorKind)
{
case BinaryOperatorKind.Or:
return 1;
case BinaryOperatorKind.And:
return 2;
case BinaryOperatorKind.Equal:
case BinaryOperatorKind.NotEqual:
case BinaryOperatorKind.GreaterThan:
case BinaryOperatorKind.GreaterThanOrEqual:
case BinaryOperatorKind.LessThan:
case BinaryOperatorKind.LessThanOrEqual:
return 3;
case BinaryOperatorKind.Add:
case BinaryOperatorKind.Subtract:
return 4;
case BinaryOperatorKind.Divide:
case BinaryOperatorKind.Multiply:
case BinaryOperatorKind.Modulo:
return 5;
case BinaryOperatorKind.Has:
return 6;
default:
return -1;
}
}
/// <summary>
/// Judge a string text is a valid SearchWord or not ?
/// </summary>
/// <param name="text">string text to be judged</param>
/// <returns>if the string is a valid SearchWord, return true, or return false.</returns>
private static bool IsValidSearchWord(string text)
{
Match match = SearchLexer.InvalidWordPattern.Match(text);
if (match.Success ||
String.Equals(text, "AND", StringComparison.Ordinal) ||
String.Equals(text, "OR", StringComparison.Ordinal) ||
String.Equals(text, "NOT", StringComparison.Ordinal))
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// Check whether Navigation source of the FilterClause rangeVariable is different from the Expression rangeVariable.
/// </summary>
/// <param name="node">Expression node.</param>
/// <returns>If Navigation Source are different, returns true. Otherwise false.</returns>
private bool IsDifferentSource(QueryNode node)
{
ResourceRangeVariableReferenceNode rangeVariableReferenceNode = GetResourceRangeVariableReferenceNode(node);
if (rangeVariableReferenceNode == null)
{
return false;
}
if (rangeVariables.Count == 0)
{
return false;
}
RangeVariable rangeVariable = rangeVariables.Peek();
if (rangeVariable == null)
{
return false;
}
return rangeVariable is ResourceRangeVariable resourceRangeVariable ?
resourceRangeVariable.NavigationSource != rangeVariableReferenceNode.NavigationSource && rangeVariableReferenceNode.Name == ExpressionConstants.It : false;
}
/// <summary>
/// We return the <see cref="ResourceRangeVariableReferenceNode"/> within a <see cref="QueryNode"/>
/// </summary>
/// <param name="node">The node to extract the ResourceRangeVariableReferenceNode.</param>
/// <returns>The extracted ResourceRangeVariableReferenceNode.</returns>
private static ResourceRangeVariableReferenceNode GetResourceRangeVariableReferenceNode(QueryNode node)
{
if (node == null)
{
return null;
}
switch (node.Kind)
{
case QueryNodeKind.SingleValuePropertyAccess:
SingleValuePropertyAccessNode singleValuePropertyAccessNode = node as SingleValuePropertyAccessNode;
return GetResourceRangeVariableReferenceNode(singleValuePropertyAccessNode.Source);
case QueryNodeKind.Convert:
ConvertNode convertNode = node as ConvertNode;
return GetResourceRangeVariableReferenceNode(convertNode.Source);
case QueryNodeKind.Any:
AnyNode anyNode = node as AnyNode;
return GetResourceRangeVariableReferenceNode(anyNode.Source);
case QueryNodeKind.SingleValueFunctionCall:
SingleValueFunctionCallNode singleValueFunctionCallNode = node as SingleValueFunctionCallNode;
return GetResourceRangeVariableReferenceNode(singleValueFunctionCallNode.Parameters.First());
case QueryNodeKind.ResourceRangeVariableReference:
return node as ResourceRangeVariableReferenceNode;
case QueryNodeKind.SingleValueOpenPropertyAccess:
SingleValueOpenPropertyAccessNode singleValueOpenPropertyAccessNode = node as SingleValueOpenPropertyAccessNode;
return GetResourceRangeVariableReferenceNode(singleValueOpenPropertyAccessNode.Source);
case QueryNodeKind.SingleComplexNode:
SingleComplexNode singleComplexNode = node as SingleComplexNode;
return GetResourceRangeVariableReferenceNode(singleComplexNode.Source);
case QueryNodeKind.CollectionComplexNode:
CollectionComplexNode collectionComplexNode = node as CollectionComplexNode;
return GetResourceRangeVariableReferenceNode(collectionComplexNode.Source);
case QueryNodeKind.CollectionNavigationNode:
CollectionNavigationNode collectionNavigationNode = node as CollectionNavigationNode;
return GetResourceRangeVariableReferenceNode(collectionNavigationNode.Source);
case QueryNodeKind.SingleNavigationNode:
SingleNavigationNode singleNavigationNode = node as SingleNavigationNode;
return GetResourceRangeVariableReferenceNode(singleNavigationNode.Source);
case QueryNodeKind.CollectionResourceFunctionCall:
CollectionResourceFunctionCallNode collectionResourceFunctionCallNode = node as CollectionResourceFunctionCallNode;
return GetResourceRangeVariableReferenceNode(collectionResourceFunctionCallNode.Source);
case QueryNodeKind.SingleResourceFunctionCall:
SingleResourceFunctionCallNode singleResourceFunctionCallNode = node as SingleResourceFunctionCallNode;
return GetResourceRangeVariableReferenceNode(singleResourceFunctionCallNode.Source);
}
return null;
}
}
}