Skip to content

Package: SesameStatement

SesameStatement

nameinstructionbranchcomplexitylinemethod
SesameStatement(StatementExecutor)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
close()
M: 1 C: 9
90%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
closeCurrentResultSet()
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
determineResult(String)
M: 6 C: 24
80%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
ensureOpen()
M: 5 C: 4
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
executeQuery(String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
executeUpdate(String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getStatementOntology()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAskQuery(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setUseBackupOntology()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
setUseTransactionalOntology()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
useBackupOntology()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
useOntology(Statement.StatementOntology)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
useTransactionalOntology()
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
validateQueryParams(String)
M: 5 C: 7
58%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%

Coverage

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.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 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 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 void close() throws Exception {
93:• if (!open) {
94: return;
95: }
96: this.open = false;
97: closeCurrentResultSet();
98: }
99:
100: private void closeCurrentResultSet() throws OntoDriverException {
101:• if (resultSet != null) {
102: resultSet.close();
103: this.resultSet = null;
104: }
105: }
106:
107: public void setUseTransactionalOntology() {
108: ensureOpen();
109: this.targetOntology = StatementOntology.TRANSACTIONAL;
110: }
111:
112: public boolean useTransactionalOntology() {
113: ensureOpen();
114:• return targetOntology == StatementOntology.TRANSACTIONAL;
115: }
116:
117: public void setUseBackupOntology() {
118: ensureOpen();
119: this.targetOntology = StatementOntology.CENTRAL;
120: }
121:
122: public boolean useBackupOntology() {
123: ensureOpen();
124:• return targetOntology == StatementOntology.CENTRAL;
125: }
126:
127: void ensureOpen() {
128:• if (!open) {
129: throw new IllegalStateException("This statement is closed.");
130: }
131: }
132: }