Search in sources :

Example 26 with JavaQueryCompiler

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

the class JPQLCompilerTest method testFromInExpressionErroneousPrimary.

/**
 * Tests for from clause with an "IN(...) alias" expression with an invalid primary.
 */
public void testFromInExpressionErroneousPrimary() {
    JavaQueryCompiler compiler = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), Department.class.getName() + " d," + "IN(d.products) n", null, null, null, null, null, null, null, null, null, null);
        compiler.compile(new HashMap(), null);
        fail("Was expecting QueryCompilerSyntaxException but compilation worked");
    } catch (QueryCompilerSyntaxException qcse) {
    // Expected
    }
}
Also used : JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) QueryCompilerSyntaxException(org.datanucleus.store.query.QueryCompilerSyntaxException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 27 with JavaQueryCompiler

use of org.datanucleus.query.compiler.JavaQueryCompiler 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 28 with JavaQueryCompiler

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

the class JPQLCompilerTest method testFilterComparisonWithAnd.

/**
 * Tests for simple field-literal comparison in filter and AND of another comparison.
 */
public void testFilterComparisonWithAnd() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "budget = 2 AND 'Sales' = name", 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("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
    DyadicExpression dyExpr = (DyadicExpression) expr;
    assertTrue("Compiled left expression should be DyadicExpression but isnt", dyExpr.getLeft() instanceof DyadicExpression);
    assertTrue("Compiled right expression should be DyadicExpression but isnt", dyExpr.getRight() instanceof DyadicExpression);
    DyadicExpression dyExpr1 = (DyadicExpression) dyExpr.getLeft();
    DyadicExpression dyExpr2 = (DyadicExpression) dyExpr.getRight();
    assertEquals("Operator between left and right is incorrect", Expression.OP_AND, dyExpr.getOperator());
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be Literal but isnt", dyExpr1.getRight() instanceof Literal);
    assertEquals("Operator between left (left and right) is incorrect", Expression.OP_EQ, dyExpr1.getOperator());
    PrimaryExpression leftExpr1 = (PrimaryExpression) dyExpr1.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "budget", leftExpr1.getId());
    Literal rightExpr1 = (Literal) dyExpr1.getRight();
    assertTrue("Compiled right expression literal is of incorrect type", rightExpr1.getLiteral() instanceof Long);
    assertEquals("Compiled right expression literal has incorrect value", 2, ((Long) rightExpr1.getLiteral()).longValue());
    assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr2.getRight() instanceof PrimaryExpression);
    assertTrue("Compiled left expression should be Literal but isnt", dyExpr2.getLeft() instanceof Literal);
    assertEquals("Operator between right (left and right) is incorrect", Expression.OP_EQ, dyExpr2.getOperator());
    PrimaryExpression rightExpr2 = (PrimaryExpression) dyExpr2.getRight();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr2.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "name", rightExpr2.getId());
    Literal leftExpr2 = (Literal) dyExpr2.getLeft();
    assertTrue("Compiled right expression literal is of incorrect type", leftExpr2.getLiteral() instanceof String);
    assertEquals("Compiled right expression literal has incorrect value", "Sales", ((String) leftExpr2.getLiteral()));
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) 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) Literal(org.datanucleus.query.expression.Literal) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 29 with JavaQueryCompiler

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

the class JDOQLCompilerTest method testFilterUnaryMinus.

/**
 * Test for unary minus.
 */
public void testFilterUnaryMinus() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "1 > -1", null, null, null, null, null, null, null, null);
        compilation = compiler.compile(new HashMap(), null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error(">> Exception thrown when compiling filter unary minus", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    // TODO Check the content of the compilation when it is compiled
    NucleusLogger.QUERY.info(">> compilation=" + compilation);
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) Inventory(org.datanucleus.samples.store.Inventory)

Example 30 with JavaQueryCompiler

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

the class JDOQLCompilerTest method testFilterExplicitParameterAsImplicit.

/**
 * Test for subquery.
 * TODO Update this to reflect a valid subquery JDOQL.
 */
/*public void testSubquery()
    {
        JavaQueryCompiler compiler = null;
        QueryCompilation compilation = null;
        try
        {
            compiler = new JDOQLCompiler(mmgr, nucCtx.getClassLoaderResolver(null), 
                null, Employee.class, null,
                "salary > (SELECT avg(salary) FROM " + Employee.class.getName() + " e)", 
                null, null, null, null, null, null, null);
            compilation = compiler.compile(new HashMap());
        }
        catch (NucleusException ne)
        {
            NucleusLogger.QUERY.error(">> Exception thrown when compiling subquery", ne);
            fail("compilation of filter with valid field threw exception : " + ne.getMessage());
        }
        // TODO Check the content of the compilation when it is compiled
        NucleusLogger.QUERY.info(">> compilation=" + compilation);
    }*/
/**
 * Test for detection of invalid param specification where the user defines explicit params
 * and puts an implicit param in the filter. Should cause exception
 */
public void testFilterExplicitParameterAsImplicit() {
    JavaQueryCompiler compiler = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == :param1", null, null, null, null, null, "java.lang.String param1", null, null);
        compiler.compile(null, null);
        fail("Compiled query without exception even though it had explicit params defined and" + " had an implicit param in the filter! Should have thrown exception");
    } catch (NucleusUserException ne) {
    // Expected
    }
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) Product(org.datanucleus.samples.store.Product)

Aggregations

JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)47 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)44 HashMap (java.util.HashMap)35 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)33 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)25 Expression (org.datanucleus.query.expression.Expression)25 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)25 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)25 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)25 VariableExpression (org.datanucleus.query.expression.VariableExpression)25 NucleusException (org.datanucleus.exceptions.NucleusException)23 Literal (org.datanucleus.query.expression.Literal)17 Product (org.datanucleus.samples.store.Product)17 List (java.util.List)16 OrderExpression (org.datanucleus.query.expression.OrderExpression)16 ArrayList (java.util.ArrayList)14 PersistenceManager (javax.jdo.PersistenceManager)14 Transaction (javax.jdo.Transaction)14 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)14 JDOQuery (org.datanucleus.api.jdo.JDOQuery)14