Skip to content

Method: prepareStatement(String)

1: /**
2: * Copyright (C) 2022 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.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.*;
18: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
20: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
23: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
24:
25: import java.net.URI;
26: import java.util.Collection;
27: import java.util.List;
28: import java.util.Objects;
29: import java.util.Set;
30:
31: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
32:
33: /**
34: * Default implementation of the {@link Connection} interface for OWLAPI driver.
35: */
36: public class OwlapiConnection implements Connection {
37:
38: private boolean open;
39: private boolean autoCommit;
40:
41: private final OwlapiAdapter adapter;
42:
43: private OwlapiTypes types;
44: private OwlapiProperties properties;
45: private OwlapiLists lists;
46:
47: private ConnectionListener listener;
48:
49: OwlapiConnection(OwlapiAdapter adapter) {
50: this.adapter = adapter;
51: this.open = true;
52: this.autoCommit = false;
53: }
54:
55: void setListener(ConnectionListener listener) {
56: ensureOpen();
57: assert listener != null;
58: this.listener = listener;
59: }
60:
61: void removeListener() {
62: this.listener = null;
63: }
64:
65: void setTypes(OwlapiTypes types) {
66: this.types = types;
67: }
68:
69: void setProperties(OwlapiProperties properties) {
70: this.properties = properties;
71: }
72:
73: void setLists(OwlapiLists lists) {
74: this.lists = lists;
75: }
76:
77: @Override
78: public boolean isOpen() {
79: return open;
80: }
81:
82: @Override
83: public void commit() {
84: ensureOpen();
85: adapter.commit();
86: }
87:
88: public void ensureOpen() {
89: if (!open) {
90: throw new IllegalStateException("Connection is closed.");
91: }
92: }
93:
94: @Override
95: public void rollback() {
96: ensureOpen();
97: adapter.rollback();
98: }
99:
100: @Override
101: public void setAutoCommit(boolean autoCommit) {
102: this.autoCommit = autoCommit;
103: }
104:
105: @Override
106: public boolean isAutoCommit() {
107: return autoCommit;
108: }
109:
110: @Override
111: public Statement createStatement() {
112: ensureOpen();
113: return adapter.createStatement(this);
114: }
115:
116: @Override
117: public PreparedStatement prepareStatement(String sparql) {
118: ensureOpen();
119: return adapter.prepareStatement(sparql, this);
120: }
121:
122: @Override
123: public boolean isConsistent(URI context) {
124: ensureOpen();
125: return adapter.isConsistent(context);
126: }
127:
128: @Override
129: public List<URI> getContexts() {
130: ensureOpen();
131: return adapter.getContexts();
132: }
133:
134: @Override
135: public boolean contains(Axiom<?> axiom, Set<URI> contexts) {
136: ensureOpen();
137: Objects.requireNonNull(axiom, getNPXMessageSupplier("axiom"));
138: return adapter.containsAxiom(axiom, contexts);
139: }
140:
141: @Override
142: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws OntoDriverException {
143: ensureOpen();
144: Objects.requireNonNull(descriptor);
145: try {
146: return adapter.find(descriptor);
147: } catch (RuntimeException e) {
148: throw new OwlapiDriverException(e);
149: }
150: }
151:
152: @Override
153: public void persist(AxiomValueDescriptor descriptor) throws OntoDriverException {
154: ensureOpen();
155: Objects.requireNonNull(descriptor);
156: try {
157: adapter.persist(descriptor);
158: commitIfAuto();
159: } catch (RuntimeException e) {
160: throw new OwlapiDriverException(e);
161: }
162: }
163:
164: @Override
165: public URI generateIdentifier(URI classUri) {
166: ensureOpen();
167: Objects.requireNonNull(classUri);
168: return adapter.generateIdentifier(classUri);
169: }
170:
171: @Override
172: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
173: ensureOpen();
174: Objects.requireNonNull(descriptor);
175: try {
176: adapter.update(descriptor);
177: commitIfAuto();
178: } catch (RuntimeException e) {
179: throw new OwlapiDriverException(e);
180: }
181: }
182:
183: @Override
184: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
185: ensureOpen();
186: Objects.requireNonNull(descriptor);
187: try {
188: adapter.remove(descriptor);
189: commitIfAuto();
190: } catch (RuntimeException e) {
191: throw new OwlapiDriverException(e);
192: }
193: }
194:
195: @Override
196: public Lists lists() {
197: ensureOpen();
198: assert lists != null;
199: return lists;
200: }
201:
202: @Override
203: public Types types() {
204: ensureOpen();
205: assert types != null;
206: return types;
207: }
208:
209: @Override
210: public Properties properties() {
211: ensureOpen();
212: assert properties != null;
213: return properties;
214: }
215:
216: @Override
217: public void close() {
218: if (!open) {
219: return;
220: }
221: if (listener != null) {
222: listener.connectionClosed(this);
223: }
224: this.open = false;
225: }
226:
227: public void commitIfAuto() {
228: if (autoCommit) {
229: adapter.commit();
230: }
231: }
232:
233: @Override
234: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
235: if (cls.isAssignableFrom(this.getClass())) {
236: return cls.cast(this);
237: }
238: return adapter.unwrap(cls);
239: }
240: }