Search in sources :

Example 51 with PrimaryExpression

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

the class InMemoryExpressionEvaluator method processComExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processComExpression(org.datanucleus.query.expression.Expression)
     */
protected Object processComExpression(Expression expr) {
    // Bitwise complement - only for integer values
    PrimaryExpression primExpr = (PrimaryExpression) expr.getLeft();
    Object primVal = getValueForPrimaryExpression(primExpr);
    int val = -1;
    if (primVal instanceof Number) {
        val = ((Number) primVal).intValue();
    }
    Integer result = Integer.valueOf(~val);
    stack.push(result);
    return stack.peek();
}
Also used : BigInteger(java.math.BigInteger) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression)

Example 52 with PrimaryExpression

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

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

Example 54 with PrimaryExpression

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

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

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