Skip to contentMethod: registerParameter(ExpressionLiteralImpl)
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.query.criteria;
19:
20: import cz.cvut.kbss.jopa.model.TypedQueryImpl;
21: import cz.cvut.kbss.jopa.model.query.criteria.ParameterExpression;
22: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionLiteralImpl;
23: import cz.cvut.kbss.jopa.query.criteria.expressions.ParameterExpressionImpl;
24: import cz.cvut.kbss.jopa.query.soql.SoqlConstants;
25:
26: import java.util.HashMap;
27: import java.util.Map;
28:
29: public class CriteriaParameterFiller {
30: private final HashMap<String, ExpressionLiteralImpl> literalParameters;
31: private int counter;
32:
33: public CriteriaParameterFiller() {
34: this.literalParameters = new HashMap<>();
35: this.counter = 0;
36: }
37:
38: /**
39: * Register literal expression as query parameter and return generated name for query.
40: *
41: * @param parameter - literal expression
42: * @return String - generated name for query
43: */
44: public String registerParameter(ExpressionLiteralImpl parameter) {
45: String name = generateParameterName();
46: literalParameters.put(name, parameter);
47: return SoqlConstants.VARIABLE_PREFIX + name;
48: }
49:
50: /**
51: * Register parameter expression as query parameter. Return real name if exists, generated name otherwise.
52: *
53: * @param parameter - parameter expression
54: * @return String - real name for query if exists, generated name for query otherwise
55: */
56: public String registerParameter(ParameterExpression parameter) {
57: if (parameter.getName() == null) {
58: String name = generateParameterName();
59: ((ParameterExpressionImpl) parameter).setNameIfUnnamed(name);
60: }
61: return SoqlConstants.VARIABLE_PREFIX + parameter.getName();
62: }
63:
64: /**
65: * Sets value from literal expressions registered as parameters to query parameters.
66: *
67: * @param query - TypedQuery fom setting parameters value
68: */
69: public <T> void setValuesToRegisteredParameters(TypedQueryImpl<T> query) {
70: for (Map.Entry<String, ExpressionLiteralImpl> e : literalParameters.entrySet()) {
71: if (e.getValue().getLanguageTag() != null) {
72: query.setParameter(e.getKey(), (String) e.getValue().getValue(), e.getValue().getLanguageTag());
73: } else {
74: query.setParameter(e.getKey(), literalParameters.get(e.getKey()).getValue());
75: }
76: }
77: }
78:
79: private String generateParameterName() {
80: return "generatedName" + this.counter++;
81: }
82: }