use of org.datanucleus.query.expression.ArrayExpression in project datanucleus-rdbms by datanucleus.
the class QueryToSQLMapper method getInvokedSqlExpressionForInvokeExpression.
protected SQLExpression getInvokedSqlExpressionForInvokeExpression(InvokeExpression expr) {
Expression invokedExpr = expr.getLeft();
SQLExpression invokedSqlExpr = null;
if (invokedExpr == null) {
// Static method
} else if (invokedExpr instanceof PrimaryExpression) {
processPrimaryExpression((PrimaryExpression) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof Literal) {
processLiteral((Literal) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof ParameterExpression) {
// TODO May be needed to set the second parameter to "false" here and then if the method
// being invoked needs the parameters as a normal SQLLiteral then allow it to convert it itself
processParameterExpression((ParameterExpression) invokedExpr, true);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof InvokeExpression) {
processInvokeExpression((InvokeExpression) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof VariableExpression) {
processVariableExpression((VariableExpression) invokedExpr);
invokedSqlExpr = stack.pop();
} else if (invokedExpr instanceof ArrayExpression) {
ArrayExpression arrExpr = (ArrayExpression) invokedExpr;
SQLExpression[] arrSqlExprs = new SQLExpression[arrExpr.getArraySize()];
for (int i = 0; i < arrExpr.getArraySize(); i++) {
Expression arrElemExpr = arrExpr.getElement(i);
arrElemExpr.evaluate(this);
arrSqlExprs[i] = stack.pop();
}
JavaTypeMapping m = exprFactory.getMappingForType(Object[].class, false);
invokedSqlExpr = new org.datanucleus.store.rdbms.sql.expression.ArrayExpression(stmt, m, arrSqlExprs);
} else if (invokedExpr instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) invokedExpr;
dyExpr.evaluate(this);
invokedSqlExpr = stack.pop();
} else {
throw new NucleusException("Dont currently support invoke expression " + invokedExpr);
}
return invokedSqlExpr;
}
use of org.datanucleus.query.expression.ArrayExpression 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