Search in sources :

Example 1 with ExpressionCompiler

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

the class JavaQueryCompiler method compileFilter.

/**
 * Compile the filter and return the compiled expression.
 * @return The compiled expression
 */
public Expression compileFilter() {
    if (filter != null) {
        // Generate the node tree for the filter
        Node node = parser.parse(filter);
        if (candidateAliasOrig != null) {
            swapCandidateAliasNodeName(node);
        }
        if (parameterSubtitutionMap != null) {
            node = swapSubqueryParameters(node);
        }
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        Expression expr = comp.compileExpression(node);
        expr.bind(symtbl);
        return expr;
    }
    return null;
}
Also used : ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Example 2 with ExpressionCompiler

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

the class JavaQueryCompiler method compileOrdering.

public Expression[] compileOrdering() {
    if (ordering == null) {
        return null;
    }
    Node[] node = parser.parseOrder(ordering);
    Expression[] expr = new Expression[node.length];
    for (int i = 0; i < node.length; i++) {
        if (candidateAliasOrig != null) {
            swapCandidateAliasNodeName(node[i]);
        }
        if (parameterSubtitutionMap != null) {
            node[i] = swapSubqueryParameters(node[i]);
        }
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        expr[i] = comp.compileOrderExpression(node[i]);
        expr[i].bind(symtbl);
    }
    return expr;
}
Also used : ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Example 3 with ExpressionCompiler

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

the class JavaQueryCompiler method compileGrouping.

public Expression[] compileGrouping() {
    if (grouping == null) {
        return null;
    }
    Node[] node = parser.parseTuple(grouping);
    Expression[] expr = new Expression[node.length];
    for (int i = 0; i < node.length; i++) {
        if (candidateAliasOrig != null) {
            swapCandidateAliasNodeName(node[i]);
        }
        if (parameterSubtitutionMap != null) {
            node[i] = swapSubqueryParameters(node[i]);
        }
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        expr[i] = comp.compileExpression(node[i]);
        expr[i].bind(symtbl);
    }
    return expr;
}
Also used : ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Example 4 with ExpressionCompiler

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

the class JavaQueryCompiler method compileFrom.

/**
 * Method to compile the "from" clause (if present for the query language).
 * @return The compiled from expression(s)
 */
protected Expression[] compileFrom() {
    if (from == null) {
        return null;
    }
    Node[] node = parser.parseFrom(from);
    Expression[] expr = new Expression[node.length];
    for (int i = 0; i < node.length; i++) {
        String className = (String) node[i].getNodeValue();
        String classAlias = null;
        Class cls = null;
        if (parentCompiler != null) {
            cls = getClassForSubqueryClassExpression(className);
        } else {
            cls = resolveClass(className);
        }
        List<Node> children = node[i].getChildNodes();
        for (Node child : children) {
            if (// Alias - maybe should assume it is the first child
            child.getNodeType() == NodeType.NAME) {
                classAlias = (String) child.getNodeValue();
            }
        }
        if (i == 0 && classAlias == null) {
            throw new QueryCompilerSyntaxException("FROM clause of query has class " + cls.getName() + " but no alias");
        }
        if (classAlias != null) {
            if (i == 0) {
                // First expression so set up candidateClass/alias
                candidateClass = cls;
                if (parentCompiler != null && parentCompiler.candidateAlias.equals(classAlias)) {
                    // The defined alias is the same as the parent query, so rename
                    candidateAliasOrig = classAlias;
                    candidateAlias = "sub_" + candidateAlias;
                    classAlias = candidateAlias;
                    swapCandidateAliasNodeName(node[i].getChildNode(0));
                } else {
                    candidateAlias = classAlias;
                }
            }
            if (symtbl.getSymbol(classAlias) == null) {
                // Add symbol for this candidate under its alias
                symtbl.addSymbol(new PropertySymbol(classAlias, cls));
            }
        }
        for (Node childNode : children) {
            // Add entries in symbol table for any joined aliases
            if (childNode.getNodeType() == NodeType.OPERATOR) {
                Node joinedNode = childNode.getFirstChild();
                // Extract alias node
                Node aliasNode = childNode.getNextChild();
                // Extract ON node (if present)
                Node onExprNode = null;
                if (childNode.hasNextChild()) {
                    onExprNode = childNode.getNextChild();
                }
                String joinedAlias = (String) joinedNode.getNodeValue();
                boolean rootNode = false;
                Symbol joinedSym = caseSensitiveAliases ? symtbl.getSymbol(joinedAlias) : symtbl.getSymbolIgnoreCase(joinedAlias);
                if (joinedSym == null) {
                    // DN Extension : Check for FROM clause including join to a new root
                    if (aliasNode != null) {
                        joinedAlias = (String) aliasNode.getNodeValue();
                        cls = resolveClass((String) joinedNode.getNodeValue());
                        if (symtbl.getSymbol(joinedAlias) == null) {
                            // Add symbol for this candidate under its alias
                            symtbl.addSymbol(new PropertySymbol(joinedAlias, cls));
                            rootNode = true;
                            NucleusLogger.QUERY.debug("Found suspected ROOT node joined to in FROM clause : attempting to process as alias=" + joinedAlias);
                        }
                        joinedSym = caseSensitiveAliases ? symtbl.getSymbol(joinedAlias) : symtbl.getSymbolIgnoreCase(joinedAlias);
                    }
                    if (joinedSym == null) {
                        throw new QueryCompilerSyntaxException("FROM clause has identifier " + joinedNode.getNodeValue() + " but this is unknown");
                    }
                }
                if (!rootNode) {
                    AbstractClassMetaData joinedCmd = metaDataManager.getMetaDataForClass(joinedSym.getValueType(), clr);
                    Class joinedCls = joinedSym.getValueType();
                    AbstractMemberMetaData joinedMmd = null;
                    while (joinedNode.getFirstChild() != null) {
                        joinedNode = joinedNode.getFirstChild();
                        String joinedMember = (String) joinedNode.getNodeValue();
                        if (joinedNode.getNodeType() == NodeType.CAST) {
                            // JOIN to "TREAT(identifier AS subcls)"
                            String castTypeName = (String) joinedNode.getNodeValue();
                            if (castTypeName.indexOf('.') < 0) {
                                // Fully-qualify with the current class name?
                                castTypeName = ClassUtils.createFullClassName(joinedCmd.getPackageName(), castTypeName);
                            }
                            joinedCls = clr.classForName(castTypeName);
                            // Update cast type now that we have resolved it
                            joinedNode.setNodeValue(castTypeName);
                        } else {
                            // Allow for multi-field joins
                            String[] joinedMembers = joinedMember.contains(".") ? StringUtils.split(joinedMember, ".") : new String[] { joinedMember };
                            for (int k = 0; k < joinedMembers.length; k++) {
                                String memberName = joinedMembers[k];
                                if (joinedCmd == null) {
                                    throw new NucleusUserException("Query has JOIN to " + memberName + " but previous element (" + joinedCls.getName() + ") has no metadata");
                                }
                                if (memberName.endsWith("#KEY")) {
                                    memberName = memberName.substring(0, memberName.length() - 4);
                                } else if (memberName.endsWith("#VALUE")) {
                                    memberName = memberName.substring(0, memberName.length() - 6);
                                }
                                AbstractMemberMetaData mmd = joinedCmd.getMetaDataForMember(memberName);
                                if (mmd == null) {
                                    if (childNode.getNodeValue().equals(JOIN_OUTER) || childNode.getNodeValue().equals(JOIN_OUTER_FETCH)) {
                                        // Polymorphic join, where the field exists in a subclass (doable since we have outer join)
                                        String[] subclasses = metaDataManager.getSubclassesForClass(joinedCmd.getFullClassName(), true);
                                        if (subclasses != null) {
                                            for (int l = 0; l < subclasses.length; l++) {
                                                AbstractClassMetaData subCmd = metaDataManager.getMetaDataForClass(subclasses[l], clr);
                                                if (subCmd != null) {
                                                    mmd = subCmd.getMetaDataForMember(memberName);
                                                    if (mmd != null) {
                                                        NucleusLogger.QUERY.debug("Polymorphic join found at " + memberName + " of " + subCmd.getFullClassName());
                                                        joinedCmd = subCmd;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (mmd == null) {
                                        throw new QueryCompilerSyntaxException("FROM clause has reference to " + joinedCmd.getFullClassName() + "." + joinedMembers[k] + " but it doesn't exist!");
                                    }
                                }
                                RelationType relationType = mmd.getRelationType(clr);
                                joinedMmd = mmd;
                                if (RelationType.isRelationSingleValued(relationType)) {
                                    joinedCls = mmd.getType();
                                    joinedCmd = metaDataManager.getMetaDataForClass(joinedCls, clr);
                                } else if (RelationType.isRelationMultiValued(relationType)) {
                                    if (mmd.hasCollection()) {
                                        // TODO Don't currently allow interface field navigation
                                        joinedCmd = mmd.getCollection().getElementClassMetaData(clr);
                                        if (joinedCmd != null) {
                                            joinedCls = clr.classForName(joinedCmd.getFullClassName());
                                        } else {
                                            joinedCls = clr.classForName(mmd.getCollection().getElementType());
                                        }
                                    } else if (mmd.hasMap()) {
                                        if (joinedMembers[k].endsWith("#KEY")) {
                                            joinedCmd = mmd.getMap().getKeyClassMetaData(clr);
                                        // TODO Set joinedCls
                                        } else {
                                            joinedCmd = mmd.getMap().getValueClassMetaData(clr);
                                            if (joinedCmd != null) {
                                                // JPA assumption that the value is an entity ... but it may not be!
                                                joinedCls = clr.classForName(joinedCmd.getFullClassName());
                                            } else {
                                                joinedCls = clr.classForName(mmd.getMap().getValueType());
                                            }
                                        }
                                    } else if (mmd.hasArray()) {
                                        // TODO Don't currently allow interface field navigation
                                        joinedCmd = mmd.getArray().getElementClassMetaData(clr);
                                        if (joinedCmd != null) {
                                            joinedCls = clr.classForName(joinedCmd.getFullClassName());
                                        } else {
                                            joinedCls = clr.classForName(mmd.getArray().getElementType());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (aliasNode != null && aliasNode.getNodeType() == NodeType.NAME) {
                        // Add JOIN alias to symbol table
                        String alias = (String) aliasNode.getNodeValue();
                        symtbl.addSymbol(new PropertySymbol(alias, joinedCls));
                        if (joinedMmd != null && joinedMmd.hasMap()) {
                            Class keyCls = clr.classForName(joinedMmd.getMap().getKeyType());
                            // Add the KEY so that we can have joins to the key from the value alias
                            symtbl.addSymbol(new PropertySymbol(alias + "#KEY", keyCls));
                            Class valueCls = clr.classForName(joinedMmd.getMap().getValueType());
                            // Add the VALUE so that we can have joins to the value from the key alias
                            symtbl.addSymbol(new PropertySymbol(alias + "#VALUE", valueCls));
                        }
                    }
                }
                if (onExprNode != null) {
                    // ON condition
                    ExpressionCompiler comp = new ExpressionCompiler();
                    comp.setSymbolTable(symtbl);
                    comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
                    Expression nextExpr = comp.compileExpression(onExprNode);
                    nextExpr.bind(symtbl);
                }
            }
        }
        boolean classIsExpression = false;
        String[] tokens = StringUtils.split(className, ".");
        if (symtbl.getParentSymbolTable() != null) {
            if (symtbl.getParentSymbolTable().hasSymbol(tokens[0])) {
                classIsExpression = true;
            }
        }
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        expr[i] = comp.compileFromExpression(node[i], classIsExpression);
        if (expr[i] != null) {
            expr[i].bind(symtbl);
        }
    }
    return expr;
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) QueryCompilerSyntaxException(org.datanucleus.store.query.QueryCompilerSyntaxException) RelationType(org.datanucleus.metadata.RelationType) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 5 with ExpressionCompiler

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

the class JavaQueryCompiler method compileHaving.

public Expression compileHaving() {
    if (having == null) {
        return null;
    }
    Node node = parser.parse(having);
    if (candidateAliasOrig != null) {
        swapCandidateAliasNodeName(node);
    }
    if (parameterSubtitutionMap != null) {
        node = swapSubqueryParameters(node);
    }
    ExpressionCompiler comp = new ExpressionCompiler();
    comp.setSymbolTable(symtbl);
    comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
    Expression expr = comp.compileExpression(node);
    expr.bind(symtbl);
    return expr;
}
Also used : ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Aggregations

Expression (org.datanucleus.query.expression.Expression)7 ExpressionCompiler (org.datanucleus.query.expression.ExpressionCompiler)7 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)7 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)7 VariableExpression (org.datanucleus.query.expression.VariableExpression)7 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 QueryCompilerSyntaxException (org.datanucleus.store.query.QueryCompilerSyntaxException)2 Field (java.lang.reflect.Field)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 RelationType (org.datanucleus.metadata.RelationType)1 Literal (org.datanucleus.query.expression.Literal)1 PrimaryExpressionIsClassLiteralException (org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException)1 PrimaryExpressionIsClassStaticFieldException (org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException)1 PrimaryExpressionIsVariableException (org.datanucleus.query.expression.PrimaryExpressionIsVariableException)1