use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.
the class ProjectionProcessor method visit.
@Override
public Object visit(ASTSelect selectNode, Object data) throws VisitorException {
// Collect all variables used in the body of the select query
Set<String> bodyVars = VariableCollector.process(selectNode.jjtGetParent());
if (selectNode.isWildcard()) {
// Make wildcard projection explicit
for (String varName : bodyVars) {
ASTProjectionElem projElemNode = new ASTProjectionElem(SyntaxTreeBuilderTreeConstants.JJTPROJECTIONELEM);
selectNode.jjtAppendChild(projElemNode);
projElemNode.jjtSetParent(selectNode);
ASTVar varNode = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
varNode.setName(varName);
projElemNode.jjtAppendChild(varNode);
varNode.jjtSetParent(projElemNode);
}
selectNode.setWildcard(false);
} else {
// Verify that all projection vars are bound
Set<String> projVars = new LinkedHashSet<String>();
for (ASTProjectionElem projElem : selectNode.getProjectionElemList()) {
projVars.addAll(VariableCollector.process(projElem.getValueExpr()));
}
projVars.removeAll(bodyVars);
if (!projVars.isEmpty()) {
StringBuilder errMsg = new StringBuilder(64);
errMsg.append("Unbound variable(s) in projection: ");
Iterator<String> iter = projVars.iterator();
while (iter.hasNext()) {
errMsg.append(iter.next());
if (iter.hasNext()) {
errMsg.append(", ");
}
}
throw new VisitorException(errMsg.toString());
}
}
return data;
}
use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.
the class AnonymousVarGenerator method createNodeElem.
private ASTNodeElem createNodeElem() {
ASTNodeElem nodeElem = new ASTNodeElem(SyntaxTreeBuilderTreeConstants.JJTNODEELEM);
ASTVar var = createAnonymousVar();
var.jjtSetParent(nodeElem);
nodeElem.jjtAppendChild(var);
return nodeElem;
}
use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar 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;
}
use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public Var visit(ASTVar node, Object data) throws VisitorException {
Var var = new Var(node.getName());
var.setAnonymous(node.isAnonymous());
return var;
}
use of org.eclipse.rdf4j.query.parser.serql.ast.ASTVar in project rdf4j by eclipse.
the class AnonymousVarGenerator method createAnonymousVar.
private ASTVar createAnonymousVar() {
ASTVar var = new ASTVar(SyntaxTreeBuilderTreeConstants.JJTVAR);
var.setName("-anon-" + anonymousVarNo++);
var.setAnonymous(true);
return var;
}
Aggregations