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: 12
50%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
getCurrent(int)
M: 5 C: 18
78%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 3
75%
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: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getLiteralValue(String)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getLiteralValue(int)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 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: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
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%
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: 0 C: 21
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
toObject(Value, Class)
M: 17 C: 32
65%
M: 5 C: 7
58%
M: 5 C: 2
29%
M: 4 C: 9
69%
M: 0 C: 1
100%

Coverage

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