Search in sources :

Example 56 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.

the class NullIfFunction method getValueForArgExpression.

protected Object getValueForArgExpression(Expression argExpr, InMemoryExpressionEvaluator eval) {
    Object argValue = null;
    if (argExpr instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) argExpr;
        argValue = eval.getValueForPrimaryExpression(primExpr);
    } else if (argExpr instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) argExpr;
        argValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (argExpr instanceof Literal) {
        argValue = ((Literal) argExpr).getLiteral();
    } else {
        throw new NucleusException("Don't support NULLIF with argument of type " + argExpr.getClass().getName());
    }
    return argValue;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 57 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.

the class EnumMatchesMethod 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 instanceof Enum)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    String arg = null;
    Object argObj = null;
    Object param = expr.getArguments().get(0);
    if (expr.getArguments().size() > 1) {
        NucleusLogger.QUERY.info("Please note that any escape character is currently ignored");
    // TODO Cater for optional second argument that is the escape character
    }
    if (param instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param;
        argObj = eval.getValueForPrimaryExpression(primExpr);
    } else if (param instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param;
        argObj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (param instanceof Literal) {
        argObj = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param, num1, num2) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    arg = QueryUtils.getStringValue(argObj);
    return ((Enum) invokedValue).toString().matches(arg) ? Boolean.TRUE : Boolean.FALSE;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 58 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression in project datanucleus-core by datanucleus.

the class CollectionContainsMethod 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 instanceof Collection)) {
        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, ((Collection) invokedValue).toArray());
        }
    } else {
        // TODO Implement this
        throw new NucleusException("Dont currently support use of contains(" + param.getClass().getName() + ")");
    }
    return ((Collection) invokedValue).contains(paramValue) ? Boolean.TRUE : 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) Collection(java.util.Collection) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 59 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression 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 60 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression 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

PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)77 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)73 Literal (org.datanucleus.query.expression.Literal)60 NucleusException (org.datanucleus.exceptions.NucleusException)59 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)54 VariableExpression (org.datanucleus.query.expression.VariableExpression)48 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)46 Expression (org.datanucleus.query.expression.Expression)46 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)25 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)23 OrderExpression (org.datanucleus.query.expression.OrderExpression)22 HashMap (java.util.HashMap)16 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)15 ClassExpression (org.datanucleus.query.expression.ClassExpression)15 JoinExpression (org.datanucleus.query.expression.JoinExpression)15 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)15 Product (org.datanucleus.samples.store.Product)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)12 JPQLCompiler (org.datanucleus.query.compiler.JPQLCompiler)9 List (java.util.List)8