Skip to content

Commit

Permalink
Remove square brackets with index/key expressions from generated SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Oct 19, 2022
1 parent affccba commit aaa22ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ public static ParsedSql parseSqlStatement(String sql) {
}
if (j - i > 1) {
parameter = sql.substring(i + 1, j);
if (parameter.contains("[")) {
parameter += "]"; // preserve end bracket for index/key
if (j < statement.length && statement[j] == ']' && parameter.contains("[")) {
// preserve end bracket for index/key
j++;
parameter = sql.substring(i + 1, j);
}
namedParameterCount = addNewNamedParameter(
namedParameters, namedParameterCount, parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ public void parseSqlStatementWithSquareBracket() {
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(psql.getNamedParameterCount()).isEqualTo(1);
assertThat(psql.getParameterNames()).containsExactly("ext");

String sqlToUse = NamedParameterUtils.substituteNamedParameters(psql, null);
assertThat(sqlToUse).isEqualTo("SELECT ARRAY[?]");
}

@Test // gh-27925
Expand All @@ -345,11 +348,14 @@ public Map<String, Object> getHeaders() {
}

Foo foo = new Foo();
Object[] params = NamedParameterUtils.buildValueArray(psql,
new BeanPropertySqlParameterSource(foo), null);
SqlParameterSource paramSource = new BeanPropertySqlParameterSource(foo);
Object[] params = NamedParameterUtils.buildValueArray(psql, paramSource, null);

assertThat(params[0]).isInstanceOf(SqlParameterValue.class);
assertThat(((SqlParameterValue) params[0]).getValue()).isEqualTo(foo.getHeaders().get("id"));

String sqlToUse = NamedParameterUtils.substituteNamedParameters(psql, paramSource);
assertThat(sqlToUse).isEqualTo("insert into foos (id) values (?)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ public static ParsedSql parseSqlStatement(String sql) {
}
if (j - i > 1) {
parameter = sql.substring(i + 1, j);
if (parameter.contains("[")) {
parameter += "]"; // preserve end bracket for index/key
if (j < statement.length && statement[j] == ']' && parameter.contains("[")) {
// preserve end bracket for index/key
j++;
parameter = sql.substring(i + 1, j);
}
namedParameterCount = addNewNamedParameter(
namedParameters, namedParameterCount, parameter);
Expand Down

0 comments on commit aaa22ad

Please sign in to comment.