Search in sources :

Example 1 with SymbolTable

use of org.datanucleus.query.compiler.SymbolTable in project datanucleus-core by datanucleus.

the class Query method checkForMissingParameters.

/**
 * Method to check for any missing parameters that the query compilation is expecting but which aren't
 * supplied to execute().
 * @param parameterValues The input parameter values keyed by their name (or position)
 */
protected void checkForMissingParameters(Map parameterValues) {
    if (compilation == null) {
        return;
    } else if (parameterValues == null) {
        parameterValues = new HashMap();
    }
    boolean namedParametersSupplied = true;
    if (!parameterValues.isEmpty()) {
        Object key = parameterValues.keySet().iterator().next();
        if (!(key instanceof String)) {
            namedParametersSupplied = false;
        }
    }
    if (namedParametersSupplied) {
        // Check for missing named parameters
        SymbolTable symtbl = compilation.getSymbolTable();
        Collection<String> symNames = symtbl.getSymbolNames();
        if (symNames != null && !symNames.isEmpty()) {
            for (String symName : symNames) {
                Symbol sym = symtbl.getSymbol(symName);
                if (sym.getType() == Symbol.PARAMETER) {
                    if (!parameterValues.containsKey(symName)) {
                        throw new QueryInvalidParametersException(Localiser.msg("021119", symName));
                    }
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Symbol(org.datanucleus.query.compiler.Symbol) SymbolTable(org.datanucleus.query.compiler.SymbolTable)

Example 2 with SymbolTable

use of org.datanucleus.query.compiler.SymbolTable in project tests by datanucleus.

the class JDOQLCompilerTest method testFilterCollectionContainsVariable.

/**
 * Tests for collection.contains(element).
 */
public void testFilterCollectionContainsVariable() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "products.contains(element) && element.price < 200", null, null, null, null, null, null, Product.class.getName() + " element", null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
    DyadicExpression dyExpr = (DyadicExpression) expr;
    // product.contains(element)
    assertTrue("Left expression should have been InvokeExpression but wasnt", dyExpr.getLeft() instanceof InvokeExpression);
    InvokeExpression leftExpr = (InvokeExpression) dyExpr.getLeft();
    assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", leftExpr.getLeft() instanceof PrimaryExpression);
    assertEquals("Left expression : Name of field upon which we invoke the method was wrong", "products", ((PrimaryExpression) leftExpr.getLeft()).getId());
    assertEquals("Left expression : Name of invoked method was wrong", "contains", leftExpr.getOperation());
    assertEquals("Left expression : Number of parameters to contains() is wrong", 1, leftExpr.getArguments().size());
    Object param1 = leftExpr.getArguments().get(0);
    assertTrue("Left expression : Parameter1 to contains() is of wrong type", param1 instanceof VariableExpression);
    VariableExpression vrExpr = (VariableExpression) param1;
    assertEquals("Left expression : Name of variable to contains() is incorrect", "element", vrExpr.getId());
    // element.price < 200
    assertTrue("Right expression should have been DyadicExpression but wasnt", dyExpr.getRight() instanceof DyadicExpression);
    DyadicExpression rightExpr = (DyadicExpression) dyExpr.getRight();
    assertTrue("Right expression (left) should have been PrimaryExpression but wasnt", rightExpr.getLeft() instanceof PrimaryExpression);
    PrimaryExpression rightExprLeft = (PrimaryExpression) rightExpr.getLeft();
    assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
    VariableExpression rightExprLeftLeft = (VariableExpression) rightExprLeft.getLeft();
    assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
    assertEquals("Right expression (left) part1 is incorrect", "element", rightExprLeftLeft.getId());
    assertEquals("Right expression (left) has incorrect number of tuples", 1, rightExprLeft.getTuples().size());
    assertEquals("Right expression (left) part2 is incorrect", "price", rightExprLeft.getTuples().get(0));
    assertEquals("Right expression : Operator between left and right is incorrect", Expression.OP_LT, rightExpr.getOperator());
    assertTrue("Right expression (right) should have been Literal but wasnt", rightExpr.getRight() instanceof Literal);
    Literal rightExprRight = (Literal) rightExpr.getRight();
    assertEquals("Right expression (right) literal has incorrect value", 200, ((Long) rightExprRight.getLiteral()).longValue());
    // Check symbols
    SymbolTable symbols = compilation.getSymbolTable();
    assertTrue("Symbol table doesnt have entry for 'element'", symbols.hasSymbol("element"));
    assertTrue("Symbol table doesnt have entry for 'this'", symbols.hasSymbol("this"));
    Symbol sy1 = symbols.getSymbol("element");
    assertEquals("Type of symbol for 'element' is wrong", Product.class, sy1.getValueType());
    Symbol sy2 = symbols.getSymbol("this");
    assertEquals("Type of symbol for 'this' is wrong", Inventory.class, sy2.getValueType());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) Symbol(org.datanucleus.query.compiler.Symbol) Product(org.datanucleus.samples.store.Product) SymbolTable(org.datanucleus.query.compiler.SymbolTable) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) 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) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) Inventory(org.datanucleus.samples.store.Inventory)

Example 3 with SymbolTable

use of org.datanucleus.query.compiler.SymbolTable in project datanucleus-api-jdo by datanucleus.

the class AbstractJDOQLTypedQuery method compile.

/**
 * Method to compile the query as it is currently defined.
 * @param mmgr Metadata manager
 * @param clr ClassLoader resolver
 * @return The generic compilation
 */
protected QueryCompilation compile(MetaDataManager mmgr, ClassLoaderResolver clr) {
    SymbolTable symtbl = new SymbolTable();
    symtbl.setSymbolResolver(new JDOQLSymbolResolver(mmgr, clr, symtbl, candidateCls, candidateAlias));
    symtbl.addSymbol(new PropertySymbol(candidateAlias, candidateCls));
    org.datanucleus.query.expression.Expression[] resultExprs = null;
    if (result != null && !result.isEmpty()) {
        resultExprs = new org.datanucleus.query.expression.Expression[result.size()];
        Iterator iter = result.iterator();
        int i = 0;
        while (iter.hasNext()) {
            ExpressionImpl result = (ExpressionImpl) iter.next();
            org.datanucleus.query.expression.Expression resultExpr = result.getQueryExpression();
            resultExpr.bind(symtbl);
            resultExprs[i++] = resultExpr;
        }
        if (resultExprs.length == 1 && resultExprs[0] instanceof PrimaryExpression) {
            // Check for special case of "Object(p)" in result, which means no special result
            String resultExprId = ((PrimaryExpression) resultExprs[0]).getId();
            if (resultExprId.equalsIgnoreCase(candidateAlias)) {
                resultExprs = null;
            }
        }
    }
    org.datanucleus.query.expression.Expression filterExpr = null;
    if (filter != null) {
        filterExpr = filter.getQueryExpression();
        if (filterExpr != null) {
            filterExpr.bind(symtbl);
        }
    }
    org.datanucleus.query.expression.Expression[] groupingExprs = null;
    if (grouping != null && !grouping.isEmpty()) {
        groupingExprs = new org.datanucleus.query.expression.Expression[grouping.size()];
        Iterator iter = grouping.iterator();
        int i = 0;
        while (iter.hasNext()) {
            ExpressionImpl grp = (ExpressionImpl) iter.next();
            org.datanucleus.query.expression.Expression groupingExpr = grp.getQueryExpression();
            groupingExpr.bind(symtbl);
            groupingExprs[i++] = groupingExpr;
        }
    }
    org.datanucleus.query.expression.Expression havingExpr = null;
    if (having != null) {
        havingExpr = having.getQueryExpression();
        havingExpr.bind(symtbl);
    }
    org.datanucleus.query.expression.Expression[] orderExprs = null;
    if (ordering != null && !ordering.isEmpty()) {
        orderExprs = new org.datanucleus.query.expression.Expression[ordering.size()];
        Iterator<OrderExpressionImpl> iter = ordering.iterator();
        int i = 0;
        while (iter.hasNext()) {
            OrderExpressionImpl order = iter.next();
            org.datanucleus.query.expression.OrderExpression orderExpr = new org.datanucleus.query.expression.OrderExpression(((ExpressionImpl) order.getExpression()).getQueryExpression(), order.getDirection() == OrderDirection.ASC ? "ascending" : "descending");
            orderExpr.bind(symtbl);
            orderExprs[i++] = orderExpr;
        }
    }
    org.datanucleus.query.expression.Expression[] updateExprs = null;
    if (this.updateExprs != null) {
        Iterator<ExpressionImpl> expIter = this.updateExprs.iterator();
        Iterator<ExpressionImpl> valIter = this.updateVals.iterator();
        updateExprs = new Expression[this.updateExprs.size()];
        int i = 0;
        while (expIter.hasNext()) {
            ExpressionImpl updateExpr = expIter.next();
            ExpressionImpl updateVal = valIter.next();
            updateExprs[i++] = new DyadicExpression(updateExpr.getQueryExpression(), Expression.OP_EQ, updateVal.getQueryExpression());
        }
    }
    compilation = new QueryCompilation(candidateCls, candidateAlias, symtbl, resultExprs, null, filterExpr, groupingExprs, havingExpr, orderExprs, updateExprs);
    compilation.setQueryLanguage(Query.LANGUAGE_JDOQL);
    return compilation;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) Expression(org.datanucleus.query.expression.Expression) SymbolTable(org.datanucleus.query.compiler.SymbolTable) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) 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) Iterator(java.util.Iterator) JDOQLSymbolResolver(org.datanucleus.query.compiler.JDOQLSymbolResolver) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 4 with SymbolTable

use of org.datanucleus.query.compiler.SymbolTable in project tests by datanucleus.

the class JPQLCompilerTest method testFromInExpression.

/**
 * Tests for from clause with an "IN(...) alias" expression.
 */
public void testFromInExpression() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), Department.class.getName() + " d, IN(d.projects) n", null, null, null, 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());
    }
    SymbolTable symtbl = compilation.getSymbolTable();
    Expression[] exprs = compilation.getExprFrom();
    assertEquals("Number of from expressions is incorrect", 2, exprs.length);
    // Candidate clause
    assertTrue("FROM candidate clause is of wrong type " + exprs[0].getClass().getName(), exprs[0] instanceof ClassExpression);
    ClassExpression clsExpr0 = (ClassExpression) exprs[0];
    assertEquals("FROM candidate clause alias is wrong", "d", clsExpr0.getAlias());
    Symbol clsSym0 = symtbl.getSymbol(clsExpr0.getAlias());
    assertEquals("FROM candidate clause class is wrong", Department.class, clsSym0.getValueType());
    assertEquals("FROM candidate clause has incorrect left()", null, clsExpr0.getLeft());
    assertEquals("FROM candidate clause has incorrect right()", null, clsExpr0.getRight());
    // Candidate+IN clause
    assertTrue("FROM candidate+IN clause is of wrong type " + exprs[1].getClass().getName(), exprs[1] instanceof ClassExpression);
    ClassExpression clsExpr1 = (ClassExpression) exprs[1];
    assertEquals("FROM candidate+IN clause alias is wrong", "d", clsExpr1.getAlias());
    Symbol clsSym1 = symtbl.getSymbol(clsExpr1.getAlias());
    assertEquals("FROM candidate+IN clause class is wrong", Department.class, clsSym1.getValueType());
    assertEquals("FROM candidate+IN clause has incorrect left()", null, clsExpr1.getLeft());
    assertTrue("FROM candidate+IN clause has incorrect right() - should be instanceof JoinExpression", clsExpr1.getRight() instanceof JoinExpression);
    JoinExpression joinExpr1 = (JoinExpression) clsExpr1.getRight();
    assertEquals("FROM candidate+IN clause join expression has incorrect alias", "n", joinExpr1.getAlias());
    assertEquals("FROM candidate+IN clause join expression has incorrect join type", JoinType.JOIN_INNER, joinExpr1.getType());
    PrimaryExpression joinPrimExpr1 = (PrimaryExpression) joinExpr1.getJoinedExpression();
    assertEquals("FROM candidate+IN clause join primary expression is incorrect", "d.projects", joinPrimExpr1.getId());
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) Symbol(org.datanucleus.query.compiler.Symbol) SymbolTable(org.datanucleus.query.compiler.SymbolTable) ClassExpression(org.datanucleus.query.expression.ClassExpression) Department(org.datanucleus.samples.annotations.models.company.Department) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) 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) JoinExpression(org.datanucleus.query.expression.JoinExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 5 with SymbolTable

use of org.datanucleus.query.compiler.SymbolTable in project tests by datanucleus.

the class JDOQLCompilerTest method testFilterCollectionContainsVariablePlusExtraJoin.

/**
 * Tests for collection.contains(element) && elem.other.field == val.
 */
public void testFilterCollectionContainsVariablePlusExtraJoin() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "products.contains(element) && element.guarantee.numberOfYears < 3", null, null, null, null, null, null, Product.class.getName() + " element", null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    Expression expr = compilation.getExprFilter();
    assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
    DyadicExpression dyExpr = (DyadicExpression) expr;
    LOG.info(">> expr=" + dyExpr);
    // product.contains(element)
    assertTrue("Left expression should have been InvokeExpression but wasnt", dyExpr.getLeft() instanceof InvokeExpression);
    InvokeExpression leftExpr = (InvokeExpression) dyExpr.getLeft();
    assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", leftExpr.getLeft() instanceof PrimaryExpression);
    assertEquals("Left expression : Name of field upon which we invoke the method was wrong", "products", ((PrimaryExpression) leftExpr.getLeft()).getId());
    assertEquals("Left expression : Name of invoked method was wrong", "contains", leftExpr.getOperation());
    assertEquals("Left expression : Number of parameters to contains() is wrong", 1, leftExpr.getArguments().size());
    Object param1 = leftExpr.getArguments().get(0);
    assertTrue("Left expression : Parameter1 to contains() is of wrong type", param1 instanceof VariableExpression);
    VariableExpression vrExpr = (VariableExpression) param1;
    assertEquals("Left expression : Name of variable to contains() is incorrect", "element", vrExpr.getId());
    // element.guarantee.numberOfYears < 3
    assertTrue("Right expression should have been DyadicExpression but wasnt", dyExpr.getRight() instanceof DyadicExpression);
    DyadicExpression rightExpr = (DyadicExpression) dyExpr.getRight();
    assertTrue("Right expression (left) should have been PrimaryExpression but wasnt", rightExpr.getLeft() instanceof PrimaryExpression);
    PrimaryExpression rightExprLeft = (PrimaryExpression) rightExpr.getLeft();
    assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
    VariableExpression rightExprLeftLeft = (VariableExpression) rightExprLeft.getLeft();
    assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
    assertEquals("Right expression (left) part1 is incorrect", "element", rightExprLeftLeft.getId());
    assertEquals("Right expression (left) has incorrect number of tuples", 2, rightExprLeft.getTuples().size());
    assertEquals("Right expression (left) part2 is incorrect", "guarantee", rightExprLeft.getTuples().get(0));
    assertEquals("Right expression (left) part2 is incorrect", "numberOfYears", rightExprLeft.getTuples().get(1));
    assertEquals("Right expression : Operator between left and right is incorrect", Expression.OP_LT, rightExpr.getOperator());
    assertTrue("Right expression (right) should have been Literal but wasnt", rightExpr.getRight() instanceof Literal);
    Literal rightExprRight = (Literal) rightExpr.getRight();
    assertEquals("Right expression (right) literal has incorrect value", 3, ((Long) rightExprRight.getLiteral()).longValue());
    // Check symbols
    SymbolTable symbols = compilation.getSymbolTable();
    assertTrue("Symbol table doesnt have entry for 'element'", symbols.hasSymbol("element"));
    assertTrue("Symbol table doesnt have entry for 'this'", symbols.hasSymbol("this"));
    Symbol sy1 = symbols.getSymbol("element");
    assertEquals("Type of symbol for 'element' is wrong", Product.class, sy1.getValueType());
    Symbol sy2 = symbols.getSymbol("this");
    assertEquals("Type of symbol for 'this' is wrong", Inventory.class, sy2.getValueType());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) Symbol(org.datanucleus.query.compiler.Symbol) Product(org.datanucleus.samples.store.Product) SymbolTable(org.datanucleus.query.compiler.SymbolTable) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) 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) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) Inventory(org.datanucleus.samples.store.Inventory)

Aggregations

SymbolTable (org.datanucleus.query.compiler.SymbolTable)5 HashMap (java.util.HashMap)4 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)4 Symbol (org.datanucleus.query.compiler.Symbol)4 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)4 Expression (org.datanucleus.query.expression.Expression)4 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)4 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)4 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)4 VariableExpression (org.datanucleus.query.expression.VariableExpression)4 NucleusException (org.datanucleus.exceptions.NucleusException)3 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)3 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)2 Literal (org.datanucleus.query.expression.Literal)2 OrderExpression (org.datanucleus.query.expression.OrderExpression)2 Inventory (org.datanucleus.samples.store.Inventory)2 Product (org.datanucleus.samples.store.Product)2 Iterator (java.util.Iterator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JDOQLSymbolResolver (org.datanucleus.query.compiler.JDOQLSymbolResolver)1