DATAJDBC-334 - Fixes broken parameter names for quoted column names.

When a column has a quoted name it contains characters illegal for parameter names.
Therefore the parameter names now get sanitised.

Compared to the version on master testing is more lenient.
This accounts for the SqlGenerator behaving slightly different then on master.

Original pull request: #120.
See also: https://jira.spring.io/browse/DATAJDBC-262.
This commit is contained in:
Bastian Wilhelm
2019-02-18 17:34:11 +01:00
committed by Jens Schauder
parent 88bcd82664
commit c78bfae44a
2 changed files with 38 additions and 4 deletions

View File

@@ -21,6 +21,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -40,6 +41,7 @@ import org.springframework.util.Assert;
*
* @author Jens Schauder
* @author Yoichi Imai
* @author Bastian Wilhelm
*/
class SqlGenerator {
@@ -61,6 +63,8 @@ class SqlGenerator {
private final Lazy<String> deleteByListSql = Lazy.of(this::createDeleteByListSql);
private final SqlGeneratorSource sqlGeneratorSource;
private final Pattern parameterPattern = Pattern.compile("\\W");
SqlGenerator(RelationalMappingContext context, RelationalPersistentEntity<?> entity,
SqlGeneratorSource sqlGeneratorSource) {
@@ -263,6 +267,7 @@ class SqlGenerator {
String tableColumns = String.join(", ", columnNamesForInsert);
String parameterNames = columnNamesForInsert.stream()//
.map(this::columnNameToParameterName)
.map(n -> String.format(":%s", n))//
.collect(Collectors.joining(", "));
@@ -274,10 +279,11 @@ class SqlGenerator {
String updateTemplate = "UPDATE %s SET %s WHERE %s = :%s";
String setClause = columnNames.stream()//
.map(n -> String.format("%s = :%s", n, n))//
.map(n -> String.format("%s = :%s", n, columnNameToParameterName(n))) //
.collect(Collectors.joining(", "));
return String.format(updateTemplate, entity.getTableName(), setClause, entity.getIdColumn(), entity.getIdColumn());
return String.format(updateTemplate, entity.getTableName(), setClause, entity.getIdColumn(),
columnNameToParameterName(entity.getIdColumn()));
}
private String createDeleteSql() {
@@ -349,4 +355,8 @@ class SqlGenerator {
entity.getTableName(), innerCondition //
);
}
private String columnNameToParameterName(String columnName){
return parameterPattern.matcher(columnName).replaceAll("");
}
}