DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor Class Reference
Inheritance diagram for Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor:
Deveel.Data.Sql.Expressions.SqlExpressionVisitor

Classes

class  BinaryEvaluateInfo
 

Public Member Functions

 ExpressionEvaluatorVisitor (EvaluateContext context)
 
override SqlExpression Visit (SqlExpression expression)
 Visits a given SQL expression. More...
 
override SqlExpression VisitBinary (SqlBinaryExpression binaryEpression)
 
override SqlExpression VisitCast (SqlCastExpression castExpression)
 
override SqlExpression VisitFunctionCall (SqlFunctionCallExpression expression)
 Visits the expression that calls the function defined. More...
 
override SqlExpression VisitQuery (SqlQueryExpression query)
 
override SqlExpression VisitReference (SqlReferenceExpression reference)
 
override SqlExpression VisitUnary (SqlUnaryExpression unary)
 
override SqlExpression VisitAssign (SqlAssignExpression assign)
 
override SqlExpression VisitTuple (SqlTupleExpression expression)
 
override SqlExpression VisitVariableReference (SqlVariableReferenceExpression reference)
 
override SqlExpression VisitConditional (SqlConditionalExpression conditional)
 
- Public Member Functions inherited from Deveel.Data.Sql.Expressions.SqlExpressionVisitor
virtual SqlExpression[] VisitExpressionList (SqlExpression[] list)
 Visits a list of expressions given. More...
 
virtual SqlExpression VisitConstant (SqlConstantExpression constant)
 

Private Member Functions

SqlExpression VisitQueryReference (QueryReferenceExpression expression)
 
SqlExpression[] EvaluateSides (SqlBinaryExpression binary)
 
SqlExpression EvaluateBinary (SqlExpression left, SqlExpressionType binaryType, SqlExpression right)
 
DataObject EvaluateBinary (DataObject left, SqlExpressionType binaryType, DataObject right)
 
DataObject EvaluateUnary (DataObject operand, SqlExpressionType unaryType)
 

Private Attributes

readonly EvaluateContext context
 

Detailed Description

Definition at line 29 of file ExpressionEvaluatorVisitor.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.ExpressionEvaluatorVisitor ( EvaluateContext  context)
inline

Definition at line 32 of file ExpressionEvaluatorVisitor.cs.

32  {
33  this.context = context;
34  }

Member Function Documentation

SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.EvaluateBinary ( SqlExpression  left,
SqlExpressionType  binaryType,
SqlExpression  right 
)
inlineprivate

Definition at line 64 of file ExpressionEvaluatorVisitor.cs.

64  {
65  if (left.ExpressionType != SqlExpressionType.Constant)
66  throw new ExpressionEvaluateException("The evaluated left side of a binary expression is not constant");
67  if (right.ExpressionType != SqlExpressionType.Constant)
68  throw new ExpressionEvaluateException("The evaluated right side of a binary expression is not constant.");
69 
70  var value1 = ((SqlConstantExpression) left).Value;
71  var value2 = ((SqlConstantExpression) right).Value;
72 
73  var result = EvaluateBinary(value1, binaryType, value2);
74 
75  return SqlExpression.Constant(result);
76  }
SqlExpression EvaluateBinary(SqlExpression left, SqlExpressionType binaryType, SqlExpression right)
SqlExpressionType
All the possible type of SqlExpression supported
DataObject Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.EvaluateBinary ( DataObject  left,
SqlExpressionType  binaryType,
DataObject  right 
)
inlineprivate

Definition at line 78 of file ExpressionEvaluatorVisitor.cs.

78  {
79  if (binaryType.IsAll())
80  return left.Any(binaryType.SubQueryPlainType(), right, context);
81  if (binaryType.IsAny())
82  return left.All(binaryType.SubQueryPlainType(), right, context);
83 
84  switch (binaryType) {
85  case SqlExpressionType.Add:
86  return left.Add(right);
87  case SqlExpressionType.Subtract:
88  return left.Subtract(right);
89  case SqlExpressionType.Multiply:
90  return left.Multiply(right);
91  case SqlExpressionType.Divide:
92  return left.Divide(right);
93  case SqlExpressionType.Modulo:
94  return left.Modulus(right);
95  case SqlExpressionType.GreaterThan:
96  return left.IsGreaterThan(right);
97  case SqlExpressionType.GreaterOrEqualThan:
98  return left.IsGreterOrEqualThan(right);
99  case SqlExpressionType.SmallerThan:
100  return left.IsSmallerThan(right);
101  case SqlExpressionType.SmallerOrEqualThan:
102  return left.IsSmallerOrEqualThan(right);
103  case SqlExpressionType.Equal:
104  return left.IsEqualTo(right);
105  case SqlExpressionType.NotEqual:
106  return left.IsNotEqualTo(right);
107  case SqlExpressionType.Is:
108  return left.Is(right);
109  case SqlExpressionType.IsNot:
110  return left.IsNot(right);
111  case SqlExpressionType.Like:
112  return left.IsLike(right);
113  case SqlExpressionType.NotLike:
114  return left.IsNotLike(right);
115  case SqlExpressionType.And:
116  return left.And(right);
117  case SqlExpressionType.Or:
118  return left.Or(right);
119  case SqlExpressionType.XOr:
120  return left.XOr(right);
121  // TODO: ANY and ALL
122  default:
123  throw new ExpressionEvaluateException(String.Format("The type {0} is not a binary expression or is not supported.", binaryType));
124  }
125  }
DataObject Multiply(DataObject other)
Definition: DataObject.cs:401
A long string in the system.
DataObject Divide(DataObject other)
Definition: DataObject.cs:410
DataObject IsLike(DataObject pattern)
When the type of this object is a string, this method verifies if the input pattern is compatible (
Definition: DataObject.cs:324
DataObject Modulus(DataObject other)
Definition: DataObject.cs:419
DataObject IsSmallerThan(DataObject other)
Definition: DataObject.cs:287
SqlExpressionType
All the possible type of SqlExpression supported
DataObject IsEqualTo(DataObject other)
Compares to the given object to verify if is it equal to the current.
Definition: DataObject.cs:251
DataObject IsNotLike(DataObject pattern)
Definition: DataObject.cs:337
DataObject IsSmallerOrEqualThan(DataObject other)
Definition: DataObject.cs:301
DataObject IsGreaterThan(DataObject other)
Definition: DataObject.cs:280
DataObject Is(DataObject other)
Compares to the given object to verify if is it compatible.
Definition: DataObject.cs:208
DataObject XOr(DataObject other)
Definition: DataObject.cs:446
DataObject IsGreterOrEqualThan(DataObject other)
Definition: DataObject.cs:294
DataObject IsNotEqualTo(DataObject other)
Compares to the given object to verify if is it not equal to the current.
Definition: DataObject.cs:273
DataObject And(DataObject other)
Definition: DataObject.cs:437
DataObject Any(SqlExpressionType type, DataObject other, EvaluateContext context)
Definition: DataObject.cs:455
DataObject IsNot(DataObject other)
Compares the given object to verify if it is not compatible with this one.
Definition: DataObject.cs:232
DataObject All(SqlExpressionType type, DataObject other, EvaluateContext context)
Definition: DataObject.cs:462
DataObject Subtract(DataObject other)
Definition: DataObject.cs:392
DataObject Add(DataObject other)
Adds the given value to this object value.
Definition: DataObject.cs:383
DataObject Or(DataObject other)
Definition: DataObject.cs:428
SqlExpression [] Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.EvaluateSides ( SqlBinaryExpression  binary)
inlineprivate

Definition at line 49 of file ExpressionEvaluatorVisitor.cs.

49  {
50  var info = new List<BinaryEvaluateInfo> {
51  new BinaryEvaluateInfo {Expression = binary.Left, Offset = 0},
52  new BinaryEvaluateInfo {Expression = binary.Right, Offset = 1}
53  }.OrderByDescending(x => x.Precedence);
54 
55  foreach (var evaluateInfo in info) {
56  evaluateInfo.Expression = Visit(evaluateInfo.Expression);
57  }
58 
59  return info.OrderBy(x => x.Offset)
60  .Select(x => x.Expression)
61  .ToArray();
62  }
override SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
DataObject Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.EvaluateUnary ( DataObject  operand,
SqlExpressionType  unaryType 
)
inlineprivate

Definition at line 240 of file ExpressionEvaluatorVisitor.cs.

240  {
241  switch (unaryType) {
242  case SqlExpressionType.UnaryPlus:
243  return operand.Plus();
244  case SqlExpressionType.Negate:
245  case SqlExpressionType.Not:
246  return operand.Negate();
247  default:
248  throw new ExpressionEvaluateException(String.Format("Expression of type '{0}' is not unary.", unaryType));
249  }
250  }
A long string in the system.
SqlExpressionType
All the possible type of SqlExpression supported
DataObject Negate()
Negates the current underlying value of the object.
Definition: DataObject.cs:359
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.Visit ( SqlExpression  expression)
inlinevirtual

Visits a given SQL expression.

Parameters
expressionThe SqlExpression to visit.
Returns
Returns an instance of SqlExpression as result of the visit.

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 36 of file ExpressionEvaluatorVisitor.cs.

36  {
37  if (expression is QueryReferenceExpression)
38  return VisitQueryReference((QueryReferenceExpression) expression);
39 
40  return base.Visit(expression);
41  }
SqlExpression VisitQueryReference(QueryReferenceExpression expression)
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitAssign ( SqlAssignExpression  assign)
inlinevirtual

Parameters
assign
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 252 of file ExpressionEvaluatorVisitor.cs.

252  {
253  if (context.Request == null)
254  throw new ExpressionEvaluateException("Cannot assign a variable outside a query context.");
255 
256  var valueExpression = Visit(assign.ValueExpression);
257 
258  if (valueExpression.ExpressionType != SqlExpressionType.Constant)
259  throw new ExpressionEvaluateException("Cannot assign a variable from a non constant value.");
260 
261  var reference = assign.ReferenceExpression;
262  var value = ((SqlConstantExpression)valueExpression).Value;
263 
264  string variableName;
265 
266  if (reference is SqlVariableReferenceExpression) {
267  variableName = ((SqlVariableReferenceExpression) reference).VariableName;
268  } else if (reference is SqlReferenceExpression) {
269  var refName = ((SqlReferenceExpression) reference).ReferenceName;
270  if (refName.Parent != null) // This might be the assignment to a complex type attribute
271  throw new NotSupportedException(string.Format("Reference to '{0}' is not supported.", refName));
272 
273  variableName = refName.Name;
274  } else {
275  throw new NotSupportedException();
276  }
277 
278  try {
279  context.Request.SetVariable(variableName, valueExpression);
280  } catch (Exception ex) {
281  throw new ExpressionEvaluateException(String.Format("Could not assign value to variable '{0}'", reference), ex);
282  }
283 
284 
285  return SqlExpression.Constant(value);
286  }
override SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
A long string in the system.
SqlExpressionType
All the possible type of SqlExpression supported
IRequest Request
Gets the query in which an expression is evaluated.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitBinary ( SqlBinaryExpression  binaryEpression)
inlinevirtual

Parameters
binaryEpression
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 127 of file ExpressionEvaluatorVisitor.cs.

127  {
128  var sides = EvaluateSides(binaryEpression);
129 
130  var newLeft = sides[0];
131  var newRight = sides[1];
132 
133  return EvaluateBinary(newLeft, binaryEpression.ExpressionType, newRight);
134  }
SqlExpression EvaluateBinary(SqlExpression left, SqlExpressionType binaryType, SqlExpression right)
SqlExpression[] EvaluateSides(SqlBinaryExpression binary)
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitCast ( SqlCastExpression  castExpression)
inlinevirtual

Parameters
castExpression
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 136 of file ExpressionEvaluatorVisitor.cs.

136  {
137  var valueExp = Visit(castExpression.Value);
138  if (valueExp.ExpressionType != SqlExpressionType.Constant)
139  throw new ExpressionEvaluateException(String.Format("Cannot CAST an expression of type {0}.", valueExp.ExpressionType));
140 
141  var value = ((SqlConstantExpression) valueExp).Value;
142  var casted = value.CastTo(castExpression.SqlType);
143  return SqlExpression.Constant(casted);
144  }
override SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
A long string in the system.
SqlExpressionType
All the possible type of SqlExpression supported
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitConditional ( SqlConditionalExpression  conditional)
inlinevirtual

Parameters
conditional
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 322 of file ExpressionEvaluatorVisitor.cs.

322  {
323  var evalTest = Visit(conditional.TestExpression);
324  if (evalTest.ExpressionType != SqlExpressionType.Constant)
325  throw new ExpressionEvaluateException("The test expression of a conditional must evaluate to a constant value.");
326 
327  var evalTestValue = ((SqlConstantExpression) evalTest).Value;
328  if (!(evalTestValue.Type is BooleanType))
329  throw new ExpressionEvaluateException("The test expression of a conditional must be a boolean value.");
330 
331  if (evalTestValue)
332  return Visit(conditional.TrueExpression);
333 
334  if (conditional.FalseExpression != null)
335  return Visit(conditional.FalseExpression);
336 
337  return SqlExpression.Constant(PrimitiveTypes.Null());
338  }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
override SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
SqlExpressionType
All the possible type of SqlExpression supported
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitFunctionCall ( SqlFunctionCallExpression  expression)
inlinevirtual

Visits the expression that calls the function defined.

Parameters
expressionThe SqlFunctionCallExpression to visit.
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 175 of file ExpressionEvaluatorVisitor.cs.

175  {
176  try {
177  var invoke = new Invoke(expression.FunctioName, expression.Arguments);
178  IRequest request = null;
179  IVariableResolver variableResolver = null;
180  IGroupResolver groupResolver = null;
181  if (context != null) {
182  request = context.Request;
183  variableResolver = context.VariableResolver;
184  groupResolver = context.GroupResolver;
185  }
186 
187  // TODO: if we don't have a return value (PROCEDURES) what should w return?
188  var result = invoke.Execute(request, variableResolver, groupResolver);
189  if (!result.HasReturnValue)
190  return SqlExpression.Constant(DataObject.Null());
191 
192  return SqlExpression.Constant(result.ReturnValue);
193  } catch (ExpressionEvaluateException) {
194  throw;
195  } catch (Exception ex) {
196  throw new ExpressionEvaluateException(String.Format("Could not evaluate function expression '{0}' because of an error.", expression), ex);
197  }
198  }
A long string in the system.
static DataObject Null(SqlType type)
Definition: DataObject.cs:630
IRequest Request
Gets the query in which an expression is evaluated.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
The information about the invocation of a routine, including the full name and arguments (as SqlExpre...
Definition: Invoke.cs:30
IGroupResolver GroupResolver
Gets the object that aggregate functions will use to resolve variable groups
IVariableResolver VariableResolver
Gets an object used to resolve variables from within the expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitQuery ( SqlQueryExpression  query)
inlinevirtual

Parameters
query
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 200 of file ExpressionEvaluatorVisitor.cs.

200  {
201  if (context.Request == null)
202  throw new ExpressionEvaluateException("A query expression is required to evaluate a query.");
203 
204  try {
205  var planner = context.Request.Context.QueryPlanner();
206  var plan = planner.PlanQuery(new QueryInfo(context.Request, query));
207  return SqlExpression.Constant(new DataObject(new QueryType(), new SqlQueryObject(plan)));
208  } catch (ExpressionEvaluateException) {
209  throw;
210  } catch (Exception ex) {
211  throw new ExpressionEvaluateException("Evaluation of a QUERY expression could not generate a plan.", ex);
212  }
213  }
IRequest Request
Gets the query in which an expression is evaluated.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitQueryReference ( QueryReferenceExpression  expression)
inlineprivate

Definition at line 43 of file ExpressionEvaluatorVisitor.cs.

43  {
44  var reference = expression.QueryReference;
45  var value = reference.Evaluate(context.VariableResolver);
46  return SqlExpression.Constant(value);
47  }
IVariableResolver VariableResolver
Gets an object used to resolve variables from within the expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitReference ( SqlReferenceExpression  reference)
inlinevirtual

Parameters
reference
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 215 of file ExpressionEvaluatorVisitor.cs.

215  {
216  var refName = reference.ReferenceName;
217 
218  if (context.VariableResolver == null)
219  throw new ExpressionEvaluateException(String.Format("A resolver is required to dereference variable '{0}'.", refName));
220 
221  try {
222  var value = context.VariableResolver.Resolve(refName);
223  return SqlExpression.Constant(value);
224  } catch (ExpressionEvaluateException) {
225  throw;
226  } catch (Exception ex) {
227  throw new ExpressionEvaluateException(String.Format("An error occurred while trying to dereference '{0}' to a constant value", refName), ex);
228  }
229  }
DataObject Resolve(ObjectName variable)
Returns the value of a given variable.
A long string in the system.
IVariableResolver VariableResolver
Gets an object used to resolve variables from within the expression.
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitTuple ( SqlTupleExpression  expression)
inlinevirtual

Parameters
expression
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 288 of file ExpressionEvaluatorVisitor.cs.

288  {
289  var list = expression.Expressions;
290  if (list != null) {
291  list = VisitExpressionList(list);
292  }
293 
294  if (list == null)
295  return SqlExpression.Constant(new DataObject(new ArrayType(-1), SqlArray.Null));
296 
297  // This is not an array, but a subquery
298  if (list.Length == 1 &&
299  list[0].ExpressionType == SqlExpressionType.Constant &&
300  ((SqlConstantExpression)list[0]).Value.Type is QueryType)
301  return list[0];
302 
303  return SqlExpression.Constant(new DataObject(new ArrayType(list.Length), new SqlArray(list)));
304  }
static readonly SqlArray Null
A SQL array that is equivalent to null.
Definition: SqlArray.cs:34
SqlExpressionType
All the possible type of SqlExpression supported
virtual SqlExpression[] VisitExpressionList(SqlExpression[] list)
Visits a list of expressions given.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
An object that provides methods for accessing a finite collection of SQL expressions.
Definition: SqlArray.cs:28
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitUnary ( SqlUnaryExpression  unary)
inlinevirtual

Parameters
unary
Returns

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 231 of file ExpressionEvaluatorVisitor.cs.

231  {
232  var operand = Visit(unary.Operand);
233  if (operand.ExpressionType != SqlExpressionType.Constant)
234  throw new ExpressionEvaluateException("Operand of a unary operator could not be evaluated to a constant.");
235 
236  var result = EvaluateUnary(((SqlConstantExpression)operand).Value, unary.ExpressionType);
237  return SqlExpression.Constant(result);
238  }
override SqlExpression Visit(SqlExpression expression)
Visits a given SQL expression.
SqlExpressionType
All the possible type of SqlExpression supported
DataObject EvaluateUnary(DataObject operand, SqlExpressionType unaryType)
override SqlExpression Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.VisitVariableReference ( SqlVariableReferenceExpression  reference)
inlinevirtual

Reimplemented from Deveel.Data.Sql.Expressions.SqlExpressionVisitor.

Definition at line 306 of file ExpressionEvaluatorVisitor.cs.

306  {
307  var refName = reference.VariableName;
308 
309  if (context.Request == null)
310  throw new ExpressionEvaluateException(String.Format("Cannot dereference variable {0} outside a query context", refName));
311  if (context.VariableResolver == null)
312  throw new ExpressionEvaluateException("The query context does not handle variables.");
313 
314 
315  var variable = context.Request.FindVariable(refName);
316  if (variable == null)
317  return SqlExpression.Constant(DataObject.Null());
318 
319  return SqlExpression.Constant(variable.Value);
320  }
A long string in the system.
static DataObject Null(SqlType type)
Definition: DataObject.cs:630
IRequest Request
Gets the query in which an expression is evaluated.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
IVariableResolver VariableResolver
Gets an object used to resolve variables from within the expression.

Member Data Documentation

readonly EvaluateContext Deveel.Data.Sql.Expressions.ExpressionEvaluatorVisitor.context
private

Definition at line 30 of file ExpressionEvaluatorVisitor.cs.


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