| java.lang.Object com.mckoi.database.Expression
Expression | final public class Expression implements java.io.Serializable,Cloneable(Code) | | An expression that can be evaluated in a statement. This is used as a more
complete and flexible version of 'Condition' as well as representing column
and aggregate functions.
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 != 39
Some 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.
author: Tobias Downer |
Method Summary | |
public void | addElement(Object ob) Adds a new element into the expression. | public void | addOperator(Operator op) Adds a new operator into the expression. | public 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. | public List | allVariables() Returns a complete List of Variable objects in this expression not
including correlated variables. | public ArrayList | breakByOperator(ArrayList list, String logical_op) Breaks this expression into a list of sub-expressions that are split
by the given operator. | public Object | clone() Performs a deep clone of this object, calling 'clone' on any elements
that are mutable or shallow copying immutable members. | public Expression | concat(Expression expr) Merges an expression with this expression. | public boolean | containsNotOperator() Returns true if the expression contains a NOT operator somewhere in it. | public void | copyTextFrom(Expression e) Copies the text from the given expression. | public ArrayList | discoverCorrelatedVariables(int level, ArrayList list) Discovers all the correlated variables in this expression. | public ArrayList | discoverTableNames(ArrayList list) Discovers all the tables in the sub-queries of this expression. | public Object | elementAt(int n) Returns the element at the given position in the postfix list. | public TObject | evaluate(GroupResolver group, VariableResolver resolver, QueryContext context) Evaluates this expression and returns an Object that represents the
result of the evaluation. | public TObject | evaluate(VariableResolver resolver, QueryContext context) Evaluation without a grouping table. | public Expression | getEndExpression() Returns the end Expression of this expression. | public QueryPlanNode | getQueryPlanNode() Returns the QueryPlanNode object in this expression if it evaluates to a
single QueryPlanNode, otherwise returns null. | public Variable | getVariable() Returns the Variable if this expression evaluates to a single variable,
otherwise returns null. | public boolean | hasAggregateFunction(QueryContext context) Cascades through the expression and if any aggregate functions are found
returns true, otherwise returns false. | public boolean | hasSubQuery() Returns true if the expression has a subquery (eg 'in ( select ... | public boolean | isConstant() Returns true if the expression doesn't include any variables or non
constant functions (is constant). | public Object | last() Returns the element at the end of the postfix list (the last element). | public static Expression | parse(String expression) Static method that parses the given string which contains an expression
into an Expression object. | public 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.
NOTE: This will not cascade through to the parameters of Function objects
however it will cascade through FunctionDef parameters. | public TType | returnTType(VariableResolver resolver, QueryContext context) Determines the type of object this expression evaluates to. | public void | setElementAt(int n, Object ob) Sets the element at the given position in the postfix list. | public static Expression | simple(Object ob1, Operator op, Object ob2) Generates a simple expression from two objects and an operator. | public int | size() Returns the number of elements and operators that are in this postfix
list. | public 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.
For example, (a + b) - (c + d) will return { (a + b), (c + d) }. | public StringBuffer | text() Returns the StringBuffer that we can use to append plain text
representation as we are parsing the expression. | public String | toString() Returns a string representation of this object for diagnostic
purposes. |
serialVersionUID | final static long serialVersionUID(Code) | | Serialization UID.
|
Expression | public Expression()(Code) | | Constructs a new Expression.
|
Expression | public Expression(Object ob)(Code) | | Constructs a new Expression with a single object element.
|
Expression | public Expression(Expression exp)(Code) | | Constructs a copy of the given Expression.
|
Expression | public Expression(Expression exp1, Operator op, Expression exp2)(Code) | | Constructs a new Expression from the concatination of expression1 and
expression2 and the operator for them.
|
addElement | public void addElement(Object ob)(Code) | | Adds a new element into the expression. String, BigNumber, Boolean and
Variable are the types of elements allowed.
Must be added in postfix order.
|
addOperator | public void addOperator(Operator op)(Code) | | Adds a new operator into the expression. Operators are represented as
an Operator (eg. ">", "+", "<<", "!=" )
Must be added in postfix order.
|
allElements | public List allElements()(Code) | | Returns a complete list of all element objects that are in this expression
and in the parameters of the functions of this expression.
|
allVariables | public List allVariables()(Code) | | Returns a complete List of Variable objects in this expression not
including correlated variables.
|
breakByOperator | public ArrayList breakByOperator(ArrayList list, String logical_op)(Code) | | Breaks this expression into a list of sub-expressions that are split
by the given operator. For example, given the expression;
(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.
|
clone | public Object clone() throws CloneNotSupportedException(Code) | | Performs a deep clone of this object, calling 'clone' on any elements
that are mutable or shallow copying immutable members.
|
concat | public Expression concat(Expression expr)(Code) | | Merges an expression with this expression. For example, given the
expression 'ab', if the expression 'abc+-' was added the expression would
become 'ababc+-'.
This method is useful when copying parts of other expressions when forming
an expression.
This always returns this expression. This does not change 'text()'.
|
containsNotOperator | public boolean containsNotOperator()(Code) | | Returns true if the expression contains a NOT operator somewhere in it.
|
copyTextFrom | public void copyTextFrom(Expression e)(Code) | | Copies the text from the given expression.
|
discoverCorrelatedVariables | public ArrayList discoverCorrelatedVariables(int level, ArrayList list)(Code) | | Discovers all the correlated variables in this expression. If this
expression contains a sub-query plan, we ask the plan to find the list of
correlated variables. The discovery process increments the 'level'
variable for each sub-plan.
|
discoverTableNames | public ArrayList discoverTableNames(ArrayList list)(Code) | | Discovers all the tables in the sub-queries of this expression. This is
used for determining all the tables that a query plan touches.
|
elementAt | public Object elementAt(int n)(Code) | | Returns the element at the given position in the postfix list. Note, this
can return Operator's.
|
evaluate | public TObject evaluate(GroupResolver group, VariableResolver resolver, QueryContext context)(Code) | | Evaluates this expression and returns an Object that represents the
result of the evaluation. The passed VariableResolver object is used
to resolve the variable name to a value. The GroupResolver object is
used if there are any aggregate functions in the evaluation - this can be
null if evaluating an expression without grouping aggregates. The query
context object contains contextual information about the environment of
the query.
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.
|
getEndExpression | public Expression getEndExpression()(Code) | | Returns the end Expression of this expression. For example, an expression
of 'ab' has an end expression of 'b'. The expression 'abc+=' has an end
expression of 'abc+='.
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.
|
getQueryPlanNode | public QueryPlanNode getQueryPlanNode()(Code) | | Returns the QueryPlanNode object in this expression if it evaluates to a
single QueryPlanNode, otherwise returns null.
|
getVariable | public Variable getVariable()(Code) | | Returns the Variable if this expression evaluates to a single variable,
otherwise returns null. A correlated variable will not be returned.
|
hasAggregateFunction | public boolean hasAggregateFunction(QueryContext context)(Code) | | Cascades through the expression and if any aggregate functions are found
returns true, otherwise returns false.
|
hasSubQuery | public boolean hasSubQuery()(Code) | | Returns true if the expression has a subquery (eg 'in ( select ... )')
somewhere in it (this cascades through function parameters also).
|
isConstant | public boolean isConstant()(Code) | | Returns true if the expression doesn't include any variables or non
constant functions (is constant). Note that a correlated variable is
considered a constant.
|
last | public Object last()(Code) | | Returns the element at the end of the postfix list (the last element).
|
parse | public static Expression parse(String expression)(Code) | | Static method that parses the given string which contains an expression
into an Expression object. For example, this will parse strings such
as '(a + 9) * 2 = b' or 'upper(concat('12', '56', id))'.
Care should be taken to not use this method inside an inner loop because
it creates a lot of objects.
|
prepare | public void prepare(ExpressionPreparer preparer) throws DatabaseException(Code) | | A general prepare that cascades through the expression and its parents and
substitutes an elements that the preparer wants to substitute.
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.
|
returnTType | public TType returnTType(VariableResolver resolver, QueryContext context)(Code) | | Determines the type of object this expression evaluates to. We determine
this by looking at the last element of the expression. If the last
element is a TType object, it returns the type. If the last element is a
Function, Operator or Variable then it returns the type that these
objects have set as their result type.
|
setElementAt | public void setElementAt(int n, Object ob)(Code) | | Sets the element at the given position in the postfix list. This should
be called after the expression has been setup to alter variable alias
names, etc.
|
size | public int size()(Code) | | Returns the number of elements and operators that are in this postfix
list.
|
split | public Expression[] split()(Code) | | 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.
For example, (a + b) - (c + d) will return { (a + b), (c + d) }. Or
more a more useful example is;
id + 3 > part_id - 2 will return ( id + 3, part_id - 2 }
|
text | public StringBuffer text()(Code) | | Returns the StringBuffer that we can use to append plain text
representation as we are parsing the expression.
|
toString | public String toString()(Code) | | Returns a string representation of this object for diagnostic
purposes.
|
|
|