Skip to content

Method: assembleQuery()

1: /**
2: * Copyright (C) 2022 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: private static final String SPARQL_LIMIT = " LIMIT ";
26: private static final String SPARQL_OFFSET = " OFFSET ";
27:
28: // Original query string
29: private final String query;
30:
31: private final Map<Parameter<?>, QueryParameter<?>> parameterSet;
32: private final Map<Object, QueryParameter<?>> identifiersToParameters;
33: // These parameters are in order matching the query parts and can appear multiple times in the list
34: private final List<QueryParameter<?>> parameters;
35: private final List<String> queryParts;
36:
37: private int offset = 0;
38:
39: private int limit = Integer.MAX_VALUE;
40:
41: public SparqlQueryHolder(String query, List<String> parts, List<QueryParameter<?>> parameters) {
42: this.query = query;
43: this.parameters = parameters;
44: this.queryParts = parts;
45: this.parameterSet = new HashMap<>();
46: parameters.forEach(p -> parameterSet.put(p, p));
47: this.identifiersToParameters = new HashMap<>(parameterSet.size());
48: parameterSet.values().forEach(p -> identifiersToParameters.put(p.getIdentifier(), p));
49: }
50:
51: @Override
52: public String getQuery() {
53: return query;
54: }
55:
56: @Override
57: public Set<Parameter<?>> getParameters() {
58: return Collections.unmodifiableSet(parameterSet.keySet());
59: }
60:
61: @Override
62: public boolean hasParameter(String name) {
63: return identifiersToParameters.containsKey(name);
64: }
65:
66: @Override
67: public boolean hasParameter(int position) {
68: return identifiersToParameters.containsKey(position);
69: }
70:
71: @Override
72: public QueryParameter<?> getParameter(String name) {
73: if (!hasParameter(name)) {
74: throw unknownParameter(name);
75: }
76: return identifiersToParameters.get(name);
77: }
78:
79: private static IllegalArgumentException unknownParameter(Object p) {
80: return new IllegalArgumentException("Parameter '" + p + "' does not exist in this query.");
81: }
82:
83: @Override
84: public Parameter<?> getParameter(int position) {
85: if (!hasParameter(position)) {
86: throw unknownParameter(position);
87: }
88: return identifiersToParameters.get(position);
89: }
90:
91: @Override
92: public Object getParameterValue(Parameter<?> parameter) {
93: assert getInternalParameter(parameter).getValue() != null;
94: return getInternalParameter(parameter).getValue().getValue();
95: }
96:
97: private QueryParameter<?> getInternalParameter(Parameter<?> p) {
98: Objects.requireNonNull(p);
99: if (!parameterSet.containsKey(p)) {
100: throw unknownParameter(p);
101: }
102: return parameterSet.get(p);
103: }
104:
105: @Override
106: public <T> void setParameter(Parameter<T> parameter, Object value) {
107: Objects.requireNonNull(value);
108: getInternalParameter(parameter).setValue(value);
109: }
110:
111: @Override
112: public <T> void setParameter(Parameter<T> parameter, String value, String language) {
113: Objects.requireNonNull(value);
114: getInternalParameter(parameter).setValue(value, language);
115: }
116:
117: @Override
118: public <T> void setUntypedParameter(Parameter<T> parameter, Object value) {
119: Objects.requireNonNull(value);
120: getInternalParameter(parameter).setUntypedValue(value);
121: }
122:
123: @Override
124: public void setFirstResult(int startPosition) {
125: this.offset = startPosition;
126: }
127:
128: @Override
129: public int getFirstResult() {
130: return offset;
131: }
132:
133: @Override
134: public void setMaxResults(int maxResults) {
135: this.limit = maxResults;
136: }
137:
138: @Override
139: public int getMaxResults() {
140: return limit;
141: }
142:
143: @Override
144: public void clearParameter(Parameter<?> parameter) {
145: getInternalParameter(parameter).resetValue();
146: }
147:
148: @Override
149: public void clearParameters() {
150: parameterSet.values().forEach(QueryParameter::resetValue);
151: }
152:
153: @Override
154: public String assembleQuery() {
155: final StringBuilder sb = new StringBuilder();
156:• for (int i = 0; i < parameters.size(); i++) {
157: sb.append(queryParts.get(i));
158: final String paramValue = parameters.get(i).getValue().getQueryString();
159: sb.append(paramValue);
160: }
161:• if (queryParts.size() > parameters.size()) {
162: sb.append(queryParts.get(parameters.size()));
163: }
164:• if (limit != Integer.MAX_VALUE) {
165: sb.append(SPARQL_LIMIT).append(limit);
166: }
167:• if (offset != 0) {
168: sb.append(SPARQL_OFFSET).append(offset);
169: }
170: return sb.toString();
171: }
172:
173: @Override
174: public String toString() {
175: return assembleQuery();
176: }
177: }