This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push: new 51d30a80 Internal refactoring 51d30a80 is described below commit 51d30a80b5ecca70534d89b480770535d13bf16f Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sat Jul 9 18:34:08 2022 -0400 Internal refactoring --- .../java/org/apache/commons/dbcp2/PStmtKey.java | 477 ++++++++------------- 1 file changed, 179 insertions(+), 298 deletions(-) diff --git a/src/main/java/org/apache/commons/dbcp2/PStmtKey.java b/src/main/java/org/apache/commons/dbcp2/PStmtKey.java index 65858fc4..f2f39f04 100644 --- a/src/main/java/org/apache/commons/dbcp2/PStmtKey.java +++ b/src/main/java/org/apache/commons/dbcp2/PStmtKey.java @@ -21,6 +21,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.Arrays; import java.util.Objects; +import java.util.function.Supplier; import org.apache.commons.dbcp2.PoolingConnection.StatementType; @@ -128,14 +129,30 @@ public class PStmtKey { Statement createStatement(Connection connection) throws SQLException; } + private static StatementBuilder toStatementBuilder(final StatementType statementType, final Supplier<StatementBuilder> prep, + final Supplier<StatementBuilder> call) { + if (statementType != null) { + switch (statementType) { + case PREPARED_STATEMENT: + return prep.get(); + case CALLABLE_STATEMENT: + return call.get(); + default: + // TODO Throw? + return null; + } + } + return null; + } + /** * SQL defining Prepared or Callable Statement */ private final String sql; /** - * Result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, - * or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * Result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or + * {@code ResultSet.TYPE_SCROLL_SENSITIVE}. */ private final Integer resultSetType; @@ -146,8 +163,8 @@ public class PStmtKey { private final Integer resultSetConcurrency; /** - * Result set holdability. One of the following {@code ResultSet} constants: - * {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. + * Result set holdability. One of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} + * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. */ private final Integer resultSetHoldability; @@ -158,8 +175,8 @@ public class PStmtKey { private final String schema; /** - * A flag indicating whether auto-generated keys should be returned; one of - * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. + * A flag indicating whether auto-generated keys should be returned; one of {@code Statement.RETURN_GENERATED_KEYS} or + * {@code Statement.NO_GENERATED_KEYS}. */ private final Integer autoGeneratedKeys; @@ -173,19 +190,20 @@ public class PStmtKey { */ private final String[] columnNames; + /** + * Statement builder. + */ + private final transient StatementBuilder statementBuilder; + /** * Statement type, prepared or callable. */ private final StatementType statementType; - /** Statement builder */ - private transient StatementBuilder builder; - /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. + * @param sql The SQL statement. * @deprecated Use {@link #PStmtKey(String, String, String)}. */ @Deprecated @@ -196,14 +214,11 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param resultSetType - * A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. + * @param sql The SQL statement. + * @param resultSetType A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. * @deprecated Use {@link #PStmtKey(String, String, String, int, int)}. */ @Deprecated @@ -214,10 +229,8 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. + * @param sql The SQL statement. + * @param catalog The catalog. * @deprecated Use {@link #PStmtKey(String, String, String)}. */ @Deprecated @@ -228,13 +241,10 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param autoGeneratedKeys - * A flag indicating whether auto-generated keys should be returned; one of - * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param autoGeneratedKeys A flag indicating whether auto-generated keys should be returned; one of + * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. * @deprecated Use {@link #PStmtKey(String, String, String, int)}. */ @Deprecated @@ -245,16 +255,12 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param resultSetType - * A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param resultSetType A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. * @deprecated Use @link {@link #PStmtKey(String, String, String, int, int)}. */ @Deprecated @@ -265,50 +271,38 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param resultSetType - * a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE} - * @param resultSetHoldability - * One of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} - * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param resultSetType a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE} + * @param resultSetHoldability One of the following {@code ResultSet} constants: + * {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. * @deprecated Use {@link #PStmtKey(String, String, String, int, int, int)}. */ @Deprecated - public PStmtKey(final String sql, final String catalog, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) { + public PStmtKey(final String sql, final String catalog, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) { this(sql, catalog, resultSetType, resultSetConcurrency, resultSetHoldability, StatementType.PREPARED_STATEMENT); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param resultSetType - * a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE} - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. - * @param resultSetHoldability - * One of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} - * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. - * @param statementType - * The SQL statement type, prepared or callable. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param resultSetType a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE} + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. + * @param resultSetHoldability One of the following {@code ResultSet} constants: + * {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. + * @param statementType The SQL statement type, prepared or callable. * @deprecated Use {@link #PStmtKey(String, String, String, int, int, int, PoolingConnection.StatementType)} */ @Deprecated - public PStmtKey(final String sql, final String catalog, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability, final StatementType statementType) { + public PStmtKey(final String sql, final String catalog, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability, + final StatementType statementType) { this.sql = sql; this.catalog = catalog; this.schema = null; @@ -319,34 +313,23 @@ public class PStmtKey { this.autoGeneratedKeys = null; this.columnIndexes = null; this.columnNames = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementWithResultSetHoldability(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallWithResultSetHoldability(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementWithResultSetHoldability::new, PreparedCallWithResultSetHoldability::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param resultSetType - * A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. - * @param statementType - * The SQL statement type, prepared or callable. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param resultSetType A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. + * @param statementType The SQL statement type, prepared or callable. * @deprecated Use {@link #PStmtKey(String, String, String, int, int, PoolingConnection.StatementType)}. */ @Deprecated - public PStmtKey(final String sql, final String catalog, final int resultSetType, final int resultSetConcurrency, - final StatementType statementType) { + public PStmtKey(final String sql, final String catalog, final int resultSetType, final int resultSetConcurrency, final StatementType statementType) { this.sql = sql; this.catalog = catalog; this.schema = null; @@ -357,24 +340,16 @@ public class PStmtKey { this.autoGeneratedKeys = null; this.columnIndexes = null; this.columnNames = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementWithResultSetConcurrency(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallWithResultSetConcurrency(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementWithResultSetConcurrency::new, PreparedCallWithResultSetConcurrency::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param columnIndexes - * An array of column indexes indicating the columns that should be returned from the inserted row or - * rows. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param columnIndexes An array of column indexes indicating the columns that should be returned from the inserted row + * or rows. * @deprecated Use {@link #PStmtKey(String, String, String, int[])}. */ @Deprecated @@ -389,19 +364,15 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - this.builder = new PreparedStatementWithColumnIndexes(); + this.statementBuilder = new PreparedStatementWithColumnIndexes(); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param statementType - * The SQL statement type, prepared or callable. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param statementType The SQL statement type, prepared or callable. * @deprecated Use {@link #PStmtKey(String, String, String, PoolingConnection.StatementType)}. */ @Deprecated @@ -416,31 +387,21 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementSQL(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallSQL(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementSQL::new, PreparedCallSQL::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param statementType - * The SQL statement type, prepared or callable. - * @param autoGeneratedKeys - * A flag indicating whether auto-generated keys should be returned; one of - * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param statementType The SQL statement type, prepared or callable. + * @param autoGeneratedKeys A flag indicating whether auto-generated keys should be returned; one of + * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. * @deprecated Use {@link #PStmtKey(String, String, String, PoolingConnection.StatementType, Integer)} */ @Deprecated - public PStmtKey(final String sql, final String catalog, final StatementType statementType, - final Integer autoGeneratedKeys) { + public PStmtKey(final String sql, final String catalog, final StatementType statementType, final Integer autoGeneratedKeys) { this.sql = sql; this.catalog = catalog; this.schema = null; @@ -451,23 +412,15 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementWithAutoGeneratedKeys(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallSQL(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementWithAutoGeneratedKeys::new, PreparedCallSQL::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema) { @@ -477,15 +430,11 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema - * @param autoGeneratedKeys - * A flag indicating whether auto-generated keys should be returned; one of - * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema + * @param autoGeneratedKeys A flag indicating whether auto-generated keys should be returned; one of + * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema, final int autoGeneratedKeys) { @@ -495,18 +444,13 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema - * @param resultSetType - * A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema + * @param resultSetType A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. */ public PStmtKey(final String sql, final String catalog, final String schema, final int resultSetType, final int resultSetConcurrency) { this(sql, catalog, schema, resultSetType, resultSetConcurrency, StatementType.PREPARED_STATEMENT); @@ -515,52 +459,39 @@ public class PStmtKey { /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema - * @param resultSetType - * a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE} - * @param resultSetHoldability - * One of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} - * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema + * @param resultSetType a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE} + * @param resultSetHoldability One of the following {@code ResultSet} constants: + * {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) { + final int resultSetHoldability) { this(sql, catalog, schema, resultSetType, resultSetConcurrency, resultSetHoldability, StatementType.PREPARED_STATEMENT); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema. - * @param resultSetType - * a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE} - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. - * @param resultSetHoldability - * One of the following {@code ResultSet} constants: {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} - * or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. - * @param statementType - * The SQL statement type, prepared or callable. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema. + * @param resultSetType a result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE} + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. + * @param resultSetHoldability One of the following {@code ResultSet} constants: + * {@code ResultSet.HOLD_CURSORS_OVER_COMMIT} or {@code ResultSet.CLOSE_CURSORS_AT_COMMIT}. + * @param statementType The SQL statement type, prepared or callable. * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability, final StatementType statementType) { + final int resultSetHoldability, final StatementType statementType) { this.sql = sql; this.catalog = catalog; this.schema = schema; @@ -571,35 +502,24 @@ public class PStmtKey { this.autoGeneratedKeys = null; this.columnIndexes = null; this.columnNames = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementWithResultSetHoldability(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallWithResultSetHoldability(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementWithResultSetHoldability::new, PreparedCallWithResultSetHoldability::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema. - * @param resultSetType - * A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. - * @param resultSetConcurrency - * A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or - * {@code ResultSet.CONCUR_UPDATABLE}. - * @param statementType - * The SQL statement type, prepared or callable. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema. + * @param resultSetType A result set type; one of {@code ResultSet.TYPE_FORWARD_ONLY}, + * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * @param resultSetConcurrency A concurrency type; one of {@code ResultSet.CONCUR_READ_ONLY} or + * {@code ResultSet.CONCUR_UPDATABLE}. + * @param statementType The SQL statement type, prepared or callable. * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema, final int resultSetType, final int resultSetConcurrency, - final StatementType statementType) { + final StatementType statementType) { this.sql = sql; this.catalog = catalog; this.schema = schema; @@ -610,26 +530,17 @@ public class PStmtKey { this.autoGeneratedKeys = null; this.columnIndexes = null; this.columnNames = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementWithResultSetConcurrency(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallWithResultSetConcurrency(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementWithResultSetConcurrency::new, PreparedCallWithResultSetConcurrency::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema. - * @param columnIndexes - * An array of column indexes indicating the columns that should be returned from the inserted row or - * rows. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema. + * @param columnIndexes An array of column indexes indicating the columns that should be returned from the inserted row + * or rows. */ public PStmtKey(final String sql, final String catalog, final String schema, final int[] columnIndexes) { this.sql = sql; @@ -642,21 +553,16 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - this.builder = new PreparedStatementWithColumnIndexes(); + this.statementBuilder = new PreparedStatementWithColumnIndexes(); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema. - * @param statementType - * The SQL statement type, prepared or callable. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema. + * @param statementType The SQL statement type, prepared or callable. * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema, final StatementType statementType) { @@ -670,32 +576,21 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementSQL(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallSQL(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementSQL::new, PreparedCallSQL::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema. - * @param statementType - * The SQL statement type, prepared or callable. - * @param autoGeneratedKeys - * A flag indicating whether auto-generated keys should be returned; one of - * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema. + * @param statementType The SQL statement type, prepared or callable. + * @param autoGeneratedKeys A flag indicating whether auto-generated keys should be returned; one of + * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. * @since 2.5.0 */ - public PStmtKey(final String sql, final String catalog, final String schema, final StatementType statementType, - final Integer autoGeneratedKeys) { + public PStmtKey(final String sql, final String catalog, final String schema, final StatementType statementType, final Integer autoGeneratedKeys) { this.sql = sql; this.catalog = catalog; this.schema = schema; @@ -706,25 +601,17 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - if (statementType == StatementType.PREPARED_STATEMENT) { - this.builder = new PreparedStatementWithAutoGeneratedKeys(); - } else if (statementType == StatementType.CALLABLE_STATEMENT) { - this.builder = new PreparedCallSQL(); - } + this.statementBuilder = toStatementBuilder(statementType, PreparedStatementWithAutoGeneratedKeys::new, PreparedCallSQL::new); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param schema - * The schema. - * @param columnNames - * An array of column names indicating the columns that should be returned from the inserted row or rows. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param schema The schema. + * @param columnNames An array of column names indicating the columns that should be returned from the inserted row or + * rows. * @since 2.5.0 */ public PStmtKey(final String sql, final String catalog, final String schema, final String[] columnNames) { @@ -738,19 +625,16 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - builder = new PreparedStatementWithColumnNames(); + this.statementBuilder = new PreparedStatementWithColumnNames(); } /** * Constructs a key to uniquely identify a prepared statement. * - * @param sql - * The SQL statement. - * @param catalog - * The catalog. - * @param columnNames - * An array of column names indicating the columns that should be returned from the inserted row or rows. + * @param sql The SQL statement. + * @param catalog The catalog. + * @param columnNames An array of column names indicating the columns that should be returned from the inserted row or + * rows. * @deprecated Use {@link #PStmtKey(String, String, String, String[])}. */ @Deprecated @@ -765,24 +649,21 @@ public class PStmtKey { this.resultSetType = null; this.resultSetConcurrency = null; this.resultSetHoldability = null; - // create builder - builder = new PreparedStatementWithColumnNames(); + this.statementBuilder = new PreparedStatementWithColumnNames(); } /** * Creates a new Statement from the given Connection. * - * @param connection - * The Connection to use to create the statement. + * @param connection The Connection to use to create the statement. * @return The statement. - * @throws SQLException - * Thrown when there is a problem creating the statement. + * @throws SQLException Thrown when there is a problem creating the statement. */ public Statement createStatement(final Connection connection) throws SQLException { - if (builder == null) { + if (statementBuilder == null) { throw new IllegalStateException("Prepared statement key is invalid."); } - return builder.createStatement(connection); + return statementBuilder.createStatement(connection); } @Override @@ -828,8 +709,8 @@ public class PStmtKey { } /** - * Gets a flag indicating whether auto-generated keys should be returned; one of - * {@code Statement.RETURN_GENERATED_KEYS} or {@code Statement.NO_GENERATED_KEYS}. + * Gets a flag indicating whether auto-generated keys should be returned; one of {@code Statement.RETURN_GENERATED_KEYS} + * or {@code Statement.NO_GENERATED_KEYS}. * * @return a flag indicating whether auto-generated keys should be returned. */ @@ -885,8 +766,8 @@ public class PStmtKey { } /** - * Gets the result set type, one of {@code ResultSet.TYPE_FORWARD_ONLY}, - * {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or {@code ResultSet.TYPE_SCROLL_SENSITIVE}. + * Gets the result set type, one of {@code ResultSet.TYPE_FORWARD_ONLY}, {@code ResultSet.TYPE_SCROLL_INSENSITIVE}, or + * {@code ResultSet.TYPE_SCROLL_SENSITIVE}. * * @return the result set type. */ @@ -923,8 +804,8 @@ public class PStmtKey { @Override public int hashCode() { - return Objects.hash(autoGeneratedKeys, catalog, Arrays.hashCode(columnIndexes), Arrays.hashCode(columnNames), - resultSetConcurrency, resultSetHoldability, resultSetType, schema, sql, statementType); + return Objects.hash(autoGeneratedKeys, catalog, Arrays.hashCode(columnIndexes), Arrays.hashCode(columnNames), resultSetConcurrency, + resultSetHoldability, resultSetType, schema, sql, statementType); } @Override