use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.
the class VarThisCompilationOptimiser method replaceVariableWithCandidateInExpression.
/**
* Method that replaces any occurrence of the specified variable in the provided expression with the
* candidate primary expression. Recurses to sub-expressions.
* @param varName Variable name
* @param expr The expression to update
* @return Updated expression
*/
private Expression replaceVariableWithCandidateInExpression(String varName, Expression expr) {
if (expr == null) {
return null;
}
if (expr instanceof VariableExpression && ((VariableExpression) expr).getId().equals(varName)) {
List<String> tuples = new ArrayList<String>();
tuples.add(compilation.getCandidateAlias());
Expression replExpr = new PrimaryExpression(tuples);
replExpr.bind(compilation.getSymbolTable());
return replExpr;
} else if (expr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) expr;
if (dyExpr.getLeft() != null) {
dyExpr.setLeft(replaceVariableWithCandidateInExpression(varName, dyExpr.getLeft()));
}
if (dyExpr.getRight() != null) {
dyExpr.setRight(replaceVariableWithCandidateInExpression(varName, dyExpr.getRight()));
}
} else if (expr instanceof PrimaryExpression) {
if (expr.getLeft() != null) {
if (expr.getLeft() instanceof VariableExpression && ((VariableExpression) expr.getLeft()).getId().equals(varName)) {
// Needs to be relative to candidate so just remove the "left"
expr.setLeft(null);
} else {
expr.setLeft(replaceVariableWithCandidateInExpression(varName, expr.getLeft()));
}
}
} else if (expr instanceof InvokeExpression) {
InvokeExpression invokeExpr = (InvokeExpression) expr;
if (invokeExpr.getLeft() != null) {
invokeExpr.setLeft(replaceVariableWithCandidateInExpression(varName, invokeExpr.getLeft()));
}
// List<Expression> args = invokeExpr.getArguments(); // TODO Process Invoke args
}
// TODO More combinations
return expr;
}
use of org.datanucleus.query.expression.Expression in project datanucleus-core by datanucleus.
the class JavaQueryInMemoryEvaluator method handleAggregates.
/**
* Checks if there are aggregates and handle it.
* @param resultSet The resultSet containing all elements
* @return A list with aggregated elements
*/
private List handleAggregates(List resultSet) {
final Expression[] grouping = compilation.getExprGrouping();
Comparator c = new Comparator() {
public int compare(Object arg0, Object arg1) {
for (int i = 0; i < grouping.length; i++) {
state.put(candidateAlias, arg0);
Object a = grouping[i].evaluate(evaluator);
state.put(candidateAlias, arg1);
Object b = grouping[i].evaluate(evaluator);
// Put any null values at the end
if (a == null && b == null) {
return 0;
} else if (a == null) {
return -1;
} else if (b == null) {
return 1;
} else {
int result = ((Comparable) a).compareTo(b);
if (result != 0) {
return result;
}
}
}
return 0;
}
};
List groups = new ArrayList();
List group = new ArrayList();
groups.add(group);
for (int i = 0; i < resultSet.size(); i++) {
if (i > 0) {
if (c.compare(resultSet.get(i - 1), resultSet.get(i)) != 0) {
group = new ArrayList();
groups.add(group);
}
}
group.add(resultSet.get(i));
}
List result = new ArrayList();
Expression having = compilation.getExprHaving();
if (having != null) {
for (int i = 0; i < groups.size(); i++) {
if (satisfiesHavingClause((List) groups.get(i))) {
result.addAll((Collection) groups.get(i));
}
}
} else {
for (int i = 0; i < groups.size(); i++) {
result.addAll((Collection) groups.get(i));
}
}
return result;
}
use of org.datanucleus.query.expression.Expression in project datanucleus-api-jdo by datanucleus.
the class ComparableExpressionImpl method lt.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.Expression#lt(java.lang.Object)
*/
public BooleanExpression lt(T t) {
Expression leftQueryExpr = queryExpr;
Expression rightQueryExpr = new Literal(t);
org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_LT, rightQueryExpr);
return new BooleanExpressionImpl(queryExpr);
}
use of org.datanucleus.query.expression.Expression in project datanucleus-api-jdo by datanucleus.
the class ComparableExpressionImpl method gt.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.Expression#gt(org.datanucleus.query.typesafe.ComparableExpression)
*/
public BooleanExpression gt(ComparableExpression expr) {
Expression leftQueryExpr = queryExpr;
Expression rightQueryExpr = ((ExpressionImpl) expr).getQueryExpression();
org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_GT, rightQueryExpr);
return new BooleanExpressionImpl(queryExpr);
}
use of org.datanucleus.query.expression.Expression in project datanucleus-api-jdo by datanucleus.
the class ComparableExpressionImpl method gt.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.Expression#gt(java.lang.Object)
*/
public BooleanExpression gt(T t) {
Expression leftQueryExpr = queryExpr;
Expression rightQueryExpr = new Literal(t);
org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_GT, rightQueryExpr);
return new BooleanExpressionImpl(queryExpr);
}
Aggregations