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