Polishing.

Simplify sanitizer. Add unit test.

See #1405
See #1406
Original pull request #1415
This commit is contained in:
Mark Paluch
2023-01-26 09:38:24 +01:00
parent 427fc4d83b
commit 82bc225b0b
4 changed files with 46 additions and 13 deletions

View File

@@ -22,16 +22,13 @@ import java.util.regex.Pattern;
* Sanitizes the name of bind parameters, so they don't contain any illegal characters.
*
* @author Jens Schauder
*
* @since 3.0
* @since 3.0.2
*/
enum BindParameterNameSanitizer {
INSTANCE;
abstract class BindParameterNameSanitizer {
private static final Pattern parameterPattern = Pattern.compile("\\W");
String sanitize(String rawName) {
static String sanitize(String rawName) {
return parameterPattern.matcher(rawName).replaceAll("");
}
}

View File

@@ -17,7 +17,6 @@ package org.springframework.data.jdbc.core.convert;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.data.domain.Pageable;
@@ -158,7 +157,7 @@ class SqlGenerator {
}
private BindMarker getBindMarker(SqlIdentifier columnName) {
return SQL.bindMarker(":" + BindParameterNameSanitizer.INSTANCE.sanitize(renderReference(columnName)));
return SQL.bindMarker(":" + BindParameterNameSanitizer.sanitize(renderReference(columnName)));
}
/**
@@ -655,7 +654,7 @@ class SqlGenerator {
private String createUpdateWithVersionSql() {
Update update = createBaseUpdate() //
.and(getVersionColumn().isEqualTo(SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
.and(getVersionColumn().isEqualTo(getBindMarker(VERSION_SQL_PARAMETER))) //
.build();
return render(update);
@@ -689,7 +688,7 @@ class SqlGenerator {
private String createDeleteByIdAndVersionSql() {
Delete delete = createBaseDeleteById(getTable()) //
.and(getVersionColumn().isEqualTo(SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
.and(getVersionColumn().isEqualTo(getBindMarker(VERSION_SQL_PARAMETER))) //
.build();
return render(delete);
@@ -698,13 +697,13 @@ class SqlGenerator {
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteById(Table table) {
return Delete.builder().from(table)
.where(getIdColumn().isEqualTo(SQL.bindMarker(":" + renderReference(ID_SQL_PARAMETER))));
.where(getIdColumn().isEqualTo(getBindMarker(ID_SQL_PARAMETER)));
}
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteByIdIn(Table table) {
return Delete.builder().from(table)
.where(getIdColumn().in(SQL.bindMarker(":" + renderReference(IDS_SQL_PARAMETER))));
.where(getIdColumn().in(getBindMarker(IDS_SQL_PARAMETER)));
}
private String createDeleteByPathAndCriteria(PersistentPropertyPathExtension path,

View File

@@ -68,7 +68,7 @@ class SqlIdentifierParameterSource extends AbstractSqlParameterSource {
void addValue(SqlIdentifier identifier, Object value, int sqlType) {
identifiers.add(identifier);
String name = BindParameterNameSanitizer.INSTANCE.sanitize(identifier.getReference(identifierProcessing));
String name = BindParameterNameSanitizer.sanitize(identifier.getReference(identifierProcessing));
namesToValues.put(name, value);
registerSqlType(name, sqlType);
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.convert;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
/**
* Unit tests for {@link BindParameterNameSanitizer}.
*
* @author Mark Paluch
*/
class BindParameterNameSanitizerUnitTests {
@Test
void shouldSanitizeNames() {
assertThat(BindParameterNameSanitizer.sanitize("___oldOptimisticLockingVersion"))
.isEqualTo("___oldOptimisticLockingVersion");
assertThat(BindParameterNameSanitizer.sanitize("fooBar")).isEqualTo("fooBar");
assertThat(BindParameterNameSanitizer.sanitize("one.two.three")).isEqualTo("onetwothree");
}
}