-
Notifications
You must be signed in to change notification settings - Fork 14
/
SelectResultSet.java
389 lines (343 loc) · 12.5 KB
/
SelectResultSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame.query;
import cz.cvut.kbss.ontodriver.Statement;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.URI;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.TupleQueryResult;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class SelectResultSet extends AbstractResultSet {
private final TupleQueryResult result;
private List<String> bindings;
private BindingSet current;
public SelectResultSet(TupleQueryResult result, Statement statement) throws QueryEvaluationException {
super(statement);
assert result != null;
this.result = result;
init();
}
private void init() throws QueryEvaluationException {
this.bindings = result.getBindingNames();
}
@Override
public void close() throws OntoDriverException {
try {
result.close();
} catch (QueryEvaluationException e) {
throw new OntoDriverException(e);
} finally {
super.close();
}
}
@Override
public int findColumn(String columnLabel) {
ensureOpen();
return bindings.indexOf(columnLabel);
}
@Override
public int getColumnCount() {
ensureOpen();
return bindings.size();
}
@Override
public boolean getBoolean(int columnIndex) throws OntoDriverException {
ensureOpen();
return toBoolean(getLiteralValue(columnIndex));
}
@Override
public boolean getBoolean(String columnLabel) throws OntoDriverException {
ensureOpen();
return toBoolean(getLiteralValue(columnLabel));
}
private boolean toBoolean(Object ob) {
if (ob instanceof Boolean) {
return (boolean) ob;
} else {
return Boolean.parseBoolean(ob.toString());
}
}
@Override
public byte getByte(int columnIndex) throws OntoDriverException {
ensureOpen();
return (byte) toInt(getLiteralValue(columnIndex));
}
@Override
public byte getByte(String columnLabel) throws OntoDriverException {
ensureOpen();
return (byte) toInt(getLiteralValue(columnLabel));
}
@Override
public double getDouble(int columnIndex) throws OntoDriverException {
ensureOpen();
return toDouble(getLiteralValue(columnIndex));
}
@Override
public double getDouble(String columnLabel) throws OntoDriverException {
ensureOpen();
return toDouble(getLiteralValue(columnLabel));
}
private double toDouble(Object ob) throws OntoDriverException {
if (ob instanceof Number) {
return ((Number) ob).doubleValue();
} else {
try {
return Double.parseDouble(ob.toString());
} catch (NumberFormatException e) {
throw new OntoDriverException(e);
}
}
}
@Override
public float getFloat(int columnIndex) throws OntoDriverException {
ensureOpen();
return toFloat(getLiteralValue(columnIndex));
}
@Override
public float getFloat(String columnLabel) throws OntoDriverException {
ensureOpen();
return toFloat(getLiteralValue(columnLabel));
}
private float toFloat(Object ob) throws OntoDriverException {
if (ob instanceof Number) {
return ((Number) ob).floatValue();
} else {
try {
return Float.parseFloat(ob.toString());
} catch (NumberFormatException e) {
throw new OntoDriverException(e);
}
}
}
@Override
public int getInt(int columnIndex) throws OntoDriverException {
ensureOpen();
return toInt(getLiteralValue(columnIndex));
}
@Override
public int getInt(String columnLabel) throws OntoDriverException {
ensureOpen();
return toInt(getLiteralValue(columnLabel));
}
private int toInt(Object ob) throws OntoDriverException {
if (ob instanceof Number) {
return ((Number) ob).intValue();
} else {
try {
return Integer.parseInt(ob.toString());
} catch (NumberFormatException e) {
throw new OntoDriverException(e);
}
}
}
@Override
public long getLong(int columnIndex) throws OntoDriverException {
ensureOpen();
return toLong(getLiteralValue(columnIndex));
}
@Override
public long getLong(String columnLabel) throws OntoDriverException {
ensureOpen();
return toLong(getLiteralValue(columnLabel));
}
private long toLong(Object ob) throws OntoDriverException {
if (ob instanceof Number) {
return ((Number) ob).longValue();
} else {
try {
return Long.parseLong(ob.toString());
} catch (NumberFormatException e) {
throw new OntoDriverException(e);
}
}
}
@Override
public Object getObject(int columnIndex) throws OntoDriverException {
ensureOpen();
return toObject(getCurrent(columnIndex));
}
@Override
public Object getObject(String columnLabel) throws OntoDriverException {
ensureOpen();
return toObject(getCurrent(columnLabel));
}
private Object toObject(Value val) {
if (val == null) {
return null;
}
if (val instanceof Literal) {
return SesameUtils.getDataPropertyValue((Literal) val);
} else if (val instanceof IRI) {
return SesameUtils.toJavaUri((IRI) val);
} else {
return val.toString();
}
}
@Override
public <T> T getObject(int columnIndex, Class<T> cls) throws OntoDriverException {
ensureOpen();
return toObject(getCurrent(columnIndex), cls);
}
@Override
public <T> T getObject(String columnLabel, Class<T> cls) throws OntoDriverException {
ensureOpen();
return toObject(getCurrent(columnLabel), cls);
}
private <T> T toObject(Value val, Class<T> cls) throws OntoDriverException {
if (val == null) {
return null;
}
if (cls.isAssignableFrom(val.getClass())) {
return cls.cast(val);
}
Object ob = null;
if (val instanceof Literal) {
ob = SesameUtils.getDataPropertyValue((Literal) val);
} else if (val instanceof IRI) {
ob = SesameUtils.toJavaUri((IRI) val);
}
if (ob != null && cls.isAssignableFrom(ob.getClass())) {
return cls.cast(ob);
}
ob = val;
return instantiateUsingConstructor(cls, val, ob);
}
/**
* Searches for a suitable constructor and creates a new instance of class {@code cls}. </p>
* <p>
* The type has to have single-argument constructor, which takes either {@code Value} or its subtypes or type of
* instance returned by {@link SesameUtils#getDataPropertyValue(Literal)}.
*
* @param cls The return type
* @param val Raw value
* @param ob Either raw value (if it is a resource) or instance returned by {@link
* SesameUtils#getDataPropertyValue(Literal)} on passing the literal {@code val}
* @return The new instance
* @throws OntoDriverException If no suitable constructor is found or the instance cannot be created
*/
private <T> T instantiateUsingConstructor(Class<T> cls, Value val, Object ob)
throws OntoDriverException {
Constructor<?>[] ctors = cls.getDeclaredConstructors();
try {
for (Constructor<?> c : ctors) {
if (c.getParameterTypes().length != 1) {
continue;
}
c.setAccessible(true);
final Class<?> type = c.getParameterTypes()[0];
if (type.isAssignableFrom(ob.getClass())) {
return (T) c.newInstance(ob);
}
if (type.isAssignableFrom(val.getClass())) {
return (T) c.newInstance(val);
}
if (type.isAssignableFrom(String.class)) {
return (T) c.newInstance(ob.toString());
}
}
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new SesameDriverException("Unable to create instance of type " + cls + " with value " + val, e);
}
throw new SesameDriverException("No suitable constructor for value " + val + " found in type " + cls);
}
@Override
public short getShort(int columnIndex) throws OntoDriverException {
ensureOpen();
return (short) toInt(getLiteralValue(columnIndex));
}
@Override
public short getShort(String columnLabel) throws OntoDriverException {
ensureOpen();
return (short) toInt(getLiteralValue(columnLabel));
}
@Override
public String getString(int columnIndex) throws OntoDriverException {
ensureOpen();
return getStringImpl(getCurrent(columnIndex));
}
@Override
public String getString(String columnLabel) throws OntoDriverException {
ensureOpen();
return getStringImpl(getCurrent(columnLabel));
}
private String getStringImpl(Value val) {
if (val instanceof Literal) {
return SesameUtils.getDataPropertyValue((Literal) val).toString();
} else {
return val.toString();
}
}
@Override
public boolean hasNext() throws OntoDriverException {
ensureOpen();
try {
return result.hasNext();
} catch (QueryEvaluationException e) {
throw new OntoDriverException(e);
}
}
@Override
public void next() throws OntoDriverException {
super.next();
try {
this.current = result.next();
} catch (QueryEvaluationException e) {
throw new OntoDriverException(e);
}
}
private Object getLiteralValue(int columnIndex) throws OntoDriverException {
final Value val = getCurrent(columnIndex);
if (!(val instanceof Literal)) {
throw new OntoDriverException("Expected value " + val + " to be a literal.");
}
return SesameUtils.getDataPropertyValue((Literal) val);
}
private Object getLiteralValue(String columnName) throws OntoDriverException {
final Value val = getCurrent(columnName);
if (!(val instanceof Literal)) {
throw new OntoDriverException("Expected value " + val + " to be a literal.");
}
return SesameUtils.getDataPropertyValue((Literal) val);
}
private Value getCurrent(int columnIndex) {
ensureState();
if (columnIndex < 0 || columnIndex >= bindings.size()) {
throw new IllegalArgumentException(
"The column index is out of bounds of the column count.");
}
return current.getValue(bindings.get(columnIndex));
}
private void ensureState() {
if (current == null) {
throw new IllegalStateException("Must call next before getting the first value.");
}
}
private Value getCurrent(String columnName) {
ensureState();
if (!bindings.contains(columnName)) {
throw new IllegalArgumentException("Unknown column name " + columnName);
}
return current.getValue(columnName);
}
}