Package: VariableResultMapper
VariableResultMapper
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
VariableResultMapper(VariableResult) |
|
|
|
|
|
||||||||||||||||||||
getName() |
|
|
|
|
|
||||||||||||||||||||
getTargetType() |
|
|
|
|
|
||||||||||||||||||||
map(ResultRow, UnitOfWorkImpl) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.query.mapper;
14:
15: import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
16: import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
17: import cz.cvut.kbss.jopa.model.annotations.VariableResult;
18: import cz.cvut.kbss.jopa.model.metamodel.Converters;
19: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
20: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
21: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
22: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
23:
24: /**
25: * Maps SPARQL query result to target value based on a {@link VariableResult} configuration.
26: */
27: class VariableResultMapper implements SparqlResultMapper {
28:
29: private final String name;
30: private final Class<?> targetType;
31:
32: VariableResultMapper(VariableResult mapping) {
33: this.name = mapping.name();
34: this.targetType = mapping.type();
35: }
36:
37: String getName() {
38: return name;
39: }
40:
41: Class<?> getTargetType() {
42: return targetType;
43: }
44:
45: /**
46: * Maps value from the current line of the specified result set according to the {@link VariableResult}
47: * configuration represented by this instance.
48: *
49: * @param resultRow Query result set to read
50: * @param uow UnitOfWork instance
51: * @return The mapped value
52: */
53: @Override
54: public Object map(ResultRow resultRow, UnitOfWorkImpl uow) {
55: try {
56:• if (!resultRow.isBound(name)) {
57: return null;
58: }
59: final Object value = resultRow.getObject(name);
60:• if (!void.class.equals(targetType)) {
61:• if (Converters.getDefaultConverters().containsKey(targetType)) {
62: return ((ConverterWrapper) Converters.getDefaultConverters()
63: .get(targetType)).convertToAttribute(value);
64: }
65: return DatatypeTransformer.transform(value, targetType);
66: }
67: return value;
68: } catch (OntoDriverException e) {
69: throw new SparqlResultMappingException(e);
70: }
71: }
72: }