Skip to content

Method: ConnectionStatementExecutor(RepositoryConnection)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.rdf4j.connector;
16:
17: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
18: import cz.cvut.kbss.ontodriver.rdf4j.query.QuerySpecification;
19: import org.eclipse.rdf4j.query.*;
20: import org.eclipse.rdf4j.repository.RepositoryConnection;
21: import org.eclipse.rdf4j.repository.RepositoryException;
22:
23: /**
24: * Actual implementation of statement processing.
25: */
26: class ConnectionStatementExecutor implements StatementExecutor {
27:
28: private final RepositoryConnection connection;
29:
30: ConnectionStatementExecutor(RepositoryConnection connection) {
31: this.connection = connection;
32: }
33:
34: @Override
35: public TupleQueryResult executeSelectQuery(QuerySpecification query) throws Rdf4jDriverException {
36: try {
37: final TupleQuery tq = connection.prepareTupleQuery(QueryLanguage.SPARQL, query.getQuery());
38: tq.setIncludeInferred(query.isIncludeInference());
39: return new QueryResult(tq.evaluate(), connection);
40: } catch (MalformedQueryException | QueryEvaluationException | RepositoryException e) {
41: throw new Rdf4jDriverException(e);
42: }
43: }
44:
45: @Override
46: public boolean executeBooleanQuery(QuerySpecification query) throws Rdf4jDriverException {
47: try {
48: final BooleanQuery bq = connection.prepareBooleanQuery(QueryLanguage.SPARQL, query.getQuery());
49: bq.setIncludeInferred(query.isIncludeInference());
50: return bq.evaluate();
51: } catch (MalformedQueryException | QueryEvaluationException | RepositoryException e) {
52: throw new Rdf4jDriverException(e);
53: }
54: }
55:
56: @Override
57: public void executeUpdate(QuerySpecification query) throws Rdf4jDriverException {
58: try {
59: final Update u = connection.prepareUpdate(QueryLanguage.SPARQL, query.getQuery());
60: u.setIncludeInferred(query.isIncludeInference());
61: u.execute();
62: } catch (MalformedQueryException | UpdateExecutionException | RepositoryException e) {
63: throw new Rdf4jDriverException(e);
64: }
65: }
66: }