Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Common / EntitySql / AST / QueryExpr.cs
1 //---------------------------------------------------------------------
2 // <copyright file="QueryExpr.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner  [....]
7 // @backupOwner [....]
8 //---------------------------------------------------------------------
9
10 namespace System.Data.Common.EntitySql.AST
11 {
12     using System;
13     using System.Globalization;
14     using System.Collections;
15     using System.Collections.Generic;
16
17     /// <summary>
18     /// Represents select kind (value,row).
19     /// </summary>
20     internal enum SelectKind
21     {
22         Value,
23         Row
24     }
25
26     /// <summary>
27     /// Represents join kind (cross,inner,leftouter,rightouter).
28     /// </summary>
29     internal enum JoinKind
30     {
31         Cross,
32         Inner,
33         LeftOuter,
34         FullOuter,
35         RightOuter
36     }
37
38     /// <summary>
39     /// Represents order kind (none=asc,asc,desc).
40     /// </summary>
41     internal enum OrderKind
42     {
43         None,
44         Asc,
45         Desc
46     }
47
48     /// <summary>
49     /// Represents distinct kind (none=all,all,distinct).
50     /// </summary>
51     internal enum DistinctKind
52     {
53         None,
54         All,
55         Distinct
56     }
57
58     /// <summary>
59     /// Represents apply kind (cross,outer).
60     /// </summary>
61     internal enum ApplyKind
62     {
63         Cross,
64         Outer
65     }
66
67     /// <summary>
68     /// Represents a query expression ast node.
69     /// </summary>
70     internal sealed class QueryExpr : Node
71     {
72         private readonly SelectClause _selectClause;
73         private readonly FromClause _fromClause;
74         private readonly Node _whereClause;
75         private readonly GroupByClause _groupByClause;
76         private readonly HavingClause _havingClause;
77         private readonly OrderByClause _orderByClause;
78
79         /// <summary>
80         /// Initializes a query expression ast node.
81         /// </summary>
82         /// <param name="selectClause">select clause</param>
83         /// <param name="fromClause">from clasuse</param>
84         /// <param name="whereClause">optional where clause</param>
85         /// <param name="groupByClause">optional group by clause</param>
86         /// <param name="havingClause">optional having clause</param>
87         /// <param name="orderByClause">optional order by clause</param>
88         internal QueryExpr(SelectClause selectClause,
89                            FromClause fromClause,
90                            Node whereClause,
91                            GroupByClause groupByClause,
92                            HavingClause havingClause,
93                            OrderByClause orderByClause)
94         {
95             _selectClause = selectClause;
96             _fromClause = fromClause;
97             _whereClause = whereClause;
98             _groupByClause = groupByClause;
99             _havingClause = havingClause;
100             _orderByClause = orderByClause;
101         }
102
103         /// <summary>
104         /// Returns select clause.
105         /// </summary>
106         internal SelectClause SelectClause
107         {
108             get { return _selectClause; }
109         }
110
111         /// <summary>
112         /// Returns from clause.
113         /// </summary>
114         internal FromClause FromClause
115         {
116             get { return _fromClause; }
117         }
118
119         /// <summary>
120         /// Returns optional where clause (expr).
121         /// </summary>
122         internal Node WhereClause
123         {
124             get { return _whereClause; }
125         }
126
127         /// <summary>
128         /// Returns optional group by clause.
129         /// </summary>
130         internal GroupByClause GroupByClause
131         {
132             get { return _groupByClause; }
133         }
134
135         /// <summary>
136         /// Returns optional having clause (expr).
137         /// </summary>
138         internal HavingClause HavingClause
139         {
140             get { return _havingClause; }
141         }
142
143         /// <summary>
144         /// Returns optional order by clause.
145         /// </summary>
146         internal OrderByClause OrderByClause
147         {
148             get { return _orderByClause; }
149         }
150
151         /// <summary>
152         /// Returns true if method calls are present.
153         /// </summary>
154         internal bool HasMethodCall
155         {
156             get
157             {
158                 return _selectClause.HasMethodCall ||
159                        (null != _havingClause && _havingClause.HasMethodCall) ||
160                        (null != _orderByClause && _orderByClause.HasMethodCall);
161             }
162         }
163     }
164
165     /// <summary>
166     /// Represents select clause.
167     /// </summary>
168     internal sealed class SelectClause : Node
169     {
170         private readonly NodeList<AliasedExpr> _selectClauseItems;
171         private readonly SelectKind _selectKind;
172         private readonly DistinctKind _distinctKind;
173         private readonly Node _topExpr;
174         private readonly uint _methodCallCount;
175
176         /// <summary>
177         /// Initialize SelectKind.SelectRow clause.
178         /// </summary>
179         internal SelectClause(NodeList<AliasedExpr> items, SelectKind selectKind, DistinctKind distinctKind, Node topExpr, uint methodCallCount)
180         {
181             _selectKind = selectKind;
182             _selectClauseItems = items;
183             _distinctKind = distinctKind;
184             _topExpr = topExpr;
185             _methodCallCount = methodCallCount;
186         }
187
188         /// <summary>
189         /// Projection list.
190         /// </summary>
191         internal NodeList<AliasedExpr> Items
192         {
193             get { return _selectClauseItems; }
194         }
195
196         /// <summary>
197         /// Select kind (row or value).
198         /// </summary>
199         internal SelectKind SelectKind
200         {
201             get { return _selectKind; }
202         }
203
204         /// <summary>
205         /// Distinct kind (none,all,distinct).
206         /// </summary>
207         internal DistinctKind DistinctKind
208         {
209             get { return _distinctKind; }
210         }
211
212         /// <summary>
213         /// Optional top expression.
214         /// </summary>
215         internal Node TopExpr
216         {
217             get { return _topExpr; }
218         }
219
220         /// <summary>
221         /// True if select list has method calls.
222         /// </summary>
223         internal bool HasMethodCall
224         {
225             get { return (_methodCallCount > 0); }
226         }
227     }
228
229     /// <summary>
230     /// Represents from clause.
231     /// </summary>
232     internal sealed class FromClause : Node
233     {
234         private readonly NodeList<FromClauseItem> _fromClauseItems;
235
236         /// <summary>
237         /// Initializes from clause.
238         /// </summary>
239         internal FromClause(NodeList<FromClauseItem> fromClauseItems)
240         {
241             _fromClauseItems = fromClauseItems;
242         }
243
244         /// <summary>
245         /// List of from clause items.
246         /// </summary>
247         internal NodeList<FromClauseItem> FromClauseItems
248         {
249             get { return _fromClauseItems; }
250         }
251     }
252
253     /// <summary>
254     /// From clause item kind.
255     /// </summary>
256     internal enum FromClauseItemKind
257     {
258         AliasedFromClause,
259         JoinFromClause,
260         ApplyFromClause
261     }
262
263     /// <summary>
264     /// Represents single from clause item.
265     /// </summary>
266     internal sealed class FromClauseItem : Node
267     {
268         private readonly Node _fromClauseItemExpr;
269         private readonly FromClauseItemKind _fromClauseItemKind;
270
271         /// <summary>
272         /// Initializes as 'simple' aliased expression.
273         /// </summary>
274         internal FromClauseItem(AliasedExpr aliasExpr)
275         {
276             _fromClauseItemExpr = aliasExpr;
277             _fromClauseItemKind = FromClauseItemKind.AliasedFromClause;
278         }
279
280         /// <summary>
281         /// Initializes as join clause item.
282         /// </summary>
283         internal FromClauseItem(JoinClauseItem joinClauseItem)
284         {
285             _fromClauseItemExpr = joinClauseItem;
286             _fromClauseItemKind = FromClauseItemKind.JoinFromClause;
287         }
288
289         /// <summary>
290         /// Initializes as apply clause item.
291         /// </summary>
292         internal FromClauseItem(ApplyClauseItem applyClauseItem)
293         {
294             _fromClauseItemExpr = applyClauseItem;
295             _fromClauseItemKind = FromClauseItemKind.ApplyFromClause;
296         }
297
298         /// <summary>
299         /// From clause item expression.
300         /// </summary>
301         internal Node FromExpr
302         {
303             get { return _fromClauseItemExpr; }
304         }
305
306         /// <summary>
307         /// From clause item kind (alias,join,apply).
308         /// </summary>
309         internal FromClauseItemKind FromClauseItemKind
310         {
311             get { return _fromClauseItemKind; }
312         }
313     }
314
315     /// <summary>
316     /// Represents group by clause.
317     /// </summary>
318     internal sealed class GroupByClause : Node
319     {
320         private readonly NodeList<AliasedExpr> _groupItems;
321
322         /// <summary>
323         /// Initializes GROUP BY clause
324         /// </summary>
325         internal GroupByClause(NodeList<AliasedExpr> groupItems)
326         {
327             _groupItems = groupItems;
328         }
329
330         /// <summary>
331         /// Group items.
332         /// </summary>
333         internal NodeList<AliasedExpr> GroupItems
334         {
335             get { return _groupItems; }
336         }
337     }
338
339     /// <summary>
340     /// Represents having clause.
341     /// </summary>
342     internal sealed class HavingClause : Node
343     {
344         private readonly Node _havingExpr;
345         private readonly uint _methodCallCount;
346
347         /// <summary>
348         /// Initializes having clause.
349         /// </summary>
350         internal HavingClause(Node havingExpr, uint methodCallCounter)
351         {
352             _havingExpr = havingExpr;
353             _methodCallCount = methodCallCounter;
354         }
355
356         /// <summary>
357         /// Returns having inner expression.
358         /// </summary>
359         internal Node HavingPredicate
360         {
361             get { return _havingExpr; }
362         }
363
364         /// <summary>
365         /// True if predicate has method calls.
366         /// </summary>
367         internal bool HasMethodCall
368         {
369             get { return (_methodCallCount > 0); }
370         }
371     }
372
373     /// <summary>
374     /// Represents order by clause.
375     /// </summary>
376     internal sealed class OrderByClause : Node
377     {
378         private readonly NodeList<OrderByClauseItem> _orderByClauseItem;
379         private readonly Node _skipExpr;
380         private readonly Node _limitExpr;
381         private readonly uint _methodCallCount;
382
383         /// <summary>
384         /// Initializes order by clause.
385         /// </summary>
386         internal OrderByClause(NodeList<OrderByClauseItem> orderByClauseItem, Node skipExpr, Node limitExpr, uint methodCallCount)
387         {
388             _orderByClauseItem = orderByClauseItem;
389             _skipExpr = skipExpr;
390             _limitExpr = limitExpr;
391             _methodCallCount = methodCallCount;
392         }
393
394         /// <summary>
395         /// Returns order by clause items.
396         /// </summary>
397         internal NodeList<OrderByClauseItem> OrderByClauseItem
398         {
399             get { return _orderByClauseItem; }
400         }
401
402         /// <summary>
403         /// Returns skip sub clause ast node.
404         /// </summary>
405         internal Node SkipSubClause
406         {
407             get { return _skipExpr; }
408         }
409
410         /// <summary>
411         /// Returns limit sub-clause ast node.
412         /// </summary>
413         internal Node LimitSubClause
414         {
415             get { return _limitExpr; }
416         }
417
418         /// <summary>
419         /// True if order by has method calls.
420         /// </summary>
421         internal bool HasMethodCall
422         {
423             get { return (_methodCallCount > 0); }
424         }
425     }
426
427     /// <summary>
428     /// Represents a order by clause item.
429     /// </summary>
430     internal sealed class OrderByClauseItem : Node
431     {
432         private readonly Node _orderExpr;
433         private readonly OrderKind _orderKind;
434         private readonly Identifier _optCollationIdentifier;
435
436         /// <summary>
437         /// Initializes non-collated order by clause item.
438         /// </summary>
439         internal OrderByClauseItem(Node orderExpr, OrderKind orderKind)
440             : this(orderExpr, orderKind, null)
441         {
442         }
443
444         /// <summary>
445         /// Initializes collated order by clause item.
446         /// </summary>
447         /// <param name="optCollationIdentifier">optional Collation identifier</param>
448         internal OrderByClauseItem(Node orderExpr, OrderKind orderKind, Identifier optCollationIdentifier)
449         {
450             _orderExpr = orderExpr;
451             _orderKind = orderKind;
452             _optCollationIdentifier = optCollationIdentifier;
453         }
454
455         /// <summary>
456         /// Oeturns order expression.
457         /// </summary>
458         internal Node OrderExpr
459         {
460             get { return _orderExpr; }
461         }
462
463         /// <summary>
464         /// Returns order kind (none,asc,desc).
465         /// </summary>
466         internal OrderKind OrderKind
467         {
468             get { return _orderKind; }
469         }
470
471         /// <summary>
472         /// Returns collattion identifier if one exists.
473         /// </summary>
474         internal Identifier Collation
475         {
476             get { return _optCollationIdentifier; }
477         }
478     }
479
480     /// <summary>
481     /// Represents join clause item.
482     /// </summary>
483     internal sealed class JoinClauseItem : Node
484     {
485         private readonly FromClauseItem _joinLeft;
486         private readonly FromClauseItem _joinRight;
487         private JoinKind _joinKind;
488         private readonly Node _onExpr;
489
490         /// <summary>
491         /// Initializes join clause item without ON expression.
492         /// </summary>
493         internal JoinClauseItem(FromClauseItem joinLeft, FromClauseItem joinRight, JoinKind joinKind)
494             : this(joinLeft, joinRight, joinKind, null)
495         {
496         }
497
498         /// <summary>
499         /// Initializes join clause item with ON expression.
500         /// </summary>
501         internal JoinClauseItem(FromClauseItem joinLeft, FromClauseItem joinRight, JoinKind joinKind, Node onExpr)
502         {
503             _joinLeft = joinLeft;
504             _joinRight = joinRight;
505             _joinKind = joinKind;
506             _onExpr = onExpr;
507         }
508
509         /// <summary>
510         /// Returns join left expression.
511         /// </summary>
512         internal FromClauseItem LeftExpr
513         {
514             get { return _joinLeft; }
515         }
516
517         /// <summary>
518         /// Returns join right expression.
519         /// </summary>
520         internal FromClauseItem RightExpr
521         {
522             get { return _joinRight; }
523         }
524
525         /// <summary>
526         /// Join kind (cross, inner, full, left outer,right outer).
527         /// </summary>
528         internal JoinKind JoinKind
529         {
530             get { return _joinKind; }
531             set { _joinKind = value; }
532         }
533
534         /// <summary>
535         /// Returns join on expression.
536         /// </summary>
537         internal Node OnExpr
538         {
539             get { return _onExpr; }
540         }
541     }
542
543     /// <summary>
544     /// Represents apply expression.
545     /// </summary>
546     internal sealed class ApplyClauseItem : Node
547     {
548         private readonly FromClauseItem _applyLeft;
549         private readonly FromClauseItem _applyRight;
550         private readonly ApplyKind _applyKind;
551
552         /// <summary>
553         /// Initializes apply clause item.
554         /// </summary>
555         internal ApplyClauseItem(FromClauseItem applyLeft, FromClauseItem applyRight, ApplyKind applyKind)
556         {
557             _applyLeft = applyLeft;
558             _applyRight = applyRight;
559             _applyKind = applyKind;
560         }
561
562         /// <summary>
563         /// Returns apply left expression.
564         /// </summary>
565         internal FromClauseItem LeftExpr
566         {
567             get { return _applyLeft; }
568         }
569
570         /// <summary>
571         /// Returns apply right expression.
572         /// </summary>
573         internal FromClauseItem RightExpr
574         {
575             get { return _applyRight; }
576         }
577
578         /// <summary>
579         /// Returns apply kind (cross,outer).
580         /// </summary>
581         internal ApplyKind ApplyKind
582         {
583             get { return _applyKind; }
584         }
585     }
586 }