Package: SparqlQueryHolder
SparqlQueryHolder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SparqlQueryHolder(String, List, List) |
|
|
|
|
|
||||||||||||||||||||
assembleQuery() |
|
|
|
|
|
||||||||||||||||||||
clearParameter(Parameter) |
|
|
|
|
|
||||||||||||||||||||
clearParameters() |
|
|
|
|
|
||||||||||||||||||||
getInternalParameter(Parameter) |
|
|
|
|
|
||||||||||||||||||||
getParameter(String) |
|
|
|
|
|
||||||||||||||||||||
getParameter(int) |
|
|
|
|
|
||||||||||||||||||||
getParameterValue(Parameter) |
|
|
|
|
|
||||||||||||||||||||
getParameters() |
|
|
|
|
|
||||||||||||||||||||
getQuery() |
|
|
|
|
|
||||||||||||||||||||
lambda$new$0(QueryParameter) |
|
|
|
|
|
||||||||||||||||||||
lambda$new$1(QueryParameter) |
|
|
|
|
|
||||||||||||||||||||
setParameter(Parameter, Object) |
|
|
|
|
|
||||||||||||||||||||
setParameter(Parameter, String, String) |
|
|
|
|
|
||||||||||||||||||||
setUntypedParameter(Parameter, Object) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
||||||||||||||||||||
unknownParameter(Object) |
|
|
|
|
|
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.jopa.query.sparql;
16:
17: import cz.cvut.kbss.jopa.model.query.Parameter;
18: import cz.cvut.kbss.jopa.query.QueryHolder;
19: import cz.cvut.kbss.jopa.query.QueryParameter;
20:
21: import java.util.*;
22:
23: public class SparqlQueryHolder implements QueryHolder {
24:
25: // Original query string
26: private final String query;
27:
28: private final Map<Parameter<?>, QueryParameter<?>> parameterSet;
29: private final Map<Object, QueryParameter<?>> identifiersToParameters;
30: // These parameters are in order matching the query parts and can appear multiple times in the list
31: private final List<QueryParameter<?>> parameters;
32: private final List<String> queryParts;
33:
34: public SparqlQueryHolder(String query, List<String> parts, List<QueryParameter<?>> parameters) {
35: this.query = query;
36: this.parameters = parameters;
37: this.queryParts = parts;
38: this.parameterSet = new HashMap<>();
39: parameters.forEach(p -> parameterSet.put(p, p));
40: this.identifiersToParameters = new HashMap<>(parameterSet.size());
41: parameterSet.values().forEach(p -> identifiersToParameters.put(p.getIdentifier(), p));
42: }
43:
44: @Override
45: public String getQuery() {
46: return query;
47: }
48:
49: @Override
50: public Set<Parameter<?>> getParameters() {
51: return Collections.unmodifiableSet(parameterSet.keySet());
52: }
53:
54: @Override
55: public QueryParameter<?> getParameter(String name) {
56:• if (!identifiersToParameters.containsKey(name)) {
57: throw unknownParameter(name);
58: }
59: return identifiersToParameters.get(name);
60: }
61:
62: private static IllegalArgumentException unknownParameter(Object p) {
63: return new IllegalArgumentException("Parameter '" + p + "' does not exist in this query.");
64: }
65:
66: @Override
67: public Parameter<?> getParameter(int position) {
68:• if (!identifiersToParameters.containsKey(position)) {
69: throw unknownParameter(position);
70: }
71: return identifiersToParameters.get(position);
72: }
73:
74: @Override
75: public Object getParameterValue(Parameter<?> parameter) {
76:• assert getInternalParameter(parameter).getValue() != null;
77: return getInternalParameter(parameter).getValue().getValue();
78: }
79:
80: private QueryParameter<?> getInternalParameter(Parameter<?> p) {
81: Objects.requireNonNull(p);
82:• if (!parameterSet.containsKey(p)) {
83: throw unknownParameter(p);
84: }
85: return parameterSet.get(p);
86: }
87:
88: @Override
89: public <T> void setParameter(Parameter<T> parameter, Object value) {
90: Objects.requireNonNull(value);
91: getInternalParameter(parameter).setValue(value);
92: }
93:
94: @Override
95: public <T> void setParameter(Parameter<T> parameter, String value, String language) {
96: Objects.requireNonNull(value);
97: getInternalParameter(parameter).setValue(value, language);
98: }
99:
100: @Override
101: public <T> void setUntypedParameter(Parameter<T> parameter, Object value) {
102: Objects.requireNonNull(value);
103: getInternalParameter(parameter).setUntypedValue(value);
104: }
105:
106: @Override
107: public void clearParameter(Parameter<?> parameter) {
108: getInternalParameter(parameter).resetValue();
109: }
110:
111: @Override
112: public void clearParameters() {
113: parameterSet.values().forEach(QueryParameter::resetValue);
114: }
115:
116: @Override
117: public String assembleQuery() {
118: final StringBuilder sb = new StringBuilder();
119:• for (int i = 0; i < parameters.size(); i++) {
120: sb.append(queryParts.get(i));
121: final String paramValue = parameters.get(i).getValue().getQueryString();
122: sb.append(paramValue);
123: }
124:• if (queryParts.size() > parameters.size()) {
125: sb.append(queryParts.get(parameters.size()));
126: }
127: return sb.toString();
128: }
129:
130: @Override
131: public String toString() {
132: return assembleQuery();
133: }
134: }