Search in sources :

Example 36 with ParameterExpression

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

the class StringMatchesMethod 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 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 if (param instanceof InvokeExpression) {
        argObj = eval.getValueForInvokeExpression((InvokeExpression) param);
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    arg = QueryUtils.getStringValue(argObj);
    if (eval.getQueryLanguage().equalsIgnoreCase(Query.LANGUAGE_JPQL)) {
        // Convert JPQL like expression to String.matches input
        String matchesArg = arg;
        matchesArg = StringUtils.replaceAll(matchesArg, "%", ".*");
        matchesArg = matchesArg.replace('_', '.');
        arg = matchesArg;
    }
    return ((String) invokedValue).matches(arg) ? 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 37 with ParameterExpression

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

the class SubstringFunction 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 Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param, num1, num2) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    if (paramValue == null) {
        return null;
    }
    Object num1 = expr.getArguments().get(1);
    int num1Value = -1;
    if (num1 instanceof Literal) {
        // Extract Integer from arithmetic Literal
        num1Value = eval.getIntegerForLiteral((Literal) num1);
    } else {
        throw new NucleusException(method + "(param, num1, num2) where num1 is instanceof " + num1.getClass().getName() + " not supported");
    }
    Object num2 = expr.getArguments().get(2);
    int num2Value = -1;
    if (num2 instanceof Literal) {
        num2Value = eval.getIntegerForLiteral((Literal) num2);
    } else {
        throw new NucleusException(method + "(param, num1, num2) where num2 is instanceof " + num2.getClass().getName() + " not supported");
    }
    return ((String) paramValue).substring(num1Value, num2Value);
}
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 38 with ParameterExpression

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

the class SizeFunction 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 Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    if (paramValue == null) {
        return null;
    }
    if (paramValue instanceof Collection) {
        return Integer.valueOf(((Collection) paramValue).size());
    } else if (paramValue instanceof Map) {
        return Integer.valueOf(((Map) paramValue).size());
    }
    return null;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) Collection(java.util.Collection) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map)

Example 39 with ParameterExpression

use of org.datanucleus.query.expression.ParameterExpression 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;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Optional(java.util.Optional) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ObjectProvider(org.datanucleus.state.ObjectProvider) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 40 with ParameterExpression

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

the class MapContainsKeyMethod 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 keys
            throw new VariableNotSetException(varExpr, ((Map) invokedValue).keySet().toArray());
        }
    } else {
        throw new NucleusException("Dont currently support use of containsKey(" + param.getClass().getName() + ")");
    }
    return ((Map) invokedValue).containsKey(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)

Aggregations

ParameterExpression (org.datanucleus.query.expression.ParameterExpression)56 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)51 Literal (org.datanucleus.query.expression.Literal)43 NucleusException (org.datanucleus.exceptions.NucleusException)42 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)31 VariableExpression (org.datanucleus.query.expression.VariableExpression)25 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)23 Expression (org.datanucleus.query.expression.Expression)23 OrderExpression (org.datanucleus.query.expression.OrderExpression)10 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)8 List (java.util.List)7 VariableNotSetException (org.datanucleus.query.inmemory.VariableNotSetException)7 Map (java.util.Map)6 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)6 CaseExpression (org.datanucleus.query.expression.CaseExpression)6 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)6 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)5 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)5 ClassExpression (org.datanucleus.query.expression.ClassExpression)5 JoinExpression (org.datanucleus.query.expression.JoinExpression)5