Skip to contentMethod: validateQueryParams(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.sesame.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.sesame.connector.StatementExecutor;
21: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
22: import org.eclipse.rdf4j.query.QueryEvaluationException;
23: import org.eclipse.rdf4j.query.TupleQueryResult;
24:
25: import java.util.Objects;
26:
27: public class SesameStatement implements Statement {
28:
29: private StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
30: private final StatementExecutor queryExecutor;
31: private ResultSet resultSet;
32:
33: private boolean open;
34:
35: public SesameStatement(StatementExecutor queryExecutor) {
36: this.queryExecutor = queryExecutor;
37: this.open = true;
38: }
39:
40: @Override
41: public ResultSet executeQuery(String sparql) throws OntoDriverException {
42: ensureOpen();
43: validateQueryParams(sparql);
44: closeCurrentResultSet();
45: this.resultSet = determineResult(sparql);
46: return resultSet;
47: }
48:
49: private ResultSet determineResult(String sparql) throws SesameDriverException {
50: if (isAskQuery(sparql)) {
51: return new AskResultSet(queryExecutor.executeBooleanQuery(sparql), this);
52: } else {
53: final TupleQueryResult tqr = queryExecutor.executeSelectQuery(sparql);
54: try {
55: return new SelectResultSet(tqr, this);
56: } catch (QueryEvaluationException e) {
57: throw new SesameDriverException(e);
58: }
59: }
60: }
61:
62: private static boolean isAskQuery(String query) {
63: return query.startsWith("ASK");
64: }
65:
66: @Override
67: public void executeUpdate(String sparql) throws OntoDriverException {
68: ensureOpen();
69: validateQueryParams(sparql);
70: closeCurrentResultSet();
71: queryExecutor.executeUpdate(sparql);
72: }
73:
74: @Override
75: public void useOntology(StatementOntology ontology) {
76: this.targetOntology = ontology;
77: }
78:
79: @Override
80: public StatementOntology getStatementOntology() {
81: return targetOntology;
82: }
83:
84: private static void validateQueryParams(String sparql) {
85: Objects.requireNonNull(sparql);
86:• if (sparql.isEmpty()) {
87: throw new IllegalArgumentException("Query string cannot be empty.");
88: }
89: }
90:
91: @Override
92: public boolean isOpen() {
93: return open;
94: }
95:
96: @Override
97: public void close() throws OntoDriverException {
98: if (!open) {
99: return;
100: }
101: this.open = false;
102: closeCurrentResultSet();
103: }
104:
105: private void closeCurrentResultSet() throws OntoDriverException {
106: if (resultSet != null) {
107: resultSet.close();
108: this.resultSet = null;
109: }
110: }
111:
112: public void setUseTransactionalOntology() {
113: ensureOpen();
114: this.targetOntology = StatementOntology.TRANSACTIONAL;
115: }
116:
117: public boolean useTransactionalOntology() {
118: ensureOpen();
119: return targetOntology == StatementOntology.TRANSACTIONAL;
120: }
121:
122: public void setUseBackupOntology() {
123: ensureOpen();
124: this.targetOntology = StatementOntology.CENTRAL;
125: }
126:
127: public boolean useBackupOntology() {
128: ensureOpen();
129: return targetOntology == StatementOntology.CENTRAL;
130: }
131:
132: void ensureOpen() {
133: if (!open) {
134: throw new IllegalStateException("This statement is closed.");
135: }
136: }
137: }