Skip to content

Commit 82bc225

Browse files
committed
Polishing.
Simplify sanitizer. Add unit test. See #1405 See #1406 Original pull request #1415
1 parent 427fc4d commit 82bc225

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/BindParameterNameSanitizer.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@
2222
* Sanitizes the name of bind parameters, so they don't contain any illegal characters.
2323
*
2424
* @author Jens Schauder
25-
*
26-
* @since 3.0
25+
* @since 3.0.2
2726
*/
28-
enum BindParameterNameSanitizer {
29-
INSTANCE;
27+
abstract class BindParameterNameSanitizer {
3028

3129
private static final Pattern parameterPattern = Pattern.compile("\\W");
3230

33-
String sanitize(String rawName) {
34-
31+
static String sanitize(String rawName) {
3532
return parameterPattern.matcher(rawName).replaceAll("");
3633
}
3734
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.*;
1919
import java.util.function.Function;
20-
import java.util.regex.Pattern;
2120
import java.util.stream.Collectors;
2221

2322
import org.springframework.data.domain.Pageable;
@@ -158,7 +157,7 @@ private Condition getSubselectCondition(PersistentPropertyPathExtension path,
158157
}
159158

160159
private BindMarker getBindMarker(SqlIdentifier columnName) {
161-
return SQL.bindMarker(":" + BindParameterNameSanitizer.INSTANCE.sanitize(renderReference(columnName)));
160+
return SQL.bindMarker(":" + BindParameterNameSanitizer.sanitize(renderReference(columnName)));
162161
}
163162

164163
/**
@@ -655,7 +654,7 @@ private String createUpdateSql() {
655654
private String createUpdateWithVersionSql() {
656655

657656
Update update = createBaseUpdate() //
658-
.and(getVersionColumn().isEqualTo(SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
657+
.and(getVersionColumn().isEqualTo(getBindMarker(VERSION_SQL_PARAMETER))) //
659658
.build();
660659

661660
return render(update);
@@ -689,7 +688,7 @@ private String createDeleteByIdInSql() {
689688
private String createDeleteByIdAndVersionSql() {
690689

691690
Delete delete = createBaseDeleteById(getTable()) //
692-
.and(getVersionColumn().isEqualTo(SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
691+
.and(getVersionColumn().isEqualTo(getBindMarker(VERSION_SQL_PARAMETER))) //
693692
.build();
694693

695694
return render(delete);
@@ -698,13 +697,13 @@ private String createDeleteByIdAndVersionSql() {
698697
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteById(Table table) {
699698

700699
return Delete.builder().from(table)
701-
.where(getIdColumn().isEqualTo(SQL.bindMarker(":" + renderReference(ID_SQL_PARAMETER))));
700+
.where(getIdColumn().isEqualTo(getBindMarker(ID_SQL_PARAMETER)));
702701
}
703702

704703
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteByIdIn(Table table) {
705704

706705
return Delete.builder().from(table)
707-
.where(getIdColumn().in(SQL.bindMarker(":" + renderReference(IDS_SQL_PARAMETER))));
706+
.where(getIdColumn().in(getBindMarker(IDS_SQL_PARAMETER)));
708707
}
709708

710709
private String createDeleteByPathAndCriteria(PersistentPropertyPathExtension path,

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlIdentifierParameterSource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void addValue(SqlIdentifier name, Object value) {
6868
void addValue(SqlIdentifier identifier, Object value, int sqlType) {
6969

7070
identifiers.add(identifier);
71-
String name = BindParameterNameSanitizer.INSTANCE.sanitize(identifier.getReference(identifierProcessing));
71+
String name = BindParameterNameSanitizer.sanitize(identifier.getReference(identifierProcessing));
7272
namesToValues.put(name, value);
7373
registerSqlType(name, sqlType);
7474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
package org.springframework.data.jdbc.core.convert;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* Unit tests for {@link BindParameterNameSanitizer}.
24+
*
25+
* @author Mark Paluch
26+
*/
27+
class BindParameterNameSanitizerUnitTests {
28+
29+
@Test
30+
void shouldSanitizeNames() {
31+
32+
assertThat(BindParameterNameSanitizer.sanitize("___oldOptimisticLockingVersion"))
33+
.isEqualTo("___oldOptimisticLockingVersion");
34+
assertThat(BindParameterNameSanitizer.sanitize("fooBar")).isEqualTo("fooBar");
35+
assertThat(BindParameterNameSanitizer.sanitize("one.two.three")).isEqualTo("onetwothree");
36+
}
37+
}

0 commit comments

Comments
 (0)