Skip to contentMethod: executeUpdate(String)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena.query;
19:
20: import cz.cvut.kbss.ontodriver.ResultSet;
21: import cz.cvut.kbss.ontodriver.Statement;
22: import cz.cvut.kbss.ontodriver.jena.connector.StatementExecutor;
23: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
24: import org.apache.jena.query.Query;
25: import org.apache.jena.query.QueryFactory;
26: import org.apache.jena.query.QueryParseException;
27:
28: import java.util.Objects;
29:
30: public class JenaStatement implements Statement {
31:
32: private final StatementExecutor executor;
33:
34: private StatementOntology targetOntology = StatementOntology.TRANSACTIONAL;
35: private boolean open = true;
36:
37: private AbstractResultSet currentResultSet;
38:
39: public JenaStatement(StatementExecutor executor) {
40: this.executor = executor;
41: }
42:
43: @Override
44: public ResultSet executeQuery(String sparql) throws JenaDriverException {
45: ensureOpen();
46: final Query query = parseQuery(Objects.requireNonNull(sparql));
47: closeCurrentResultSet();
48: final AbstractResultSet resultSet;
49: if (query.isAskType()) {
50: resultSet = executor.executeAskQuery(query, targetOntology);
51: } else {
52: resultSet = executor.executeSelectQuery(query, targetOntology);
53: }
54: resultSet.setStatement(this);
55: this.currentResultSet = resultSet;
56: return resultSet;
57: }
58:
59: void ensureOpen() {
60: if (!open) {
61: throw new IllegalStateException("Statement is closed.");
62: }
63: }
64:
65: private static Query parseQuery(String sparql) throws JenaDriverException {
66: try {
67: return QueryFactory.create(sparql);
68: } catch (QueryParseException e) {
69: throw new JenaDriverException("Unable to parse query " + sparql, e);
70: }
71: }
72:
73: private void closeCurrentResultSet() throws JenaDriverException {
74: if (currentResultSet != null) {
75: currentResultSet.close();
76: this.currentResultSet = null;
77: }
78: }
79:
80: @Override
81: public void executeUpdate(String sparql) throws JenaDriverException {
82: ensureOpen();
83: Objects.requireNonNull(sparql);
84: closeCurrentResultSet();
85: executor.executeUpdate(sparql, targetOntology);
86: }
87:
88: @Override
89: public void useOntology(StatementOntology ontology) {
90: ensureOpen();
91: this.targetOntology = Objects.requireNonNull(ontology);
92: }
93:
94: @Override
95: public StatementOntology getStatementOntology() {
96: return targetOntology;
97: }
98:
99: @Override
100: public void close() throws JenaDriverException {
101: this.open = false;
102: closeCurrentResultSet();
103: }
104:
105: @Override
106: public void disableInference() {
107: // TODO
108: }
109:
110: @Override
111: public boolean isInferenceDisabled() {
112: // TODO
113: return false;
114: }
115:
116: @Override
117: public boolean isOpen() {
118: return open;
119: }
120: }