#23 - Polishing.
Formatting. Made Tests simpler and stricter by using `containsExactly`. Added @Test annotation to ignored database specific tests so they actually show up as ignored. Original pull request: #47.
This commit is contained in:
committed by
Mark Paluch
parent
b43b11936f
commit
29f1edce0e
@@ -36,14 +36,18 @@ import org.springframework.data.r2dbc.dialect.BindMarkersFactory;
|
||||
*/
|
||||
public class NamedParameterExpander {
|
||||
|
||||
/** Default maximum number of entries for the SQL cache: 256. */
|
||||
/**
|
||||
* Default maximum number of entries for the SQL cache: 256.
|
||||
*/
|
||||
public static final int DEFAULT_CACHE_LIMIT = 256;
|
||||
|
||||
private volatile int cacheLimit = DEFAULT_CACHE_LIMIT;
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
/** Cache of original SQL String to ParsedSql representation. */
|
||||
/**
|
||||
* Cache of original SQL String to ParsedSql representation.
|
||||
*/
|
||||
@SuppressWarnings("serial") private final Map<String, ParsedSql> parsedSqlCache = new LinkedHashMap<String, ParsedSql>(
|
||||
DEFAULT_CACHE_LIMIT, 0.75f, true) {
|
||||
@Override
|
||||
@@ -101,8 +105,10 @@ public class NamedParameterExpander {
|
||||
}
|
||||
|
||||
synchronized (this.parsedSqlCache) {
|
||||
|
||||
ParsedSql parsedSql = this.parsedSqlCache.get(sql);
|
||||
if (parsedSql == null) {
|
||||
|
||||
parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
this.parsedSqlCache.put(sql, parsedSql);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.data.r2dbc.dialect.SqlServerDialect;
|
||||
* Unit tests for {@link NamedParameterUtils}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class NamedParameterUtilsUnitTests {
|
||||
|
||||
@@ -42,19 +43,19 @@ public class NamedParameterUtilsUnitTests {
|
||||
|
||||
String sql = "xxx :a yyyy :b :c :a zzzzz";
|
||||
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
assertThat(psql.getParameterNames()).containsSequence("a", "b", "c", "a");
|
||||
assertThat(psql.getParameterNames()).containsExactly("a", "b", "c", "a");
|
||||
assertThat(psql.getTotalParameterCount()).isEqualTo(4);
|
||||
assertThat(psql.getNamedParameterCount()).isEqualTo(3);
|
||||
|
||||
String sql2 = "xxx &a yyyy ? zzzzz";
|
||||
ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2);
|
||||
assertThat(psql2.getParameterNames().get(0)).isEqualTo("a");
|
||||
assertThat(psql2.getParameterNames()).containsExactly("a");
|
||||
assertThat(psql2.getTotalParameterCount()).isEqualTo(2);
|
||||
assertThat(psql2.getNamedParameterCount()).isEqualTo(1);
|
||||
|
||||
String sql3 = "xxx &ä+:ö" + '\t' + ":ü%10 yyyy ? zzzzz";
|
||||
ParsedSql psql3 = NamedParameterUtils.parseSqlStatement(sql3);
|
||||
assertThat(psql3.getParameterNames()).containsSequence("ä", "ö", "ü");
|
||||
assertThat(psql3.getParameterNames()).containsExactly("ä", "ö", "ü");
|
||||
}
|
||||
|
||||
@Test // gh-23
|
||||
@@ -177,9 +178,8 @@ public class NamedParameterUtilsUnitTests {
|
||||
String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2";
|
||||
|
||||
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
assertThat(parsedSql.getParameterNames()).hasSize(2);
|
||||
assertThat(parsedSql.getParameterNames().get(0)).isEqualTo("p1");
|
||||
assertThat(parsedSql.getParameterNames().get(1)).isEqualTo("p2");
|
||||
|
||||
assertThat(parsedSql.getParameterNames()).containsExactly("p1", "p2");
|
||||
assertThat(expand(parsedSql)).isEqualTo(expectedSql);
|
||||
}
|
||||
|
||||
@@ -190,9 +190,7 @@ public class NamedParameterUtilsUnitTests {
|
||||
String sql = "select foo from bar where baz = b:{p1}:{p2}z";
|
||||
|
||||
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
assertThat(parsedSql.getParameterNames()).hasSize(2);
|
||||
assertThat(parsedSql.getParameterNames().get(0)).isEqualTo("p1");
|
||||
assertThat(parsedSql.getParameterNames().get(1)).isEqualTo("p2");
|
||||
assertThat(parsedSql.getParameterNames()).containsExactly("p1", "p2");
|
||||
assertThat(expand(parsedSql)).isEqualTo(expectedSql);
|
||||
}
|
||||
|
||||
@@ -217,12 +215,12 @@ public class NamedParameterUtilsUnitTests {
|
||||
|
||||
@Test // gh-23
|
||||
public void parseSqlStatementWithSingleLetterInBrackets() {
|
||||
|
||||
String expectedSql = "select foo from bar where baz = b$1z";
|
||||
String sql = "select foo from bar where baz = b:{p}z";
|
||||
|
||||
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
assertThat(parsedSql.getParameterNames()).hasSize(1);
|
||||
assertThat(parsedSql.getParameterNames().get(0)).isEqualTo("p");
|
||||
assertThat(parsedSql.getParameterNames()).containsExactly("p");
|
||||
assertThat(expand(parsedSql)).isEqualTo(expectedSql);
|
||||
}
|
||||
|
||||
@@ -260,7 +258,7 @@ public class NamedParameterUtilsUnitTests {
|
||||
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
|
||||
assertThat(psql.getTotalParameterCount()).isEqualTo(1);
|
||||
assertThat(psql.getParameterNames().get(0)).isEqualTo("xxx");
|
||||
assertThat(psql.getParameterNames()).containsExactly("xxx");
|
||||
}
|
||||
|
||||
@Test // gh-23
|
||||
@@ -271,7 +269,7 @@ public class NamedParameterUtilsUnitTests {
|
||||
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
|
||||
assertThat(psql.getTotalParameterCount()).isEqualTo(1);
|
||||
assertThat(psql.getParameterNames().get(0)).isEqualTo("xxx");
|
||||
assertThat(psql.getParameterNames()).containsExactly("xxx");
|
||||
}
|
||||
|
||||
@Test // gh-23
|
||||
@@ -282,7 +280,7 @@ public class NamedParameterUtilsUnitTests {
|
||||
ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2);
|
||||
|
||||
assertThat(psql2.getTotalParameterCount()).isEqualTo(1);
|
||||
assertThat(psql2.getParameterNames().get(0)).isEqualTo("xxx");
|
||||
assertThat(psql2.getParameterNames()).containsExactly("xxx");
|
||||
}
|
||||
|
||||
private String expand(ParsedSql sql) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.r2dbc.testing.ExternalDatabase;
|
||||
import org.springframework.data.r2dbc.testing.PostgresTestSupport;
|
||||
|
||||
@@ -49,10 +50,12 @@ public class PostgresDatabaseClientIntegrationTests extends AbstractDatabaseClie
|
||||
}
|
||||
|
||||
@Ignore("Adding RETURNING * lets Postgres report 0 affected rows.")
|
||||
@Test
|
||||
@Override
|
||||
public void insert() {}
|
||||
|
||||
@Ignore("Adding RETURNING * lets Postgres report 0 affected rows.")
|
||||
@Test
|
||||
@Override
|
||||
public void insertTypedObject() {}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,6 @@ public class PostgresTestSupport {
|
||||
+ " manual integer NULL\n" //
|
||||
+ ");";
|
||||
|
||||
public static String INSERT_INTO_LEGOSET = "INSERT INTO legoset (id, name, manual) VALUES($1, $2, $3)";
|
||||
|
||||
/**
|
||||
* Returns a database either hosted locally at {@code postgres:@localhost:5432/postgres} or running inside Docker.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user