Search in sources :

Example 1 with ASTDescribeQuery

use of org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery in project rdf4j by eclipse.

the class SPARQLParser method parseQuery.

@Override
public ParsedQuery parseQuery(String queryStr, String baseURI) throws MalformedQueryException {
    try {
        ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(queryStr);
        StringEscapesProcessor.process(qc);
        BaseDeclProcessor.process(qc, baseURI);
        Map<String, String> prefixes = PrefixDeclProcessor.process(qc);
        WildcardProjectionProcessor.process(qc);
        BlankNodeVarProcessor.process(qc);
        if (qc.containsQuery()) {
            // handle query operation
            TupleExpr tupleExpr = buildQueryModel(qc);
            ParsedQuery query;
            ASTQuery queryNode = qc.getQuery();
            if (queryNode instanceof ASTSelectQuery) {
                query = new ParsedTupleQuery(queryStr, tupleExpr);
            } else if (queryNode instanceof ASTConstructQuery) {
                query = new ParsedGraphQuery(queryStr, tupleExpr, prefixes);
            } else if (queryNode instanceof ASTAskQuery) {
                query = new ParsedBooleanQuery(queryStr, tupleExpr);
            } else if (queryNode instanceof ASTDescribeQuery) {
                query = new ParsedDescribeQuery(queryStr, tupleExpr, prefixes);
            } else {
                throw new RuntimeException("Unexpected query type: " + queryNode.getClass());
            }
            // Handle dataset declaration
            Dataset dataset = DatasetDeclProcessor.process(qc);
            if (dataset != null) {
                query.setDataset(dataset);
            }
            return query;
        } else {
            throw new IncompatibleOperationException("supplied string is not a query operation");
        }
    } catch (ParseException e) {
        throw new MalformedQueryException(e.getMessage(), e);
    } catch (TokenMgrError e) {
        throw new MalformedQueryException(e.getMessage(), e);
    }
}
Also used : ASTAskQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTAskQuery) ParsedDescribeQuery(org.eclipse.rdf4j.query.parser.ParsedDescribeQuery) IncompatibleOperationException(org.eclipse.rdf4j.query.IncompatibleOperationException) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Dataset(org.eclipse.rdf4j.query.Dataset) ParsedGraphQuery(org.eclipse.rdf4j.query.parser.ParsedGraphQuery) ASTQueryContainer(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer) TokenMgrError(org.eclipse.rdf4j.query.parser.sparql.ast.TokenMgrError) ASTDescribeQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr) ASTQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTQuery) ParsedBooleanQuery(org.eclipse.rdf4j.query.parser.ParsedBooleanQuery) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTConstructQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTConstructQuery) ASTSelectQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery) ParseException(org.eclipse.rdf4j.query.parser.sparql.ast.ParseException) ParsedTupleQuery(org.eclipse.rdf4j.query.parser.ParsedTupleQuery)

Example 2 with ASTDescribeQuery

use of org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery in project rdf4j by eclipse.

the class WildcardProjectionProcessor method process.

public static void process(ASTOperationContainer container) throws MalformedQueryException {
    ASTOperation operation = container.getOperation();
    // scan for nested SELECT clauses in the operation's WHERE clause
    if (operation != null) {
        ASTWhereClause whereClause = operation.getWhereClause();
        // DESCRIBE queries and certain update operations can be without a WHERE clause
        if (whereClause != null) {
            SelectClauseCollector collector = new SelectClauseCollector();
            try {
                whereClause.jjtAccept(collector, null);
                Set<ASTSelect> selectClauses = collector.getSelectClauses();
                for (ASTSelect selectClause : selectClauses) {
                    if (selectClause.isWildcard()) {
                        ASTSelectQuery q = (ASTSelectQuery) selectClause.jjtGetParent();
                        addQueryVars(q.getWhereClause(), selectClause);
                        selectClause.setWildcard(false);
                    }
                }
            } catch (VisitorException e) {
                throw new MalformedQueryException(e);
            }
        }
    }
    if (operation instanceof ASTSelectQuery) {
        // check for wildcard in upper SELECT query
        ASTSelectQuery selectQuery = (ASTSelectQuery) operation;
        ASTSelect selectClause = selectQuery.getSelect();
        if (selectClause.isWildcard()) {
            addQueryVars(selectQuery.getWhereClause(), selectClause);
            selectClause.setWildcard(false);
        }
    } else if (operation instanceof ASTDescribeQuery) {
        // check for possible wildcard in DESCRIBE query
        ASTDescribeQuery describeQuery = (ASTDescribeQuery) operation;
        ASTDescribe describeClause = describeQuery.getDescribe();
        if (describeClause.isWildcard()) {
            addQueryVars(describeQuery.getWhereClause(), describeClause);
            describeClause.setWildcard(false);
        }
    }
}
Also used : ASTOperation(org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation) ASTWhereClause(org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) ASTSelectQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery) VisitorException(org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException) ASTDescribeQuery(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery) ASTDescribe(org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe) ASTSelect(org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelect)

Aggregations

MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 ASTDescribeQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribeQuery)2 ASTSelectQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelectQuery)2 Dataset (org.eclipse.rdf4j.query.Dataset)1 IncompatibleOperationException (org.eclipse.rdf4j.query.IncompatibleOperationException)1 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)1 ParsedBooleanQuery (org.eclipse.rdf4j.query.parser.ParsedBooleanQuery)1 ParsedDescribeQuery (org.eclipse.rdf4j.query.parser.ParsedDescribeQuery)1 ParsedGraphQuery (org.eclipse.rdf4j.query.parser.ParsedGraphQuery)1 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)1 ParsedTupleQuery (org.eclipse.rdf4j.query.parser.ParsedTupleQuery)1 ASTAskQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTAskQuery)1 ASTConstructQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTConstructQuery)1 ASTDescribe (org.eclipse.rdf4j.query.parser.sparql.ast.ASTDescribe)1 ASTOperation (org.eclipse.rdf4j.query.parser.sparql.ast.ASTOperation)1 ASTQuery (org.eclipse.rdf4j.query.parser.sparql.ast.ASTQuery)1 ASTQueryContainer (org.eclipse.rdf4j.query.parser.sparql.ast.ASTQueryContainer)1 ASTSelect (org.eclipse.rdf4j.query.parser.sparql.ast.ASTSelect)1 ASTWhereClause (org.eclipse.rdf4j.query.parser.sparql.ast.ASTWhereClause)1 ParseException (org.eclipse.rdf4j.query.parser.sparql.ast.ParseException)1