use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-core by datanucleus.
the class TemporalMinuteMethod 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) {
if (invokedValue == null && expr.getArguments() != null) {
// Specified as static function, so use argument of InvokeExpression
List<Expression> argExprs = expr.getArguments();
if (argExprs.size() > 1) {
throw new NucleusUserException("Incorrect number of arguments to MINUTE");
}
Expression argExpr = argExprs.get(0);
invokedValue = eval.getValueForExpression(argExpr);
}
if (invokedValue == null) {
return Boolean.FALSE;
}
if (!(invokedValue instanceof Date)) {
throw new NucleusException(Localiser.msg("021011", expr.getOperation(), invokedValue.getClass().getName()));
}
if (invokedValue instanceof Date) {
Calendar cal = Calendar.getInstance();
cal.setTime((Date) invokedValue);
return Integer.valueOf(cal.get(Calendar.MINUTE));
} else if (invokedValue instanceof Calendar) {
return Integer.valueOf(((Calendar) invokedValue).get(Calendar.MINUTE));
} else if (invokedValue instanceof LocalTime) {
return ((LocalTime) invokedValue).getMinute();
} else if (invokedValue instanceof LocalDateTime) {
return ((LocalDateTime) invokedValue).getMinute();
} else {
throw new NucleusUserException("We do not currently support MINUTE() with argument of type " + invokedValue.getClass().getName());
}
}
use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-core by datanucleus.
the class JavaQueryInMemoryEvaluator method handleResult.
/**
* Checks if there are aggregates and handles it.
* @param resultSet The resultSet containing all elements
* @return A list with aggregated elements
*/
private List handleResult(List resultSet) {
List result = new ArrayList();
final Expression[] grouping = compilation.getExprGrouping();
if (grouping != null) {
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();
if (!resultSet.isEmpty()) {
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));
}
// Apply the result to the generated groups
for (int i = 0; i < groups.size(); i++) {
group = (List) groups.get(i);
result.add(result(group));
}
} else {
boolean aggregates = false;
Expression[] resultExprs = compilation.getExprResult();
if (resultExprs.length > 0 && resultExprs[0] instanceof CreatorExpression) {
Expression[] resExpr = ((CreatorExpression) resultExprs[0]).getArguments().toArray(new Expression[((CreatorExpression) resultExprs[0]).getArguments().size()]);
for (int i = 0; i < resExpr.length; i++) {
if (resExpr[i] instanceof InvokeExpression) {
String method = ((InvokeExpression) resExpr[i]).getOperation().toLowerCase();
if (method.equals("count") || method.equals("sum") || method.equals("avg") || method.equals("min") || method.equals("max")) {
aggregates = true;
}
}
}
} else {
for (int i = 0; i < resultExprs.length; i++) {
if (resultExprs[i] instanceof InvokeExpression) {
String method = ((InvokeExpression) resultExprs[i]).getOperation().toLowerCase();
if (method.equals("count") || method.equals("sum") || method.equals("avg") || method.equals("min") || method.equals("max")) {
aggregates = true;
}
}
}
}
if (aggregates) {
result.add(result(resultSet));
} else {
for (int i = 0; i < resultSet.size(); i++) {
result.add(result(resultSet.get(i)));
}
}
}
if (!result.isEmpty() && ((Object[]) result.get(0)).length == 1) {
List r = result;
result = new ArrayList();
for (int i = 0; i < r.size(); i++) {
result.add(((Object[]) r.get(i))[0]);
}
}
return result;
}
use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-core by datanucleus.
the class InMemoryExpressionEvaluator method getValueForPrimaryExpression.
/**
* Convenience method to get the value for a PrimaryExpression.
* @param primExpr Expression
* @return The value in the object for this expression
*/
public Object getValueForPrimaryExpression(PrimaryExpression primExpr) {
Object value = null;
if (primExpr.getLeft() != null) {
// Get value of left expression
if (primExpr.getLeft() instanceof DyadicExpression) {
DyadicExpression dyExpr = (DyadicExpression) primExpr.getLeft();
if (dyExpr.getOperator() == Expression.OP_CAST) {
Expression castLeftExpr = dyExpr.getLeft();
if (castLeftExpr instanceof PrimaryExpression) {
value = getValueForPrimaryExpression((PrimaryExpression) castLeftExpr);
String castClassName = (String) ((Literal) dyExpr.getRight()).getLiteral();
if (value != null) {
// TODO Do this in the compilation stage, and check for ClassNotResolvedException
Class castClass = imports.resolveClassDeclaration(castClassName, clr, null);
if (!castClass.isAssignableFrom(value.getClass())) {
NucleusLogger.QUERY.debug("In-memory query requires cast of " + StringUtils.toJVMIDString(value) + " to " + castClass.getName() + " which is impossible, so exiting for this candidate");
return new InMemoryFailure();
}
}
} else if (castLeftExpr instanceof VariableExpression) {
value = getValueForVariableExpression((VariableExpression) castLeftExpr);
String castClassName = (String) ((Literal) dyExpr.getRight()).getLiteral();
if (value != null) {
// TODO Do this in the compilation stage, and check for ClassNotResolvedException
Class castClass = imports.resolveClassDeclaration(castClassName, clr, null);
if (!castClass.isAssignableFrom(value.getClass())) {
NucleusLogger.QUERY.debug("In-memory query requires cast of " + StringUtils.toJVMIDString(value) + " to " + castClass.getName() + " which is impossible, so exiting for this candidate");
return new InMemoryFailure();
}
}
} else {
// TODO Allow for cast of ParameterExpression
NucleusLogger.QUERY.warn("Dont currently support CastExpression of " + castLeftExpr);
return new InMemoryFailure();
}
} else {
NucleusLogger.QUERY.error("Dont currently support PrimaryExpression starting with DyadicExpression of " + dyExpr);
return new InMemoryFailure();
}
} else if (primExpr.getLeft() instanceof ParameterExpression) {
value = QueryUtils.getValueForParameterExpression(parameterValues, (ParameterExpression) primExpr.getLeft());
} else if (primExpr.getLeft() instanceof InvokeExpression) {
InvokeExpression invokeExpr = (InvokeExpression) primExpr.getLeft();
value = getValueForInvokeExpression(invokeExpr);
} else if (primExpr.getLeft() instanceof VariableExpression) {
VariableExpression varExpr = (VariableExpression) primExpr.getLeft();
try {
value = getValueForVariableExpression(varExpr);
} catch (VariableNotSetException vnse) {
// We don't know the possible values here!
NucleusLogger.QUERY.error("Attempt to access variable " + varExpr.getId() + " as part of primaryExpression " + primExpr + " : variable is not yet set!");
return new InMemoryFailure();
}
} else {
NucleusLogger.QUERY.warn("Dont currently support PrimaryExpression with left-side of " + primExpr.getLeft());
return new InMemoryFailure();
}
}
int firstTupleToProcess = 0;
if (value == null) {
if (state.containsKey(primExpr.getTuples().get(0))) {
// Get value from the first node
value = state.get(primExpr.getTuples().get(0));
firstTupleToProcess = 1;
} else if (state.containsKey(candidateAlias)) {
// Get value from the candidate
value = state.get(candidateAlias);
}
}
// Evaluate the field of this value
for (int i = firstTupleToProcess; i < primExpr.getTuples().size(); i++) {
String fieldName = primExpr.getTuples().get(i);
if (value instanceof Optional) {
// Treat Optional as the wrapped value
Optional opt = (Optional) value;
value = opt.isPresent() ? opt.get() : null;
}
if (!fieldName.equals(candidateAlias)) {
boolean getValueByReflection = true;
if (ec.getApiAdapter().isPersistent(value)) {
// Make sure this field is loaded
ObjectProvider valueOP = ec.findObjectProvider(value);
if (valueOP != null) {
AbstractMemberMetaData mmd = valueOP.getClassMetaData().getMetaDataForMember(fieldName);
if (mmd == null) {
NucleusLogger.QUERY.error("Cannot find " + fieldName + " member of " + valueOP.getClassMetaData().getFullClassName());
return new InMemoryFailure();
}
if (mmd.getAbsoluteFieldNumber() >= 0) {
// Field is managed so make sure it is loaded, and get its value
valueOP.isLoaded(mmd.getAbsoluteFieldNumber());
value = valueOP.provideField(mmd.getAbsoluteFieldNumber());
getValueByReflection = false;
}
}
}
if (getValueByReflection) {
value = ClassUtils.getValueOfFieldByReflection(value, fieldName);
}
}
}
return value;
}
use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-core by datanucleus.
the class NullIfFunction 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 ignored, InMemoryExpressionEvaluator eval) {
List<Expression> args = expr.getArguments();
if (args == null || args.isEmpty()) {
throw new NucleusException("NULLIF requires two arguments");
} else if (args.size() == 1) {
return getValueForArgExpression(args.get(0), eval);
}
Expression argExpr1 = args.get(0);
Expression argExpr2 = args.get(1);
Object argValue1 = getValueForArgExpression(argExpr1, eval);
Object argValue2 = getValueForArgExpression(argExpr2, eval);
if (argValue1 == argValue2) {
return null;
}
return argValue1;
}
use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-core by datanucleus.
the class CoalesceFunction 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 ignored, InMemoryExpressionEvaluator eval) {
List<Expression> args = expr.getArguments();
if (args == null || args.isEmpty()) {
return null;
}
Iterator<Expression> iter = args.iterator();
Object argValue = null;
while (iter.hasNext()) {
Expression argExpr = iter.next();
argValue = getValueForArgExpression(argExpr, eval);
if (argValue != null) {
return argValue;
}
}
return null;
}
Aggregations