Skip to content

Method: executeQuery(String)

1: /**
2: * Copyright (C) 2020 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.owlapi.query;
16:
17: import cz.cvut.kbss.ontodriver.ResultSet;
18: import cz.cvut.kbss.ontodriver.Statement;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20: import cz.cvut.kbss.ontodriver.owlapi.OwlapiConnection;
21:
22: import java.util.Objects;
23:
24: public class OwlapiStatement implements Statement {
25:
26: private StatementOntology targetOntology;
27: private boolean open;
28:
29: private final StatementExecutorFactory executorFactory;
30: final OwlapiConnection connection;
31:
32: ResultSet resultSet;
33:
34: public OwlapiStatement(StatementExecutorFactory executorFactory, OwlapiConnection connection) {
35: this.executorFactory = executorFactory;
36: this.connection = connection;
37: this.open = true;
38: }
39:
40: void ensureOpen() {
41: if (!open) {
42: throw new IllegalStateException("The statement is closed.");
43: }
44: }
45:
46: @Override
47: public ResultSet executeQuery(String sparql) throws OntoDriverException {
48: ensureOpen();
49: Objects.requireNonNull(sparql);
50: closeExistingResultSet();
51: this.resultSet = getExecutor().executeQuery(sparql, this);
52: return resultSet;
53: }
54:
55: StatementExecutor getExecutor() {
56: return executorFactory.getStatementExecutor(targetOntology);
57: }
58:
59: @Override
60: public void executeUpdate(String sparql) throws OntoDriverException {
61: ensureOpen();
62: Objects.requireNonNull(sparql);
63: closeExistingResultSet();
64: getExecutor().executeUpdate(sparql);
65: connection.commitIfAuto();
66: }
67:
68: @Override
69: public void useOntology(StatementOntology ontology) {
70: this.targetOntology = ontology;
71: }
72:
73: @Override
74: public StatementOntology getStatementOntology() {
75: return targetOntology;
76: }
77:
78:
79: @Override
80: public boolean isOpen() {
81: return open;
82: }
83:
84: @Override
85: public void close() throws OntoDriverException {
86: this.open = false;
87: closeExistingResultSet();
88: }
89:
90: void closeExistingResultSet() throws OntoDriverException {
91: if (resultSet != null) {
92: resultSet.close();
93: this.resultSet = null;
94: }
95: }
96: }