Search in sources :

Example 1 with GraphQuery

use of org.eclipse.rdf4j.query.GraphQuery in project rdf4j by eclipse.

the class Repositories method graphQuery.

/**
 * Performs a SPARQL Construct or Describe query on the given Repository within a transaction and passes
 * the results to the given {@link RDFHandler}.
 *
 * @param repository
 *        The {@link Repository} to open a connection to.
 * @param query
 *        The SPARQL Construct or Describe query to execute.
 * @param handler
 *        An {@link RDFHandler} that consumes the results.
 * @throws RepositoryException
 *         If there was an exception dealing with the Repository.
 * @throws UnknownTransactionStateException
 *         If the transaction state was not properly recognised. (Optional specific exception)
 * @throws MalformedQueryException
 *         If the supplied query is malformed
 * @throws QueryEvaluationException
 *         If there was an error evaluating the query
 */
public static void graphQuery(Repository repository, String query, RDFHandler handler) throws RepositoryException, UnknownTransactionStateException, MalformedQueryException, QueryEvaluationException {
    consume(repository, conn -> {
        GraphQuery preparedQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, query);
        preparedQuery.evaluate(handler);
    });
}
Also used : GraphQuery(org.eclipse.rdf4j.query.GraphQuery)

Example 2 with GraphQuery

use of org.eclipse.rdf4j.query.GraphQuery in project rdf4j by eclipse.

the class Repositories method graphQueryNoTransaction.

/**
 * Performs a SPARQL Construct or Describe query on the given Repository without opening a transaction and
 * passes the results to the given {@link RDFHandler}.
 *
 * @param repository
 *        The {@link Repository} to open a connection to.
 * @param query
 *        The SPARQL Construct or Describe query to execute.
 * @param handler
 *        An {@link RDFHandler} that consumes the results.
 * @throws RepositoryException
 *         If there was an exception dealing with the Repository.
 * @throws UnknownTransactionStateException
 *         If the transaction state was not properly recognised. (Optional specific exception)
 * @throws MalformedQueryException
 *         If the supplied query is malformed
 * @throws QueryEvaluationException
 *         If there was an error evaluating the query
 */
public static void graphQueryNoTransaction(Repository repository, String query, RDFHandler handler) throws RepositoryException, UnknownTransactionStateException, MalformedQueryException, QueryEvaluationException {
    consumeNoTransaction(repository, conn -> {
        GraphQuery preparedQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, query);
        preparedQuery.evaluate(handler);
    });
}
Also used : GraphQuery(org.eclipse.rdf4j.query.GraphQuery)

Example 3 with GraphQuery

use of org.eclipse.rdf4j.query.GraphQuery in project rdf4j by eclipse.

the class SPARQLConnection method exportStatements.

public void exportStatements(Resource subj, IRI pred, Value obj, boolean includeInferred, RDFHandler handler, Resource... contexts) throws RepositoryException, RDFHandlerException {
    try {
        GraphQuery query = prepareGraphQuery(SPARQL, EVERYTHING, "");
        setBindings(query, subj, pred, obj, contexts);
        query.evaluate(handler);
    } catch (MalformedQueryException e) {
        throw new RepositoryException(e);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    }
}
Also used : QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SPARQLGraphQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLGraphQuery) GraphQuery(org.eclipse.rdf4j.query.GraphQuery)

Example 4 with GraphQuery

use of org.eclipse.rdf4j.query.GraphQuery in project rdf4j by eclipse.

the class SPARQLConnection method getStatementGeneral.

private RepositoryResult<Statement> getStatementGeneral(Resource subj, IRI pred, Value obj, boolean includeInferred, Resource... contexts) throws RepositoryException, MalformedQueryException, QueryEvaluationException {
    GraphQueryResult gRes = null;
    RepositoryResult<Statement> result = null;
    boolean allGood = false;
    try {
        GraphQuery query = prepareGraphQuery(SPARQL, EVERYTHING, "");
        query.setIncludeInferred(includeInferred);
        setBindings(query, subj, pred, obj, contexts);
        gRes = query.evaluate();
        result = new RepositoryResult<Statement>(new ExceptionConvertingIteration<Statement, RepositoryException>(gRes) {

            @Override
            protected RepositoryException convert(Exception e) {
                return new RepositoryException(e);
            }
        });
        allGood = true;
        return result;
    } finally {
        if (!allGood) {
            try {
                if (result != null) {
                    result.close();
                }
            } finally {
                if (gRes != null) {
                    gRes.close();
                }
            }
        }
    }
}
Also used : Statement(org.eclipse.rdf4j.model.Statement) ExceptionConvertingIteration(org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SPARQLGraphQuery(org.eclipse.rdf4j.repository.sparql.query.SPARQLGraphQuery) GraphQuery(org.eclipse.rdf4j.query.GraphQuery) GraphQueryResult(org.eclipse.rdf4j.query.GraphQueryResult) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) UnknownTransactionStateException(org.eclipse.rdf4j.repository.UnknownTransactionStateException) UnsupportedQueryLanguageException(org.eclipse.rdf4j.query.UnsupportedQueryLanguageException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Aggregations

GraphQuery (org.eclipse.rdf4j.query.GraphQuery)4 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)2 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)2 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 SPARQLGraphQuery (org.eclipse.rdf4j.repository.sparql.query.SPARQLGraphQuery)2 IOException (java.io.IOException)1 ExceptionConvertingIteration (org.eclipse.rdf4j.common.iteration.ExceptionConvertingIteration)1 Statement (org.eclipse.rdf4j.model.Statement)1 GraphQueryResult (org.eclipse.rdf4j.query.GraphQueryResult)1 UnsupportedQueryLanguageException (org.eclipse.rdf4j.query.UnsupportedQueryLanguageException)1 UpdateExecutionException (org.eclipse.rdf4j.query.UpdateExecutionException)1 UnknownTransactionStateException (org.eclipse.rdf4j.repository.UnknownTransactionStateException)1 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)1 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)1