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