Skip to content

Package: QueryImpl

QueryImpl

nameinstructionbranchcomplexitylinemethod
QueryImpl(QueryHolder, ConnectionWrapper)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
extractRow(ResultRow)
M: 0 C: 42
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
extractRowAsOptional(ResultRow)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getResultList()
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getResultListImpl()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getResultStream()
M: 16 C: 5
24%
M: 2 C: 0
0%
M: 1 C: 1
50%
M: 7 C: 1
13%
M: 0 C: 1
100%
getSingleResult()
M: 0 C: 58
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
lambda$getResultListImpl$0(List, ResultRow)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setFirstResult(int)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setMaxResults(int)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.jopa.model;
16:
17: import cz.cvut.kbss.jopa.exceptions.NoResultException;
18: import cz.cvut.kbss.jopa.exceptions.NoUniqueResultException;
19: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
20: import cz.cvut.kbss.jopa.model.query.Query;
21: import cz.cvut.kbss.jopa.query.QueryHolder;
22: import cz.cvut.kbss.jopa.sessions.ConnectionWrapper;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
25:
26: import java.util.ArrayList;
27: import java.util.Collections;
28: import java.util.List;
29: import java.util.Optional;
30: import java.util.stream.Stream;
31:
32: public class QueryImpl extends AbstractQuery implements Query {
33:
34: public QueryImpl(final QueryHolder query, final ConnectionWrapper connection) {
35: super(query, connection);
36: }
37:
38: @Override
39: public List getResultList() {
40: ensureOpen();
41: try {
42:• if (getMaxResults() == 0) {
43: return Collections.emptyList();
44: }
45: return getResultListImpl();
46: } catch (OntoDriverException e) {
47: markTransactionForRollback();
48: throw queryEvaluationException(e);
49: } catch (RuntimeException e) {
50: markTransactionForRollback();
51: throw e;
52: }
53: }
54:
55: private List<?> getResultListImpl() throws OntoDriverException {
56: final List<Object> res = new ArrayList<>();
57: executeQuery(rs -> res.add(extractRow(rs)));
58: return res;
59: }
60:
61: @Override
62: public Object getSingleResult() {
63: ensureOpen();
64: try {
65: final List<?> list = getResultListImpl();
66:• if (list.isEmpty()) {
67: throw new NoResultException("No result found for query " + query);
68: }
69:• if (list.size() > 1) {
70: throw new NoUniqueResultException("Multiple results found for query " + query);
71: }
72: return list.get(0);
73: } catch (OntoDriverException e) {
74: markTransactionForRollback();
75: throw queryEvaluationException(e);
76: } catch (RuntimeException e) {
77:• if (exceptionCausesRollback(e)) {
78: markTransactionForRollback();
79: }
80: throw e;
81: }
82: }
83:
84: @Override
85: public Stream getResultStream() {
86: try {
87: return executeQueryForStream(this::extractRowAsOptional);
88: } catch (OntoDriverException e) {
89: markTransactionForRollback();
90: throw queryEvaluationException(e);
91: } catch (RuntimeException e) {
92:• if (exceptionCausesRollback(e)) {
93: markTransactionForRollback();
94: }
95: throw e;
96: }
97: }
98:
99: @Override
100: public Query setMaxResults(int maxResults) {
101: ensureOpen();
102: checkNumericParameter(maxResults, "max results");
103: query.setMaxResults(maxResults);
104: return this;
105: }
106:
107: @Override
108: public Query setFirstResult(int startPosition) {
109: ensureOpen();
110: checkNumericParameter(startPosition, "first result offset");
111: query.setFirstResult(startPosition);
112: return this;
113: }
114:
115: Optional<Object> extractRowAsOptional(ResultRow row) {
116: return Optional.of(extractRow(row));
117: }
118:
119: Object extractRow(ResultRow resultRow) {
120: try {
121: final int columnCount = resultRow.getColumnCount();
122:• if (columnCount == 1) {
123: return resultRow.getObject(0);
124: } else {
125: final Object[] row = new Object[columnCount];
126:• for (int i = 0; i < columnCount; i++) {
127:• final Object ob = resultRow.isBound(i) ? resultRow.getObject(i) : null;
128: row[i] = ob;
129: }
130: return row;
131: }
132: } catch (OntoDriverException e) {
133: throw new OWLPersistenceException(e);
134: }
135: }
136: }