use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class ConcatFunction 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();
Object param = expr.getArguments().get(0);
Object paramValue = null;
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
paramValue = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
paramValue = ((Literal) param).getLiteral();
} else {
throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
}
Object param2 = expr.getArguments().get(1);
Object param2Value = null;
if (param2 instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
param2Value = eval.getValueForPrimaryExpression(primExpr);
} else if (param2 instanceof ParameterExpression) {
ParameterExpression param2Expr = (ParameterExpression) param2;
param2Value = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), param2Expr);
} else if (param2 instanceof Literal) {
param2Value = ((Literal) param).getLiteral();
} else {
throw new NucleusException(method + "(param, param2) where param2 is instanceof " + param2.getClass().getName() + " not supported");
}
if (paramValue == null) {
return null;
}
return ((String) paramValue).concat((String) param2Value);
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class LengthFunction 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();
Object param = expr.getArguments().get(0);
Object paramValue = null;
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
paramValue = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
paramValue = ((Literal) param).getLiteral();
} else {
throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
}
if (paramValue == null) {
return Integer.valueOf(0);
}
if (!(paramValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, paramValue.getClass().getName()));
}
return Integer.valueOf(((String) paramValue).length());
}
use of org.datanucleus.query.expression.Literal in project datanucleus-core by datanucleus.
the class UpperFunction 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();
Object param = expr.getArguments().get(0);
Object paramValue = null;
if (param instanceof PrimaryExpression) {
PrimaryExpression primExpr = (PrimaryExpression) param;
paramValue = eval.getValueForPrimaryExpression(primExpr);
} else if (param instanceof ParameterExpression) {
ParameterExpression paramExpr = (ParameterExpression) param;
paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
} else if (param instanceof Literal) {
paramValue = ((Literal) param).getLiteral();
} else {
throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
}
if (paramValue == null) {
return null;
}
if (!(paramValue instanceof String)) {
throw new NucleusException(Localiser.msg("021011", method, paramValue.getClass().getName()));
}
return ((String) paramValue).toUpperCase();
}
use of org.datanucleus.query.expression.Literal 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.Literal in project datanucleus-core by datanucleus.
the class InMemoryExpressionEvaluator method getValueForInvokeExpression.
/**
* Method to evaluate an InvokeExpression.
* Will navigate along chained invocations, evaluating the first one, then the second one etc
* until it gets the value for the passed in expression.
* @param invokeExpr The InvokeExpression
* @return The value
*/
public Object getValueForInvokeExpression(InvokeExpression invokeExpr) {
String method = invokeExpr.getOperation();
if (invokeExpr.getLeft() == null) {
// Static function
if (method.toLowerCase().equals("count")) {
Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
SetExpression setexpr = new SetExpression(coll, candidateAlias);
Expression paramExpr = invokeExpr.getArguments().get(0);
if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
// No dups in HashSet
coll = new HashSet(coll);
}
int stackSizeOrig = stack.size();
Object returnVal = setexpr.count(paramExpr, this);
while (stack.size() > stackSizeOrig) {
// Remove any expressions put on the stack while evaluating the aggregate
stack.pop();
}
return returnVal;
} else if (method.toLowerCase().equals("sum")) {
Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
SetExpression setexpr = new SetExpression(coll, candidateAlias);
Expression paramExpr = invokeExpr.getArguments().get(0);
if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
// No dups in HashSet
coll = new HashSet(coll);
}
int stackSizeOrig = stack.size();
Object returnVal = setexpr.sum(paramExpr, this, state);
while (stack.size() > stackSizeOrig) {
// Remove any expressions put on the stack while evaluating the aggregate
stack.pop();
}
return returnVal;
} else if (method.toLowerCase().equals("avg")) {
Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
SetExpression setexpr = new SetExpression(coll, candidateAlias);
Expression paramExpr = invokeExpr.getArguments().get(0);
if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
// No dups in HashSet
coll = new HashSet(coll);
}
int stackSizeOrig = stack.size();
Object returnVal = setexpr.avg(paramExpr, this, state);
while (stack.size() > stackSizeOrig) {
// Remove any expressions put on the stack while evaluating the aggregate
stack.pop();
}
return returnVal;
} else if (method.toLowerCase().equals("min")) {
Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
SetExpression setexpr = new SetExpression(coll, candidateAlias);
Expression paramExpr = invokeExpr.getArguments().get(0);
if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
// No dups in HashSet
coll = new HashSet(coll);
}
int stackSizeOrig = stack.size();
Object returnVal = setexpr.min(paramExpr, this, state);
while (stack.size() > stackSizeOrig) {
// Remove any expressions put on the stack while evaluating the aggregate
stack.pop();
}
return returnVal;
} else if (method.toLowerCase().equals("max")) {
Collection coll = (Collection) state.get(JavaQueryInMemoryEvaluator.RESULTS_SET);
SetExpression setexpr = new SetExpression(coll, candidateAlias);
Expression paramExpr = invokeExpr.getArguments().get(0);
if (paramExpr.getOperator() == Expression.OP_DISTINCT) {
// No dups in HashSet
coll = new HashSet(coll);
}
int stackSizeOrig = stack.size();
Object returnVal = setexpr.max(paramExpr, this, state);
while (stack.size() > stackSizeOrig) {
// Remove any expressions put on the stack while evaluating the aggregate
stack.pop();
}
return returnVal;
} else {
// Try to find a supported static method with this name
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(null, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, null, this);
}
NucleusLogger.QUERY.warn("Query contains call to static method " + method + " yet no support is available for in-memory evaluation of this");
return new InMemoryFailure();
}
} else if (invokeExpr.getLeft() instanceof ParameterExpression) {
// {paramExpr}.method(...)
Object invokedValue = QueryUtils.getValueForParameterExpression(parameterValues, (ParameterExpression) invokeExpr.getLeft());
// Invoke method on this object
Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, invokedValue, this);
}
if (invokedValue != null) {
NucleusLogger.QUERY.warn("Query contains call to method " + invokedValue.getClass().getName() + "." + method + " yet no support is available for this");
} else {
NucleusLogger.QUERY.warn("Query contains call to static method " + method + " yet no support is available for this");
}
return new InMemoryFailure();
} else if (invokeExpr.getLeft() instanceof PrimaryExpression) {
// {primaryExpr}.method(...)
Object invokedValue = getValueForPrimaryExpression((PrimaryExpression) invokeExpr.getLeft());
if (invokedValue instanceof InMemoryFailure) {
return invokedValue;
}
// Invoke method on this object
Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, invokedValue, this);
}
NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
return new InMemoryFailure();
} else if (invokeExpr.getLeft() instanceof InvokeExpression) {
// {invokeExpr}.method(...)
Object invokedValue = getValueForInvokeExpression((InvokeExpression) invokeExpr.getLeft());
// Invoke method on this object
Class invokedType = invokedValue != null ? invokedValue.getClass() : (invokeExpr.getLeft().getSymbol() != null ? invokeExpr.getLeft().getSymbol().getValueType() : null);
if (invokedType == null) {
return new InMemoryFailure();
}
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, invokedValue, this);
}
NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
return new InMemoryFailure();
} else if (invokeExpr.getLeft() instanceof VariableExpression) {
// {invokeExpr}.method(...)
Object invokedValue = getValueForVariableExpression((VariableExpression) invokeExpr.getLeft());
// Invoke method on this object
Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, invokedValue, this);
}
NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
return new InMemoryFailure();
} else if (invokeExpr.getLeft() instanceof Literal) {
// {invokeExpr}.method(...)
Object invokedValue = ((Literal) invokeExpr.getLeft()).getLiteral();
// Invoke method on this object
Class invokedType = invokedValue != null ? invokedValue.getClass() : invokeExpr.getLeft().getSymbol().getValueType();
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, invokedValue, this);
}
NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
return new InMemoryFailure();
} else if (invokeExpr.getLeft() instanceof ArrayExpression) {
// {invokeExpr}.method(...)
Object invokedValue = getValueForArrayExpression((ArrayExpression) invokeExpr.getLeft());
// Invoke method on this object
Class invokedType = invokedValue.getClass();
InvocationEvaluator methodEval = queryMgr.getInMemoryEvaluatorForMethod(invokedType, method);
if (methodEval != null) {
return methodEval.evaluate(invokeExpr, invokedValue, this);
}
NucleusLogger.QUERY.warn("Query contains call to method " + invokedType.getName() + "." + method + " yet no support is available for this");
return new InMemoryFailure();
} else {
NucleusLogger.QUERY.warn("No support is available for in-memory evaluation of methods invoked" + " on expressions of type " + invokeExpr.getLeft().getClass().getName());
return new InMemoryFailure();
}
}
Aggregations