Browse Source

Deprecate TableMetaDataContext.getSimulationQueryForGetGeneratedKey

pull/1744/merge
Juergen Hoeller 8 years ago
parent
commit
2c7efbb9d0
  1. 46
      spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java
  2. 21
      spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java

46
spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java

@ -36,7 +36,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Class to manage context metadata used for the configuration * Class to manage context meta-data used for the configuration
* and execution of operations on a database table. * and execution of operations on a database table.
* *
* @author Thomas Risberg * @author Thomas Risberg
@ -63,13 +63,13 @@ public class TableMetaDataContext {
// List of columns objects to be used in this context // List of columns objects to be used in this context
private List<String> tableColumns = new ArrayList<>(); private List<String> tableColumns = new ArrayList<>();
// Should we access insert parameter meta data info or not // Should we access insert parameter meta-data info or not
private boolean accessTableColumnMetaData = true; private boolean accessTableColumnMetaData = true;
// Should we override default for including synonyms for meta data lookups // Should we override default for including synonyms for meta-data lookups
private boolean overrideIncludeSynonymsDefault = false; private boolean overrideIncludeSynonymsDefault = false;
// The provider of table meta data // The provider of table meta-data
@Nullable @Nullable
private TableMetaDataProvider metaDataProvider; private TableMetaDataProvider metaDataProvider;
@ -123,14 +123,14 @@ public class TableMetaDataContext {
} }
/** /**
* Specify whether we should access table column meta data. * Specify whether we should access table column meta-data.
*/ */
public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData) { public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData) {
this.accessTableColumnMetaData = accessTableColumnMetaData; this.accessTableColumnMetaData = accessTableColumnMetaData;
} }
/** /**
* Are we accessing table meta data? * Are we accessing table meta-data?
*/ */
public boolean isAccessTableColumnMetaData() { public boolean isAccessTableColumnMetaData() {
return this.accessTableColumnMetaData; return this.accessTableColumnMetaData;
@ -160,7 +160,7 @@ public class TableMetaDataContext {
/** /**
* Process the current meta data with the provided configuration options. * Process the current meta-data with the provided configuration options.
* @param dataSource the DataSource being used * @param dataSource the DataSource being used
* @param declaredColumns any columns that are declared * @param declaredColumns any columns that are declared
* @param generatedKeyNames name of generated keys * @param generatedKeyNames name of generated keys
@ -176,7 +176,7 @@ public class TableMetaDataContext {
} }
/** /**
* Compare columns created from metadata with declared columns and return a reconciled list. * Compare columns created from meta-data with declared columns and return a reconciled list.
* @param declaredColumns declared column names * @param declaredColumns declared column names
* @param generatedKeyNames names of generated key columns * @param generatedKeyNames names of generated key columns
*/ */
@ -207,7 +207,7 @@ public class TableMetaDataContext {
public List<Object> matchInParameterValuesWithInsertColumns(SqlParameterSource parameterSource) { public List<Object> matchInParameterValuesWithInsertColumns(SqlParameterSource parameterSource) {
List<Object> values = new ArrayList<>(); List<Object> values = new ArrayList<>();
// For parameter source lookups we need to provide case-insensitive lookup support since the // For parameter source lookups we need to provide case-insensitive lookup support since the
// database metadata is not necessarily providing case-sensitive column names // database meta-data is not necessarily providing case-sensitive column names
Map<String, String> caseInsensitiveParameterNames = Map<String, String> caseInsensitiveParameterNames =
SqlParameterSourceUtils.extractCaseInsensitiveParameterNames(parameterSource); SqlParameterSourceUtils.extractCaseInsensitiveParameterNames(parameterSource);
for (String column : this.tableColumns) { for (String column : this.tableColumns) {
@ -257,7 +257,7 @@ public class TableMetaDataContext {
/** /**
* Build the insert string based on configuration and metadata information * Build the insert string based on configuration and meta-data information
* @return the insert string to be used * @return the insert string to be used
*/ */
public String createInsertString(String... generatedKeyNames) { public String createInsertString(String... generatedKeyNames) {
@ -305,14 +305,13 @@ public class TableMetaDataContext {
} }
/** /**
* Build the array of {@link java.sql.Types} based on configuration and metadata information * Build the array of {@link java.sql.Types} based on configuration and meta-data information.
* @return the array of types to be used * @return the array of types to be used
*/ */
public int[] createInsertTypes() { public int[] createInsertTypes() {
int[] types = new int[getTableColumns().size()]; int[] types = new int[getTableColumns().size()];
List<TableParameterMetaData> parameters = obtainMetaDataProvider().getTableParameterMetaData(); List<TableParameterMetaData> parameters = obtainMetaDataProvider().getTableParameterMetaData();
Map<String, TableParameterMetaData> parameterMap = Map<String, TableParameterMetaData> parameterMap = new LinkedHashMap<>(parameters.size());
new LinkedHashMap<>(parameters.size());
for (TableParameterMetaData tpmd : parameters) { for (TableParameterMetaData tpmd : parameters) {
parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd); parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd);
} }
@ -337,7 +336,7 @@ public class TableMetaDataContext {
/** /**
* Does this database support the JDBC 3.0 feature of retrieving generated keys * Does this database support the JDBC 3.0 feature of retrieving generated keys:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}? * {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/ */
public boolean isGetGeneratedKeysSupported() { public boolean isGetGeneratedKeysSupported() {
@ -346,7 +345,7 @@ public class TableMetaDataContext {
/** /**
* Does this database support simple query to retrieve generated keys * Does this database support simple query to retrieve generated keys
* when the JDBC 3.0 feature is not supported. * when the JDBC 3.0 feature is not supported:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}? * {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/ */
public boolean isGetGeneratedKeysSimulated() { public boolean isGetGeneratedKeysSimulated() {
@ -354,17 +353,26 @@ public class TableMetaDataContext {
} }
/** /**
* Does this database support simple query to retrieve generated keys * @deprecated as of 4.3.15, in favor of {@link #getSimpleQueryForGetGeneratedKey}
* when the JDBC 3.0 feature is not supported.
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/ */
@Deprecated
@Nullable @Nullable
public String getSimulationQueryForGetGeneratedKey(String tableName, String keyColumnName) { public String getSimulationQueryForGetGeneratedKey(String tableName, String keyColumnName) {
return getSimpleQueryForGetGeneratedKey(tableName, keyColumnName);
}
/**
* Does this database support a simple query to retrieve generated keys
* when the JDBC 3.0 feature is not supported:
* {@link java.sql.DatabaseMetaData#supportsGetGeneratedKeys()}?
*/
@Nullable
public String getSimpleQueryForGetGeneratedKey(String tableName, String keyColumnName) {
return obtainMetaDataProvider().getSimpleQueryForGetGeneratedKey(tableName, keyColumnName); return obtainMetaDataProvider().getSimpleQueryForGetGeneratedKey(tableName, keyColumnName);
} }
/** /**
* Is a column name String array for retrieving generated keys supported? * Is a column name String array for retrieving generated keys supported:
* {@link java.sql.Connection#createStruct(String, Object[])}? * {@link java.sql.Connection#createStruct(String, Object[])}?
*/ */
public boolean isGeneratedKeysColumnNameArraySupported() { public boolean isGeneratedKeysColumnNameArraySupported() {

21
spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java

@ -50,8 +50,9 @@ import org.springframework.util.Assert;
/** /**
* Abstract class to provide base functionality for easy inserts * Abstract class to provide base functionality for easy inserts
* based on configuration options and database metadata. * based on configuration options and database meta-data.
* This class provides the base SPI for {@link SimpleJdbcInsert}. *
* <p>This class provides the base SPI for {@link SimpleJdbcInsert}.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Juergen Hoeller * @author Juergen Hoeller
@ -65,7 +66,7 @@ public abstract class AbstractJdbcInsert {
/** Lower-level class used to execute SQL */ /** Lower-level class used to execute SQL */
private final JdbcTemplate jdbcTemplate; private final JdbcTemplate jdbcTemplate;
/** Context used to retrieve and manage database metadata */ /** Context used to retrieve and manage database meta-data */
private final TableMetaDataContext tableMetaDataContext = new TableMetaDataContext(); private final TableMetaDataContext tableMetaDataContext = new TableMetaDataContext();
/** List of columns objects to be used in insert statement */ /** List of columns objects to be used in insert statement */
@ -204,7 +205,7 @@ public abstract class AbstractJdbcInsert {
} }
/** /**
* Specify whether the parameter metadata for the call should be used. * Specify whether the parameter meta-data for the call should be used.
* The default is {@code true}. * The default is {@code true}.
*/ */
public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData) { public void setAccessTableColumnMetaData(boolean accessTableColumnMetaData) {
@ -239,7 +240,7 @@ public abstract class AbstractJdbcInsert {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
/** /**
* Compile this JdbcInsert using provided parameters and meta data plus other settings. * Compile this JdbcInsert using provided parameters and meta-data plus other settings.
* This finalizes the configuration for this object and subsequent attempts to compile are * This finalizes the configuration for this object and subsequent attempts to compile are
* ignored. This will be implicitly called the first time an un-compiled insert is executed. * ignored. This will be implicitly called the first time an un-compiled insert is executed.
* @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized, * @throws InvalidDataAccessApiUsageException if the object hasn't been correctly initialized,
@ -315,7 +316,7 @@ public abstract class AbstractJdbcInsert {
protected void checkIfConfigurationModificationIsAllowed() { protected void checkIfConfigurationModificationIsAllowed() {
if (isCompiled()) { if (isCompiled()) {
throw new InvalidDataAccessApiUsageException( throw new InvalidDataAccessApiUsageException(
"Configuration can't be altered once the class has been compiled or used"); "Configuration cannot be altered once the class has been compiled or used");
} }
} }
@ -453,9 +454,9 @@ public abstract class AbstractJdbcInsert {
} }
Assert.state(getTableName() != null, "No table name set"); Assert.state(getTableName() != null, "No table name set");
final String keyQuery = this.tableMetaDataContext.getSimulationQueryForGetGeneratedKey( final String keyQuery = this.tableMetaDataContext.getSimpleQueryForGetGeneratedKey(
getTableName(), getGeneratedKeyNames()[0]); getTableName(), getGeneratedKeyNames()[0]);
Assert.state(keyQuery != null, "Query for simulating get generated keys can't be null"); Assert.state(keyQuery != null, "Query for simulating get generated keys must not be null");
// This is a hack to be able to get the generated key from a database that doesn't support // This is a hack to be able to get the generated key from a database that doesn't support
// get generated keys feature. HSQL is one, PostgreSQL is another. Postgres uses a RETURNING // get generated keys feature. HSQL is one, PostgreSQL is another. Postgres uses a RETURNING
@ -602,7 +603,7 @@ public abstract class AbstractJdbcInsert {
/** /**
* Match the provided in parameter values with registered parameters and parameters * Match the provided in parameter values with registered parameters and parameters
* defined via metadata processing. * defined via meta-data processing.
* @param parameterSource the parameter values provided as a {@link SqlParameterSource} * @param parameterSource the parameter values provided as a {@link SqlParameterSource}
* @return Map with parameter names and values * @return Map with parameter names and values
*/ */
@ -612,7 +613,7 @@ public abstract class AbstractJdbcInsert {
/** /**
* Match the provided in parameter values with registered parameters and parameters * Match the provided in parameter values with registered parameters and parameters
* defined via metadata processing. * defined via meta-data processing.
* @param args the parameter values provided in a Map * @param args the parameter values provided in a Map
* @return Map with parameter names and values * @return Map with parameter names and values
*/ */

Loading…
Cancel
Save