Polishing

This commit is contained in:
Juergen Hoeller
2018-11-22 20:20:36 +01:00
parent ed9afa3686
commit 85b5c5a134
18 changed files with 118 additions and 115 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -914,6 +914,7 @@ public interface JdbcOperations {
* @param pss ParameterizedPreparedStatementSetter to use
* @return an array containing for each batch another array containing the numbers of rows affected
* by each update in the batch
* @since 3.1
*/
<T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize,
ParameterizedPreparedStatementSetter<T> pss) throws DataAccessException;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -1018,11 +1018,7 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
public int[][] doInPreparedStatement(PreparedStatement ps) throws SQLException {
List<int[]> rowsAffected = new ArrayList<int[]>();
try {
boolean batchSupported = true;
if (!JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
batchSupported = false;
logger.warn("JDBC Driver does not support Batch updates; resorting to single statement execution");
}
boolean batchSupported = JdbcUtils.supportsBatchUpdates(ps.getConnection());
int n = 0;
for (T obj : batchArgs) {
pss.setValues(ps, obj);

View File

@@ -99,25 +99,25 @@ public abstract class NamedParameterUtils {
char c = statement[i];
if (c == ':' || c == '&') {
int j = i + 1;
if (j < statement.length && statement[j] == ':' && c == ':') {
if (c == ':' && j < statement.length && statement[j] == ':') {
// Postgres-style "::" casting operator should be skipped
i = i + 2;
continue;
}
String parameter = null;
if (j < statement.length && c == ':' && statement[j] == '{') {
if (c == ':' && j < statement.length && statement[j] == '{') {
// :{x} style parameter
while (j < statement.length && statement[j] != '}') {
while (statement[j] != '}') {
j++;
if (j >= statement.length) {
throw new InvalidDataAccessApiUsageException("Non-terminated named parameter declaration " +
"at position " + i + " in statement: " + sql);
}
if (statement[j] == ':' || statement[j] == '{') {
throw new InvalidDataAccessApiUsageException("Parameter name contains invalid character '" +
statement[j] + "' at position " + i + " in statement: " + sql);
}
}
if (j >= statement.length) {
throw new InvalidDataAccessApiUsageException(
"Non-terminated named parameter declaration at position " + i + " in statement: " + sql);
}
if (j - i > 2) {
parameter = sql.substring(i + 2, j);
namedParameterCount = addNewNamedParameter(namedParameters, namedParameterCount, parameter);
@@ -190,7 +190,7 @@ public abstract class NamedParameterUtils {
}
/**
* Skip over comments and quoted names present in an SQL statement
* Skip over comments and quoted names present in an SQL statement.
* @param statement character array containing SQL statement
* @param position current position of statement
* @return next position to process after any comments or quotes are skipped

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -41,7 +41,7 @@ import org.springframework.util.Assert;
* arguments. Subclasses should be JavaBeans, allowing easy configuration.
*
* <p>This class and subclasses throw runtime exceptions, defined in the
* <codeorg.springframework.dao package</code> (and as thrown by the
* {@code org.springframework.dao} package (and as thrown by the
* {@code org.springframework.jdbc.core} package, which the classes
* in this package use under the hood to perform raw JDBC operations).
*
@@ -71,7 +71,7 @@ public abstract class RdbmsOperation implements InitializingBean {
private boolean returnGeneratedKeys = false;
private String[] generatedKeysColumnNames = null;
private String[] generatedKeysColumnNames;
private String sql;
@@ -282,7 +282,7 @@ public abstract class RdbmsOperation implements InitializingBean {
* Add one or more declared parameters. Used for configuring this operation
* when used in a bean factory. Each parameter will specify SQL type and (optionally)
* the parameter's name.
* @param parameters Array containing the declared {@link SqlParameter} objects
* @param parameters an array containing the declared {@link SqlParameter} objects
* @see #declaredParameters
*/
public void setParameters(SqlParameter... parameters) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2018 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.
@@ -38,12 +38,6 @@ import org.springframework.jdbc.core.SqlParameter;
*/
public abstract class SqlCall extends RdbmsOperation {
/**
* Object enabling us to create CallableStatementCreators
* efficiently, based on this class's declared parameters.
*/
private CallableStatementCreatorFactory callableStatementFactory;
/**
* Flag used to indicate that this call is for a function and to
* use the {? = call get_invoice_count(?)} syntax.
@@ -51,19 +45,24 @@ public abstract class SqlCall extends RdbmsOperation {
private boolean function = false;
/**
* Flag used to indicate that the sql for this call should be used exactly as it is
* defined. No need to add the escape syntax and parameter place holders.
* Flag used to indicate that the sql for this call should be used exactly as
* it is defined. No need to add the escape syntax and parameter place holders.
*/
private boolean sqlReadyForUse = false;
/**
* Call string as defined in java.sql.CallableStatement.
* String of form {call add_invoice(?, ?, ?)}
* or {? = call get_invoice_count(?)} if isFunction is set to true
* Updated after each parameter is added.
* String of form {call add_invoice(?, ?, ?)} or {? = call get_invoice_count(?)}
* if isFunction is set to true. Updated after each parameter is added.
*/
private String callString;
/**
* Object enabling us to create CallableStatementCreators
* efficiently, based on this class's declared parameters.
*/
private CallableStatementCreatorFactory callableStatementFactory;
/**
* Constructor to allow use as a JavaBean.
@@ -79,8 +78,8 @@ public abstract class SqlCall extends RdbmsOperation {
/**
* Create a new SqlCall object with SQL, but without parameters.
* Must add parameters or settle with none.
* @param ds DataSource to obtain connections from
* @param sql SQL to execute
* @param ds the DataSource to obtain connections from
* @param sql the SQL to execute
*/
public SqlCall(DataSource ds, String sql) {
setDataSource(ds);
@@ -99,7 +98,7 @@ public abstract class SqlCall extends RdbmsOperation {
* Return whether this call is for a function.
*/
public boolean isFunction() {
return function;
return this.function;
}
/**
@@ -113,7 +112,7 @@ public abstract class SqlCall extends RdbmsOperation {
* Return whether the SQL can be used as is.
*/
public boolean isSqlReadyForUse() {
return sqlReadyForUse;
return this.sqlReadyForUse;
}

View File

@@ -52,11 +52,11 @@ public class NamedParameterUtilsTests {
assertEquals(2, psql2.getTotalParameterCount());
assertEquals(1, psql2.getNamedParameterCount());
String sql3 = "xxx &a+:b" + '\t' + ":c%10 yyyy ? zzzzz";
String sql3 = "xxx &ä+:ö" + '\t' + ":ü%10 yyyy ? zzzzz";
ParsedSql psql3 = NamedParameterUtils.parseSqlStatement(sql3);
assertEquals("a", psql3.getParameterNames().get(0));
assertEquals("b", psql3.getParameterNames().get(1));
assertEquals("c", psql3.getParameterNames().get(2));
assertEquals("ä", psql3.getParameterNames().get(0));
assertEquals("ö", psql3.getParameterNames().get(1));
assertEquals("ü", psql3.getParameterNames().get(2));
}
@Test
@@ -117,13 +117,13 @@ public class NamedParameterUtilsTests {
}
@Test(expected = InvalidDataAccessApiUsageException.class)
public void buildValueArrayWithMissingParameterValue() throws Exception {
public void buildValueArrayWithMissingParameterValue() {
String sql = "select count(0) from foo where id = :id";
NamedParameterUtils.buildValueArray(sql, Collections.<String, Object>emptyMap());
}
@Test
public void substituteNamedParametersWithStringContainingQuotes() throws Exception {
public void substituteNamedParametersWithStringContainingQuotes() {
String expectedSql = "select 'first name' from artists where id = ? and quote = 'exsqueeze me?'";
String sql = "select 'first name' from artists where id = :id and quote = 'exsqueeze me?'";
String newSql = NamedParameterUtils.substituteNamedParameters(sql, new MapSqlParameterSource());
@@ -131,7 +131,7 @@ public class NamedParameterUtilsTests {
}
@Test
public void testParseSqlStatementWithStringContainingQuotes() throws Exception {
public void testParseSqlStatementWithStringContainingQuotes() {
String expectedSql = "select 'first name' from artists where id = ? and quote = 'exsqueeze me?'";
String sql = "select 'first name' from artists where id = :id and quote = 'exsqueeze me?'";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
@@ -173,7 +173,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-4612
public void parseSqlStatementWithPostgresCasting() throws Exception {
public 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);
@@ -181,7 +181,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-13582
public void parseSqlStatementWithPostgresContainedOperator() throws Exception {
public 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);
@@ -190,7 +190,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-15382
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() throws Exception {
public 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[]";
@@ -200,7 +200,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-15382
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() throws Exception {
public 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'";
@@ -210,7 +210,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-7476
public void parseSqlStatementWithEscapedColon() throws Exception {
public 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";
@@ -223,7 +223,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-7476
public void parseSqlStatementWithBracketDelimitedParameterNames() throws Exception {
public void parseSqlStatementWithBracketDelimitedParameterNames() {
String expectedSql = "select foo from bar where baz = b??z";
String sql = "select foo from bar where baz = b:{p1}:{p2}z";
@@ -236,7 +236,7 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-7476
public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() throws Exception {
public 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);
@@ -257,7 +257,7 @@ public class NamedParameterUtilsTests {
public void parseSqlStatementWithSingleLetterInBrackets() {
String expectedSql = "select foo from bar where baz = b?z";
String sql = "select foo from bar where baz = b:{p}z";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals(1, parsedSql.getParameterNames().size());
assertEquals("p", parsedSql.getParameterNames().get(0));
@@ -273,14 +273,14 @@ public class NamedParameterUtilsTests {
}
@Test // SPR-2544
public void substituteNamedParametersWithLogicalAnd() throws Exception {
public void substituteNamedParametersWithLogicalAnd() {
String expectedSql = "xxx & yyyy";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql);
}
@Test // SPR-3173
public void variableAssignmentOperator() throws Exception {
public void variableAssignmentOperator() {
String expectedSql = "x := 1";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql);