use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class StringConcatMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return null;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
Expression arg0Expr = expr.getArguments().get(0);
Object arg0Val = null;
if (arg0Expr instanceof PrimaryExpression) {
arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
} else if (arg0Expr instanceof ParameterExpression) {
arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
} else if (arg0Expr instanceof Literal) {
arg0Val = ((Literal) arg0Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
}
if (!(arg0Val instanceof String)) {
throw new NucleusException(method + "(param1) : param1 must be String");
}
return ((String) invokedValue).concat((String) arg0Val);
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class StringSubstringMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return null;
}
if (!(invokedValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
int arg0 = -1;
Expression arg0Expr = expr.getArguments().get(0);
Object arg0Val = null;
if (arg0Expr instanceof PrimaryExpression) {
arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
} else if (arg0Expr instanceof ParameterExpression) {
arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
} else if (arg0Expr instanceof Literal) {
arg0Val = ((Literal) arg0Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
}
if (!(arg0Val instanceof Number)) {
throw new NucleusException(method + "(param1[,param2]) : param1 must be numeric");
}
arg0 = ((Number) arg0Val).intValue();
String result = null;
if (expr.getArguments().size() == 2) {
int arg1 = -1;
Expression arg1Expr = expr.getArguments().get(1);
Object arg1Val = null;
if (arg1Expr instanceof PrimaryExpression) {
arg1Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg1Expr);
} else if (arg1Expr instanceof ParameterExpression) {
arg1Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg1Expr);
} else if (arg1Expr instanceof Literal) {
arg1Val = ((Literal) arg1Expr).getLiteral();
} else if (arg0Expr instanceof DyadicExpression) {
arg1Val = ((DyadicExpression) arg1Expr).evaluate(eval);
}
if (!(arg1Val instanceof Number)) {
throw new NucleusException(method + "(param1,param2) : param2 must be numeric");
}
arg1 = ((Number) arg1Val).intValue();
if (((String) invokedValue).length() < arg1) {
if (((String) invokedValue).length() < arg0) {
return null;
}
return ((String) invokedValue).substring(arg0);
}
result = ((String) invokedValue).substring(arg0, arg1);
} else {
if (((String) invokedValue).length() < arg0) {
return null;
}
result = ((String) invokedValue).substring(arg0);
}
return result;
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class OptionalOrElseMethod method evaluate.
/* (non-Javadoc)
* @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
*/
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
String method = expr.getOperation();
if (invokedValue == null) {
return null;
}
if (!(invokedValue instanceof Optional)) {
throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
}
List<Expression> args = expr.getArguments();
if (args == null || args.isEmpty() || args.size() != 1) {
throw new NucleusException("orElse requires one argument");
}
Expression argExpr = args.get(0);
Object argument = null;
if (argExpr instanceof Literal) {
argument = ((Literal) argExpr).getLiteral();
} else if (argExpr instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) argExpr;
argument = eval.getValueForPrimaryExpression(primExpr);
} else if (argExpr instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) argExpr;
argument = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (argExpr instanceof InvokeExpression) {
argument = eval.getValueForInvokeExpression((InvokeExpression) argExpr);
} else if (argExpr instanceof DyadicExpression) {
argument = ((DyadicExpression) argExpr).evaluate(eval);
} else {
throw new NucleusException(method + "(param1) where param is instanceof " + argExpr.getClass().getName() + " not supported");
}
return ((Optional) invokedValue).orElse(argument);
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class JDOQLCompiler method containsOnlyGroupingOrAggregates.
/**
* Convenience method to check the provided expression for whether it contains only grouping expressions
* or aggregates
* @param expr The expression to check
* @param exprGrouping The grouping expressions
* @return Whether it contains only grouping or aggregates
*/
private static boolean containsOnlyGroupingOrAggregates(Expression expr, Expression[] exprGrouping) {
if (expr == null) {
return true;
} else if (expr instanceof DyadicExpression) {
Expression left = expr.getLeft();
Expression right = expr.getRight();
if (!containsOnlyGroupingOrAggregates(left, exprGrouping)) {
return false;
}
if (!containsOnlyGroupingOrAggregates(right, exprGrouping)) {
return false;
}
return true;
} else if (expr instanceof InvokeExpression) {
InvokeExpression invExpr = (InvokeExpression) expr;
if (isExpressionGroupingOrAggregate(invExpr, exprGrouping)) {
return true;
}
Expression invokedExpr = invExpr.getLeft();
if (invokedExpr != null && !containsOnlyGroupingOrAggregates(invokedExpr, exprGrouping)) {
// Check invoked object
return false;
}
List<Expression> invArgs = invExpr.getArguments();
if (invArgs != null) {
// Check invocation arguments
Iterator<Expression> iter = invArgs.iterator();
while (iter.hasNext()) {
Expression argExpr = iter.next();
if (!containsOnlyGroupingOrAggregates(argExpr, exprGrouping)) {
return false;
}
}
}
return true;
} else if (expr instanceof PrimaryExpression) {
return isExpressionGroupingOrAggregate(expr, exprGrouping);
} else if (expr instanceof Literal) {
return true;
} else if (expr instanceof ParameterExpression) {
return true;
} else if (expr instanceof VariableExpression) {
return true;
}
return false;
}
use of org.datanucleus.query.expression.DyadicExpression in project datanucleus-core by datanucleus.
the class JDOQLQueryHelper method getJDOQLForExpression.
public static 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(JDOQLQueryHelper.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_BIT_AND) {
str.append(" & ");
} else if (dyExpr.getOperator() == Expression.OP_BIT_OR) {
str.append(" | ");
} else if (dyExpr.getOperator() == Expression.OP_BIT_XOR) {
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(JDOQLQueryHelper.getJDOQLForExpression(right));
}
str.append(")");
return str.toString();
} else if (expr instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) expr;
if (primExpr.getLeft() != null) {
return JDOQLQueryHelper.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(JDOQLQueryHelper.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(JDOQLQueryHelper.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");
}
}
Aggregations