Skip to contentMethod: extractRow(ResultRow)
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model;
19:
20: import cz.cvut.kbss.jopa.exceptions.NoResultException;
21: import cz.cvut.kbss.jopa.exceptions.NoUniqueResultException;
22: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
23: import cz.cvut.kbss.jopa.model.query.Query;
24: import cz.cvut.kbss.jopa.query.QueryHolder;
25: import cz.cvut.kbss.jopa.sessions.ConnectionWrapper;
26: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
27: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
28:
29: import java.util.ArrayList;
30: import java.util.Collections;
31: import java.util.List;
32: import java.util.Optional;
33: import java.util.stream.Stream;
34:
35: public class QueryImpl extends AbstractQuery implements Query {
36:
37: public QueryImpl(final QueryHolder query, final ConnectionWrapper connection) {
38: super(query, connection);
39: }
40:
41: @Override
42: public List getResultList() {
43: ensureOpen();
44: try {
45: if (getMaxResults() == 0) {
46: return Collections.emptyList();
47: }
48: return getResultListImpl();
49: } catch (OntoDriverException e) {
50: markTransactionForRollback();
51: throw queryEvaluationException(e);
52: } catch (RuntimeException e) {
53: markTransactionForRollback();
54: throw e;
55: }
56: }
57:
58: private List<?> getResultListImpl() throws OntoDriverException {
59: final List<Object> res = new ArrayList<>();
60: executeQuery(rs -> res.add(extractRow(rs)));
61: return res;
62: }
63:
64: @Override
65: public Object getSingleResult() {
66: ensureOpen();
67: try {
68: final List<?> list = getResultListImpl();
69: if (list.isEmpty()) {
70: throw new NoResultException("No result found for query " + query);
71: }
72: if (list.size() > 1) {
73: throw new NoUniqueResultException("Multiple results found for query " + query);
74: }
75: return list.get(0);
76: } catch (OntoDriverException e) {
77: markTransactionForRollback();
78: throw queryEvaluationException(e);
79: } catch (RuntimeException e) {
80: if (exceptionCausesRollback(e)) {
81: markTransactionForRollback();
82: }
83: throw e;
84: }
85: }
86:
87: @Override
88: public Stream getResultStream() {
89: try {
90: return executeQueryForStream(this::extractRowAsOptional);
91: } catch (OntoDriverException e) {
92: markTransactionForRollback();
93: throw queryEvaluationException(e);
94: } catch (RuntimeException e) {
95: if (exceptionCausesRollback(e)) {
96: markTransactionForRollback();
97: }
98: throw e;
99: }
100: }
101:
102: @Override
103: public Query setMaxResults(int maxResults) {
104: ensureOpen();
105: checkNumericParameter(maxResults, "max results");
106: query.setMaxResults(maxResults);
107: return this;
108: }
109:
110: @Override
111: public Query setFirstResult(int startPosition) {
112: ensureOpen();
113: checkNumericParameter(startPosition, "first result offset");
114: query.setFirstResult(startPosition);
115: return this;
116: }
117:
118: Optional<Object> extractRowAsOptional(ResultRow row) {
119: return Optional.of(extractRow(row));
120: }
121:
122: Object extractRow(ResultRow resultRow) {
123: try {
124: final int columnCount = resultRow.getColumnCount();
125:• if (columnCount == 1) {
126: return resultRow.getObject(0);
127: } else {
128: final Object[] row = new Object[columnCount];
129:• for (int i = 0; i < columnCount; i++) {
130:• final Object ob = resultRow.isBound(i) ? resultRow.getObject(i) : null;
131: row[i] = ob;
132: }
133: return row;
134: }
135: } catch (OntoDriverException e) {
136: throw new OWLPersistenceException(e);
137: }
138: }
139: }