Search in sources :

Example 96 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project tests by datanucleus.

the class JPQLCompilerTest method testFilterUnaryMinus.

/**
 * Test for unary minus.
 */
public void testFilterUnaryMinus() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "1 > -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());
    }
    // TODO Check the content of the compilation when it is compiled
    NucleusLogger.QUERY.info(">> compilation=" + compilation);
}
Also used : Project(org.datanucleus.samples.annotations.models.company.Project) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) HashMap(java.util.HashMap) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 97 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project tests by datanucleus.

the class JDOQLCompilerTest 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 JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "statusId == 2 && 100.0 > price", null, null, null, null, null, null, null, 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;
    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", "statusId", 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_GT, 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", "price", rightExpr2.getId());
    Literal leftExpr2 = (Literal) dyExpr2.getLeft();
    // TODO Why BigDecimal and not Double??
    assertTrue("Compiled right expression literal is of incorrect type", leftExpr2.getLiteral() instanceof BigDecimal);
    assertEquals("Compiled right expression literal has incorrect value", 100.0, ((BigDecimal) leftExpr2.getLiteral()).longValue(), 0.1);
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) 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) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal)

Example 98 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project tests by datanucleus.

the class JDOQLCompilerTest method testFilterComparison.

/**
 * Tests for simple field-literal comparison in filter.
 */
public void testFilterComparison() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "statusId == 2", null, null, null, null, null, null, null, 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;
    assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr.getLeft() instanceof PrimaryExpression);
    assertTrue("Compiled right expression should be Literal but isnt", dyExpr.getRight() instanceof Literal);
    assertEquals("Operator between left and right is incorrect", Expression.OP_EQ, dyExpr.getOperator());
    PrimaryExpression leftExpr1 = (PrimaryExpression) dyExpr.getLeft();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "statusId", leftExpr1.getId());
    Literal rightExpr1 = (Literal) dyExpr.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());
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "100.0 > price", null, null, null, null, null, null, null, null);
    } catch (NucleusException ne) {
        NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
        fail("compilation of filter with valid field threw exception : " + ne.getMessage());
    }
    compilation = compiler.compile(new HashMap(), null);
    expr = compilation.getExprFilter();
    assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
    dyExpr = (DyadicExpression) expr;
    assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr.getRight() instanceof PrimaryExpression);
    assertTrue("Compiled left expression should be Literal but isnt", dyExpr.getLeft() instanceof Literal);
    assertEquals("Operator between left and right is incorrect", Expression.OP_GT, dyExpr.getOperator());
    PrimaryExpression rightExpr2 = (PrimaryExpression) dyExpr.getRight();
    assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr2.getTuples().size());
    assertEquals("Compiled left expression 'id' is incorrect", "price", rightExpr2.getId());
    Literal leftExpr2 = (Literal) dyExpr.getLeft();
    // TODO Why BigDecimal and not Double??
    assertTrue("Compiled right expression literal is of incorrect type", leftExpr2.getLiteral() instanceof BigDecimal);
    assertEquals("Compiled right expression literal has incorrect value", 100.0, ((BigDecimal) leftExpr2.getLiteral()).longValue(), 0.1);
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) 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) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal)

Example 99 with NucleusException

use of org.datanucleus.exceptions.NucleusException in project tests by datanucleus.

the class JDOQLCompilerTest method testFilterWithStringEqualsLiteral.

/**
 * Tests for "String.equals(Literal)" in filter.
 */
public void testFilterWithStringEqualsLiteral() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name.equals(\"Kettle\")", null, null, null, null, null, null, null, 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 InvokeExpression but wasnt", expr instanceof InvokeExpression);
    InvokeExpression invExpr = (InvokeExpression) expr;
    assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", invExpr.getLeft() instanceof PrimaryExpression);
    assertEquals("Name of field upon which we invoke the method was wrong", "name", ((PrimaryExpression) invExpr.getLeft()).getId());
    assertEquals("Name of invoked method was wrong", "equals", invExpr.getOperation());
    assertEquals("Number of parameters is wrong", 1, invExpr.getArguments().size());
    Object param = invExpr.getArguments().get(0);
    assertTrue("Parameter to equals() is of wrong type", param instanceof Literal);
    Literal paramLit = (Literal) param;
    assertEquals("Parameter to equals() has wrong value", "Kettle", paramLit.getLiteral());
}
Also used : JDOQLCompiler(org.datanucleus.query.compiler.JDOQLCompiler) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) JavaQueryCompiler(org.datanucleus.query.compiler.JavaQueryCompiler) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) 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) Product(org.datanucleus.samples.store.Product) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 100 with NucleusException

use of org.datanucleus.exceptions.NucleusException 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)

Aggregations

NucleusException (org.datanucleus.exceptions.NucleusException)326 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)71 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)67 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)62 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)53 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)52 ArrayList (java.util.ArrayList)48 Literal (org.datanucleus.query.expression.Literal)47 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)44 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)43 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)40 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)37 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)36 StringExpression (org.datanucleus.store.rdbms.sql.expression.StringExpression)35 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)35 Expression (org.datanucleus.query.expression.Expression)32 HashMap (java.util.HashMap)31 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)31 VariableExpression (org.datanucleus.query.expression.VariableExpression)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)26