Search in sources :

Example 1 with ASTString

use of org.eclipse.rdf4j.query.parser.serql.ast.ASTString in project rdf4j by eclipse.

the class ProjectionAliasProcessor method visit.

@Override
public Object visit(ASTSelect node, Object data) throws VisitorException {
    // Iterate over all projection elements to retrieve the defined aliases
    Set<String> aliases = new HashSet<String>();
    List<Node> unaliasedNodes = new ArrayList<Node>();
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        ASTProjectionElem projElem = (ASTProjectionElem) node.jjtGetChild(i);
        String alias = projElem.getAlias();
        if (alias == null && projElem.getValueExpr() instanceof ASTVar) {
            alias = ((ASTVar) projElem.getValueExpr()).getName();
        }
        if (alias != null) {
            boolean isUnique = aliases.add(alias);
            if (!isUnique) {
                throw new VisitorException("Duplicate projection element names: '" + alias + "'");
            }
        } else {
            unaliasedNodes.add(projElem);
        }
    }
    // Iterate over the unaliased nodes and generate aliases for them
    int aliasNo = 1;
    for (Node projElem : unaliasedNodes) {
        // Generate unique alias for projection element
        String alias;
        while (aliases.contains(alias = "_" + aliasNo++)) {
        // try again
        }
        aliases.add(alias);
        ASTString aliasNode = new ASTString(SyntaxTreeBuilderTreeConstants.JJTSTRING);
        aliasNode.setValue(alias);
        aliasNode.jjtSetParent(projElem);
        projElem.jjtAppendChild(aliasNode);
    }
    return data;
}
Also used : ASTVar(org.eclipse.rdf4j.query.parser.serql.ast.ASTVar) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) Node(org.eclipse.rdf4j.query.parser.serql.ast.Node) ArrayList(java.util.ArrayList) ASTString(org.eclipse.rdf4j.query.parser.serql.ast.ASTString) ASTProjectionElem(org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem) VisitorException(org.eclipse.rdf4j.query.parser.serql.ast.VisitorException) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ASTProjectionElem (org.eclipse.rdf4j.query.parser.serql.ast.ASTProjectionElem)1 ASTString (org.eclipse.rdf4j.query.parser.serql.ast.ASTString)1 ASTVar (org.eclipse.rdf4j.query.parser.serql.ast.ASTVar)1 Node (org.eclipse.rdf4j.query.parser.serql.ast.Node)1 VisitorException (org.eclipse.rdf4j.query.parser.serql.ast.VisitorException)1