From cddcf3637d1bb035a47e09f1d669040507025047 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 16 Apr 2015 21:54:03 +0200 Subject: [PATCH] Polishing --- .../metadata/CallMetaDataProviderFactory.java | 19 +++++++------ .../GenericTableMetaDataProvider.java | 28 ++++++------------- .../core/metadata/TableMetaDataContext.java | 21 +++++++------- .../TableMetaDataProviderFactory.java | 5 ++-- 4 files changed, 33 insertions(+), 40 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProviderFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProviderFactory.java index a76e56e933..be91db1556 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProviderFactory.java +++ b/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"); * 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; /** - * 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 supportedDatabaseProductsForProcedures = Arrays.asList( "Apache Derby", @@ -51,6 +49,7 @@ public class CallMetaDataProviderFactory { "PostgreSQL", "Sybase" ); + /** List of supported database products for function calls */ public static final List supportedDatabaseProductsForFunctions = Arrays.asList( "MySQL", @@ -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 { } 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); } - } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java index 24e66c42fe..ebc4ec87fc 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/GenericTableMetaDataProvider.java +++ b/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"); * 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"); /** Collection of TableParameterMetaData objects */ - private List insertParameterMetaData = new ArrayList(); + private List tableParameterMetaData = new ArrayList(); /** NativeJdbcExtractor that can be used to retrieve the native connection */ private NativeJdbcExtractor nativeJdbcExtractor; @@ -109,7 +109,7 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider { @Override public List getTableParameterMetaData() { - return this.insertParameterMetaData; + return this.tableParameterMetaData; } @Override @@ -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 { } } 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()); } } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java index c8aaf23b6b..63ea39ec76 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataContext.java +++ b/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"); * 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 */ public void processMetaData(DataSource dataSource, List 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 { } 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 { 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 { * @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 parameters = this.metaDataProvider.getTableParameterMetaData(); Map parameterMap = new LinkedHashMap(parameters.size()); @@ -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; } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataProviderFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataProviderFactory.java index 36bc50ed84..2d9c5b5476 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/TableMetaDataProviderFactory.java +++ b/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"); * 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; /** - * 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