From 286a92834747d749052c75a85412e1b7c3978041 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Wed, 3 Aug 2016 10:00:27 +0200 Subject: [PATCH 1/2] Improve database initializers This commit improves database initializers for Spring Batch and Spring Session by introducing `AbstractDatabaseInitializer` which eliminates duplicated logic in existing initializers. Additionally, database platform resolution now relies on `DatabaseDriver`. See gh-6543 --- .../AbstractDatabaseInitializer.java | 91 +++++++++++++++++++ .../batch/BatchAutoConfiguration.java | 6 +- .../batch/BatchDatabaseInitializer.java | 66 +++++--------- .../session/JdbcSessionConfiguration.java | 6 +- .../JdbcSessionDatabaseInitializer.java | 73 +++------------ 5 files changed, 135 insertions(+), 107 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java new file mode 100644 index 00000000000..902f2e0d3a1 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2016 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.springframework.boot.jdbc.DatabaseDriver; +import org.springframework.core.io.ResourceLoader; +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; +import org.springframework.jdbc.support.JdbcUtils; +import org.springframework.jdbc.support.MetaDataAccessException; +import org.springframework.util.Assert; + +/** + * Abstract base class for database schema initializers. + * + * @author Vedran Pavic + * @since 1.5.0 + */ +public abstract class AbstractDatabaseInitializer { + + private static final String PLATFORM_PLACEHOLDER = "@@platform@@"; + + private DataSource dataSource; + + private ResourceLoader resourceLoader; + + public AbstractDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader) { + Assert.notNull(dataSource, "DataSource must not be null"); + Assert.notNull(resourceLoader, "ResourceLoader must not be null"); + this.dataSource = dataSource; + this.resourceLoader = resourceLoader; + } + + @PostConstruct + protected void initialize() { + if (isEnabled()) { + ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + String schemaLocation = getSchemaLocation(); + if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) { + String platform = customizeDatabaseName(getDatabaseName()); + schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform); + } + populator.addScript(this.resourceLoader.getResource(schemaLocation)); + populator.setContinueOnError(true); + DatabasePopulatorUtils.execute(populator, this.dataSource); + } + } + + protected abstract boolean isEnabled(); + + protected abstract String getSchemaLocation(); + + protected String customizeDatabaseName(String databaseName) { + return databaseName; + } + + private String getDatabaseName() { + try { + String databaseProductName = JdbcUtils.extractDatabaseMetaData( + this.dataSource, "getDatabaseProductName").toString(); + databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName); + DatabaseDriver databaseDriver = DatabaseDriver.fromProductName( + databaseProductName); + if (databaseDriver == DatabaseDriver.UNKNOWN) { + throw new IllegalStateException("Unable to detect database type"); + } + return databaseDriver.toString().toLowerCase(); + } + catch (MetaDataAccessException ex) { + throw new IllegalStateException("Unable to detect database type", ex); + } + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index 4229b6f8555..cb9c3515c8e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -40,6 +40,7 @@ import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfigurat import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.util.StringUtils; @@ -77,8 +78,9 @@ public class BatchAutoConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnBean(DataSource.class) - public BatchDatabaseInitializer batchDatabaseInitializer() { - return new BatchDatabaseInitializer(); + public BatchDatabaseInitializer batchDatabaseInitializer(DataSource dataSource, + ResourceLoader resourceLoader) { + return new BatchDatabaseInitializer(dataSource, resourceLoader, this.properties); } @Bean diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java index 7d4d4fe9de4..9dc52fb1c08 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java @@ -16,63 +16,45 @@ package org.springframework.boot.autoconfigure.batch; -import javax.annotation.PostConstruct; import javax.sql.DataSource; -import org.springframework.batch.support.DatabaseType; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer; import org.springframework.core.io.ResourceLoader; -import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; -import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; -import org.springframework.jdbc.support.MetaDataAccessException; -import org.springframework.stereotype.Component; +import org.springframework.util.Assert; /** * Initialize the Spring Batch schema (ignoring errors, so should be idempotent). * * @author Dave Syer + * @author Vedran Pavic */ -@Component -public class BatchDatabaseInitializer { +public class BatchDatabaseInitializer extends AbstractDatabaseInitializer { - @Autowired private BatchProperties properties; - @Autowired - private DataSource dataSource; - - @Autowired - private ResourceLoader resourceLoader; - - @PostConstruct - protected void initialize() { - if (this.properties.getInitializer().isEnabled()) { - String platform = getDatabaseType(); - if ("hsql".equals(platform)) { - platform = "hsqldb"; - } - if ("postgres".equals(platform)) { - platform = "postgresql"; - } - if ("oracle".equals(platform)) { - platform = "oracle10g"; - } - ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); - String schemaLocation = this.properties.getSchema(); - schemaLocation = schemaLocation.replace("@@platform@@", platform); - populator.addScript(this.resourceLoader.getResource(schemaLocation)); - populator.setContinueOnError(true); - DatabasePopulatorUtils.execute(populator, this.dataSource); - } + public BatchDatabaseInitializer(DataSource dataSource, + ResourceLoader resourceLoader, BatchProperties properties) { + super(dataSource, resourceLoader); + Assert.notNull(properties, "BatchProperties must not be null"); + this.properties = properties; } - private String getDatabaseType() { - try { - return DatabaseType.fromMetaData(this.dataSource).toString().toLowerCase(); - } - catch (MetaDataAccessException ex) { - throw new IllegalStateException("Unable to detect database type", ex); + @Override + protected boolean isEnabled() { + return this.properties.getInitializer().isEnabled(); + } + + @Override + protected String getSchemaLocation() { + return this.properties.getSchema(); + } + + @Override + protected String customizeDatabaseName(String databaseName) { + if ("oracle".equals(databaseName)) { + return "oracle10g"; } + return databaseName; } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java index 5c20e9750d2..621573958f4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionConfiguration.java @@ -47,9 +47,9 @@ class JdbcSessionConfiguration { @Bean @ConditionalOnMissingBean public JdbcSessionDatabaseInitializer jdbcSessionDatabaseInitializer( - SessionProperties properties, DataSource dataSource, - ResourceLoader resourceLoader) { - return new JdbcSessionDatabaseInitializer(properties, dataSource, resourceLoader); + DataSource dataSource, ResourceLoader resourceLoader, + SessionProperties properties) { + return new JdbcSessionDatabaseInitializer(dataSource, resourceLoader, properties); } @Configuration diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java index 96aecc69fc3..82b802d58f5 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java @@ -16,18 +16,10 @@ package org.springframework.boot.autoconfigure.session; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import javax.annotation.PostConstruct; import javax.sql.DataSource; +import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer; import org.springframework.core.io.ResourceLoader; -import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; -import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; -import org.springframework.jdbc.support.JdbcUtils; -import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.util.Assert; /** @@ -36,64 +28,25 @@ import org.springframework.util.Assert; * @author Vedran Pavic * @since 1.4.0 */ -public class JdbcSessionDatabaseInitializer { +public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer { - private static Map ALIASES; + private SessionProperties.Jdbc properties; - static { - Map aliases = new HashMap(); - aliases.put("apache derby", "derby"); - aliases.put("hsql database engine", "hsqldb"); - aliases.put("microsoft sql server", "sqlserver"); - ALIASES = Collections.unmodifiableMap(aliases); - } - - private SessionProperties properties; - - private DataSource dataSource; - - private ResourceLoader resourceLoader; - - public JdbcSessionDatabaseInitializer(SessionProperties properties, - DataSource dataSource, ResourceLoader resourceLoader) { + public JdbcSessionDatabaseInitializer(DataSource dataSource, + ResourceLoader resourceLoader, SessionProperties properties) { + super(dataSource, resourceLoader); Assert.notNull(properties, "SessionProperties must not be null"); - Assert.notNull(dataSource, "DataSource must not be null"); - Assert.notNull(resourceLoader, "ResourceLoader must not be null"); - this.properties = properties; - this.dataSource = dataSource; - this.resourceLoader = resourceLoader; + this.properties = properties.getJdbc(); } - @PostConstruct - protected void initialize() { - if (this.properties.getJdbc().getInitializer().isEnabled()) { - ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); - String schemaLocation = this.properties.getJdbc().getSchema(); - schemaLocation = schemaLocation.replace("@@platform@@", getPlatform()); - populator.addScript(this.resourceLoader.getResource(schemaLocation)); - populator.setContinueOnError(true); - DatabasePopulatorUtils.execute(populator, this.dataSource); - } + @Override + protected boolean isEnabled() { + return this.properties.getInitializer().isEnabled(); } - private String getPlatform() { - String databaseName = getDatabaseName(); - if (ALIASES.containsKey(databaseName)) { - return ALIASES.get(databaseName); - } - return databaseName; - } - - private String getDatabaseName() { - try { - String databaseProductName = JdbcUtils - .extractDatabaseMetaData(this.dataSource, "getDatabaseProductName") - .toString(); - return JdbcUtils.commonDatabaseName(databaseProductName).toLowerCase(); - } - catch (MetaDataAccessException ex) { - throw new IllegalStateException("Unable to detect database type", ex); - } + @Override + protected String getSchemaLocation() { + return this.properties.getSchema(); } } From 3e1425ebede7e699a1956a39d9eb134eb3dfd6df Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 10 Oct 2016 10:43:12 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-6543 --- .../AbstractDatabaseInitializer.java | 19 +++--- .../batch/BatchDatabaseInitializer.java | 7 ++- .../JdbcSessionDatabaseInitializer.java | 2 +- .../boot/jdbc/DatabaseDriver.java | 60 +++++++++++-------- 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java index 902f2e0d3a1..6e00a9f1f58 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDatabaseInitializer.java @@ -28,20 +28,21 @@ import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.util.Assert; /** - * Abstract base class for database schema initializers. + * Base class used for database initialization. * * @author Vedran Pavic + * @author Stephane Nicoll * @since 1.5.0 */ public abstract class AbstractDatabaseInitializer { private static final String PLATFORM_PLACEHOLDER = "@@platform@@"; - private DataSource dataSource; + private final DataSource dataSource; - private ResourceLoader resourceLoader; + private final ResourceLoader resourceLoader; - public AbstractDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader) { + protected AbstractDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader) { Assert.notNull(dataSource, "DataSource must not be null"); Assert.notNull(resourceLoader, "ResourceLoader must not be null"); this.dataSource = dataSource; @@ -54,7 +55,7 @@ public abstract class AbstractDatabaseInitializer { ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); String schemaLocation = getSchemaLocation(); if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) { - String platform = customizeDatabaseName(getDatabaseName()); + String platform = getDatabaseName(); schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform); } populator.addScript(this.resourceLoader.getResource(schemaLocation)); @@ -67,11 +68,7 @@ public abstract class AbstractDatabaseInitializer { protected abstract String getSchemaLocation(); - protected String customizeDatabaseName(String databaseName) { - return databaseName; - } - - private String getDatabaseName() { + protected String getDatabaseName() { try { String databaseProductName = JdbcUtils.extractDatabaseMetaData( this.dataSource, "getDatabaseProductName").toString(); @@ -81,7 +78,7 @@ public abstract class AbstractDatabaseInitializer { if (databaseDriver == DatabaseDriver.UNKNOWN) { throw new IllegalStateException("Unable to detect database type"); } - return databaseDriver.toString().toLowerCase(); + return databaseDriver.getId(); } catch (MetaDataAccessException ex) { throw new IllegalStateException("Unable to detect database type", ex); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java index 9dc52fb1c08..96d47adb282 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDatabaseInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2016 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,7 @@ import org.springframework.util.Assert; */ public class BatchDatabaseInitializer extends AbstractDatabaseInitializer { - private BatchProperties properties; + private final BatchProperties properties; public BatchDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader, BatchProperties properties) { @@ -50,7 +50,8 @@ public class BatchDatabaseInitializer extends AbstractDatabaseInitializer { } @Override - protected String customizeDatabaseName(String databaseName) { + protected String getDatabaseName() { + String databaseName = super.getDatabaseName(); if ("oracle".equals(databaseName)) { return "oracle10g"; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java index 82b802d58f5..b8c2bc8924c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDatabaseInitializer.java @@ -30,7 +30,7 @@ import org.springframework.util.Assert; */ public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer { - private SessionProperties.Jdbc properties; + private final SessionProperties.Jdbc properties; public JdbcSessionDatabaseInitializer(DataSource dataSource, ResourceLoader resourceLoader, SessionProperties properties) { diff --git a/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java b/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java index aa340384c3d..202287a4096 100644 --- a/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java +++ b/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java @@ -33,77 +33,77 @@ public enum DatabaseDriver { /** * Unknown type. */ - UNKNOWN(null, null), + UNKNOWN("unknown", null, null), /** * Apache Derby. */ - DERBY("Apache Derby", "org.apache.derby.jdbc.EmbeddedDriver", + DERBY("derby", "Apache Derby", "org.apache.derby.jdbc.EmbeddedDriver", "org.apache.derby.jdbc.EmbeddedXADataSource", "SELECT 1 FROM SYSIBM.SYSDUMMY1"), /** * H2. */ - H2("H2", "org.h2.Driver", "org.h2.jdbcx.JdbcDataSource", "SELECT 1"), + H2("h2", "H2", "org.h2.Driver", "org.h2.jdbcx.JdbcDataSource", "SELECT 1"), /** * HyperSQL DataBase. */ - HSQLDB("HSQL Database Engine", "org.hsqldb.jdbc.JDBCDriver", + HSQLDB("hsqldb", "HSQL Database Engine", "org.hsqldb.jdbc.JDBCDriver", "org.hsqldb.jdbc.pool.JDBCXADataSource", "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS"), /** * SQL Lite. */ - SQLITE("SQLite", "org.sqlite.JDBC"), + SQLITE("sqlite", "SQLite", "org.sqlite.JDBC"), /** * MySQL. */ - MYSQL("MySQL", "com.mysql.jdbc.Driver", + MYSQL("mysql", "MySQL", "com.mysql.jdbc.Driver", "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource", "SELECT 1"), /** * Maria DB. */ - MARIADB("MySQL", "org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource", - "SELECT 1"), + MARIADB("maridb", "MySQL", "org.mariadb.jdbc.Driver", + "org.mariadb.jdbc.MariaDbDataSource", "SELECT 1"), /** * Google App Engine. */ - GAE(null, "com.google.appengine.api.rdbms.AppEngineDriver"), + GAE("gae", null, "com.google.appengine.api.rdbms.AppEngineDriver"), /** * Oracle. */ - ORACLE("Oracle", "oracle.jdbc.OracleDriver", + ORACLE("oracle", "Oracle", "oracle.jdbc.OracleDriver", "oracle.jdbc.xa.client.OracleXADataSource", "SELECT 'Hello' from DUAL"), /** * Postgres. */ - POSTGRESQL("PostgreSQL", "org.postgresql.Driver", "org.postgresql.xa.PGXADataSource", - "SELECT 1"), + POSTGRESQL("postgresql", "PostgreSQL", "org.postgresql.Driver", + "org.postgresql.xa.PGXADataSource", "SELECT 1"), /** * jTDS. As it can be used for several databases, there isn't a single product name we * could rely on. */ - JTDS(null, "net.sourceforge.jtds.jdbc.Driver"), + JTDS("jtds", null, "net.sourceforge.jtds.jdbc.Driver"), /** * SQL Server. */ - SQLSERVER("SQL SERVER", "com.microsoft.sqlserver.jdbc.SQLServerDriver", + SQLSERVER("sqlserver", "SQL SERVER", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "com.microsoft.sqlserver.jdbc.SQLServerXADataSource", "SELECT 1"), /** * Firebird. */ - FIREBIRD("Firebird", "org.firebirdsql.jdbc.FBDriver", + FIREBIRD("firebird", "Firebird", "org.firebirdsql.jdbc.FBDriver", "org.firebirdsql.pool.FBConnectionPoolDataSource", "SELECT 1 FROM RDB$DATABASE") { @@ -117,7 +117,7 @@ public enum DatabaseDriver { /** * DB2 Server. */ - DB2("DB2", "com.ibm.db2.jcc.DB2Driver", "com.ibm.db2.jcc.DB2XADataSource", + DB2("db2", "DB2", "com.ibm.db2.jcc.DB2Driver", "com.ibm.db2.jcc.DB2XADataSource", "SELECT 1 FROM SYSIBM.SYSDUMMY1") { @Override @@ -130,7 +130,7 @@ public enum DatabaseDriver { /** * DB2 AS400 Server. */ - DB2_AS400("DB2 UDB for AS/400", "com.ibm.as400.access.AS400JDBCDriver", + DB2_AS400("db2", "DB2 UDB for AS/400", "com.ibm.as400.access.AS400JDBCDriver", "com.ibm.as400.access.AS400JDBCXADataSource", "SELECT 1 FROM SYSIBM.SYSDUMMY1") { @@ -144,14 +144,16 @@ public enum DatabaseDriver { /** * Teradata. */ - TERADATA("Teradata", "com.teradata.jdbc.TeraDriver"), + TERADATA("teradata", "Teradata", "com.teradata.jdbc.TeraDriver"), /** * Informix. */ - INFORMIX("Informix Dynamic Server", "com.informix.jdbc.IfxDriver", null, + INFORMIX("informix", "Informix Dynamic Server", "com.informix.jdbc.IfxDriver", null, "select count(*) from systables"); + private final String id; + private final String productName; private final String driverClassName; @@ -160,22 +162,32 @@ public enum DatabaseDriver { private final String validationQuery; - DatabaseDriver(String name, String driverClassName) { - this(name, driverClassName, null); + DatabaseDriver(String id, String name, String driverClassName) { + this(id, name, driverClassName, null); } - DatabaseDriver(String name, String driverClassName, String xaDataSourceClassName) { - this(name, driverClassName, xaDataSourceClassName, null); + DatabaseDriver(String id, String name, String driverClassName, + String xaDataSourceClassName) { + this(id, name, driverClassName, xaDataSourceClassName, null); } - DatabaseDriver(String productName, String driverClassName, + DatabaseDriver(String id, String productName, String driverClassName, String xaDataSourceClassName, String validationQuery) { + this.id = id; this.productName = productName; this.driverClassName = driverClassName; this.xaDataSourceClassName = xaDataSourceClassName; this.validationQuery = validationQuery; } + /** + * Return the identifier of this driver. + * @return the identifier + */ + public String getId() { + return this.id; + } + protected boolean matchProductName(String productName) { return this.productName != null && this.productName.equalsIgnoreCase(productName); }