Skip to content

Package: SelectResultSet

SelectResultSet

nameinstructionbranchcomplexitylinemethod
SelectResultSet(TupleQueryResult, Statement)
M: 4 C: 12
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
close()
M: 6 C: 6
50%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 3
60%
M: 0 C: 1
100%
ensureState()
M: 5 C: 4
44%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
findColumn(String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getBoolean(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getBoolean(int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getByte(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getByte(int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getColumnCount()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getCurrent(String)
M: 12 C: 30
71%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 6
86%
M: 0 C: 1
100%
getCurrent(int)
M: 5 C: 36
88%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
getDouble(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getDouble(int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getFloat(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getFloat(int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getInt(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getInt(int)
M: 6 C: 2
25%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 1
50%
M: 0 C: 1
100%
getLiteralValue(String)
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getLiteralValue(int)
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getLong(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getLong(int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getObject(String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getObject(String, Class)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getObject(int)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getObject(int, Class)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getShort(String)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getShort(int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getString(String)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getString(int)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getStringImpl(Value)
M: 5 C: 6
55%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
hasNext()
M: 6 C: 6
50%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 2
50%
M: 0 C: 1
100%
init()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
instantiateUsingConstructor(Class, Value, Object)
M: 110 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
isBound(String)
M: 0 C: 17
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 2
100%
M: 0 C: 1
100%
isBound(int)
M: 0 C: 20
100%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
next()
M: 6 C: 10
63%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toBoolean(Object)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
toDouble(Object)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
toFloat(Object)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
toInt(Object)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
toLong(Object)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
toObject(Value)
M: 4 C: 20
83%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 6
100%
M: 0 C: 1
100%
toObject(Value, Class)
M: 21 C: 31
60%
M: 6 C: 6
50%
M: 6 C: 1
14%
M: 4 C: 8
67%
M: 0 C: 1
100%

Coverage

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