Package: SesameStatement
SesameStatement
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SesameStatement(StatementExecutor) |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
closeCurrentResultSet() |
|
|
|
|
|
||||||||||||||||||||
determineResult(String) |
|
|
|
|
|
||||||||||||||||||||
ensureOpen() |
|
|
|
|
|
||||||||||||||||||||
executeQuery(String) |
|
|
|
|
|
||||||||||||||||||||
executeUpdate(String) |
|
|
|
|
|
||||||||||||||||||||
getStatementOntology() |
|
|
|
|
|
||||||||||||||||||||
isAskQuery(String) |
|
|
|
|
|
||||||||||||||||||||
isOpen() |
|
|
|
|
|
||||||||||||||||||||
setUseBackupOntology() |
|
|
|
|
|
||||||||||||||||||||
setUseTransactionalOntology() |
|
|
|
|
|
||||||||||||||||||||
useBackupOntology() |
|
|
|
|
|
||||||||||||||||||||
useOntology(Statement.StatementOntology) |
|
|
|
|
|
||||||||||||||||||||
useTransactionalOntology() |
|
|
|
|
|
||||||||||||||||||||
validateQueryParams(String) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame.query;
14:
15: import cz.cvut.kbss.ontodriver.ResultSet;
16: import cz.cvut.kbss.ontodriver.Statement;
17: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
18: import cz.cvut.kbss.ontodriver.sesame.connector.StatementExecutor;
19: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
20: import org.eclipse.rdf4j.query.QueryEvaluationException;
21: import org.eclipse.rdf4j.query.TupleQueryResult;
22:
23: import java.util.Objects;
24:
25: public class SesameStatement implements Statement {
26:
27: private StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
28: private final StatementExecutor queryExecutor;
29: private ResultSet resultSet;
30:
31: private boolean open;
32:
33: public SesameStatement(StatementExecutor queryExecutor) {
34: this.queryExecutor = queryExecutor;
35: this.open = true;
36: }
37:
38: @Override
39: public ResultSet executeQuery(String sparql) throws OntoDriverException {
40: ensureOpen();
41: validateQueryParams(sparql);
42: closeCurrentResultSet();
43: this.resultSet = determineResult(sparql);
44: return resultSet;
45: }
46:
47: private ResultSet determineResult(String sparql) throws SesameDriverException {
48:• if (isAskQuery(sparql)) {
49: return new AskResultSet(queryExecutor.executeBooleanQuery(sparql), this);
50: } else {
51: final TupleQueryResult tqr = queryExecutor.executeSelectQuery(sparql);
52: try {
53: return new SelectResultSet(tqr, this);
54: } catch (QueryEvaluationException e) {
55: throw new SesameDriverException(e);
56: }
57: }
58: }
59:
60: private static boolean isAskQuery(String query) {
61: return query.startsWith("ASK");
62: }
63:
64: @Override
65: public void executeUpdate(String sparql) throws OntoDriverException {
66: ensureOpen();
67: validateQueryParams(sparql);
68: closeCurrentResultSet();
69: queryExecutor.executeUpdate(sparql);
70: }
71:
72: @Override
73: public void useOntology(StatementOntology ontology) {
74: this.targetOntology = ontology;
75: }
76:
77: @Override
78: public StatementOntology getStatementOntology() {
79: return targetOntology;
80: }
81:
82: private static void validateQueryParams(String sparql) {
83: Objects.requireNonNull(sparql);
84:• if (sparql.isEmpty()) {
85: throw new IllegalArgumentException("Query string cannot be empty.");
86: }
87: }
88:
89: @Override
90: public boolean isOpen() {
91: return open;
92: }
93:
94: @Override
95: public void close() throws OntoDriverException {
96:• if (!open) {
97: return;
98: }
99: this.open = false;
100: closeCurrentResultSet();
101: }
102:
103: private void closeCurrentResultSet() throws OntoDriverException {
104:• if (resultSet != null) {
105: resultSet.close();
106: this.resultSet = null;
107: }
108: }
109:
110: public void setUseTransactionalOntology() {
111: ensureOpen();
112: this.targetOntology = StatementOntology.TRANSACTIONAL;
113: }
114:
115: public boolean useTransactionalOntology() {
116: ensureOpen();
117:• return targetOntology == StatementOntology.TRANSACTIONAL;
118: }
119:
120: public void setUseBackupOntology() {
121: ensureOpen();
122: this.targetOntology = StatementOntology.CENTRAL;
123: }
124:
125: public boolean useBackupOntology() {
126: ensureOpen();
127:• return targetOntology == StatementOntology.CENTRAL;
128: }
129:
130: void ensureOpen() {
131:• if (!open) {
132: throw new IllegalStateException("This statement is closed.");
133: }
134: }
135: }