Skip to content

Method: createStatement()

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