Skip to content

Commit 82515a3

Browse files
committed
Consistent incrementer arrangement for PostgreSQL, DB2 and SAP HANA
Includes related polishing in core.metadata and datasource.embedded and a revision of the corresponding database definitions in sql-error-codes. Issue: SPR-16558
1 parent 4a4f2c2 commit 82515a3

30 files changed

+423
-167
lines changed

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,7 +67,7 @@ public class CallMetaDataProviderFactory {
6767
*/
6868
static public CallMetaDataProvider createMetaDataProvider(DataSource dataSource, final CallMetaDataContext context) {
6969
try {
70-
CallMetaDataProvider result = (CallMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
70+
return (CallMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
7171
String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
7272
boolean accessProcedureColumnMetaData = context.isAccessCallParameterMetaData();
7373
if (context.isFunction()) {
@@ -94,31 +94,33 @@ static public CallMetaDataProvider createMetaDataProvider(DataSource dataSource,
9494
}
9595
}
9696
}
97+
9798
CallMetaDataProvider provider;
9899
if ("Oracle".equals(databaseProductName)) {
99100
provider = new OracleCallMetaDataProvider(databaseMetaData);
100101
}
101-
else if ("DB2".equals(databaseProductName)) {
102-
provider = new Db2CallMetaDataProvider((databaseMetaData));
102+
else if ("PostgreSQL".equals(databaseProductName)) {
103+
provider = new PostgresCallMetaDataProvider((databaseMetaData));
103104
}
104105
else if ("Apache Derby".equals(databaseProductName)) {
105106
provider = new DerbyCallMetaDataProvider((databaseMetaData));
106107
}
107-
else if ("PostgreSQL".equals(databaseProductName)) {
108-
provider = new PostgresCallMetaDataProvider((databaseMetaData));
108+
else if ("DB2".equals(databaseProductName)) {
109+
provider = new Db2CallMetaDataProvider((databaseMetaData));
109110
}
110-
else if ("Sybase".equals(databaseProductName)) {
111-
provider = new SybaseCallMetaDataProvider((databaseMetaData));
111+
else if ("HDB".equals(databaseProductName)) {
112+
provider = new HanaCallMetaDataProvider((databaseMetaData));
112113
}
113114
else if ("Microsoft SQL Server".equals(databaseProductName)) {
114115
provider = new SqlServerCallMetaDataProvider((databaseMetaData));
115116
}
116-
else if ("HDB".equals(databaseProductName)) {
117-
provider = new HanaCallMetaDataProvider((databaseMetaData));
117+
else if ("Sybase".equals(databaseProductName)) {
118+
provider = new SybaseCallMetaDataProvider((databaseMetaData));
118119
}
119120
else {
120121
provider = new GenericCallMetaDataProvider(databaseMetaData);
121122
}
123+
122124
if (logger.isDebugEnabled()) {
123125
logger.debug("Using " + provider.getClass().getName());
124126
}
@@ -129,7 +131,6 @@ else if ("HDB".equals(databaseProductName)) {
129131
}
130132
return provider;
131133
});
132-
return result;
133134
}
134135
catch (MetaDataAccessException ex) {
135136
throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,6 +70,7 @@ public String metaDataSchemaNameToUse(@Nullable String schemaName) {
7070
if (schemaName != null) {
7171
return super.metaDataSchemaNameToUse(schemaName);
7272
}
73+
7374
// Use current user schema if no schema specified...
7475
String userName = getUserName();
7576
return (userName != null ? userName.toUpperCase() : null);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,12 +35,14 @@ public DerbyCallMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLEx
3535
super(databaseMetaData);
3636
}
3737

38+
3839
@Override
3940
@Nullable
4041
public String metaDataSchemaNameToUse(@Nullable String schemaName) {
4142
if (schemaName != null) {
4243
return super.metaDataSchemaNameToUse(schemaName);
4344
}
45+
4446
// Use current user schema if no schema specified...
4547
String userName = getUserName();
4648
return (userName != null ? userName.toUpperCase() : null);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,8 @@
2020
import java.sql.SQLException;
2121

2222
/**
23-
* The Derby specific implementation of the {@link org.springframework.jdbc.core.metadata.TableMetaDataProvider}.
24-
* Overrides the Derby metadata info regarding retrieving generated keys. It seems to work OK so not sure why
25-
* they claim it's not supported.
23+
* The Derby specific implementation of {@link TableMetaDataProvider}.
24+
* Overrides the Derby metadata info regarding retrieving generated keys.
2625
*
2726
* @author Thomas Risberg
2827
* @since 3.0
@@ -53,4 +52,5 @@ public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQL
5352
public boolean isGetGeneratedKeysSupported() {
5453
return (super.isGetGeneratedKeysSupported() || this.supportsGeneratedKeysOverride);
5554
}
55+
5656
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.util.StringUtils;
3535

3636
/**
37-
* Generic implementation for the {@link CallMetaDataProvider} interface.
37+
* A generic implementation of the {@link CallMetaDataProvider} interface.
3838
* This class can be extended to provide database specific behavior.
3939
*
4040
* @author Thomas Risberg

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,8 +34,8 @@
3434
import org.springframework.lang.Nullable;
3535

3636
/**
37-
* A generic implementation of the {@link TableMetaDataProvider} that should provide
38-
* enough features for all supported databases.
37+
* A generic implementation of the {@link TableMetaDataProvider} interface
38+
* which should provide enough features for all supported databases.
3939
*
4040
* @author Thomas Risberg
4141
* @author Juergen Hoeller

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ public HanaCallMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLExc
3333
super(databaseMetaData);
3434
}
3535

36+
3637
@Override
3738
public void initializeWithMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
3839
super.initializeWithMetaData(databaseMetaData);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ public HsqlTableMetaDataProvider(DatabaseMetaData databaseMetaData) throws SQLEx
3333
super(databaseMetaData);
3434
}
3535

36+
3637
@Override
3738
public boolean isGetGeneratedKeysSimulated() {
3839
return true;

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,24 @@ public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource
5050
JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
5151
boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
5252
TableMetaDataProvider provider;
53+
5354
if ("Oracle".equals(databaseProductName)) {
54-
provider = new OracleTableMetaDataProvider(databaseMetaData,
55-
context.isOverrideIncludeSynonymsDefault());
56-
}
57-
else if ("HSQL Database Engine".equals(databaseProductName)) {
58-
provider = new HsqlTableMetaDataProvider(databaseMetaData);
55+
provider = new OracleTableMetaDataProvider(
56+
databaseMetaData, context.isOverrideIncludeSynonymsDefault());
5957
}
6058
else if ("PostgreSQL".equals(databaseProductName)) {
6159
provider = new PostgresTableMetaDataProvider(databaseMetaData);
6260
}
6361
else if ("Apache Derby".equals(databaseProductName)) {
6462
provider = new DerbyTableMetaDataProvider(databaseMetaData);
6563
}
64+
else if ("HSQL Database Engine".equals(databaseProductName)) {
65+
provider = new HsqlTableMetaDataProvider(databaseMetaData);
66+
}
6667
else {
6768
provider = new GenericTableMetaDataProvider(databaseMetaData);
6869
}
70+
6971
if (logger.isDebugEnabled()) {
7072
logger.debug("Using " + provider.getClass().getSimpleName());
7173
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/AbstractEmbeddedDatabaseConfigurer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,8 @@
2424
import org.apache.commons.logging.LogFactory;
2525

2626
/**
27-
* Base class for {@link EmbeddedDatabaseConfigurer} implementations providing common shutdown behavior.
27+
* Base class for {@link EmbeddedDatabaseConfigurer} implementations
28+
* providing common shutdown behavior through a "SHUTDOWN" statement.
2829
*
2930
* @author Oliver Gierke
3031
* @author Juergen Hoeller

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/DerbyEmbeddedDatabaseConfigurer.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,8 @@
2626
import org.springframework.lang.Nullable;
2727

2828
/**
29-
* {@link EmbeddedDatabaseConfigurer} for the Apache Derby database 10.6+.
29+
* {@link EmbeddedDatabaseConfigurer} for the Apache Derby database.
30+
*
3031
* <p>Call {@link #getInstance()} to get the singleton instance of this class.
3132
*
3233
* @author Oliver Gierke
@@ -43,10 +44,9 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
4344

4445
/**
4546
* Get the singleton {@link DerbyEmbeddedDatabaseConfigurer} instance.
46-
* @return the configurer
47-
* @throws ClassNotFoundException if Derby is not on the classpath
47+
* @return the configurer instance
4848
*/
49-
public static synchronized DerbyEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
49+
public static synchronized DerbyEmbeddedDatabaseConfigurer getInstance() {
5050
if (instance == null) {
5151
// disable log file
5252
System.setProperty("derby.stream.error.method",

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,16 +30,15 @@
3030
public interface EmbeddedDatabaseConfigurer {
3131

3232
/**
33-
* Configure the properties required to create and connect to the embedded
34-
* database instance.
33+
* Configure the properties required to create and connect to the embedded database.
3534
* @param properties connection properties to configure
3635
* @param databaseName the name of the embedded database
3736
*/
3837
void configureConnectionProperties(ConnectionProperties properties, String databaseName);
3938

4039
/**
4140
* Shut down the embedded database instance that backs the supplied {@link DataSource}.
42-
* @param dataSource the data source
41+
* @param dataSource the corresponding {@link DataSource}
4342
* @param databaseName the name of the database being shut down
4443
*/
4544
void shutdown(DataSource dataSource, String databaseName);

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurerFactory.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@
1919
import org.springframework.util.Assert;
2020

2121
/**
22-
* Maps well-known {@linkplain EmbeddedDatabaseType embedded database types} to
23-
* {@link EmbeddedDatabaseConfigurer} strategies.
22+
* Maps well-known {@linkplain EmbeddedDatabaseType embedded database types}
23+
* to {@link EmbeddedDatabaseConfigurer} strategies.
2424
*
2525
* @author Keith Donald
2626
* @author Oliver Gierke
@@ -29,6 +29,12 @@
2929
*/
3030
final class EmbeddedDatabaseConfigurerFactory {
3131

32+
/**
33+
* Return a configurer instance for the given embedded database type.
34+
* @param type HSQL, H2 or Derby
35+
* @return the configurer instance
36+
* @throws IllegalStateException if the driver for the specified database type is not available
37+
*/
3238
public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type) throws IllegalStateException {
3339
Assert.notNull(type, "EmbeddedDatabaseType is required");
3440
try {
@@ -43,14 +49,9 @@ public static EmbeddedDatabaseConfigurer getConfigurer(EmbeddedDatabaseType type
4349
throw new UnsupportedOperationException("Embedded database type [" + type + "] is not supported");
4450
}
4551
}
46-
catch (ClassNotFoundException ex) {
47-
throw new IllegalStateException("Driver for test database type [" + type +
48-
"] is not available in the classpath", ex);
52+
catch (ClassNotFoundException | NoClassDefFoundError ex) {
53+
throw new IllegalStateException("Driver for test database type [" + type + "] is not available", ex);
4954
}
5055
}
5156

52-
private EmbeddedDatabaseConfigurerFactory() {
53-
/* no-op */
54-
}
55-
5657
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
/**
2525
* {@link EmbeddedDatabaseConfigurer} for an H2 embedded database instance.
26+
*
2627
* <p>Call {@link #getInstance()} to get the singleton instance of this class.
2728
*
2829
* @author Oliver Gierke
@@ -40,7 +41,7 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu
4041

4142
/**
4243
* Get the singleton {@code H2EmbeddedDatabaseConfigurer} instance.
43-
* @return the configurer
44+
* @return the configurer instance
4445
* @throws ClassNotFoundException if H2 is not on the classpath
4546
*/
4647
@SuppressWarnings("unchecked")

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323

2424
/**
2525
* {@link EmbeddedDatabaseConfigurer} for an HSQL embedded database instance.
26+
*
2627
* <p>Call {@link #getInstance()} to get the singleton instance of this class.
2728
*
2829
* @author Keith Donald
@@ -39,7 +40,7 @@ final class HsqlEmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfi
3940

4041
/**
4142
* Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance.
42-
* @return the configurer
43+
* @return the configurer instance
4344
* @throws ClassNotFoundException if HSQL is not on the classpath
4445
*/
4546
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)