Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Feb 17, 2024
1 parent d4cde29 commit d86af57
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
public abstract class NamedParameterUtils {

/**
* Set of characters that qualify as comment or quotes starting characters.
* Set of characters that qualify as comment or quote starting characters.
*/
private static final String[] START_SKIP = new String[] {"'", "\"", "--", "/*", "`"};
private static final String[] START_SKIP = {"'", "\"", "--", "/*", "`"};

/**
* Set of characters that at are the corresponding comment or quotes ending characters.
* Set of characters that are the corresponding comment or quote ending characters.
*/
private static final String[] STOP_SKIP = new String[] {"'", "\"", "\n", "*/", "`"};
private static final String[] STOP_SKIP = {"'", "\"", "\n", "*/", "`"};

/**
* Set of characters that qualify as parameter separators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void testParseSqlStatementWithStringContainingQuotes() {
}

@Test // SPR-4789
public void parseSqlContainingComments() {
void parseSqlContainingComments() {
String sql1 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX\n";
ParsedSql parsedSql1 = NamedParameterUtils.parseSqlStatement(sql1);
assertThat(NamedParameterUtils.substituteNamedParameters(parsedSql1, null)).isEqualTo("/*+ HINT */ xxx /* comment ? */ ? yyyy ? ? ? zzzzz -- :xx XX\n");
Expand All @@ -179,15 +179,15 @@ public void parseSqlContainingComments() {
}

@Test // SPR-4612
public void parseSqlStatementWithPostgresCasting() {
void parseSqlStatementWithPostgresCasting() {
String expectedSql = "select 'first name' from artists where id = ? and birth_date=?::timestamp";
String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(substituteNamedParameters(parsedSql)).isEqualTo(expectedSql);
}

@Test // SPR-13582
public void parseSqlStatementWithPostgresContainedOperator() {
void parseSqlStatementWithPostgresContainedOperator() {
String expectedSql = "select 'first name' from artists where info->'stat'->'albums' = ?? ? and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
String sql = "select 'first name' from artists where info->'stat'->'albums' = ?? :album and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
Expand All @@ -196,7 +196,7 @@ public void parseSqlStatementWithPostgresContainedOperator() {
}

@Test // SPR-15382
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() {
void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() {
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
String sql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";

Expand All @@ -206,7 +206,7 @@ public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() {
}

@Test // SPR-15382
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() {
void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() {
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND ? = 'Back in Black'";
String sql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND :album = 'Back in Black'";

Expand All @@ -216,7 +216,7 @@ public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() {
}

@Test // SPR-7476
public void parseSqlStatementWithEscapedColon() {
void parseSqlStatementWithEscapedColon() {
String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE(? 23:59:59) and baz = ?";
String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2";

Expand All @@ -227,7 +227,7 @@ public void parseSqlStatementWithEscapedColon() {
}

@Test // SPR-7476
public void parseSqlStatementWithBracketDelimitedParameterNames() {
void parseSqlStatementWithBracketDelimitedParameterNames() {
String expectedSql = "select foo from bar where baz = b??z";
String sql = "select foo from bar where baz = b:{p1}:{p2}z";

Expand All @@ -238,7 +238,7 @@ public void parseSqlStatementWithBracketDelimitedParameterNames() {
}

@Test // SPR-7476
public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() {
void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() {
String expectedSql = "select foo from bar where baz = b:{}z";
String sql = "select foo from bar where baz = b:{}z";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
Expand Down Expand Up @@ -267,21 +267,21 @@ void parseSqlStatementWithSingleLetterInBrackets() {
}

@Test // SPR-2544
public void parseSqlStatementWithLogicalAnd() {
void parseSqlStatementWithLogicalAnd() {
String expectedSql = "xxx & yyyy";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(expectedSql);
assertThat(substituteNamedParameters(parsedSql)).isEqualTo(expectedSql);
}

@Test // SPR-2544
public void substituteNamedParametersWithLogicalAnd() {
void substituteNamedParametersWithLogicalAnd() {
String expectedSql = "xxx & yyyy";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertThat(newSql).isEqualTo(expectedSql);
}

@Test // SPR-3173
public void variableAssignmentOperator() {
void variableAssignmentOperator() {
String expectedSql = "x := 1";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertThat(newSql).isEqualTo(expectedSql);
Expand All @@ -295,14 +295,14 @@ public void variableAssignmentOperator() {
"SELECT \":foo\"\":doo\", :xxx FROM DUAL",
"SELECT `:foo``:doo`, :xxx FROM DUAL"
})
void parseSqlStatementWithParametersInsideQuote(String sql) {
void parseSqlStatementWithParametersInsideQuotesAndComments(String sql) {
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(parsedSql.getTotalParameterCount()).isEqualTo(1);
assertThat(parsedSql.getParameterNames()).containsExactly("xxx");
}

@Test // gh-27716
public void parseSqlStatementWithSquareBracket() {
void parseSqlStatementWithSquareBracket() {
String sql = "SELECT ARRAY[:ext]";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(parsedSql.getNamedParameterCount()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -54,14 +54,14 @@
abstract class NamedParameterUtils {

/**
* Set of characters that qualify as comment or quotes starting characters.
* Set of characters that qualify as comment or quote starting characters.
*/
private static final String[] START_SKIP = new String[] {"'", "\"", "--", "/*"};
private static final String[] START_SKIP = {"'", "\"", "--", "/*"};

/**
* Set of characters that at are the corresponding comment or quotes ending characters.
* Set of characters that are the corresponding comment or quote ending characters.
*/
private static final String[] STOP_SKIP = new String[] {"'", "\"", "\n", "*/"};
private static final String[] STOP_SKIP = {"'", "\"", "\n", "*/"};

/**
* Set of characters that qualify as parameter separators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import io.r2dbc.spi.Parameters;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import org.springframework.r2dbc.core.binding.BindMarkersFactory;
import org.springframework.r2dbc.core.binding.BindTarget;
Expand Down Expand Up @@ -243,40 +245,40 @@ void variableAssignmentOperator() {
assertThat(expand(expectedSql)).isEqualTo(expectedSql);
}

@Test
void parseSqlStatementWithQuotedSingleQuote() {
String sql = "SELECT ':foo'':doo', :xxx FROM DUAL";

ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(psql.getTotalParameterCount()).isEqualTo(1);
assertThat(psql.getParameterNames()).containsExactly("xxx");
}

@Test
void parseSqlStatementWithQuotesAndCommentBefore() {
String sql = "SELECT /*:doo*/':foo', :xxx FROM DUAL";

ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(psql.getTotalParameterCount()).isEqualTo(1);
assertThat(psql.getParameterNames()).containsExactly("xxx");
}

@Test
void parseSqlStatementWithQuotesAndCommentAfter() {
String sql2 = "SELECT ':foo'/*:doo*/, :xxx FROM DUAL";

ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2);
assertThat(psql2.getTotalParameterCount()).isEqualTo(1);
assertThat(psql2.getParameterNames()).containsExactly("xxx");
@ParameterizedTest // SPR-8280 and others
@ValueSource(strings = {
"SELECT ':foo'':doo', :xxx FROM DUAL",
"SELECT /*:doo*/':foo', :xxx FROM DUAL",
"SELECT ':foo'/*:doo*/, :xxx FROM DUAL",
"SELECT \":foo\"\":doo\", :xxx FROM DUAL",
})
void parseSqlStatementWithParametersInsideQuotesAndComments(String sql) {
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(parsedSql.getTotalParameterCount()).isEqualTo(1);
assertThat(parsedSql.getParameterNames()).containsExactly("xxx");
}

@Test // gh-27716
public void parseSqlStatementWithSquareBracket() {
void parseSqlStatementWithSquareBracket() {
String sql = "SELECT ARRAY[:ext]";

ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(psql.getNamedParameterCount()).isEqualTo(1);
assertThat(psql.getParameterNames()).containsExactly("ext");

assertThat(expand(psql)).isEqualTo("SELECT ARRAY[$1]");
}

@Test // gh-31596
void paramNameWithNestedSquareBrackets() {
String sql = "insert into GeneratedAlways (id, first_name, last_name) values " +
"(:records[0].id, :records[0].firstName, :records[0].lastName), " +
"(:records[1].id, :records[1].firstName, :records[1].lastName)";

ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(parsedSql.getParameterNames()).containsOnly(
"records[0].id", "records[0].firstName", "records[0].lastName",
"records[1].id", "records[1].firstName", "records[1].lastName");
}

@Test // gh-27925
Expand Down

0 comments on commit d86af57

Please sign in to comment.