use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-api-jdo by datanucleus.
the class ExpressionImpl method eq.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.Expression#eq(java.lang.Object)
*/
public BooleanExpression eq(T t) {
org.datanucleus.query.expression.Expression leftQueryExpr = queryExpr;
org.datanucleus.query.expression.Expression rightQueryExpr = new Literal(t);
return new BooleanExpressionImpl(new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_EQ, rightQueryExpr));
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-api-jdo by datanucleus.
the class AbstractJDOQLTypedQuery method compile.
/**
* Method to compile the query as it is currently defined.
* @param mmgr Metadata manager
* @param clr ClassLoader resolver
* @return The generic compilation
*/
protected QueryCompilation compile(MetaDataManager mmgr, ClassLoaderResolver clr) {
SymbolTable symtbl = new SymbolTable();
symtbl.setSymbolResolver(new JDOQLSymbolResolver(mmgr, clr, symtbl, candidateCls, candidateAlias));
symtbl.addSymbol(new PropertySymbol(candidateAlias, candidateCls));
org.datanucleus.query.expression.Expression[] resultExprs = null;
if (result != null && !result.isEmpty()) {
resultExprs = new org.datanucleus.query.expression.Expression[result.size()];
Iterator iter = result.iterator();
int i = 0;
while (iter.hasNext()) {
ExpressionImpl result = (ExpressionImpl) iter.next();
org.datanucleus.query.expression.Expression resultExpr = result.getQueryExpression();
resultExpr.bind(symtbl);
resultExprs[i++] = resultExpr;
}
if (resultExprs.length == 1 && resultExprs[0] instanceof PrimaryExpression) {
// Check for special case of "Object(p)" in result, which means no special result
String resultExprId = ((PrimaryExpression) resultExprs[0]).getId();
if (resultExprId.equalsIgnoreCase(candidateAlias)) {
resultExprs = null;
}
}
}
org.datanucleus.query.expression.Expression filterExpr = null;
if (filter != null) {
filterExpr = filter.getQueryExpression();
if (filterExpr != null) {
filterExpr.bind(symtbl);
}
}
org.datanucleus.query.expression.Expression[] groupingExprs = null;
if (grouping != null && !grouping.isEmpty()) {
groupingExprs = new org.datanucleus.query.expression.Expression[grouping.size()];
Iterator iter = grouping.iterator();
int i = 0;
while (iter.hasNext()) {
ExpressionImpl grp = (ExpressionImpl) iter.next();
org.datanucleus.query.expression.Expression groupingExpr = grp.getQueryExpression();
groupingExpr.bind(symtbl);
groupingExprs[i++] = groupingExpr;
}
}
org.datanucleus.query.expression.Expression havingExpr = null;
if (having != null) {
havingExpr = having.getQueryExpression();
havingExpr.bind(symtbl);
}
org.datanucleus.query.expression.Expression[] orderExprs = null;
if (ordering != null && !ordering.isEmpty()) {
orderExprs = new org.datanucleus.query.expression.Expression[ordering.size()];
Iterator<OrderExpressionImpl> iter = ordering.iterator();
int i = 0;
while (iter.hasNext()) {
OrderExpressionImpl order = iter.next();
org.datanucleus.query.expression.OrderExpression orderExpr = new org.datanucleus.query.expression.OrderExpression(((ExpressionImpl) order.getExpression()).getQueryExpression(), order.getDirection() == OrderDirection.ASC ? "ascending" : "descending");
orderExpr.bind(symtbl);
orderExprs[i++] = orderExpr;
}
}
org.datanucleus.query.expression.Expression[] updateExprs = null;
if (this.updateExprs != null) {
Iterator<ExpressionImpl> expIter = this.updateExprs.iterator();
Iterator<ExpressionImpl> valIter = this.updateVals.iterator();
updateExprs = new Expression[this.updateExprs.size()];
int i = 0;
while (expIter.hasNext()) {
ExpressionImpl updateExpr = expIter.next();
ExpressionImpl updateVal = valIter.next();
updateExprs[i++] = new DyadicExpression(updateExpr.getQueryExpression(), Expression.OP_EQ, updateVal.getQueryExpression());
}
}
compilation = new QueryCompilation(candidateCls, candidateAlias, symtbl, resultExprs, null, filterExpr, groupingExprs, havingExpr, orderExprs, updateExprs);
compilation.setQueryLanguage(Query.LANGUAGE_JDOQL);
return compilation;
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-api-jdo by datanucleus.
the class AbstractJDOQLTypedQuery method getJDOQLForExpression.
public String getJDOQLForExpression(Expression expr) {
if (expr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) expr;
Expression left = dyExpr.getLeft();
Expression right = dyExpr.getRight();
StringBuilder str = new StringBuilder("(");
if (dyExpr.getOperator() == Expression.OP_DISTINCT) {
// Distinct goes in front of the left expression
str.append("DISTINCT ");
}
if (left != null) {
str.append(getJDOQLForExpression(left));
}
// Special cases
if (dyExpr.getOperator() == Expression.OP_AND) {
str.append(" && ");
} else if (dyExpr.getOperator() == Expression.OP_OR) {
str.append(" || ");
} else if (dyExpr.getOperator() == Expression.OP_ADD) {
str.append(" + ");
} else if (dyExpr.getOperator() == Expression.OP_SUB) {
str.append(" - ");
} else if (dyExpr.getOperator() == Expression.OP_MUL) {
str.append(" * ");
} else if (dyExpr.getOperator() == Expression.OP_DIV) {
str.append(" / ");
} else if (dyExpr.getOperator() == Expression.OP_EQ) {
str.append(" == ");
} else if (dyExpr.getOperator() == Expression.OP_GT) {
str.append(" > ");
} else if (dyExpr.getOperator() == Expression.OP_LT) {
str.append(" < ");
} else if (dyExpr.getOperator() == Expression.OP_GTEQ) {
str.append(" >= ");
} else if (dyExpr.getOperator() == Expression.OP_LTEQ) {
str.append(" <= ");
} else if (dyExpr.getOperator() == Expression.OP_NOTEQ) {
str.append(" != ");
} else if (dyExpr.getOperator() == Expression.OP_DISTINCT) {
// Processed above
} else {
// TODO Support other operators
throw new UnsupportedOperationException("Dont currently support operator " + dyExpr.getOperator() + " in JDOQL conversion");
}
if (right != null) {
str.append(getJDOQLForExpression(right));
}
str.append(")");
return str.toString();
} else if (expr instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) expr;
if (primExpr.getLeft() != null) {
return getJDOQLForExpression(primExpr.getLeft()) + "." + primExpr.getId();
}
return primExpr.getId();
} else if (expr instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) expr;
if (paramExpr.getId() != null) {
return ":" + paramExpr.getId();
}
return "?" + paramExpr.getPosition();
} else if (expr instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) expr;
return varExpr.getId();
} else if (expr instanceof InvokeExpression) {
InvokeExpression invExpr = (InvokeExpression) expr;
StringBuilder str = new StringBuilder();
if (invExpr.getLeft() != null) {
str.append(getJDOQLForExpression(invExpr.getLeft())).append(".");
}
str.append(invExpr.getOperation());
str.append("(");
List<Expression> args = invExpr.getArguments();
if (args != null) {
Iterator<Expression> iter = args.iterator();
while (iter.hasNext()) {
str.append(getJDOQLForExpression(iter.next()));
if (iter.hasNext()) {
str.append(",");
}
}
}
str.append(")");
return str.toString();
} else if (expr instanceof Literal) {
Literal litExpr = (Literal) expr;
Object value = litExpr.getLiteral();
if (value instanceof String || value instanceof Character) {
return "'" + value.toString() + "'";
} else if (value instanceof Boolean) {
return ((Boolean) value ? "TRUE" : "FALSE");
} else {
if (litExpr.getLiteral() == null) {
return "null";
}
return litExpr.getLiteral().toString();
}
} else {
throw new UnsupportedOperationException("Dont currently support " + expr.getClass().getName() + " in JDOQLHelper");
}
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-api-jdo by datanucleus.
the class BooleanExpressionImpl method or.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.BooleanExpression#or(org.datanucleus.query.typesafe.BooleanExpression)
*/
public BooleanExpression or(BooleanExpression expr) {
Expression leftQueryExpr = queryExpr;
Expression rightQueryExpr = ((ExpressionImpl) expr).getQueryExpression();
org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_OR, rightQueryExpr);
return new BooleanExpressionImpl(queryExpr);
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-api-jdo by datanucleus.
the class BooleanExpressionImpl method and.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.BooleanExpression#and(org.datanucleus.query.typesafe.BooleanExpression)
*/
public BooleanExpression and(BooleanExpression expr) {
Expression leftQueryExpr = queryExpr;
Expression rightQueryExpr = ((ExpressionImpl) expr).getQueryExpression();
org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_AND, rightQueryExpr);
return new BooleanExpressionImpl(queryExpr);
}
Aggregations