|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.r2dbc.repository.query; |
| 17 | + |
| 18 | +import static org.springframework.data.r2dbc.repository.query.ExpressionQuery.*; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +import org.springframework.data.r2dbc.core.DatabaseClient; |
| 26 | +import org.springframework.data.r2dbc.mapping.SettableValue; |
| 27 | +import org.springframework.data.relational.repository.query.RelationalParameterAccessor; |
| 28 | +import org.springframework.data.repository.query.Parameter; |
| 29 | +import org.springframework.data.repository.query.Parameters; |
| 30 | +import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; |
| 31 | +import org.springframework.expression.EvaluationContext; |
| 32 | +import org.springframework.expression.Expression; |
| 33 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 34 | +import org.springframework.util.Assert; |
| 35 | + |
| 36 | +/** |
| 37 | + * {@link ExpressionEvaluatingParameterBinder} allows to evaluate, convert and bind parameters to placeholders within a |
| 38 | + * {@link String}. |
| 39 | + * |
| 40 | + * @author Mark Paluch |
| 41 | + * @since 1.1 |
| 42 | + */ |
| 43 | +class ExpressionEvaluatingParameterBinder { |
| 44 | + |
| 45 | + private final SpelExpressionParser expressionParser; |
| 46 | + |
| 47 | + private final QueryMethodEvaluationContextProvider evaluationContextProvider; |
| 48 | + |
| 49 | + private final ExpressionQuery expressionQuery; |
| 50 | + |
| 51 | + private final Map<String, Boolean> namedParameters = new ConcurrentHashMap<>(); |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates new {@link ExpressionEvaluatingParameterBinder} |
| 55 | + * |
| 56 | + * @param expressionParser must not be {@literal null}. |
| 57 | + * @param evaluationContextProvider must not be {@literal null}. |
| 58 | + * @param expressionQuery must not be {@literal null}. |
| 59 | + */ |
| 60 | + ExpressionEvaluatingParameterBinder(SpelExpressionParser expressionParser, |
| 61 | + QueryMethodEvaluationContextProvider evaluationContextProvider, ExpressionQuery expressionQuery) { |
| 62 | + |
| 63 | + Assert.notNull(expressionParser, "ExpressionParser must not be null"); |
| 64 | + Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null"); |
| 65 | + Assert.notNull(expressionQuery, "ExpressionQuery must not be null"); |
| 66 | + |
| 67 | + this.expressionParser = expressionParser; |
| 68 | + this.evaluationContextProvider = evaluationContextProvider; |
| 69 | + this.expressionQuery = expressionQuery; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Bind values provided by {@link RelationalParameterAccessor} to placeholders in {@link ExpressionQuery} while |
| 74 | + * considering potential conversions and parameter types. |
| 75 | + * |
| 76 | + * @param bindSpec must not be {@literal null}. |
| 77 | + * @param parameterAccessor must not be {@literal null}. |
| 78 | + */ |
| 79 | + public <T extends DatabaseClient.BindSpec<T>> T bind(T bindSpec, RelationalParameterAccessor parameterAccessor) { |
| 80 | + |
| 81 | + Object[] values = parameterAccessor.getValues(); |
| 82 | + Parameters<?, ?> bindableParameters = parameterAccessor.getBindableParameters(); |
| 83 | + |
| 84 | + T bindSpecToUse = bindExpressions(bindSpec, values, bindableParameters); |
| 85 | + bindSpecToUse = bindParameters(bindSpecToUse, parameterAccessor.hasBindableNullValue(), values, bindableParameters); |
| 86 | + |
| 87 | + return bindSpecToUse; |
| 88 | + } |
| 89 | + |
| 90 | + private <T extends DatabaseClient.BindSpec<T>> T bindExpressions(T bindSpec, Object[] values, |
| 91 | + Parameters<?, ?> bindableParameters) { |
| 92 | + |
| 93 | + T bindSpecToUse = bindSpec; |
| 94 | + |
| 95 | + for (ParameterBinding binding : expressionQuery.getBindings()) { |
| 96 | + |
| 97 | + SettableValue valueForBinding = getParameterValueForBinding(bindableParameters, values, binding); |
| 98 | + |
| 99 | + if (valueForBinding.isEmpty()) { |
| 100 | + bindSpecToUse = bindSpecToUse.bindNull(binding.getParameterName(), valueForBinding.getType()); |
| 101 | + } else { |
| 102 | + bindSpecToUse = bindSpecToUse.bind(binding.getParameterName(), valueForBinding.getValue()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return bindSpecToUse; |
| 107 | + } |
| 108 | + |
| 109 | + private <T extends DatabaseClient.BindSpec<T>> T bindParameters(T bindSpec, boolean bindableNull, Object[] values, |
| 110 | + Parameters<?, ?> bindableParameters) { |
| 111 | + |
| 112 | + T bindSpecToUse = bindSpec; |
| 113 | + int index = 0; |
| 114 | + int bindingIndex = 0; |
| 115 | + |
| 116 | + for (Object value : values) { |
| 117 | + |
| 118 | + Parameter bindableParameter = bindableParameters.getBindableParameter(index++); |
| 119 | + |
| 120 | + Optional<String> name = bindableParameter.getName(); |
| 121 | + |
| 122 | + if ((name.isPresent() && isNamedParameterUsed(name)) || !expressionQuery.getBindings().isEmpty()) { |
| 123 | + |
| 124 | + if (isNamedParameterUsed(name)) { |
| 125 | + |
| 126 | + if (value == null) { |
| 127 | + if (bindableNull) { |
| 128 | + bindSpecToUse = bindSpecToUse.bindNull(name.get(), bindableParameter.getType()); |
| 129 | + } |
| 130 | + } else { |
| 131 | + bindSpecToUse = bindSpecToUse.bind(name.get(), value); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // skip unused named parameters if there is SpEL |
| 136 | + } else { |
| 137 | + if (value == null) { |
| 138 | + if (bindableNull) { |
| 139 | + bindSpecToUse = bindSpecToUse.bindNull(bindingIndex++, bindableParameter.getType()); |
| 140 | + } |
| 141 | + } else { |
| 142 | + bindSpecToUse = bindSpecToUse.bind(bindingIndex++, value); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return bindSpecToUse; |
| 148 | + } |
| 149 | + |
| 150 | + private boolean isNamedParameterUsed(Optional<String> name) { |
| 151 | + |
| 152 | + if (!name.isPresent()) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + return namedParameters.computeIfAbsent(name.get(), it -> { |
| 157 | + |
| 158 | + Pattern namedParameterPattern = Pattern.compile("(\\W)[:#$@]" + Pattern.quote(it) + "(\\W|$)"); |
| 159 | + return namedParameterPattern.matcher(expressionQuery.getQuery()).find(); |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns the value to be used for the given {@link ParameterBinding}. |
| 165 | + * |
| 166 | + * @param parameters must not be {@literal null}. |
| 167 | + * @param binding must not be {@literal null}. |
| 168 | + * @return the value used for the given {@link ParameterBinding}. |
| 169 | + */ |
| 170 | + private SettableValue getParameterValueForBinding(Parameters<?, ?> parameters, Object[] values, |
| 171 | + ParameterBinding binding) { |
| 172 | + return evaluateExpression(binding.getExpression(), parameters, values); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Evaluates the given {@code expressionString}. |
| 177 | + * |
| 178 | + * @param expressionString must not be {@literal null} or empty. |
| 179 | + * @param parameters must not be {@literal null}. |
| 180 | + * @param parameterValues must not be {@literal null}. |
| 181 | + * @return the value of the {@code expressionString} evaluation. |
| 182 | + */ |
| 183 | + private SettableValue evaluateExpression(String expressionString, Parameters<?, ?> parameters, |
| 184 | + Object[] parameterValues) { |
| 185 | + |
| 186 | + EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(parameters, parameterValues); |
| 187 | + Expression expression = expressionParser.parseExpression(expressionString); |
| 188 | + |
| 189 | + Object value = expression.getValue(evaluationContext, Object.class); |
| 190 | + Class<?> valueType = expression.getValueType(evaluationContext); |
| 191 | + |
| 192 | + return SettableValue.fromOrEmpty(value, valueType != null ? valueType : Object.class); |
| 193 | + } |
| 194 | +} |
0 commit comments