Skip to content

Commit d43740e

Browse files
committed
introduced proper implementation of ValueFunction
1 parent 1e63e31 commit d43740e

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/repository/query/ParameterMetadataProvider.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121

2222
import org.springframework.data.relational.core.dialect.Escaper;
23-
import org.springframework.data.relational.core.query.ValueFunction;
2423
import org.springframework.data.repository.query.Parameter;
2524
import org.springframework.data.repository.query.Parameters;
2625
import org.springframework.data.repository.query.parser.Part;
@@ -139,12 +138,12 @@ protected Object prepareParameterValue(@Nullable Object value, Class<?> valueTyp
139138

140139
switch (partType) {
141140
case STARTING_WITH:
142-
return (ValueFunction<String>) escaper -> escaper.escape(value.toString()) + "%";
141+
return SimpleValueFunction.of(value, s -> s + "%");
143142
case ENDING_WITH:
144-
return (ValueFunction<String>) escaper -> "%" + escaper.escape(value.toString());
143+
return SimpleValueFunction.of(value, s -> "%" + s);
145144
case CONTAINING:
146145
case NOT_CONTAINING:
147-
return (ValueFunction<String>) escaper -> "%" + escaper.escape(value.toString()) + "%";
146+
return SimpleValueFunction.of(value, s -> "%" + s + "%");
148147
default:
149148
return value;
150149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2023 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+
17+
package org.springframework.data.relational.repository.query;
18+
19+
import org.springframework.data.relational.core.dialect.Escaper;
20+
import org.springframework.data.relational.core.query.ValueFunction;
21+
22+
import java.util.function.Function;
23+
24+
record SimpleValueFunction(Object value, Function<String, String> modifier) implements ValueFunction<String> {
25+
26+
static SimpleValueFunction of(Object value, Function<String, String> modifier) {
27+
return new SimpleValueFunction(value, modifier);
28+
}
29+
30+
@Override
31+
public String apply(Escaper escaper) {
32+
return modifier.apply(escaper.escape(value.toString()));
33+
}
34+
}

0 commit comments

Comments
 (0)