Search in sources :

Example 16 with VariableExpression

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

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

Example 18 with VariableExpression

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

the class JavaQueryCompiler method compileResult.

public Expression[] compileResult() {
    if (result == null) {
        return null;
    }
    Node[] node = parser.parseResult(result);
    Expression[] expr = new Expression[node.length];
    for (int i = 0; i < node.length; i++) {
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        // Find the last child of this node, and check for an alias (type = NAME)
        String alias = null;
        Node aliasNode = null;
        List<Node> childNodes = node[i].getChildNodes();
        if (childNodes != null && childNodes.size() > 0) {
            Node lastChild = childNodes.get(childNodes.size() - 1);
            if (lastChild.getNodeType() == NodeType.NAME) {
                // Alias node
                aliasNode = lastChild;
            }
        }
        if (aliasNode != null) {
            alias = (String) aliasNode.getNodeValue();
            node[i].removeChildNode(aliasNode);
        }
        if (candidateAliasOrig != null) {
            swapCandidateAliasNodeName(node[i]);
        }
        if (parameterSubtitutionMap != null) {
            node[i] = swapSubqueryParameters(node[i]);
        }
        expr[i] = comp.compileExpression(node[i]);
        if (alias != null) {
            expr[i].setAlias(alias);
        }
        try {
            expr[i].bind(symtbl);
        } catch (PrimaryExpressionIsClassLiteralException peil) {
            // PrimaryExpression should be swapped for a class Literal
            expr[i] = peil.getLiteral();
            expr[i].bind(symtbl);
        } catch (PrimaryExpressionIsClassStaticFieldException peil) {
            // PrimaryExpression should be swapped for a static field Literal
            Field fld = peil.getLiteralField();
            try {
                // Get the value of the static field
                Object value = fld.get(null);
                expr[i] = new Literal(value);
                expr[i].bind(symtbl);
            } catch (Exception e) {
                throw new NucleusUserException("Error processing static field " + fld.getName(), e);
            }
        } catch (PrimaryExpressionIsVariableException pive) {
            // PrimaryExpression should be swapped for an implicit variable
            expr[i] = pive.getVariableExpression();
            expr[i].bind(symtbl);
        }
        if (expr[i] instanceof PrimaryExpression) {
            String id = ((PrimaryExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        } else if (expr[i] instanceof ParameterExpression) {
            String id = ((ParameterExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        } else if (expr[i] instanceof VariableExpression) {
            String id = ((VariableExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        }
    }
    return expr;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryExpressionIsClassLiteralException(org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryExpressionIsClassStaticFieldException(org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException) QueryCompilerSyntaxException(org.datanucleus.store.query.QueryCompilerSyntaxException) PrimaryExpressionIsVariableException(org.datanucleus.query.expression.PrimaryExpressionIsVariableException) NucleusException(org.datanucleus.exceptions.NucleusException) PrimaryExpressionIsClassLiteralException(org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) PrimaryExpressionIsClassStaticFieldException(org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException) Field(java.lang.reflect.Field) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) PrimaryExpressionIsVariableException(org.datanucleus.query.expression.PrimaryExpressionIsVariableException) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Example 19 with VariableExpression

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

the class VarThisCompilationOptimiser method findRedundantFilterVariables.

/**
 * Method to process the provided filter expression and find any variables that are to all intents
 * and purposes redundant. Checks for "var == this". In this case we can just replace the variable
 * occurrences with "this".
 * @param filterExpr The filter
 * @param varNames The variable names that are redundant (updated by this method)
 */
private void findRedundantFilterVariables(Expression filterExpr, Set<String> varNames) {
    if (filterExpr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) filterExpr;
        if (dyExpr.getOperator() == Expression.OP_EQ) {
            if (dyExpr.getLeft() instanceof VariableExpression) {
                if (dyExpr.getRight() instanceof PrimaryExpression) {
                    PrimaryExpression rightExpr = (PrimaryExpression) dyExpr.getRight();
                    if (rightExpr.getId().equals(compilation.getCandidateAlias())) {
                        varNames.add(((VariableExpression) dyExpr.getLeft()).getId());
                    }
                }
            } else if (dyExpr.getRight() instanceof VariableExpression) {
                if (dyExpr.getLeft() instanceof PrimaryExpression) {
                    PrimaryExpression leftExpr = (PrimaryExpression) dyExpr.getLeft();
                    if (leftExpr.getId().equals(compilation.getCandidateAlias())) {
                        varNames.add(((VariableExpression) dyExpr.getRight()).getId());
                    }
                }
            }
        } else if (dyExpr.getOperator() == Expression.OP_AND) {
            findRedundantFilterVariables(dyExpr.getLeft(), varNames);
            findRedundantFilterVariables(dyExpr.getRight(), varNames);
        } else if (dyExpr.getOperator() == Expression.OP_OR) {
            findRedundantFilterVariables(dyExpr.getLeft(), varNames);
            findRedundantFilterVariables(dyExpr.getRight(), varNames);
        }
    }
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 20 with VariableExpression

use of org.datanucleus.query.expression.VariableExpression in project tests by datanucleus.

the class JPQLCompilerTest method testFilterWithExistsSubquery.

/**
 * Tests for "EXISTS (subquery)".
 */
public void testFilterWithExistsSubquery() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "EXISTS (SUBQ_1)", null, null, null, null, null, null, null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception during compile", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Expression is not a SubqueryExpression", expr instanceof SubqueryExpression);
    SubqueryExpression subExpr = (SubqueryExpression) expr;
    assertEquals("Subquery keyword is incorrect", "EXISTS", subExpr.getKeyword());
    assertTrue("Subquery right expression is not VariableExpression", subExpr.getRight() instanceof VariableExpression);
    VariableExpression varExpr = (VariableExpression) subExpr.getRight();
    assertEquals("VariableExpression name is incorrect", "SUBQ_1", varExpr.getId());
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) 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) VariableExpression(org.datanucleus.query.expression.VariableExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression)

Aggregations

VariableExpression (org.datanucleus.query.expression.VariableExpression)21 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)20 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)18 Literal (org.datanucleus.query.expression.Literal)17 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)12 Expression (org.datanucleus.query.expression.Expression)12 NucleusException (org.datanucleus.exceptions.NucleusException)11 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)11 VariableNotSetException (org.datanucleus.query.inmemory.VariableNotSetException)7 Map (java.util.Map)4 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)4 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)4 HashMap (java.util.HashMap)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)3 OrderExpression (org.datanucleus.query.expression.OrderExpression)3 Collection (java.util.Collection)2 List (java.util.List)2 Symbol (org.datanucleus.query.compiler.Symbol)2 SymbolTable (org.datanucleus.query.compiler.SymbolTable)2