Search in sources :

Example 66 with Literal

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

the class MapContainsValueMethod 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 Map)) {
        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 values
            throw new VariableNotSetException(varExpr, ((Map) invokedValue).values().toArray());
        }
    } else {
        throw new NucleusException("Dont currently support use of containsValue(" + param.getClass().getName() + ")");
    }
    return ((Map) invokedValue).containsValue(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) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map)

Example 67 with Literal

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

the class ModFunction 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 param1 = expr.getArguments().get(0);
    int param1Value = -1;
    if (param1 instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param1;
        Object val = eval.getValueForPrimaryExpression(primExpr);
        if (val instanceof Number) {
            param1Value = ((Number) val).intValue();
        } else {
            throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " but should be integer");
        }
    } else if (param1 instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param1;
        Object val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
        if (val instanceof Number) {
            param1Value = ((Number) val).intValue();
        } else {
            throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " but should be integer");
        }
    } else if (param1 instanceof Literal) {
        Object val = ((Literal) param1).getLiteral();
        if (val instanceof Number) {
            param1Value = ((Number) val).intValue();
        } else {
            throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " but should be integer");
        }
    } else {
        throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param1.getClass().getName() + " not supported");
    }
    Object param2 = expr.getArguments().get(1);
    int param2Value = -1;
    if (param2 instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param2;
        Object val = eval.getValueForPrimaryExpression(primExpr);
        if (val instanceof Number) {
            param2Value = ((Number) val).intValue();
        } else {
            throw new NucleusException(method + "(num1, num2) where num2 is instanceof " + param2.getClass().getName() + " but should be integer");
        }
    } else if (param2 instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param2;
        Object val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
        if (val instanceof Number) {
            param2Value = ((Number) val).intValue();
        } else {
            throw new NucleusException(method + "(num1, num2) where num1 is instanceof " + param2.getClass().getName() + " but should be integer");
        }
    } else if (param2 instanceof Literal) {
        Object val = ((Literal) param2).getLiteral();
        if (val instanceof Number) {
            param2Value = ((Number) val).intValue();
        } else {
            throw new NucleusException(method + "(num1, num2) where num2 is instanceof " + param2.getClass().getName() + " but should be integer");
        }
    } else {
        throw new NucleusException(method + "(num1, num2) where num2 is instanceof " + param2.getClass().getName() + " not supported");
    }
    return Integer.valueOf(param1Value % param2Value);
}
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 68 with Literal

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

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

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

Aggregations

Literal (org.datanucleus.query.expression.Literal)83 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)62 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)61 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)60 NucleusException (org.datanucleus.exceptions.NucleusException)54 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)52 VariableExpression (org.datanucleus.query.expression.VariableExpression)38 Expression (org.datanucleus.query.expression.Expression)37 ArrayList (java.util.ArrayList)18 BooleanExpression (javax.jdo.query.BooleanExpression)18 NumericExpression (javax.jdo.query.NumericExpression)18 PersistableExpression (javax.jdo.query.PersistableExpression)18 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)18 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)17 OrderExpression (org.datanucleus.query.expression.OrderExpression)17 HashMap (java.util.HashMap)14 Expression (javax.jdo.query.Expression)14 ClassExpression (org.datanucleus.query.expression.ClassExpression)12 JoinExpression (org.datanucleus.query.expression.JoinExpression)12 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)12