Browse Source

Polishing

pull/778/head
Juergen Hoeller 10 years ago
parent
commit
cddcf3637d
  1. 19
      spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProviderFactory.java
  2. 28
      spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java
  3. 21
      spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java
  4. 5
      spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataProviderFactory.java

19
spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProviderFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,16 +31,14 @@ import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.jdbc.support.MetaDataAccessException;
/** /**
* Factory used to create a {@link CallMetaDataProvider} implementation based on the type of databse being used. * Factory used to create a {@link CallMetaDataProvider} implementation
* based on the type of databse being used.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @since 2.5 * @since 2.5
*/ */
public class CallMetaDataProviderFactory { public class CallMetaDataProviderFactory {
/** Logger */
private static final Log logger = LogFactory.getLog(CallMetaDataProviderFactory.class);
/** List of supported database products for procedure calls */ /** List of supported database products for procedure calls */
public static final List<String> supportedDatabaseProductsForProcedures = Arrays.asList( public static final List<String> supportedDatabaseProductsForProcedures = Arrays.asList(
"Apache Derby", "Apache Derby",
@ -51,6 +49,7 @@ public class CallMetaDataProviderFactory {
"PostgreSQL", "PostgreSQL",
"Sybase" "Sybase"
); );
/** List of supported database products for function calls */ /** List of supported database products for function calls */
public static final List<String> supportedDatabaseProductsForFunctions = Arrays.asList( public static final List<String> supportedDatabaseProductsForFunctions = Arrays.asList(
"MySQL", "MySQL",
@ -59,6 +58,9 @@ public class CallMetaDataProviderFactory {
"PostgreSQL" "PostgreSQL"
); );
private static final Log logger = LogFactory.getLog(CallMetaDataProviderFactory.class);
/** /**
* Create a CallMetaDataProvider based on the database metadata * Create a CallMetaDataProvider based on the database metadata
* @param dataSource used to retrieve metadata * @param dataSource used to retrieve metadata
@ -124,17 +126,16 @@ public class CallMetaDataProviderFactory {
} }
provider.initializeWithMetaData(databaseMetaData); provider.initializeWithMetaData(databaseMetaData);
if (accessProcedureColumnMetaData) { if (accessProcedureColumnMetaData) {
provider.initializeWithProcedureColumnMetaData( provider.initializeWithProcedureColumnMetaData(databaseMetaData,
databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getProcedureName()); context.getCatalogName(), context.getSchemaName(), context.getProcedureName());
} }
return provider; return provider;
} }
}); });
} }
catch (MetaDataAccessException ex) { catch (MetaDataAccessException ex) {
throw new DataAccessResourceFailureException("Error retreiving database metadata", ex); throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);
} }
} }
} }

28
spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -70,7 +70,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
Arrays.asList("Apache Derby", "HSQL Database Engine"); Arrays.asList("Apache Derby", "HSQL Database Engine");
/** Collection of TableParameterMetaData objects */ /** Collection of TableParameterMetaData objects */
private List<TableParameterMetaData> insertParameterMetaData = new ArrayList<TableParameterMetaData>(); private List<TableParameterMetaData> tableParameterMetaData = new ArrayList<TableParameterMetaData>();
/** NativeJdbcExtractor that can be used to retrieve the native connection */ /** NativeJdbcExtractor that can be used to retrieve the native connection */
private NativeJdbcExtractor nativeJdbcExtractor; private NativeJdbcExtractor nativeJdbcExtractor;
@ -109,7 +109,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
@Override @Override
public List<TableParameterMetaData> getTableParameterMetaData() { public List<TableParameterMetaData> getTableParameterMetaData() {
return this.insertParameterMetaData; return this.tableParameterMetaData;
} }
@Override @Override
@ -376,17 +376,14 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
} }
try { try {
tableColumns = databaseMetaData.getColumns( tableColumns = databaseMetaData.getColumns(
metaDataCatalogName, metaDataCatalogName, metaDataSchemaName, metaDataTableName, null);
metaDataSchemaName,
metaDataTableName,
null);
while (tableColumns.next()) { while (tableColumns.next()) {
String columnName = tableColumns.getString("COLUMN_NAME"); String columnName = tableColumns.getString("COLUMN_NAME");
int dataType = tableColumns.getInt("DATA_TYPE"); int dataType = tableColumns.getInt("DATA_TYPE");
if (dataType == Types.DECIMAL) { if (dataType == Types.DECIMAL) {
String typeName = tableColumns.getString("TYPE_NAME"); String typeName = tableColumns.getString("TYPE_NAME");
int decimalDigits = tableColumns.getInt("DECIMAL_DIGITS"); int decimalDigits = tableColumns.getInt("DECIMAL_DIGITS");
// override a DECIMAL data type for no-decimal numerics // Override a DECIMAL data type for no-decimal numerics
// (this is for better Oracle support where there have been issues // (this is for better Oracle support where there have been issues
// using DECIMAL for certain inserts (see SPR-6912)) // using DECIMAL for certain inserts (see SPR-6912))
if ("NUMBER".equals(typeName) && decimalDigits == 0) { if ("NUMBER".equals(typeName) && decimalDigits == 0) {
@ -400,18 +397,11 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
} }
} }
boolean nullable = tableColumns.getBoolean("NULLABLE"); boolean nullable = tableColumns.getBoolean("NULLABLE");
TableParameterMetaData meta = new TableParameterMetaData( TableParameterMetaData meta = new TableParameterMetaData(columnName, dataType, nullable);
columnName, this.tableParameterMetaData.add(meta);
dataType,
nullable
);
this.insertParameterMetaData.add(meta);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Retrieved metadata: " logger.debug("Retrieved metadata: " + meta.getParameterName() +
+ meta.getParameterName() + " " + meta.getSqlType() + " " + meta.isNullable());
" " + meta.getSqlType() +
" " + meta.isNullable()
);
} }
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -202,7 +202,8 @@ public class TableMetaDataContext {
* @param generatedKeyNames name of generated keys * @param generatedKeyNames name of generated keys
*/ */
public void processMetaData(DataSource dataSource, List<String> declaredColumns, String[] generatedKeyNames) { public void processMetaData(DataSource dataSource, List<String> declaredColumns, String[] generatedKeyNames) {
this.metaDataProvider = TableMetaDataProviderFactory.createMetaDataProvider(dataSource, this, this.nativeJdbcExtractor); this.metaDataProvider =
TableMetaDataProviderFactory.createMetaDataProvider(dataSource, this, this.nativeJdbcExtractor);
this.tableColumns = reconcileColumnsToUse(declaredColumns, generatedKeyNames); this.tableColumns = reconcileColumnsToUse(declaredColumns, generatedKeyNames);
} }
@ -299,14 +300,14 @@ public class TableMetaDataContext {
} }
StringBuilder insertStatement = new StringBuilder(); StringBuilder insertStatement = new StringBuilder();
insertStatement.append("INSERT INTO "); insertStatement.append("INSERT INTO ");
if (this.getSchemaName() != null) { if (getSchemaName() != null) {
insertStatement.append(this.getSchemaName()); insertStatement.append(getSchemaName());
insertStatement.append("."); insertStatement.append(".");
} }
insertStatement.append(this.getTableName()); insertStatement.append(getTableName());
insertStatement.append(" ("); insertStatement.append(" (");
int columnCount = 0; int columnCount = 0;
for (String columnName : this.getTableColumns()) { for (String columnName : getTableColumns()) {
if (!keys.contains(columnName.toUpperCase())) { if (!keys.contains(columnName.toUpperCase())) {
columnCount++; columnCount++;
if (columnCount > 1) { if (columnCount > 1) {
@ -319,11 +320,11 @@ public class TableMetaDataContext {
if (columnCount < 1) { if (columnCount < 1) {
if (this.generatedKeyColumnsUsed) { if (this.generatedKeyColumnsUsed) {
logger.info("Unable to locate non-key columns for table '" + logger.info("Unable to locate non-key columns for table '" +
this.getTableName() + "' so an empty insert statement is generated"); getTableName() + "' so an empty insert statement is generated");
} }
else { else {
throw new InvalidDataAccessApiUsageException("Unable to locate columns for table '" + throw new InvalidDataAccessApiUsageException("Unable to locate columns for table '" +
this.getTableName() + "' so an insert statement can't be generated"); getTableName() + "' so an insert statement can't be generated");
} }
} }
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
@ -341,7 +342,7 @@ public class TableMetaDataContext {
* @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[this.getTableColumns().size()]; int[] types = new int[getTableColumns().size()];
List<TableParameterMetaData> parameters = this.metaDataProvider.getTableParameterMetaData(); List<TableParameterMetaData> parameters = this.metaDataProvider.getTableParameterMetaData();
Map<String, TableParameterMetaData> parameterMap = Map<String, TableParameterMetaData> parameterMap =
new LinkedHashMap<String, TableParameterMetaData>(parameters.size()); new LinkedHashMap<String, TableParameterMetaData>(parameters.size());
@ -349,7 +350,7 @@ public class TableMetaDataContext {
parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd); parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd);
} }
int typeIndx = 0; int typeIndx = 0;
for (String column : this.getTableColumns()) { for (String column : getTableColumns()) {
if (column == null) { if (column == null) {
types[typeIndx] = SqlTypeValue.TYPE_UNKNOWN; types[typeIndx] = SqlTypeValue.TYPE_UNKNOWN;
} }

5
spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataProviderFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,7 +30,8 @@ import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor; import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
/** /**
* Factory used to create a {@link TableMetaDataProvider} implementation based on the type of databse being used. * Factory used to create a {@link TableMetaDataProvider} implementation
* based on the type of databse being used.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @since 2.5 * @since 2.5

Loading…
Cancel
Save