Search in sources :

Example 1 with InvokeExpression

use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method processPrimaryExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processPrimaryExpression(org.datanucleus.query.expression.PrimaryExpression)
     */
protected Object processPrimaryExpression(PrimaryExpression expr) {
    SQLExpression sqlExpr = null;
    if (expr.getLeft() != null) {
        if (expr.getLeft() instanceof DyadicExpression && expr.getLeft().getOperator() == Expression.OP_CAST) {
            String exprCastName = null;
            if (expr.getLeft().getLeft() instanceof PrimaryExpression) {
                exprCastName = "CAST_" + ((PrimaryExpression) expr.getLeft().getLeft()).getId();
            } else if (expr.getLeft().getLeft() instanceof VariableExpression) {
                exprCastName = "CAST_" + ((VariableExpression) expr.getLeft().getLeft()).getId();
            } else if (expr.getLeft().getLeft() instanceof InvokeExpression) {
                exprCastName = "CAST_" + expr.getLeft().getLeft();
            } else {
                throw new NucleusException("Don't currently support cast of " + expr.getLeft().getLeft());
            }
            expr.getLeft().getLeft().evaluate(this);
            sqlExpr = stack.pop();
            JavaTypeMapping mapping = sqlExpr.getJavaTypeMapping();
            if (mapping instanceof EmbeddedMapping) {
                // Cast of an embedded field, so use same table
                // Extract what we are casting it to
                Literal castLitExpr = (Literal) expr.getLeft().getRight();
                Class castType = resolveClass((String) castLitExpr.getLiteral());
                AbstractClassMetaData castCmd = ec.getMetaDataManager().getMetaDataForClass(castType, clr);
                JavaTypeMapping discMapping = ((EmbeddedMapping) mapping).getDiscriminatorMapping();
                if (discMapping != null) {
                    // Should have a discriminator always when casting this
                    SQLExpression discExpr = exprFactory.newExpression(stmt, sqlExpr.getSQLTable(), discMapping);
                    Object discVal = castCmd.getDiscriminatorValue();
                    SQLExpression discValExpr = exprFactory.newLiteral(stmt, discMapping, discVal);
                    BooleanExpression discRestrictExpr = discExpr.eq(discValExpr);
                    Iterator<String> subclassIter = storeMgr.getSubClassesForClass(castType.getName(), true, clr).iterator();
                    while (subclassIter.hasNext()) {
                        String subclassName = subclassIter.next();
                        AbstractClassMetaData subtypeCmd = storeMgr.getMetaDataManager().getMetaDataForClass(subclassName, clr);
                        discVal = subtypeCmd.getDiscriminatorValue();
                        discValExpr = exprFactory.newLiteral(stmt, discMapping, discVal);
                        BooleanExpression subtypeExpr = discExpr.eq(discValExpr);
                        discRestrictExpr = discRestrictExpr.ior(subtypeExpr);
                    }
                    stmt.whereAnd(discRestrictExpr, true);
                }
                SQLTableMapping tblMapping = new SQLTableMapping(sqlExpr.getSQLTable(), castCmd, sqlExpr.getJavaTypeMapping());
                setSQLTableMappingForAlias(exprCastName, tblMapping);
                SQLTableMapping sqlMapping = getSQLTableMappingForPrimaryExpression(stmt, exprCastName, expr, Boolean.FALSE);
                if (sqlMapping == null) {
                    throw new NucleusException("PrimaryExpression " + expr + " is not yet supported");
                }
                sqlExpr = exprFactory.newExpression(stmt, sqlMapping.table, sqlMapping.mapping);
                stack.push(sqlExpr);
                return sqlExpr;
            }
            // Evaluate the cast
            expr.getLeft().evaluate(this);
            sqlExpr = stack.pop();
            // Extract what we are casting it to
            Literal castLitExpr = (Literal) expr.getLeft().getRight();
            AbstractClassMetaData castCmd = ec.getMetaDataManager().getMetaDataForClass(resolveClass((String) castLitExpr.getLiteral()), clr);
            SQLTableMapping tblMapping = new SQLTableMapping(sqlExpr.getSQLTable(), castCmd, sqlExpr.getJavaTypeMapping());
            setSQLTableMappingForAlias(exprCastName, tblMapping);
            SQLTableMapping sqlMapping = getSQLTableMappingForPrimaryExpression(stmt, exprCastName, expr, Boolean.FALSE);
            if (sqlMapping == null) {
                throw new NucleusException("PrimaryExpression " + expr + " is not yet supported");
            }
            sqlExpr = exprFactory.newExpression(stmt, sqlMapping.table, sqlMapping.mapping);
            stack.push(sqlExpr);
            return sqlExpr;
        } else if (expr.getLeft() instanceof ParameterExpression) {
            // "{paramExpr}.field[.field[.field]]"
            // Need parameter values to process this
            setNotPrecompilable();
            ParameterExpression paramExpr = (ParameterExpression) expr.getLeft();
            Symbol paramSym = compilation.getSymbolTable().getSymbol(paramExpr.getId());
            if (paramSym.getValueType() != null && paramSym.getValueType().isArray()) {
                // Special case : array "methods" (particularly "length")
                String first = expr.getTuples().get(0);
                processParameterExpression(paramExpr, true);
                SQLExpression paramSqlExpr = stack.pop();
                sqlExpr = exprFactory.invokeMethod(stmt, "ARRAY", first, paramSqlExpr, null);
                stack.push(sqlExpr);
                return sqlExpr;
            }
            // Create Literal for the parameter (since we need to perform operations on it)
            processParameterExpression(paramExpr, true);
            SQLExpression paramSqlExpr = stack.pop();
            SQLLiteral lit = (SQLLiteral) paramSqlExpr;
            Object paramValue = lit.getValue();
            List<String> tuples = expr.getTuples();
            Iterator<String> tuplesIter = tuples.iterator();
            Object objValue = paramValue;
            while (tuplesIter.hasNext()) {
                String fieldName = tuplesIter.next();
                if (objValue == null) {
                    NucleusLogger.QUERY.warn(">> Compilation of " + expr + " : need to direct through field \"" + fieldName + "\" on null value, hence not compilable!");
                    // Null value, and we have further path to navigate TODO Handle this "NPE"
                    break;
                }
                objValue = getValueForObjectField(objValue, fieldName);
                // Using literal value of parameter, so cannot precompile it
                setNotPrecompilable();
            }
            if (objValue == null) {
                sqlExpr = exprFactory.newLiteral(stmt, null, null);
                stack.push(sqlExpr);
                return sqlExpr;
            }
            JavaTypeMapping m = exprFactory.getMappingForType(objValue.getClass(), false);
            sqlExpr = exprFactory.newLiteral(stmt, m, objValue);
            stack.push(sqlExpr);
            return sqlExpr;
        } else if (expr.getLeft() instanceof VariableExpression) {
            // "{varExpr}.field[.field[.field]]"
            VariableExpression varExpr = (VariableExpression) expr.getLeft();
            processVariableExpression(varExpr);
            SQLExpression varSqlExpr = stack.pop();
            if (varSqlExpr instanceof UnboundExpression) {
                // Bind as CROSS JOIN for now
                processUnboundExpression((UnboundExpression) varSqlExpr);
                varSqlExpr = stack.pop();
            }
            Class varType = clr.classForName(varSqlExpr.getJavaTypeMapping().getType());
            if (varSqlExpr.getSQLStatement() == stmt.getParentStatement()) {
                // Use parent mapper to get the mapping for this field since it has the table
                SQLTableMapping sqlMapping = parentMapper.getSQLTableMappingForPrimaryExpression(stmt, null, expr, Boolean.FALSE);
                if (sqlMapping == null) {
                    throw new NucleusException("PrimaryExpression " + expr.getId() + " is not yet supported");
                }
                // TODO Cater for the table required to join to not being the primary table of the outer query
                // This should check on
                // getDatastoreAdapter().supportsOption(RDBMSAdapter.ACCESS_PARENTQUERY_IN_SUBQUERY))
                sqlExpr = exprFactory.newExpression(varSqlExpr.getSQLStatement(), sqlMapping.table, sqlMapping.mapping);
                stack.push(sqlExpr);
                return sqlExpr;
            }
            SQLTableMapping varTblMapping = getSQLTableMappingForAlias(varExpr.getId());
            if (varTblMapping == null) {
                throw new NucleusUserException("Variable " + varExpr.getId() + " is not yet bound, so cannot get field " + expr.getId());
            }
            if (varTblMapping.cmd == null) {
                throw new NucleusUserException("Variable " + varExpr.getId() + " of type " + varType.getName() + " cannot evaluate " + expr.getId());
            }
            SQLTableMapping sqlMapping = getSQLTableMappingForPrimaryExpression(varSqlExpr.getSQLStatement(), varExpr.getId(), expr, Boolean.FALSE);
            sqlExpr = exprFactory.newExpression(sqlMapping.table.getSQLStatement(), sqlMapping.table, sqlMapping.mapping);
            stack.push(sqlExpr);
            return sqlExpr;
        } else if (expr.getLeft() instanceof InvokeExpression) {
            InvokeExpression invokeExpr = (InvokeExpression) expr.getLeft();
            SQLExpression invokedSqlExpr = getInvokedSqlExpressionForInvokeExpression(invokeExpr);
            processInvokeExpression(invokeExpr, invokedSqlExpr);
            SQLExpression invokeSqlExpr = stack.pop();
            Table tbl = invokeSqlExpr.getSQLTable().getTable();
            if (expr.getTuples().size() > 1) {
                throw new NucleusUserException("Dont currently support evaluating " + expr.getId() + " on " + invokeSqlExpr);
            }
            SQLTable invokeSqlTbl = invokeSqlExpr.getSQLTable();
            if (invokedSqlExpr.getJavaTypeMapping() instanceof OptionalMapping && invokeExpr.getOperation().equals("get") && expr.getTuples().size() == 1) {
                OptionalMapping opMapping = (OptionalMapping) invokedSqlExpr.getJavaTypeMapping();
                if (opMapping.getWrappedMapping() instanceof PersistableMapping) {
                    // Special case of Optional.get().{field}, so we need to join to the related table
                    AbstractMemberMetaData mmd = invokedSqlExpr.getJavaTypeMapping().getMemberMetaData();
                    AbstractClassMetaData otherCmd = ec.getMetaDataManager().getMetaDataForClass(mmd.getCollection().getElementType(), clr);
                    Table otherTbl = storeMgr.getDatastoreClass(otherCmd.getFullClassName(), clr);
                    // Optional type so do LEFT OUTER JOIN since if it is null then we would eliminate all other results
                    invokeSqlTbl = stmt.join(JoinType.LEFT_OUTER_JOIN, invokeSqlExpr.getSQLTable(), opMapping.getWrappedMapping(), otherTbl, null, otherTbl.getIdMapping(), null, null, true);
                    tbl = invokeSqlTbl.getTable();
                }
            }
            if (tbl instanceof DatastoreClass) {
                // Table of a class, so assume to have field in the table of the class
                // TODO Allow joins to superclasses if required
                JavaTypeMapping mapping = ((DatastoreClass) tbl).getMemberMapping(expr.getId());
                if (mapping == null) {
                    throw new NucleusUserException("Dont currently support evaluating " + expr.getId() + " on " + invokeSqlExpr + ". The field " + expr.getId() + " doesnt exist in table " + tbl);
                }
                sqlExpr = exprFactory.newExpression(stmt, invokeSqlTbl, mapping);
                stack.push(sqlExpr);
                return sqlExpr;
            } else if (tbl instanceof JoinTable) {
                if (invokeSqlExpr.getJavaTypeMapping() instanceof EmbeddedMapping) {
                    // Table containing an embedded element/key/value so assume we have a column in the join table
                    EmbeddedMapping embMapping = (EmbeddedMapping) invokeSqlExpr.getJavaTypeMapping();
                    JavaTypeMapping mapping = embMapping.getJavaTypeMapping(expr.getId());
                    if (mapping == null) {
                        throw new NucleusUserException("Dont currently support evaluating " + expr.getId() + " on " + invokeSqlExpr + ". The field " + expr.getId() + " doesnt exist in table " + tbl);
                    }
                    sqlExpr = exprFactory.newExpression(stmt, invokeSqlTbl, mapping);
                    stack.push(sqlExpr);
                    return sqlExpr;
                }
            }
            throw new NucleusUserException("Dont currently support evaluating " + expr.getId() + " on " + invokeSqlExpr + " with invoke having table of " + tbl);
        } else {
            throw new NucleusUserException("Dont currently support PrimaryExpression with 'left' of " + expr.getLeft());
        }
    }
    // Real primary expression ("field.field", "alias.field.field" etc)
    SQLTableMapping sqlMapping = getSQLTableMappingForPrimaryExpression(stmt, null, expr, null);
    if (sqlMapping == null) {
        throw new NucleusException("PrimaryExpression " + expr.getId() + " is not yet supported");
    }
    sqlExpr = exprFactory.newExpression(stmt, sqlMapping.table, sqlMapping.mapping);
    if (sqlMapping.mmd != null && sqlExpr instanceof MapExpression) {
        // This sqlMapping is for something joined in a FROM clause, so set the alias on the returned MapExpression to avoid doing the same joins
        String alias = getAliasForSQLTableMapping(sqlMapping);
        if (alias == null && parentMapper != null) {
            alias = parentMapper.getAliasForSQLTableMapping(sqlMapping);
        }
        ((MapExpression) sqlExpr).setAliasForMapTable(alias);
    }
    stack.push(sqlExpr);
    return sqlExpr;
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) Symbol(org.datanucleus.query.compiler.Symbol) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) TemporalLiteral(org.datanucleus.store.rdbms.sql.expression.TemporalLiteral) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral) Literal(org.datanucleus.query.expression.Literal) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) Table(org.datanucleus.store.rdbms.table.Table) JoinTable(org.datanucleus.store.rdbms.table.JoinTable) ClassTable(org.datanucleus.store.rdbms.table.ClassTable) ElementContainerTable(org.datanucleus.store.rdbms.table.ElementContainerTable) MapTable(org.datanucleus.store.rdbms.table.MapTable) SQLTable(org.datanucleus.store.rdbms.sql.SQLTable) ArrayTable(org.datanucleus.store.rdbms.table.ArrayTable) CollectionTable(org.datanucleus.store.rdbms.table.CollectionTable) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) OptionalMapping(org.datanucleus.store.rdbms.mapping.java.OptionalMapping) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) EmbeddedMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedMapping) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) FetchPlanForClass(org.datanucleus.FetchPlanForClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) JoinTable(org.datanucleus.store.rdbms.table.JoinTable)

Example 2 with InvokeExpression

use of org.datanucleus.query.expression.InvokeExpression in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method getInvokedSqlExpressionForInvokeExpression.

protected SQLExpression getInvokedSqlExpressionForInvokeExpression(InvokeExpression expr) {
    Expression invokedExpr = expr.getLeft();
    SQLExpression invokedSqlExpr = null;
    if (invokedExpr == null) {
    // Static method
    } else if (invokedExpr instanceof PrimaryExpression) {
        processPrimaryExpression((PrimaryExpression) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof Literal) {
        processLiteral((Literal) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof ParameterExpression) {
        // TODO May be needed to set the second parameter to "false" here and then if the method
        // being invoked needs the parameters as a normal SQLLiteral then allow it to convert it itself
        processParameterExpression((ParameterExpression) invokedExpr, true);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof InvokeExpression) {
        processInvokeExpression((InvokeExpression) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof VariableExpression) {
        processVariableExpression((VariableExpression) invokedExpr);
        invokedSqlExpr = stack.pop();
    } else if (invokedExpr instanceof ArrayExpression) {
        ArrayExpression arrExpr = (ArrayExpression) invokedExpr;
        SQLExpression[] arrSqlExprs = new SQLExpression[arrExpr.getArraySize()];
        for (int i = 0; i < arrExpr.getArraySize(); i++) {
            Expression arrElemExpr = arrExpr.getElement(i);
            arrElemExpr.evaluate(this);
            arrSqlExprs[i] = stack.pop();
        }
        JavaTypeMapping m = exprFactory.getMappingForType(Object[].class, false);
        invokedSqlExpr = new org.datanucleus.store.rdbms.sql.expression.ArrayExpression(stmt, m, arrSqlExprs);
    } else if (invokedExpr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) invokedExpr;
        dyExpr.evaluate(this);
        invokedSqlExpr = stack.pop();
    } else {
        throw new NucleusException("Dont currently support invoke expression " + invokedExpr);
    }
    return invokedSqlExpr;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) CaseExpression(org.datanucleus.query.expression.CaseExpression) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) StringExpression(org.datanucleus.store.rdbms.sql.expression.StringExpression) JoinExpression(org.datanucleus.query.expression.JoinExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) ClassExpression(org.datanucleus.query.expression.ClassExpression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) MapExpression(org.datanucleus.store.rdbms.sql.expression.MapExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) NewObjectExpression(org.datanucleus.store.rdbms.sql.expression.NewObjectExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) BooleanExpression(org.datanucleus.store.rdbms.sql.expression.BooleanExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) TemporalExpression(org.datanucleus.store.rdbms.sql.expression.TemporalExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) ResultAliasExpression(org.datanucleus.store.rdbms.sql.expression.ResultAliasExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) Expression(org.datanucleus.query.expression.Expression) TypeExpression(org.datanucleus.query.expression.TypeExpression) NumericExpression(org.datanucleus.store.rdbms.sql.expression.NumericExpression) CollectionExpression(org.datanucleus.store.rdbms.sql.expression.CollectionExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ColumnExpression(org.datanucleus.store.rdbms.sql.expression.ColumnExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) TemporalLiteral(org.datanucleus.store.rdbms.sql.expression.TemporalLiteral) SQLLiteral(org.datanucleus.store.rdbms.sql.expression.SQLLiteral) ParameterLiteral(org.datanucleus.store.rdbms.sql.expression.ParameterLiteral) BooleanLiteral(org.datanucleus.store.rdbms.sql.expression.BooleanLiteral) IntegerLiteral(org.datanucleus.store.rdbms.sql.expression.IntegerLiteral) Literal(org.datanucleus.query.expression.Literal) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 3 with InvokeExpression

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

the class JPQLCompilerTest method testFilterWithStringIndexOfLiteral.

/**
 * Tests for "String.indexOf(Literal, int)" in filter.
 */
public void testFilterWithStringIndexOfLiteral() {
    JavaQueryCompiler compiler = null;
    QueryCompilation compilation = null;
    try {
        compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "LENGTH(name) > 5", 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("DyadicExpression operator should have been > but wasnt", dyExpr.getOperator() == Expression.OP_GT);
    assertTrue("DyadicExpression left should have been InvokeExpression but wasnt", dyExpr.getLeft() instanceof InvokeExpression);
    InvokeExpression leftInvExpr = (InvokeExpression) dyExpr.getLeft();
    assertTrue("InvokeExpression invoked object should have been PrimaryExpression but wasnt", leftInvExpr.getLeft() instanceof PrimaryExpression);
    PrimaryExpression invPrimExpr = (PrimaryExpression) leftInvExpr.getLeft();
    assertEquals("PrimaryExpression field is wrong", "name", invPrimExpr.getId());
    assertEquals("InvokeExpression method is wrong", "length", leftInvExpr.getOperation());
    List args = leftInvExpr.getArguments();
    assertTrue("Number of args should be 0 but isnt", args == null || args.isEmpty());
    assertTrue("DyadicExpression left should have been Literal but wasnt", dyExpr.getRight() instanceof Literal);
    Literal rightExpr = (Literal) dyExpr.getRight();
    assertEquals("Parameter2 to indexOf() has wrong value", new Long(5), rightExpr.getLiteral());
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) HashMap(java.util.HashMap) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) Project(org.datanucleus.samples.annotations.models.company.Project) 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) Literal(org.datanucleus.query.expression.Literal) List(java.util.List) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) NucleusException(org.datanucleus.exceptions.NucleusException) JPQLCompiler(org.datanucleus.query.compiler.JPQLCompiler)

Example 4 with InvokeExpression

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

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

InvokeExpression (org.datanucleus.query.expression.InvokeExpression)65 Literal (org.datanucleus.query.expression.Literal)42 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)40 Expression (org.datanucleus.query.expression.Expression)39 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)38 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)37 NucleusException (org.datanucleus.exceptions.NucleusException)32 ArrayList (java.util.ArrayList)25 VariableExpression (org.datanucleus.query.expression.VariableExpression)21 BooleanExpression (javax.jdo.query.BooleanExpression)17 NumericExpression (javax.jdo.query.NumericExpression)17 PersistableExpression (javax.jdo.query.PersistableExpression)17 Expression (javax.jdo.query.Expression)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)13 OrderExpression (org.datanucleus.query.expression.OrderExpression)12 List (java.util.List)10 CharacterExpression (javax.jdo.query.CharacterExpression)10 StringExpression (javax.jdo.query.StringExpression)10 LocalDateTime (java.time.LocalDateTime)7 Calendar (java.util.Calendar)7