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 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -31,16 +31,14 @@ import org.springframework.jdbc.support.JdbcUtils; @@ -31,16 +31,14 @@ import org.springframework.jdbc.support.JdbcUtils;
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
* @since 2.5
*/
public class CallMetaDataProviderFactory {
/** Logger */
private static final Log logger = LogFactory.getLog(CallMetaDataProviderFactory.class);
/** List of supported database products for procedure calls */
public static final List<String> supportedDatabaseProductsForProcedures = Arrays.asList(
"Apache Derby",
@ -51,6 +49,7 @@ public class CallMetaDataProviderFactory { @@ -51,6 +49,7 @@ public class CallMetaDataProviderFactory {
"PostgreSQL",
"Sybase"
);
/** List of supported database products for function calls */
public static final List<String> supportedDatabaseProductsForFunctions = Arrays.asList(
"MySQL",
@ -59,6 +58,9 @@ public class CallMetaDataProviderFactory { @@ -59,6 +58,9 @@ public class CallMetaDataProviderFactory {
"PostgreSQL"
);
private static final Log logger = LogFactory.getLog(CallMetaDataProviderFactory.class);
/**
* Create a CallMetaDataProvider based on the database metadata
* @param dataSource used to retrieve metadata
@ -124,17 +126,16 @@ public class CallMetaDataProviderFactory { @@ -124,17 +126,16 @@ public class CallMetaDataProviderFactory {
}
provider.initializeWithMetaData(databaseMetaData);
if (accessProcedureColumnMetaData) {
provider.initializeWithProcedureColumnMetaData(
databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getProcedureName());
provider.initializeWithProcedureColumnMetaData(databaseMetaData,
context.getCatalogName(), context.getSchemaName(), context.getProcedureName());
}
return provider;
}
});
}
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 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -70,7 +70,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider { @@ -70,7 +70,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
Arrays.asList("Apache Derby", "HSQL Database Engine");
/** 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 */
private NativeJdbcExtractor nativeJdbcExtractor;
@ -109,7 +109,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider { @@ -109,7 +109,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
@Override
public List<TableParameterMetaData> getTableParameterMetaData() {
return this.insertParameterMetaData;
return this.tableParameterMetaData;
}
@Override
@ -376,17 +376,14 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider { @@ -376,17 +376,14 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
}
try {
tableColumns = databaseMetaData.getColumns(
metaDataCatalogName,
metaDataSchemaName,
metaDataTableName,
null);
metaDataCatalogName, metaDataSchemaName, metaDataTableName, null);
while (tableColumns.next()) {
String columnName = tableColumns.getString("COLUMN_NAME");
int dataType = tableColumns.getInt("DATA_TYPE");
if (dataType == Types.DECIMAL) {
String typeName = tableColumns.getString("TYPE_NAME");
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
// using DECIMAL for certain inserts (see SPR-6912))
if ("NUMBER".equals(typeName) && decimalDigits == 0) {
@ -400,18 +397,11 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider { @@ -400,18 +397,11 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
}
}
boolean nullable = tableColumns.getBoolean("NULLABLE");
TableParameterMetaData meta = new TableParameterMetaData(
columnName,
dataType,
nullable
);
this.insertParameterMetaData.add(meta);
TableParameterMetaData meta = new TableParameterMetaData(columnName, dataType, nullable);
this.tableParameterMetaData.add(meta);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved metadata: "
+ meta.getParameterName() +
" " + meta.getSqlType() +
" " + meta.isNullable()
);
logger.debug("Retrieved metadata: " + meta.getParameterName() +
" " + meta.getSqlType() + " " + meta.isNullable());
}
}
}

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -202,7 +202,8 @@ public class TableMetaDataContext { @@ -202,7 +202,8 @@ public class TableMetaDataContext {
* @param generatedKeyNames name of generated keys
*/
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);
}
@ -299,14 +300,14 @@ public class TableMetaDataContext { @@ -299,14 +300,14 @@ public class TableMetaDataContext {
}
StringBuilder insertStatement = new StringBuilder();
insertStatement.append("INSERT INTO ");
if (this.getSchemaName() != null) {
insertStatement.append(this.getSchemaName());
if (getSchemaName() != null) {
insertStatement.append(getSchemaName());
insertStatement.append(".");
}
insertStatement.append(this.getTableName());
insertStatement.append(getTableName());
insertStatement.append(" (");
int columnCount = 0;
for (String columnName : this.getTableColumns()) {
for (String columnName : getTableColumns()) {
if (!keys.contains(columnName.toUpperCase())) {
columnCount++;
if (columnCount > 1) {
@ -319,11 +320,11 @@ public class TableMetaDataContext { @@ -319,11 +320,11 @@ public class TableMetaDataContext {
if (columnCount < 1) {
if (this.generatedKeyColumnsUsed) {
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 {
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++) {
@ -341,7 +342,7 @@ public class TableMetaDataContext { @@ -341,7 +342,7 @@ public class TableMetaDataContext {
* @return the array of types to be used
*/
public int[] createInsertTypes() {
int[] types = new int[this.getTableColumns().size()];
int[] types = new int[getTableColumns().size()];
List<TableParameterMetaData> parameters = this.metaDataProvider.getTableParameterMetaData();
Map<String, TableParameterMetaData> parameterMap =
new LinkedHashMap<String, TableParameterMetaData>(parameters.size());
@ -349,7 +350,7 @@ public class TableMetaDataContext { @@ -349,7 +350,7 @@ public class TableMetaDataContext {
parameterMap.put(tpmd.getParameterName().toUpperCase(), tpmd);
}
int typeIndx = 0;
for (String column : this.getTableColumns()) {
for (String column : getTableColumns()) {
if (column == null) {
types[typeIndx] = SqlTypeValue.TYPE_UNKNOWN;
}

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -30,7 +30,8 @@ import org.springframework.jdbc.support.MetaDataAccessException; @@ -30,7 +30,8 @@ import org.springframework.jdbc.support.MetaDataAccessException;
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
* @since 2.5

Loading…
Cancel
Save