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, URI[])
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, URI[])
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: * <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: private StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
31: private final StatementExecutor queryExecutor;
32: private 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: closeCurrentResultSet();
46: this.resultSet = determineResult(sparql);
47: return resultSet;
48: }
49:
50: private ResultSet determineResult(String sparql) throws SesameDriverException {
51:• if (isAskQuery(sparql)) {
52: return new AskResultSet(queryExecutor.executeBooleanQuery(sparql), this);
53: } else {
54: final TupleQueryResult tqr = queryExecutor.executeSelectQuery(sparql);
55: try {
56: return new SelectResultSet(tqr, this);
57: } catch (QueryEvaluationException e) {
58: throw new SesameDriverException(e);
59: }
60: }
61: }
62:
63: private boolean isAskQuery(String query) {
64: return query.startsWith("ASK");
65: }
66:
67: @Override
68: public void executeUpdate(String sparql, URI... contexts) throws OntoDriverException {
69: ensureOpen();
70: validateQueryParams(sparql);
71: closeCurrentResultSet();
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);
87:• if (sparql.isEmpty()) {
88: throw new IllegalArgumentException("Query string cannot be empty.");
89: }
90: }
91:
92: @Override
93: public void close() throws Exception {
94:• if (!open) {
95: return;
96: }
97: this.open = false;
98: closeCurrentResultSet();
99: }
100:
101: private void closeCurrentResultSet() throws OntoDriverException {
102:• if (resultSet != null) {
103: resultSet.close();
104: this.resultSet = null;
105: }
106: }
107:
108: public void setUseTransactionalOntology() {
109: ensureOpen();
110: this.targetOntology = StatementOntology.TRANSACTIONAL;
111: }
112:
113: public boolean useTransactionalOntology() {
114: ensureOpen();
115:• return targetOntology == StatementOntology.TRANSACTIONAL;
116: }
117:
118: public void setUseBackupOntology() {
119: ensureOpen();
120: this.targetOntology = StatementOntology.CENTRAL;
121: }
122:
123: public boolean useBackupOntology() {
124: ensureOpen();
125:• return targetOntology == StatementOntology.CENTRAL;
126: }
127:
128: void ensureOpen() {
129:• if (!open) {
130: throw new IllegalStateException("This statement is closed.");
131: }
132: }
133: }