Search in sources :

Example 16 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class SPARQLUpdateOperation method execute.

public void execute(RepositoryConnection con) throws RepositoryException {
    try {
        Update preparedUpdate = con.prepareUpdate(QueryLanguage.SPARQL, getUpdateString(), getBaseURI());
        preparedUpdate.setIncludeInferred(isIncludeInferred());
        preparedUpdate.setDataset(getDataset());
        if (getBindings() != null) {
            for (Binding binding : getBindings()) {
                preparedUpdate.setBinding(binding.getName(), binding.getValue());
            }
        }
        preparedUpdate.execute();
    } catch (MalformedQueryException e) {
        throw new RepositoryException(e);
    } catch (UpdateExecutionException e) {
        throw new RepositoryException(e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) UpdateExecutionException(org.eclipse.rdf4j.query.UpdateExecutionException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) Update(org.eclipse.rdf4j.query.Update)

Example 17 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class HTTPUpdate method execute.

@Override
public void execute() throws UpdateExecutionException {
    try {
        if (httpCon.getRepository().useCompatibleMode()) {
            if (httpCon.isAutoCommit()) {
                // execute update immediately
                SPARQLProtocolSession client = getHttpClient();
                try {
                    client.sendUpdate(getQueryLanguage(), getQueryString(), getBaseURI(), dataset, includeInferred, getMaxExecutionTime(), getBindingsArray());
                } catch (UnauthorizedException e) {
                    throw new HTTPUpdateExecutionException(e.getMessage(), e);
                } catch (QueryInterruptedException e) {
                    throw new HTTPUpdateExecutionException(e.getMessage(), e);
                } catch (MalformedQueryException e) {
                    throw new HTTPUpdateExecutionException(e.getMessage(), e);
                } catch (IOException e) {
                    throw new HTTPUpdateExecutionException(e.getMessage(), e);
                }
            } else {
                // defer execution as part of transaction.
                httpCon.scheduleUpdate(this);
            }
            return;
        }
        SPARQLProtocolSession client = getHttpClient();
        try {
            httpCon.flushTransactionState(Action.UPDATE);
            client.sendUpdate(getQueryLanguage(), getQueryString(), getBaseURI(), dataset, includeInferred, getMaxExecutionTime(), getBindingsArray());
        } catch (UnauthorizedException e) {
            throw new HTTPUpdateExecutionException(e.getMessage(), e);
        } catch (QueryInterruptedException e) {
            throw new HTTPUpdateExecutionException(e.getMessage(), e);
        } catch (MalformedQueryException e) {
            throw new HTTPUpdateExecutionException(e.getMessage(), e);
        } catch (IOException e) {
            throw new HTTPUpdateExecutionException(e.getMessage(), e);
        }
    } catch (RepositoryException e) {
        throw new HTTPUpdateExecutionException(e.getMessage(), e);
    }
}
Also used : SPARQLProtocolSession(org.eclipse.rdf4j.http.client.SPARQLProtocolSession) UnauthorizedException(org.eclipse.rdf4j.http.protocol.UnauthorizedException) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) QueryInterruptedException(org.eclipse.rdf4j.query.QueryInterruptedException)

Example 18 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class SPARQLConnection method clear.

public void clear(Resource... contexts) throws RepositoryException {
    OpenRDFUtil.verifyContextNotNull(contexts);
    boolean localTransaction = startLocalTransaction();
    if (contexts.length == 0) {
        sparqlTransaction.append("CLEAR ALL ");
        sparqlTransaction.append("; ");
    } else {
        for (Resource context : contexts) {
            if (context == null) {
                sparqlTransaction.append("CLEAR DEFAULT ");
                sparqlTransaction.append("; ");
            } else if (context instanceof IRI) {
                sparqlTransaction.append("CLEAR GRAPH <" + context.stringValue() + "> ");
                sparqlTransaction.append("; ");
            } else {
                throw new RepositoryException("SPARQL does not support named graphs identified by blank nodes.");
            }
        }
    }
    try {
        conditionalCommit(localTransaction);
    } catch (RepositoryException e) {
        conditionalRollback(localTransaction);
        throw e;
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException)

Example 19 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException 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 20 with RepositoryException

use of org.eclipse.rdf4j.repository.RepositoryException in project rdf4j by eclipse.

the class SPARQLConnection method setBindings.

/* protected/private methods */
private void setBindings(Query query, Resource subj, IRI pred, Value obj, Resource... contexts) throws RepositoryException {
    if (subj != null) {
        query.setBinding("s", subj);
    }
    if (pred != null) {
        query.setBinding("p", pred);
    }
    if (obj != null) {
        query.setBinding("o", obj);
    }
    if (contexts != null && contexts.length > 0) {
        SimpleDataset dataset = new SimpleDataset();
        for (Resource ctx : contexts) {
            if (ctx == null || ctx instanceof IRI) {
                dataset.addDefaultGraph((IRI) ctx);
            } else {
                throw new RepositoryException("Contexts must be URIs");
            }
        }
        query.setDataset(dataset);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset)

Aggregations

RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)67 IOException (java.io.IOException)24 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)20 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)15 RDF4JException (org.eclipse.rdf4j.RDF4JException)13 HttpResponse (org.apache.http.HttpResponse)11 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)10 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 HttpGet (org.apache.http.client.methods.HttpGet)7 Resource (org.eclipse.rdf4j.model.Resource)7 Statement (org.eclipse.rdf4j.model.Statement)6 ArrayList (java.util.ArrayList)5 HttpPut (org.apache.http.client.methods.HttpPut)5 RDF4JProtocolSession (org.eclipse.rdf4j.http.client.RDF4JProtocolSession)5 UnauthorizedException (org.eclipse.rdf4j.http.protocol.UnauthorizedException)5 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 TupleQuery (org.eclipse.rdf4j.query.TupleQuery)5 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4