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: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
determineResult(String)
M: 6 C: 29
83%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 2 C: 6
75%
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, URI[])
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
executeUpdate(String, URI[])
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
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: * <p>
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: * <p>
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.net.URI;
26: import java.util.Objects;
27:
28: public class SesameStatement implements Statement {
29:
30: protected StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
31: protected final StatementExecutor queryExecutor;
32: protected ResultSet resultSet;
33:
34: private boolean open;
35:
36: public SesameStatement(StatementExecutor queryExecutor) {
37: this.queryExecutor = queryExecutor;
38: this.open = true;
39: }
40:
41: @Override
42: public ResultSet executeQuery(String sparql, URI... contexts) throws OntoDriverException {
43: ensureOpen();
44: validateQueryParams(sparql);
45: determineResult(sparql);
46: return resultSet;
47: }
48:
49: private void determineResult(String sparql) throws SesameDriverException {
50:• if (isAskQuery(sparql)) {
51: this.resultSet = new AskResultSet(queryExecutor.executeBooleanQuery(sparql), this);
52: } else {
53: final TupleQueryResult tqr = queryExecutor.executeSelectQuery(sparql);
54: try {
55: this.resultSet = 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, URI... contexts) throws OntoDriverException {
68: ensureOpen();
69: validateQueryParams(sparql);
70: queryExecutor.executeUpdate(sparql);
71: }
72:
73: @Override
74: public void useOntology(StatementOntology ontology) {
75: this.targetOntology = ontology;
76: }
77:
78: @Override
79: public StatementOntology getStatementOntology() {
80: return targetOntology;
81: }
82:
83: private void validateQueryParams(String sparql) {
84: Objects.requireNonNull(sparql);
85:• if (sparql.isEmpty()) {
86: throw new IllegalArgumentException("Query string cannot be empty.");
87: }
88: }
89:
90: public void close() throws Exception {
91:• if (!open) {
92: return;
93: }
94: this.open = false;
95:• if (resultSet != null) {
96: resultSet.close();
97: this.resultSet = null;
98: }
99: }
100:
101: public void setUseTransactionalOntology() {
102: ensureOpen();
103: this.targetOntology = StatementOntology.TRANSACTIONAL;
104: }
105:
106: public boolean useTransactionalOntology() {
107: ensureOpen();
108:• return targetOntology == StatementOntology.TRANSACTIONAL;
109: }
110:
111: public void setUseBackupOntology() {
112: ensureOpen();
113: this.targetOntology = StatementOntology.CENTRAL;
114: }
115:
116: public boolean useBackupOntology() {
117: ensureOpen();
118:• return targetOntology == StatementOntology.CENTRAL;
119: }
120:
121: protected void ensureOpen() {
122:• if (!open) {
123: throw new IllegalStateException("This statement is closed.");
124: }
125: }
126: }