DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlNodeVisitor.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 {
32  public virtual void Visit(ISqlNode node) {
33  if (node is IntegerLiteralNode) {
34  VisitIntegerLiteral((IntegerLiteralNode) node);
35  } else if (node is NumberLiteralNode) {
36  VisitNumberLiteral((NumberLiteralNode) node);
37  } else if (node is StringLiteralNode) {
38  VisitStringLiteral((StringLiteralNode) node);
39  } else if (node is DataTypeNode) {
40  VisitDataType((DataTypeNode) node);
41  } else if (node is IExpressionNode) {
42  VisitExpression((IExpressionNode) node);
43  } else if (node is IStatementNode) {
44  VisitStatement((IStatementNode) node);
45  } else if (node is ISqlVisitableNode) {
46  ((ISqlVisitableNode) node).Accept(this);
47  }
48  }
49 
50  public virtual void VisitStringLiteral(StringLiteralNode node) {
51  }
52 
53  public virtual void VisitNumberLiteral(NumberLiteralNode node) {
54  }
55 
56  public virtual void VisitIntegerLiteral(IntegerLiteralNode node) {
57  }
58 
59  public virtual void VisitDataType(DataTypeNode node) {
60  }
61 
62  public virtual void VisitExpression(IExpressionNode node) {
63  if (node == null)
64  return;
65 
66  if (node is SqlConstantExpressionNode) {
67  VisitConstantExpression((SqlConstantExpressionNode) node);
68  } else if (node is SqlReferenceExpressionNode) {
69  VisitReferenceExpression((SqlReferenceExpressionNode) node);
70  } else if (node is SqlVariableRefExpressionNode) {
71  VisitVariableRefExpression((SqlVariableRefExpressionNode) node);
72  } else if (node is SqlBetweenExpressionNode) {
73  VisitBetweenExpression((SqlBetweenExpressionNode) node);
74  } else if (node is SqlCaseExpressionNode) {
75  VisitCaseExpression((SqlCaseExpressionNode) node);
76  } else if (node is SqlFunctionCallExpressionNode) {
77  VisitFunctionCall((SqlFunctionCallExpressionNode) node);
78  } else if (node is SqlExpressionTupleNode) {
79  VisitTupleExpression((SqlExpressionTupleNode) node);
80  } else if (node is SqlBinaryExpressionNode) {
81  VisitBinaryExpression((SqlBinaryExpressionNode) node);
82  } else if (node is SqlUnaryExpressionNode) {
83  VisitUnaryExpression((SqlUnaryExpressionNode) node);
84  } else if (node is SqlQueryExpressionNode) {
85  VisitQueryExpression((SqlQueryExpressionNode) node);
86  } else {
87  throw new InvalidOperationException(String.Format("The expression node of type '{0}' is invalid.", node.GetType()));
88  }
89  }
90 
91  public virtual void VisitNodeList(IEnumerable<ISqlNode> nodes) {
92  foreach (var node in nodes) {
93  Visit(node);
94  }
95  }
96 
97  #region Expressions
98 
99  public virtual void VisitQueryExpression(SqlQueryExpressionNode node) {
100  }
101 
102  public virtual void VisitTupleExpression(SqlExpressionTupleNode node) {
103  var exps = node.Expressions;
104  if (exps != null)
105  VisitNodeList(exps.Cast<ISqlNode>());
106  }
107 
108  public virtual void VisitUnaryExpression(SqlUnaryExpressionNode node) {
109  }
110 
111  public virtual void VisitBinaryExpression(SqlBinaryExpressionNode node) {
112  }
113 
115  }
116 
117  public virtual void VisitCaseExpression(SqlCaseExpressionNode node) {
118  }
119 
121  }
122 
124  }
125 
127  }
128 
130  }
131 
132  #endregion
133 
134  #region Statements
135 
136  public virtual void VisitStatement(IStatementNode node) {
137  if (node is CreateTableNode)
138  VisitCreateTable((CreateTableNode) node);
139  if (node is CreateViewNode)
140  VisitCreateView((CreateViewNode) node);
141  if (node is CreateTriggerNode) {
142  VisitCreateTrigger((CreateTriggerNode) node);
143  } else if (node is SelectStatementNode) {
144  VisitSelect((SelectStatementNode) node);
145  } else if (node is UpdateStatementNode) {
146  VisitUpdate((UpdateStatementNode) node);
147  } else if (node is InsertStatementNode) {
148  VisitInsert((InsertStatementNode) node);
149  }
150  }
151 
152  public virtual void VisitSelect(SelectStatementNode node) {
153  var exp = node.QueryExpression;
154  if (exp != null)
155  VisitExpression(exp);
156  }
157 
158  public virtual void VisitCreateTrigger(CreateTriggerNode node) {
159  if (node.ProcedureArguments != null)
160  VisitNodeList(node.ProcedureArguments);
161 
162  // TODO: handle the body
163  }
164 
165  public virtual void VisitCreateView(CreateViewNode node) {
166  }
167 
168  public virtual void VisitCreateTable(CreateTableNode node) {
169  if (node.Columns != null)
170  VisitTableColumns(node.Columns);
171  if (node.Constraints != null)
172  VisitTableConstraints(node.Constraints);
173  }
174 
175  public virtual void VisitAlterTable(AlterTableNode node) {
176  if (node.CreateTable != null)
177  VisitCreateTable(node.CreateTable);
178 
179  if (node.Actions != null) {
180  foreach (var action in node.Actions) {
181  VisitAlterTableAction(action);
182  }
183  }
184  }
185 
186  public virtual void VisitAlterTableAction(IAlterActionNode action) {
187  }
188 
189  public virtual void VisitTableConstraints(IEnumerable<TableConstraintNode> constraints) {
190  foreach (var constraint in constraints) {
191  VisitTableConstraint(constraint);
192  }
193  }
194 
195  public virtual void VisitTableConstraint(TableConstraintNode arg) {
196 
197  }
198 
199  public virtual void VisitTableColumns(IEnumerable<TableColumnNode> columnNodes) {
200  }
201 
202  public virtual void VisitUpdate(UpdateStatementNode node) {
203  if (node.SimpleUpdate != null)
204  VisitSimpleUpdate(node.SimpleUpdate);
205  if (node.QueryUpdate != null)
206  VisitQueryUpdate(node.QueryUpdate);
207  }
208 
209  public virtual void VisitSimpleUpdate(SimpleUpdateNode node) {
210 
211  }
212 
213  public virtual void VisitQueryUpdate(QueryUpdateNode node) {
214 
215  }
216 
217 
218  public virtual void VisitInsert(InsertStatementNode node) {
219  }
220 
221 
222  #endregion
223  }
224 }
virtual void VisitBetweenExpression(SqlBetweenExpressionNode node)
virtual void VisitNodeList(IEnumerable< ISqlNode > nodes)
An interface that allows implementations of the visitor pattern on SQL compiled nodes.
virtual void VisitNumberLiteral(NumberLiteralNode node)
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
virtual void VisitCreateTable(CreateTableNode node)
virtual void VisitReferenceExpression(SqlReferenceExpressionNode node)
virtual void VisitSelect(SelectStatementNode node)
IEnumerable< IAlterActionNode > Actions
virtual void VisitFunctionCall(SqlFunctionCallExpressionNode node)
virtual void VisitUpdate(UpdateStatementNode node)
An expression that encapsulates a unary operator for a given operand.
An SQL BETWEEN expression node that evaluates to true if the Expression given is between MinValue (in...
virtual void VisitTableConstraint(TableConstraintNode arg)
virtual void VisitTableConstraints(IEnumerable< TableConstraintNode > constraints)
Handles a numeric literal value, belonging to a wider group than integer numbers, spanning from real ...
An expression containing a set of other expressions.
virtual void VisitConstantExpression(SqlConstantExpressionNode node)
IEnumerable< TableColumnNode > Columns
virtual void VisitCreateTrigger(CreateTriggerNode node)
The root node of an expression used to select a set of items from a set of sources defined...
An implementation of ISqlNode that accepts visits from a ISqlNodeVisitor
An expression node that references an object within the database context (such as a table...
A node containing a constant literal string passed within an SQL command.
virtual void VisitIntegerLiteral(IntegerLiteralNode node)
virtual void VisitTableColumns(IEnumerable< TableColumnNode > columnNodes)
virtual void Visit(ISqlNode node)
Visits the given SQL node.
virtual void VisitSimpleUpdate(SimpleUpdateNode node)
virtual void VisitUnaryExpression(SqlUnaryExpressionNode node)
virtual void VisitQueryUpdate(QueryUpdateNode node)
virtual void VisitCaseExpression(SqlCaseExpressionNode node)
virtual void VisitExpression(IExpressionNode node)
References a variable within a SQL execution context.
virtual void VisitBinaryExpression(SqlBinaryExpressionNode node)
IEnumerable< TableConstraintNode > Constraints
virtual void VisitTupleExpression(SqlExpressionTupleNode node)
virtual void VisitStringLiteral(StringLiteralNode node)
virtual void VisitVariableRefExpression(SqlVariableRefExpressionNode node)
virtual void VisitAlterTableAction(IAlterActionNode action)
virtual void VisitQueryExpression(SqlQueryExpressionNode node)
virtual void VisitDataType(DataTypeNode node)
An node that represents a constant value set within a context of an SQL command.
Describes the information of a data type as found in a SQL string.
Definition: DataTypeNode.cs:28
virtual void VisitStatement(IStatementNode node)
virtual void VisitAlterTable(AlterTableNode node)
virtual void VisitCreateView(CreateViewNode node)
This interface acts like a marker that indicates if a ISqlNode represents a SQL expression.
Represents an expression that evaluates between two other expressions.
IEnumerable< IExpressionNode > Expressions
Gets a read-only list of expression that are contained within this tuple.
A node in a SQL command tree that is used to request a function.
virtual void VisitInsert(InsertStatementNode node)
Encapsulates a number that is any falling in the group of integers.
An SQL node describing an in-line CASE conditional expression.
The default implementation of a ISqlNodeVisitor that implements the visitor as a protected accessor...