Package: JenaStatement
JenaStatement
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JenaStatement(StatementExecutor) |
|
|
|
|
|
||||||||||||||||||||
close() |
|
|
|
|
|
||||||||||||||||||||
closeCurrentResultSet() |
|
|
|
|
|
||||||||||||||||||||
ensureOpen() |
|
|
|
|
|
||||||||||||||||||||
executeQuery(String) |
|
|
|
|
|
||||||||||||||||||||
executeUpdate(String) |
|
|
|
|
|
||||||||||||||||||||
getStatementOntology() |
|
|
|
|
|
||||||||||||||||||||
isOpen() |
|
|
|
|
|
||||||||||||||||||||
parseQuery(String) |
|
|
|
|
|
||||||||||||||||||||
useOntology(Statement.StatementOntology) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2020 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.jena.query;
16:
17: import cz.cvut.kbss.ontodriver.ResultSet;
18: import cz.cvut.kbss.ontodriver.Statement;
19: import cz.cvut.kbss.ontodriver.jena.connector.StatementExecutor;
20: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
21: import org.apache.jena.query.Query;
22: import org.apache.jena.query.QueryFactory;
23: import org.apache.jena.query.QueryParseException;
24:
25: import java.util.Objects;
26:
27: public class JenaStatement implements Statement {
28:
29: private final StatementExecutor executor;
30:
31: private StatementOntology targetOntology = StatementOntology.CENTRAL;
32: private boolean open = true;
33:
34: private AbstractResultSet currentResultSet;
35:
36: public JenaStatement(StatementExecutor executor) {
37: this.executor = executor;
38: }
39:
40: @Override
41: public ResultSet executeQuery(String sparql) throws JenaDriverException {
42: ensureOpen();
43: final Query query = parseQuery(Objects.requireNonNull(sparql));
44: closeCurrentResultSet();
45: final AbstractResultSet resultSet;
46:• if (query.isAskType()) {
47: resultSet = executor.executeAskQuery(query, targetOntology);
48: } else {
49: resultSet = executor.executeSelectQuery(query, targetOntology);
50: }
51: resultSet.setStatement(this);
52: this.currentResultSet = resultSet;
53: return resultSet;
54: }
55:
56: void ensureOpen() {
57:• if (!open) {
58: throw new IllegalStateException("Statement is closed.");
59: }
60: }
61:
62: private Query parseQuery(String sparql) throws JenaDriverException {
63: try {
64: return QueryFactory.create(sparql);
65: } catch (QueryParseException e) {
66: throw new JenaDriverException("Unable to parse query " + sparql, e);
67: }
68: }
69:
70: private void closeCurrentResultSet() throws JenaDriverException {
71:• if (currentResultSet != null) {
72: currentResultSet.close();
73: this.currentResultSet = null;
74: }
75: }
76:
77: @Override
78: public void executeUpdate(String sparql) throws JenaDriverException {
79: ensureOpen();
80: Objects.requireNonNull(sparql);
81: closeCurrentResultSet();
82: executor.executeUpdate(sparql, targetOntology);
83: }
84:
85: @Override
86: public void useOntology(StatementOntology ontology) {
87: ensureOpen();
88: this.targetOntology = Objects.requireNonNull(ontology);
89: }
90:
91: @Override
92: public StatementOntology getStatementOntology() {
93: return targetOntology;
94: }
95:
96: @Override
97: public void close() throws JenaDriverException {
98: this.open = false;
99: closeCurrentResultSet();
100: }
101:
102: @Override
103: public boolean isOpen() {
104: return open;
105: }
106: }