18 using System.Collections.Generic;
25 namespace Deveel.Data.Sql.Parser {
29 return VisitVariableRefExpression((SqlVariableRefExpressionNode) node);
31 return VisitTupleExpression((SqlExpressionTupleNode) node);
33 return VisitQueryExpression((SqlQueryExpressionNode) node);
35 return VisitCaseExpression((SqlCaseExpressionNode) node);
37 return VisitConstantExpression((SqlConstantExpressionNode) node);
39 return VisitFunctionCall((SqlFunctionCallExpressionNode) node);
41 return VisitReferenceExpression((SqlReferenceExpressionNode) node);
43 return VisitBinaryExpression((SqlBinaryExpressionNode) node);
45 return VisitUnaryExpression((SqlUnaryExpressionNode) node);
47 return VisitBetweenExpression((SqlBetweenExpressionNode) node);
49 throw new NotSupportedException();
61 var selectColumns = GetSelectColumns(node);
65 SetFromClause(exp.FromClause, node.
FromClause);
73 var groupBy =
new List<SqlExpression>();
77 exp.GroupBy = groupBy.ToList();
81 exp.HavingExpression = Build(having);
96 if (String.Equals(s,
"UNION", StringComparison.OrdinalIgnoreCase))
98 if (String.Equals(s,
"EXCEPT", StringComparison.OrdinalIgnoreCase))
100 if (String.Equals(s,
"INTERSECT", StringComparison.OrdinalIgnoreCase))
103 throw new InvalidOperationException(String.Format(
"Composite function {0} is invalid.", s));
107 AddSourceToClause(clause, source);
111 if (!String.IsNullOrEmpty(join.
JoinType))
112 joinType = GetJoinType(join.
JoinType);
118 clause.
Join(joinType, onExpression);
126 if (source.
Alias != null)
130 var tableSource = (FromTableSourceNode)source;
131 clause.
AddTable(alias, tableSource.TableName.Name);
133 var querySource = (FromQuerySourceNode)source;
140 if (String.Equals(typeName,
"INNER", StringComparison.OrdinalIgnoreCase) ||
141 String.Equals(typeName,
"INNER JOIN", StringComparison.OrdinalIgnoreCase) ||
142 String.Equals(typeName,
",", StringComparison.OrdinalIgnoreCase))
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))
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))
159 SetFromTableInClause(clause, node.
Source, node.
Join);
167 var items =
new List<SelectColumn>();
170 if (item.Name != null) {
172 }
else if (item.Expression != null) {
173 exp = Build(item.Expression);
175 throw new InvalidOperationException();
179 if (item.Alias != null)
180 alias = item.Alias.Text;
185 return items.ToArray();
189 throw new NotImplementedException();
193 var sqlValue = expressionNode.
Value;
201 }
else if (sqlValue is
SqlNull) {
204 throw new NotSupportedException(
"Constant value not supported.");
215 var args =
new List<SqlExpression>();
217 args.AddRange(node.
Arguments.Select(Build));
223 var testExp = Build(expressionNode.
Expression);
224 var minValue = Build(expressionNode.
MinValue);
225 var maxValue = Build(expressionNode.
MaxValue);
232 if (expressionNode.
Not)
239 var left = Build(expressionNode.
Left);
240 var right = Build(expressionNode.
Right);
243 var expType = GetBinaryExpressionType(op);
259 String.Equals(op,
"MOD", StringComparison.OrdinalIgnoreCase))
273 if (String.Equals(op,
"LIKE", StringComparison.OrdinalIgnoreCase))
275 if (String.Equals(op,
"NOT LIKE", StringComparison.OrdinalIgnoreCase))
278 if (String.Equals(op,
"IS", StringComparison.OrdinalIgnoreCase))
280 if (String.Equals(op,
"IS NOT", StringComparison.OrdinalIgnoreCase))
283 if (String.Equals(op,
"AND", StringComparison.OrdinalIgnoreCase))
285 if (String.Equals(op,
"OR", StringComparison.OrdinalIgnoreCase))
288 if (String.Equals(op,
"IN", StringComparison.OrdinalIgnoreCase))
290 if (String.Equals(op,
"NOT IN", StringComparison.OrdinalIgnoreCase))
293 throw new ArgumentException(String.Format(
"The operator {0} is not a binary one.", op));
297 var expressionType = GetUnaryExpressionType(expressionNode.
Operator);
298 var operand = Build(expressionNode.
Operand);
308 if (String.Equals(op,
"NOT", StringComparison.OrdinalIgnoreCase))
311 throw new ArgumentException(String.Format(
"The operator {0} is not a unary one.", op));
static SqlExpressionType GetUnaryExpressionType(string op)
string CompositeFunction
Gets the function used to compose the two queries.
static JoinType GetJoinType(string typeName)
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
bool IsAll
Gets a boolean value indicating if the selection will return all columns from the sources specified i...
IExpressionNode Operand
Gets the argument of the operator.
static SqlBinaryExpression And(SqlExpression left, SqlExpression right)
static SqlBinaryExpression SmallerOrEqualThan(SqlExpression left, SqlExpression right)
IExpressionNode Right
Gets the right side argument of the expression.
static SqlExpression VisitConstantExpression(SqlConstantExpressionNode expressionNode)
JoinType
Enumerates the kind of group join in a selection query.
static SqlExpression VisitBetweenExpression(SqlBetweenExpressionNode expressionNode)
static IEnumerable< SelectColumn > GetSelectColumns(SqlQueryExpressionNode node)
void AddTable(string alias, FromTable table)
Adds a table as source to the query with a given alias.
static CompositeFunction GetCompositeFunction(string s)
string Operator
Gets the string representation of the operator.
static DataObject Number(SqlNumber value)
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.
static DataObject Null(SqlType type)
IExpressionNode MinValue
Gets the minimum value (inclusive) that Expression must be to evaluate to true.
static SqlTupleExpression Tuple(SqlExpression[] expressions)
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...
static SqlVariableReferenceExpression VariableReference(string varName)
Describes the name of an object within a database.
The node in an SQL query that defines the sources from which to retrieve the data queried...
ObjectNameNode Reference
Gets the full name of the object references.
static DataObject Boolean(SqlBoolean value)
A node in the grammar tree that defines a sub-query in a FROM clause.
static SqlExpression VisitFunctionCall(SqlFunctionCallExpressionNode node)
static SqlUnaryExpression Not(SqlExpression operand)
static SqlExpression VisitUnaryExpression(SqlUnaryExpressionNode expressionNode)
ISqlObject Value
Gets an immutable instance of ISqlObject that represents the constant value.
SqlExpressionType
All the possible type of SqlExpression supported
An expression containing a set of other expressions.
string FunctionName
Gets the name of the function to be invoked.
static SqlExpression VisitReferenceExpression(SqlReferenceExpressionNode node)
Represents the node that is a database table as source of a query.
void AddSubQuery(SqlQueryExpression subQuery)
Adds a sub-query expression as source of the query.
static SqlExpression VisitTupleExpression(SqlExpressionTupleNode node)
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< IExpressionNode > Arguments
Gets an optional read-only list of arguments to be passed to the function invoked.
static SqlExpression VisitCaseExpression(SqlCaseExpressionNode expressionNode)
A container for the FROM clause of a select statement.
static void AddSourceToClause(FromClause clause, IFromSourceNode source)
static SqlExpression VisitVariableRefExpression(SqlVariableRefExpressionNode node)
SqlQueryExpressionNode QueryExpression
Gets the other query to compose the results.
IExpressionNode HavingExpression
Gets the HAVING expression used to filter the grouped results.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
IEnumerable< SelectItemNode > SelectItems
Gets a read-only list of items that will be returned by the query.
Represents a column selected to be in the output of a select statement.
References a variable within a SQL execution context.
Defines the base contract of the source of a query.
string Name
The full object name as composed from the input SQL string analyzed.
IEnumerable< IExpressionNode > GroupExpressions
Gets the expression node to group the results.
string Text
Gets the textual content of the identifier.
string Variable
Gets the name of the variable to reference.
static SqlExpression VisitBinaryExpression(SqlBinaryExpressionNode expressionNode)
static DataObject VarChar(string s)
A node describing the JOIN between two sources within a query.
static void SetFromClause(FromClause clause, FromClauseNode node)
IFromSourceNode Source
Gets a read-only list of sources for the query.
bool IsAll
Gets a boolean value indicating whether the composition will be done on all records.
static SqlReferenceExpression Reference(ObjectName objectName)
GroupByNode GroupBy
Gets an optional clause used to group and filter the results of a query.
IdentifierNode Alias
Gets an alias that uniquely identifies the source within a query context.
IExpressionNode WhereExpression
Gets an optional clause that is used to filter the queried objects.
IExpressionNode Left
Gets the left side argument of the expression.
string JoinType
Gets the type of join, as a string that will be operated between the two sources
IExpressionNode MaxValue
Gets the maximum value (inclusive) that Expression must be to evaluate to true.
IExpressionNode OnExpression
Gets the expression used as condition for creating the joined group in a query.
CompositeFunction
The kind of composite function in a CompositeTable.
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.
An node that represents a constant value set within a context of an SQL command.
static SqlConstantExpression Constant(object value)
static void SetFromTableInClause(FromClause clause, IFromSourceNode source, JoinNode join)
string Operator
Gets the binary operator that will be used to evaluate the final result.
IExpressionNode Expression
Gets the expression to be tested against MinValue and MaxValue.
static SqlFunctionCallExpression FunctionCall(ObjectName functionName)
static SqlExpression VisitQueryExpression(SqlQueryExpressionNode 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.
static SqlUnaryExpression Unary(SqlExpressionType expressionType, SqlExpression operand)
static SqlBinaryExpression GreaterOrEqualThan(SqlExpression left, SqlExpression right)
FromClauseNode FromClause
Gets the clause defining the sources from where to query.
QueryCompositeNode Composite
Gets an optional definition for a composition between this query and another.
An SQL node describing an in-line CASE conditional expression.