public final class Expression extends Object implements Serializable, Cloneable
This class can represent constant expressions (expressions that have no variable input), as well as variable expressions. Optimizations may be possible when evaluating constant expressions.
Some examples of constant expressions;
( 9 + 3 ) * 90 ( ? * 9 ) / 1 lower("CaPS MUMma") 40 & 0x0FF != 39Some examples of variable expressions;
upper(Part.description) Part.id >= 50 VendorMakesPart.part_id == Part.id Part.value_of <= Part.cost_of / 4
NOTE: the expression is stored in postfix orientation. eg. "8 + 9 * 3" becomes "8,9,3,*,+"
NOTE: This class is NOT thread safe. Do not use an expression instance between threads.
Constructor and Description |
---|
Expression()
Constructs a new Expression.
|
Expression(Expression exp)
Constructs a copy of the given Expression.
|
Expression(Expression exp1,
Operator op,
Expression exp2)
Constructs a new Expression from the concatination of expression1 and
expression2 and the operator for them.
|
Expression(Object ob)
Constructs a new Expression with a single object element.
|
Modifier and Type | Method and Description |
---|---|
void |
addElement(Object ob)
Adds a new element into the expression.
|
void |
addOperator(Operator op)
Adds a new operator into the expression.
|
List |
allElements()
Returns a complete list of all element objects that are in this expression
and in the parameters of the functions of this expression.
|
List |
allVariables()
Returns a complete List of Variable objects in this expression not
including correlated variables.
|
ArrayList |
breakByOperator(ArrayList list,
String logical_op)
Breaks this expression into a list of sub-expressions that are split
by the given operator.
|
Object |
clone()
Performs a deep clone of this object, calling 'clone' on any elements
that are mutable or shallow copying immutable members.
|
Expression |
concat(Expression expr)
Merges an expression with this expression.
|
boolean |
containsNotOperator()
Returns true if the expression contains a NOT operator somewhere in it.
|
void |
copyTextFrom(Expression e)
Copies the text from the given expression.
|
ArrayList |
discoverCorrelatedVariables(int level,
ArrayList list)
Discovers all the correlated variables in this expression.
|
ArrayList |
discoverTableNames(ArrayList list)
Discovers all the tables in the sub-queries of this expression.
|
Object |
elementAt(int n)
Returns the element at the given position in the postfix list.
|
TObject |
evaluate(GroupResolver group,
VariableResolver resolver,
QueryContext context)
Evaluates this expression and returns an Object that represents the
result of the evaluation.
|
TObject |
evaluate(VariableResolver resolver,
QueryContext context)
Evaluation without a grouping table.
|
Expression |
getEndExpression()
Returns the end Expression of this expression.
|
QueryPlanNode |
getQueryPlanNode()
Returns the QueryPlanNode object in this expression if it evaluates to a
single QueryPlanNode, otherwise returns null.
|
Variable |
getVariable()
Returns the Variable if this expression evaluates to a single variable,
otherwise returns null.
|
boolean |
hasAggregateFunction(QueryContext context)
Cascades through the expression and if any aggregate functions are found
returns true, otherwise returns false.
|
boolean |
hasSubQuery()
Returns true if the expression has a subquery (eg 'in ( select ...
|
boolean |
isConstant()
Returns true if the expression doesn't include any variables or non
constant functions (is constant).
|
Object |
last()
Returns the element at the end of the postfix list (the last element).
|
static Expression |
parse(String expression)
Static method that parses the given string which contains an expression
into an Expression object.
|
void |
prepare(ExpressionPreparer preparer)
A general prepare that cascades through the expression and its parents and
substitutes an elements that the preparer wants to substitute.
|
TType |
returnTType(VariableResolver resolver,
QueryContext context)
Determines the type of object this expression evaluates to.
|
void |
setElementAt(int n,
Object ob)
Sets the element at the given position in the postfix list.
|
static Expression |
simple(Object ob1,
Operator op,
Object ob2)
Generates a simple expression from two objects and an operator.
|
int |
size()
Returns the number of elements and operators that are in this postfix
list.
|
Expression[] |
split()
Returns an array of two Expression objects that represent the left hand
and right and side of the last operator in the post fix notation.
|
StringBuffer |
text()
Returns the StringBuffer that we can use to append plain text
representation as we are parsing the expression.
|
String |
toString()
Returns a string representation of this object for diagnostic
purposes.
|
public Expression()
public Expression(Object ob)
public Expression(Expression exp)
public Expression(Expression exp1, Operator op, Expression exp2)
public StringBuffer text()
public void copyTextFrom(Expression e)
public static Expression parse(String expression)
Care should be taken to not use this method inside an inner loop because it creates a lot of objects.
public static Expression simple(Object ob1, Operator op, Object ob2)
public void addElement(Object ob)
Must be added in postfix order.
public Expression concat(Expression expr)
This method is useful when copying parts of other expressions when forming an expression.
This always returns this expression. This does not change 'text()'.
public void addOperator(Operator op)
Must be added in postfix order.
public int size()
public Object elementAt(int n)
public Object last()
public void setElementAt(int n, Object ob)
public List allVariables()
public List allElements()
public void prepare(ExpressionPreparer preparer) throws DatabaseException
NOTE: This will not cascade through to the parameters of Function objects however it will cascade through FunctionDef parameters. For this reason you MUST call 'prepareFunctions' after this method.
DatabaseException
public boolean isConstant()
public boolean hasSubQuery()
public boolean containsNotOperator()
public ArrayList discoverCorrelatedVariables(int level, ArrayList list)
public ArrayList discoverTableNames(ArrayList list)
public QueryPlanNode getQueryPlanNode()
public Variable getVariable()
public Expression[] split()
id + 3 > part_id - 2 will return ( id + 3, part_id - 2 }
public Expression getEndExpression()
This is a useful method to call in the middle of an Expression object being formed. It allows for the last complete expression to be returned.
If this is called when an expression is completely formed it will always return the complete expression.
public ArrayList breakByOperator(ArrayList list, String logical_op)
(a = b AND b = c AND (a = 2 OR c = 1))
Calling this method with logical_op = "and" will return a list of the three expressions.
This is a common function used to split up an expressions into logical components for processing.
public TObject evaluate(GroupResolver group, VariableResolver resolver, QueryContext context)
NOTE: This method is gonna be called a lot, so we need it to be optimal.
NOTE: This method is not thread safe! The reason it's not safe is because of the evaluation stack.
public TObject evaluate(VariableResolver resolver, QueryContext context)
public boolean hasAggregateFunction(QueryContext context)
public TType returnTType(VariableResolver resolver, QueryContext context)
public Object clone() throws CloneNotSupportedException
clone
in class Object
CloneNotSupportedException
Copyright © 2015. All rights reserved.