Search in sources :

Example 6 with Literal

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

the class MathFunction 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 param = expr.getArguments().get(0);
    Object paramValue = null;
    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 InvokeExpression) {
        InvokeExpression invokeExpr = (InvokeExpression) param;
        paramValue = eval.getValueForInvokeExpression(invokeExpr);
    } else if (param instanceof Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else if (param instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) param;
        paramValue = dyExpr.evaluate(eval);
    } else {
        throw new NucleusException(method + "(num) where num is instanceof " + param.getClass().getName() + " not supported");
    }
    Object result = null;
    if (paramValue instanceof Double) {
        result = new Double(evaluateMathFunction(((Double) paramValue).doubleValue()));
    } else if (paramValue instanceof Float) {
        result = new Float(evaluateMathFunction(((Float) paramValue).floatValue()));
    } else if (paramValue instanceof BigDecimal) {
        result = new BigDecimal(evaluateMathFunction(((BigDecimal) paramValue).doubleValue()));
    } else if (paramValue instanceof Integer) {
        result = new Double(evaluateMathFunction(((Integer) paramValue).doubleValue()));
    } else if (paramValue instanceof Long) {
        result = new Double(evaluateMathFunction(((Long) paramValue).doubleValue()));
    } else {
        throw new NucleusException("Not possible to use " + getFunctionName() + " on value of type " + paramValue.getClass().getName());
    }
    return result;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 7 with Literal

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

the class StringCharAtMethod 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 Integer.valueOf(-1);
    }
    if (!(invokedValue instanceof String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    // Evaluate the argument
    Object arg1Obj = null;
    Object param = expr.getArguments().get(0);
    if (param instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param;
        arg1Obj = eval.getValueForPrimaryExpression(primExpr);
    } else if (param instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param;
        arg1Obj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (param instanceof Literal) {
        arg1Obj = ((Literal) param).getLiteral();
    } else if (param instanceof InvokeExpression) {
        arg1Obj = eval.getValueForInvokeExpression((InvokeExpression) param);
    } else if (param instanceof DyadicExpression) {
        arg1Obj = ((DyadicExpression) param).evaluate(eval);
    } else {
        throw new NucleusException(method + "(param1) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    if (!(arg1Obj instanceof Number)) {
        throw new NucleusException(method + "(param1]) : param1 must be numeric");
    }
    int arg1 = ((Number) arg1Obj).intValue();
    return Integer.valueOf(((String) invokedValue).indexOf(arg1));
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 8 with Literal

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

the class StringConcatMethod 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 String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    Expression arg0Expr = expr.getArguments().get(0);
    Object arg0Val = null;
    if (arg0Expr instanceof PrimaryExpression) {
        arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
    } else if (arg0Expr instanceof ParameterExpression) {
        arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
    } else if (arg0Expr instanceof Literal) {
        arg0Val = ((Literal) arg0Expr).getLiteral();
    } else if (arg0Expr instanceof DyadicExpression) {
        arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
    }
    if (!(arg0Val instanceof String)) {
        throw new NucleusException(method + "(param1) : param1 must be String");
    }
    return ((String) invokedValue).concat((String) arg0Val);
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 9 with Literal

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

the class StringEqualsIgnoreCaseMethod 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 String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    String arg1 = null;
    Object argObj = null;
    Object param = expr.getArguments().get(0);
    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 if (param instanceof InvokeExpression) {
        argObj = eval.getValueForInvokeExpression((InvokeExpression) param);
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    arg1 = QueryUtils.getStringValue(argObj);
    return ((String) invokedValue).equalsIgnoreCase(arg1) ? Boolean.TRUE : Boolean.FALSE;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 10 with Literal

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

the class StringStartsWithMethod 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 String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    String arg1 = null;
    Object argObj = null;
    Object param = expr.getArguments().get(0);
    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 if (param instanceof InvokeExpression) {
        argObj = eval.getValueForInvokeExpression((InvokeExpression) param);
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    arg1 = QueryUtils.getStringValue(argObj);
    Boolean result = null;
    if (expr.getArguments().size() == 2) {
        Literal param2 = (Literal) expr.getArguments().get(1);
        int arg2 = -1;
        if (param2.getLiteral() instanceof Number) {
            arg2 = ((Number) param2.getLiteral()).intValue();
        }
        result = ((String) invokedValue).startsWith(arg1, arg2) ? Boolean.TRUE : Boolean.FALSE;
    } else {
        result = ((String) invokedValue).startsWith(arg1) ? Boolean.TRUE : Boolean.FALSE;
    }
    return result;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) 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