Search in sources :

Example 6 with VariableNotSetException

use of org.datanucleus.query.inmemory.VariableNotSetException in project datanucleus-core by datanucleus.

the class ArrayContainsMethod 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 Boolean.FALSE;
    }
    if (!invokedValue.getClass().isArray()) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    Object param = expr.getArguments().get(0);
    Object paramValue = null;
    if (param instanceof Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else 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 VariableExpression) {
        VariableExpression varExpr = (VariableExpression) param;
        try {
            paramValue = eval.getValueForVariableExpression(varExpr);
        } catch (VariableNotSetException vnse) {
            // Throw an exception with the possible values
            throw new VariableNotSetException(varExpr, (Object[]) invokedValue);
        }
    } else {
        // TODO Implement this
        throw new NucleusException("Dont currently support use of Array.contains(" + param.getClass().getName() + ")");
    }
    for (int i = 0; i < Array.getLength(invokedValue); i++) {
        Object elem = Array.get(invokedValue, i);
        if (elem == null && paramValue == null) {
            return Boolean.TRUE;
        } else if (elem != null && paramValue != null) {
            if (elem.equals(paramValue)) {
                return Boolean.TRUE;
            }
            if (!paramValue.getClass().isAssignableFrom(elem.getClass()) && !elem.getClass().isAssignableFrom(paramValue.getClass())) {
                // Types are different, so add specific type conversion handling
                if ((paramValue.getClass() == Long.class || paramValue.getClass() == Integer.class || paramValue.getClass() == Short.class) && (elem.getClass() == Long.class || elem.getClass() == Integer.class || elem.getClass() == Short.class)) {
                    long paramLong = ((Number) paramValue).longValue();
                    long elemLong = ((Number) elem).longValue();
                    if (paramLong == elemLong) {
                        return Boolean.TRUE;
                    }
                }
            }
        }
    }
    return Boolean.FALSE;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableNotSetException(org.datanucleus.query.inmemory.VariableNotSetException) Literal(org.datanucleus.query.expression.Literal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 7 with VariableNotSetException

use of org.datanucleus.query.inmemory.VariableNotSetException in project datanucleus-core by datanucleus.

the class ListGetMethod 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 List)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    Object param = expr.getArguments().get(0);
    Object paramValue = null;
    if (param instanceof Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else 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 VariableExpression) {
        VariableExpression varExpr = (VariableExpression) param;
        try {
            paramValue = eval.getValueForVariableExpression(varExpr);
        } catch (VariableNotSetException vnse) {
            // Throw an exception with the possible values of elements
            throw new VariableNotSetException(varExpr, ((List) invokedValue).toArray());
        }
    } else {
        throw new NucleusException("Dont currently support use of get(" + param.getClass().getName() + ")");
    }
    int paramInt;
    if (paramValue instanceof Number) {
        paramInt = ((Number) paramValue).intValue();
    } else {
        throw new NucleusException("List.get() should take in an integer but is " + paramValue);
    }
    return ((List) invokedValue).get(paramInt);
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableNotSetException(org.datanucleus.query.inmemory.VariableNotSetException) Literal(org.datanucleus.query.expression.Literal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) List(java.util.List) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)7 Literal (org.datanucleus.query.expression.Literal)7 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)7 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)7 VariableExpression (org.datanucleus.query.expression.VariableExpression)7 VariableNotSetException (org.datanucleus.query.inmemory.VariableNotSetException)7 Map (java.util.Map)4 Collection (java.util.Collection)1 List (java.util.List)1