Skip to content

Commit

Permalink
SqlIdentifierParameterSource now sanitizes identifier names
Browse files Browse the repository at this point in the history
  • Loading branch information
IAM20 committed Mar 23, 2023
1 parent e6986da commit 13fd49a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -88,6 +87,7 @@
import org.springframework.util.Assert;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlFunction;
import com.navercorp.spring.data.jdbc.plus.sql.parametersource.BindParameterNameSanitizer;

/**
* Generates SQL statements to be used by {@link org.springframework.data.jdbc.repository.support.SimpleJdbcRepository}
Expand All @@ -113,8 +113,6 @@ class SqlGenerator {
static final SqlIdentifier IDS_SQL_PARAMETER = SqlIdentifier.unquoted("ids");
static final SqlIdentifier ROOT_ID_PARAMETER = SqlIdentifier.unquoted("rootId");

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

private final RelationalPersistentEntity<?> entity;
private final MappingContext<RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
private final RenderContext renderContext;
Expand Down Expand Up @@ -236,7 +234,7 @@ private Condition getSubselectCondition(PersistentPropertyPathExtension path,
}

private BindMarker getBindMarker(SqlIdentifier columnName) {
return SQL.bindMarker(":" + parameterPattern.matcher(renderReference(columnName)).replaceAll(""));
return SQL.bindMarker(":" + BindParameterNameSanitizer.sanitize(renderReference(columnName)));
}

/**
Expand Down Expand Up @@ -887,7 +885,7 @@ private String createUpdateWithVersionSql() {

Update update = createBaseUpdate() //
.and(getVersionColumn().isEqualTo(
SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
getBindMarker(VERSION_SQL_PARAMETER))) //
.build();

return render(update);
Expand Down Expand Up @@ -950,7 +948,7 @@ private String createDeleteByIdAndVersionSql() {

Delete delete = createBaseDeleteById(getDmlTable()) //
.and(getVersionColumn().isEqualTo(
SQL.bindMarker(":" + renderReference(VERSION_SQL_PARAMETER)))) //
getBindMarker(VERSION_SQL_PARAMETER))) //
.build();

return render(delete);
Expand All @@ -959,13 +957,13 @@ private String createDeleteByIdAndVersionSql() {
private DeleteBuilder.DeleteWhereAndOr createBaseDeleteById(Table table) {
return Delete.builder().from(table)
.where(getIdColumn().isEqualTo(
SQL.bindMarker(":" + renderReference(ID_SQL_PARAMETER))));
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.navercorp.spring.data.jdbc.plus.sql.parametersource;

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.2
*
* COPY: org.springframework.data.jdbc.core.convert.BindParameterNameSanitizer
*/
public abstract class BindParameterNameSanitizer {

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

public static String sanitize(String rawName) {
return parameterPattern.matcher(rawName).replaceAll("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void addValue(SqlIdentifier name, Object value) {
void addValue(SqlIdentifier identifier, Object value, int sqlType) {

identifiers.add(identifier);
String name = identifier.getReference(identifierProcessing);
String name = BindParameterNameSanitizer.sanitize(identifier.getReference(identifierProcessing));
namesToValues.put(name, value);
registerSqlType(name, sqlType);
}
Expand Down

0 comments on commit 13fd49a

Please sign in to comment.