DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Static Public Member Functions | Static Private Member Functions | List of all members
Deveel.Data.Sql.Parser.ExpressionBuilder Class Reference

Static Public Member Functions

static SqlExpression Build (IExpressionNode node)
 

Static Private Member Functions

static SqlExpression VisitVariableRefExpression (SqlVariableRefExpressionNode node)
 
static SqlExpression VisitTupleExpression (SqlExpressionTupleNode node)
 
static SqlExpression VisitQueryExpression (SqlQueryExpressionNode node)
 
static CompositeFunction GetCompositeFunction (string s)
 
static void SetFromTableInClause (FromClause clause, IFromSourceNode source, JoinNode join)
 
static void AddSourceToClause (FromClause clause, IFromSourceNode source)
 
static JoinType GetJoinType (string typeName)
 
static void SetFromClause (FromClause clause, FromClauseNode node)
 
static IEnumerable< SelectColumnGetSelectColumns (SqlQueryExpressionNode node)
 
static SqlExpression VisitCaseExpression (SqlCaseExpressionNode expressionNode)
 
static SqlExpression VisitConstantExpression (SqlConstantExpressionNode expressionNode)
 
static SqlExpression VisitReferenceExpression (SqlReferenceExpressionNode node)
 
static SqlExpression VisitFunctionCall (SqlFunctionCallExpressionNode node)
 
static SqlExpression VisitBetweenExpression (SqlBetweenExpressionNode expressionNode)
 
static SqlExpression VisitBinaryExpression (SqlBinaryExpressionNode expressionNode)
 
static SqlExpressionType GetBinaryExpressionType (string op)
 
static SqlExpression VisitUnaryExpression (SqlUnaryExpressionNode expressionNode)
 
static SqlExpressionType GetUnaryExpressionType (string op)
 

Detailed Description

Definition at line 26 of file ExpressionBuilder.cs.

Member Function Documentation

static void Deveel.Data.Sql.Parser.ExpressionBuilder.AddSourceToClause ( FromClause  clause,
IFromSourceNode  source 
)
inlinestaticprivate

Definition at line 124 of file ExpressionBuilder.cs.

124  {
125  string alias = null;
126  if (source.Alias != null)
127  alias = source.Alias.Text;
128 
129  if (source is FromTableSourceNode) {
130  var tableSource = (FromTableSourceNode)source;
131  clause.AddTable(alias, tableSource.TableName.Name);
132  } else if (source is FromQuerySourceNode) {
133  var querySource = (FromQuerySourceNode)source;
134  var queryExpression = (SqlQueryExpression) Build(querySource.Query);
135  clause.AddSubQuery(alias, queryExpression);
136  }
137  }
void AddTable(string alias, FromTable table)
Adds a table as source to the query with a given alias.
Definition: FromClause.cs:93
static SqlExpression Build(IExpressionNode node)
void AddSubQuery(SqlQueryExpression subQuery)
Adds a sub-query expression as source of the query.
Definition: FromClause.cs:134
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.Build ( IExpressionNode  node)
inlinestatic

Definition at line 27 of file ExpressionBuilder.cs.

27  {
28  if (node is SqlVariableRefExpressionNode)
29  return VisitVariableRefExpression((SqlVariableRefExpressionNode) node);
30  if (node is SqlExpressionTupleNode)
31  return VisitTupleExpression((SqlExpressionTupleNode) node);
32  if (node is SqlQueryExpressionNode)
33  return VisitQueryExpression((SqlQueryExpressionNode) node);
34  if (node is SqlCaseExpressionNode)
35  return VisitCaseExpression((SqlCaseExpressionNode) node);
36  if (node is SqlConstantExpressionNode)
37  return VisitConstantExpression((SqlConstantExpressionNode) node);
38  if (node is SqlFunctionCallExpressionNode)
39  return VisitFunctionCall((SqlFunctionCallExpressionNode) node);
40  if (node is SqlReferenceExpressionNode)
41  return VisitReferenceExpression((SqlReferenceExpressionNode) node);
42  if (node is SqlBinaryExpressionNode)
43  return VisitBinaryExpression((SqlBinaryExpressionNode) node);
44  if (node is SqlUnaryExpressionNode)
45  return VisitUnaryExpression((SqlUnaryExpressionNode) node);
46  if (node is SqlBetweenExpressionNode)
47  return VisitBetweenExpression((SqlBetweenExpressionNode) node);
48 
49  throw new NotSupportedException();
50  }
static SqlExpression VisitConstantExpression(SqlConstantExpressionNode expressionNode)
static SqlExpression VisitBetweenExpression(SqlBetweenExpressionNode expressionNode)
static SqlExpression VisitFunctionCall(SqlFunctionCallExpressionNode node)
static SqlExpression VisitUnaryExpression(SqlUnaryExpressionNode expressionNode)
static SqlExpression VisitReferenceExpression(SqlReferenceExpressionNode node)
static SqlExpression VisitTupleExpression(SqlExpressionTupleNode node)
static SqlExpression VisitCaseExpression(SqlCaseExpressionNode expressionNode)
static SqlExpression VisitVariableRefExpression(SqlVariableRefExpressionNode node)
static SqlExpression VisitBinaryExpression(SqlBinaryExpressionNode expressionNode)
static SqlExpression VisitQueryExpression(SqlQueryExpressionNode node)
static SqlExpressionType Deveel.Data.Sql.Parser.ExpressionBuilder.GetBinaryExpressionType ( string  op)
inlinestaticprivate

Definition at line 248 of file ExpressionBuilder.cs.

248  {
249  if (op == "+" ||
250  op == "||")
251  return SqlExpressionType.Add;
252  if (op == "-")
253  return SqlExpressionType.Subtract;
254  if (op == "*")
255  return SqlExpressionType.Multiply;
256  if (op == "/")
257  return SqlExpressionType.Divide;
258  if (op == "%" ||
259  String.Equals(op, "MOD", StringComparison.OrdinalIgnoreCase))
260  return SqlExpressionType.Modulo;
261  if (op == "=")
262  return SqlExpressionType.Equal;
263  if (op == "<>")
264  return SqlExpressionType.NotEqual;
265  if (op == ">")
266  return SqlExpressionType.GreaterThan;
267  if (op == ">=")
268  return SqlExpressionType.GreaterOrEqualThan;
269  if (op == "<")
270  return SqlExpressionType.SmallerThan;
271  if (op == "<=")
272  return SqlExpressionType.SmallerOrEqualThan;
273  if (String.Equals(op, "LIKE", StringComparison.OrdinalIgnoreCase))
274  return SqlExpressionType.Like;
275  if (String.Equals(op, "NOT LIKE", StringComparison.OrdinalIgnoreCase))
276  return SqlExpressionType.NotLike;
277 
278  if (String.Equals(op, "IS", StringComparison.OrdinalIgnoreCase))
279  return SqlExpressionType.Is;
280  if (String.Equals(op, "IS NOT", StringComparison.OrdinalIgnoreCase))
281  return SqlExpressionType.IsNot;
282 
283  if (String.Equals(op, "AND", StringComparison.OrdinalIgnoreCase))
284  return SqlExpressionType.And;
285  if (String.Equals(op, "OR", StringComparison.OrdinalIgnoreCase))
286  return SqlExpressionType.Or;
287 
288  if (String.Equals(op, "IN", StringComparison.OrdinalIgnoreCase))
289  return SqlExpressionType.AnyEqual;
290  if (String.Equals(op, "NOT IN", StringComparison.OrdinalIgnoreCase))
291  return SqlExpressionType.AllNotEqual;
292 
293  throw new ArgumentException(String.Format("The operator {0} is not a binary one.", op));
294  }
A long string in the system.
SqlExpressionType
All the possible type of SqlExpression supported
static CompositeFunction Deveel.Data.Sql.Parser.ExpressionBuilder.GetCompositeFunction ( string  s)
inlinestaticprivate

Definition at line 95 of file ExpressionBuilder.cs.

95  {
96  if (String.Equals(s, "UNION", StringComparison.OrdinalIgnoreCase))
97  return CompositeFunction.Union;
98  if (String.Equals(s, "EXCEPT", StringComparison.OrdinalIgnoreCase))
99  return CompositeFunction.Except;
100  if (String.Equals(s, "INTERSECT", StringComparison.OrdinalIgnoreCase))
101  return CompositeFunction.Intersect;
102 
103  throw new InvalidOperationException(String.Format("Composite function {0} is invalid.", s));
104  }
A long string in the system.
CompositeFunction
The kind of composite function in a CompositeTable.
static JoinType Deveel.Data.Sql.Parser.ExpressionBuilder.GetJoinType ( string  typeName)
inlinestaticprivate

Definition at line 139 of file ExpressionBuilder.cs.

139  {
140  if (String.Equals(typeName, "INNER", StringComparison.OrdinalIgnoreCase) ||
141  String.Equals(typeName, "INNER JOIN", StringComparison.OrdinalIgnoreCase) ||
142  String.Equals(typeName, ",", StringComparison.OrdinalIgnoreCase))
143  return JoinType.Inner;
144  if (String.Equals(typeName, "LEFT OUTER", StringComparison.OrdinalIgnoreCase) ||
145  String.Equals(typeName, "LFT OUTER JOIN", StringComparison.OrdinalIgnoreCase) ||
146  String.Equals(typeName, "LEFT", StringComparison.OrdinalIgnoreCase) ||
147  String.Equals(typeName, "LFT JOIN", StringComparison.OrdinalIgnoreCase))
148  return JoinType.Left;
149  if (String.Equals(typeName, "RIGHT OUTER", StringComparison.OrdinalIgnoreCase) ||
150  String.Equals(typeName, "RIGHT OUTER JOIN", StringComparison.OrdinalIgnoreCase) ||
151  String.Equals(typeName, "RIGHT", StringComparison.OrdinalIgnoreCase) ||
152  String.Equals(typeName, "RIGHT JOIN", StringComparison.OrdinalIgnoreCase))
153  return JoinType.Right;
154 
155  return JoinType.None;
156  }
A long string in the system.
JoinType
Enumerates the kind of group join in a selection query.
Definition: JoinType.cs:23
static IEnumerable<SelectColumn> Deveel.Data.Sql.Parser.ExpressionBuilder.GetSelectColumns ( SqlQueryExpressionNode  node)
inlinestaticprivate

Definition at line 162 of file ExpressionBuilder.cs.

162  {
163  if (node.IsAll) {
164  return new[] {new SelectColumn(SqlExpression.Reference(new ObjectName("*")))};
165  }
166 
167  var items = new List<SelectColumn>();
168  foreach (var item in node.SelectItems) {
169  SqlExpression exp;
170  if (item.Name != null) {
171  exp = SqlExpression.Reference(ObjectName.Parse(item.Name.Name));
172  } else if (item.Expression != null) {
173  exp = Build(item.Expression);
174  } else {
175  throw new InvalidOperationException();
176  }
177 
178  string alias = null;
179  if (item.Alias != null)
180  alias = item.Alias.Text;
181 
182  items.Add(new SelectColumn(exp, alias));
183  }
184 
185  return items.ToArray();
186  }
static SqlExpression Build(IExpressionNode node)
Represents a column selected to be in the output of a select statement.
Definition: SelectColumn.cs:31
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlExpressionType Deveel.Data.Sql.Parser.ExpressionBuilder.GetUnaryExpressionType ( string  op)
inlinestaticprivate

Definition at line 303 of file ExpressionBuilder.cs.

303  {
304  if (op == "+")
305  return SqlExpressionType.UnaryPlus;
306  if (op == "-")
307  return SqlExpressionType.Negate;
308  if (String.Equals(op, "NOT", StringComparison.OrdinalIgnoreCase))
309  return SqlExpressionType.Not;
310 
311  throw new ArgumentException(String.Format("The operator {0} is not a unary one.", op));
312  }
A long string in the system.
SqlExpressionType
All the possible type of SqlExpression supported
static void Deveel.Data.Sql.Parser.ExpressionBuilder.SetFromClause ( FromClause  clause,
FromClauseNode  node 
)
inlinestaticprivate

Definition at line 158 of file ExpressionBuilder.cs.

158  {
159  SetFromTableInClause(clause, node.Source, node.Join);
160  }
void Join(JoinType joinType, SqlExpression onExpression)
Sets a join between the last added table and the one that preceeds it.
Definition: FromClause.cs:154
static void SetFromTableInClause(FromClause clause, IFromSourceNode source, JoinNode join)
static void Deveel.Data.Sql.Parser.ExpressionBuilder.SetFromTableInClause ( FromClause  clause,
IFromSourceNode  source,
JoinNode  join 
)
inlinestaticprivate

Definition at line 106 of file ExpressionBuilder.cs.

106  {
107  AddSourceToClause(clause, source);
108 
109  if (join != null) {
110  var joinType = JoinType.Inner;
111  if (!String.IsNullOrEmpty(join.JoinType))
112  joinType = GetJoinType(join.JoinType);
113 
114  SqlExpression onExpression = null;
115  if (join.OnExpression != null)
116  onExpression = Build(join.OnExpression);
117 
118  clause.Join(joinType, onExpression);
119 
120  SetFromTableInClause(clause, join.Source, join.NextJoin);
121  }
122  }
static JoinType GetJoinType(string typeName)
A long string in the system.
JoinType
Enumerates the kind of group join in a selection query.
Definition: JoinType.cs:23
static SqlExpression Build(IExpressionNode node)
void Join(JoinType joinType, SqlExpression onExpression)
Sets a join between the last added table and the one that preceeds it.
Definition: FromClause.cs:154
static void AddSourceToClause(FromClause clause, IFromSourceNode source)
Defines the base class for instances that represent SQL expression tree nodes.
static void SetFromTableInClause(FromClause clause, IFromSourceNode source, JoinNode join)
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitBetweenExpression ( SqlBetweenExpressionNode  expressionNode)
inlinestaticprivate

Definition at line 222 of file ExpressionBuilder.cs.

222  {
223  var testExp = Build(expressionNode.Expression);
224  var minValue = Build(expressionNode.MinValue);
225  var maxValue = Build(expressionNode.MaxValue);
226 
227  var smallerExp = SqlExpression.SmallerOrEqualThan(testExp, maxValue);
228  var greaterExp = SqlExpression.GreaterOrEqualThan(testExp, minValue);
229 
230  SqlExpression exp = SqlExpression.And(smallerExp, greaterExp);
231 
232  if (expressionNode.Not)
233  exp = SqlExpression.Not(exp);
234 
235  return exp;
236  }
static SqlBinaryExpression And(SqlExpression left, SqlExpression right)
static SqlBinaryExpression SmallerOrEqualThan(SqlExpression left, SqlExpression right)
static SqlExpression Build(IExpressionNode node)
static SqlUnaryExpression Not(SqlExpression operand)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlBinaryExpression GreaterOrEqualThan(SqlExpression left, SqlExpression right)
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitBinaryExpression ( SqlBinaryExpressionNode  expressionNode)
inlinestaticprivate

Definition at line 238 of file ExpressionBuilder.cs.

238  {
239  var left = Build(expressionNode.Left);
240  var right = Build(expressionNode.Right);
241  var op = expressionNode.Operator;
242 
243  var expType = GetBinaryExpressionType(op);
244 
245  return SqlExpression.Binary(left, expType, right);
246  }
static SqlExpression Build(IExpressionNode node)
static SqlBinaryExpression Binary(SqlExpression left, SqlExpressionType expressionType, SqlExpression right)
static SqlExpressionType GetBinaryExpressionType(string op)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitCaseExpression ( SqlCaseExpressionNode  expressionNode)
inlinestaticprivate

Definition at line 188 of file ExpressionBuilder.cs.

188  {
189  throw new NotImplementedException();
190  }
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitConstantExpression ( SqlConstantExpressionNode  expressionNode)
inlinestaticprivate

Definition at line 192 of file ExpressionBuilder.cs.

192  {
193  var sqlValue = expressionNode.Value;
194  DataObject obj;
195  if (sqlValue is SqlString) {
196  obj = DataObject.VarChar((SqlString) sqlValue);
197  } else if (sqlValue is SqlBoolean) {
198  obj = DataObject.Boolean((SqlBoolean) sqlValue);
199  } else if (sqlValue is SqlNumber) {
200  obj = DataObject.Number((SqlNumber) sqlValue);
201  } else if (sqlValue is SqlNull) {
202  obj = DataObject.Null();
203  } else {
204  throw new NotSupportedException("Constant value not supported.");
205  }
206 
207  return SqlExpression.Constant(obj);
208  }
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitFunctionCall ( SqlFunctionCallExpressionNode  node)
inlinestaticprivate

Definition at line 214 of file ExpressionBuilder.cs.

214  {
215  var args = new List<SqlExpression>();
216  if (node.Arguments != null)
217  args.AddRange(node.Arguments.Select(Build));
218 
219  return SqlExpression.FunctionCall(node.FunctionName, args.ToArray());
220  }
static SqlExpression Build(IExpressionNode node)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlFunctionCallExpression FunctionCall(ObjectName functionName)
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitQueryExpression ( SqlQueryExpressionNode  node)
inlinestaticprivate

Definition at line 60 of file ExpressionBuilder.cs.

60  {
61  var selectColumns = GetSelectColumns(node);
62  var exp = new SqlQueryExpression(selectColumns);
63 
64  if (node.FromClause != null) {
65  SetFromClause(exp.FromClause, node.FromClause);
66  }
67 
68  if (node.WhereExpression != null) {
69  exp.WhereExpression = Build(node.WhereExpression);
70  }
71 
72  if (node.GroupBy != null) {
73  var groupBy = new List<SqlExpression>();
74  if (node.GroupBy.GroupExpressions != null)
75  groupBy.AddRange(node.GroupBy.GroupExpressions.Select(Build));
76 
77  exp.GroupBy = groupBy.ToList();
78 
79  var having = node.GroupBy.HavingExpression;
80  if (having != null)
81  exp.HavingExpression = Build(having);
82 
83  }
84 
85  if (node.Composite != null) {
86  var compositeExp = Build(node.Composite.QueryExpression);
87  exp.NextComposite = compositeExp as SqlQueryExpression;
88  exp.IsCompositeAll = node.Composite.IsAll;
89  exp.CompositeFunction = GetCompositeFunction(node.Composite.CompositeFunction);
90  }
91 
92  return exp;
93  }
static IEnumerable< SelectColumn > GetSelectColumns(SqlQueryExpressionNode node)
static CompositeFunction GetCompositeFunction(string s)
static SqlExpression Build(IExpressionNode node)
static void SetFromClause(FromClause clause, FromClauseNode node)
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitReferenceExpression ( SqlReferenceExpressionNode  node)
inlinestaticprivate

Definition at line 210 of file ExpressionBuilder.cs.

210  {
211  return SqlExpression.Reference(ObjectName.Parse(node.Reference.Name));
212  }
static SqlReferenceExpression Reference(ObjectName objectName)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitTupleExpression ( SqlExpressionTupleNode  node)
inlinestaticprivate

Definition at line 56 of file ExpressionBuilder.cs.

56  {
57  return SqlExpression.Tuple(node.Expressions.Select(Build).ToArray());
58  }
static SqlExpression Build(IExpressionNode node)
static SqlTupleExpression Tuple(SqlExpression[] expressions)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitUnaryExpression ( SqlUnaryExpressionNode  expressionNode)
inlinestaticprivate

Definition at line 296 of file ExpressionBuilder.cs.

296  {
297  var expressionType = GetUnaryExpressionType(expressionNode.Operator);
298  var operand = Build(expressionNode.Operand);
299 
300  return SqlExpression.Unary(expressionType, operand);
301  }
static SqlExpressionType GetUnaryExpressionType(string op)
static SqlExpression Build(IExpressionNode node)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlUnaryExpression Unary(SqlExpressionType expressionType, SqlExpression operand)
static SqlExpression Deveel.Data.Sql.Parser.ExpressionBuilder.VisitVariableRefExpression ( SqlVariableRefExpressionNode  node)
inlinestaticprivate

Definition at line 52 of file ExpressionBuilder.cs.

52  {
53  return SqlExpression.VariableReference(node.Variable);
54  }
static SqlVariableReferenceExpression VariableReference(string varName)
Defines the base class for instances that represent SQL expression tree nodes.

The documentation for this class was generated from the following file: