DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlExpressionExtensions.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 
19 using Deveel.Data;
20 using Deveel.Data.Sql.Objects;
21 using Deveel.Data.Sql.Query;
22 using Deveel.Data.Types;
23 
24 namespace Deveel.Data.Sql.Expressions {
28  public static class SqlExpressionExtensions {
37  public static ObjectName AsReferenceName(this SqlExpression expression) {
38  var refExpression = expression as SqlReferenceExpression;
39  if (refExpression == null)
40  return null;
41 
42  return refExpression.ReferenceName;
43  }
44 
45  public static IQueryPlanNode AsQueryPlan(this SqlExpression expression) {
46  var constantExp = expression as SqlConstantExpression;
47  if (constantExp == null)
48  return null;
49 
50  var value = constantExp.Value;
51  if (value.Value is SqlQueryObject)
52  return ((SqlQueryObject) value.Value).QueryPlan;
53 
54  return null;
55  }
56 
75  public static DataObject EvaluateToConstant(this SqlExpression expression, EvaluateContext context) {
76  var evalExp = expression.Evaluate(context);
77  if (evalExp == null)
78  throw new InvalidOperationException();
79 
80  var constantExp = evalExp as SqlConstantExpression;
81  if (constantExp == null)
82  throw new InvalidOperationException();
83 
84  return constantExp.Value;
85  }
86 
87  public static DataObject EvaluateToConstant(this SqlExpression expression, IRequest request, IVariableResolver variableResolver) {
88  return expression.EvaluateToConstant(new EvaluateContext(request, variableResolver));
89  }
90 
103  public static SqlType ReturnType(this SqlExpression expression, IRequest query, IVariableResolver variableResolver) {
104  var visitor = new ReturnTypeVisitor(query, variableResolver);
105  return visitor.GetType(expression);
106  }
107 
118  public static bool HasAggregate(this SqlExpression expression, IRequest query) {
119  var visitor = new AggregateChecker(query);
120  return visitor.HasAggregate(expression);
121  }
122 
123  public static bool IsConstant(this SqlExpression expression) {
124  var visitor = new ConstantVisitor();
125  visitor.Visit(expression);
126  return visitor.IsConstant;
127  }
128  }
129 }
ObjectName ReferenceName
Gets the name of the object referenced by the expression.
An expression that references an object within a context.
static bool IsConstant(this SqlExpression expression)
static IQueryPlanNode AsQueryPlan(this SqlExpression expression)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
DataObject Value
Gets the constant value of the expression.
A node element of a query plan tree. /summary>
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
static SqlType ReturnType(this SqlExpression expression, IRequest query, IVariableResolver variableResolver)
Gets the return type of the expression when evaluated.
An interface to resolve a variable name to a constant object.
An expression that holds a constant value.
static ObjectName AsReferenceName(this SqlExpression expression)
Extracts the name of the reference from the expression.
Defines the base class for instances that represent SQL expression tree nodes.
Encapsulates the elements needed to evaluate an SqlExpression
static DataObject EvaluateToConstant(this SqlExpression expression, IRequest request, IVariableResolver variableResolver)
static DataObject EvaluateToConstant(this SqlExpression expression, EvaluateContext context)
Evaluates the expression and reduces to a constant expression, whose value is then returned...
static bool HasAggregate(this SqlExpression expression, IRequest query)
Verifies if the expression contains any aggregate function in the tree.