DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlQueryExpressionNode.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 
21 namespace Deveel.Data.Sql.Parser {
28  }
29 
34  public bool IsAll { get; private set; }
35 
40  public bool IsDistinct { get; private set; }
41 
42  public SqlReferenceExpressionNode IntoClause { get; private set; }
43 
48  public IEnumerable<SelectItemNode> SelectItems { get; private set; }
49 
53  public FromClauseNode FromClause { get; private set; }
54 
58  public IExpressionNode WhereExpression { get; private set; }
59 
64  public GroupByNode GroupBy { get; private set; }
65 
71  public QueryCompositeNode Composite { get; private set; }
72 
74  protected override ISqlNode OnChildNode(ISqlNode node) {
75  if (node.NodeName == "select_restrict_opt") {
76  GetRestrict(node);
77  } else if (node.NodeName == "select_set") {
78  GetSelectSet(node);
79  } else if (node.NodeName == "from_clause_opt") {
80  var clause = node.ChildNodes.FirstOrDefault();
81  if (clause != null)
82  FromClause = (FromClauseNode) clause;
83  } else if (node.NodeName == "where_clause_opt") {
84  GetWhereClause(node);
85  } else if (node.NodeName == "group_by_opt") {
86  GroupBy = node.ChildNodes.FirstOrDefault() as GroupByNode;
87  } else if (node.NodeName == "query_composite_opt") {
88  var composite = node.ChildNodes.FirstOrDefault();
89  if (composite != null)
90  Composite = (QueryCompositeNode) composite;
91  } else if (node.NodeName == "select_into_opt") {
92  // TODO:
93  }
94 
95  return base.OnChildNode(node);
96  }
97 
98  private void GetWhereClause(ISqlNode node) {
99  foreach (var childNode in node.ChildNodes) {
100  if (childNode is IExpressionNode) {
101  WhereExpression = childNode as IExpressionNode;
102  break;
103  }
104  }
105  }
106 
107  private void GetRestrict(ISqlNode node) {
108  foreach (var childNode in node.ChildNodes) {
109  if (childNode is SqlKeyNode) {
110  var value = ((SqlKeyNode) childNode).Text;
111  if (value == "ALL") {
112  IsAll = true;
113  } else if (value == "DISTINCT") {
114  IsDistinct = true;
115  }
116  }
117  }
118  }
119 
120  private void GetSelectSet(ISqlNode node) {
121  foreach (var childNode in node.ChildNodes) {
122  if (childNode is SqlKeyNode &&
123  ((SqlKeyNode) childNode).Text == "*") {
124  IsAll = true;
125  } else if (childNode.NodeName == "select_item_list") {
126  GetSelectItems(childNode);
127  }
128  }
129  }
130 
131  private void GetSelectItems(ISqlNode node) {
132  var items = new List<SelectItemNode>();
133  foreach (var childNode in node.ChildNodes) {
134  if (childNode is SelectItemNode)
135  items.Add((SelectItemNode)childNode);
136  }
137 
138  SelectItems = items.ToArray();
139  }
140  }
141 }
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
Represents a keyword found during the compilation of a source text.
Definition: SqlKeyNode.cs:25
string NodeName
Gets the name of the node analyzed from the parser.
Definition: ISqlNode.cs:29
The node in an SQL query that defines the sources from which to retrieve the data queried...
The root node of an expression used to select a set of items from a set of sources defined...
An expression node that references an object within the database context (such as a table...
IEnumerable< ISqlNode > ChildNodes
Gets a read-only enumeration of the children nodes, if any.
Definition: ISqlNode.cs:39
A single item selected within a query node tree.
A node that describes the GROUP BY clause in a SQL query.
Definition: GroupByNode.cs:25
Composes two queries to obtain a set that is the result of a given composition function.
This interface acts like a marker that indicates if a ISqlNode represents a SQL expression.
override ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
The default implementation of ISqlNode, that is a node in the text analysis parsing of SQL commands...
Definition: SqlNode.cs:32