Polish contribution

Closes gh-6543
pull/7164/head
Stephane Nicoll 8 years ago
parent 286a928347
commit 3e1425ebed

@ -28,20 +28,21 @@ import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Abstract base class for database schema initializers. * Base class used for database initialization.
* *
* @author Vedran Pavic * @author Vedran Pavic
* @author Stephane Nicoll
* @since 1.5.0 * @since 1.5.0
*/ */
public abstract class AbstractDatabaseInitializer { public abstract class AbstractDatabaseInitializer {
private static final String PLATFORM_PLACEHOLDER = "@@platform@@"; 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(dataSource, "DataSource must not be null");
Assert.notNull(resourceLoader, "ResourceLoader must not be null"); Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.dataSource = dataSource; this.dataSource = dataSource;
@ -54,7 +55,7 @@ public abstract class AbstractDatabaseInitializer {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
String schemaLocation = getSchemaLocation(); String schemaLocation = getSchemaLocation();
if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) { if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) {
String platform = customizeDatabaseName(getDatabaseName()); String platform = getDatabaseName();
schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform); schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform);
} }
populator.addScript(this.resourceLoader.getResource(schemaLocation)); populator.addScript(this.resourceLoader.getResource(schemaLocation));
@ -67,11 +68,7 @@ public abstract class AbstractDatabaseInitializer {
protected abstract String getSchemaLocation(); protected abstract String getSchemaLocation();
protected String customizeDatabaseName(String databaseName) { protected String getDatabaseName() {
return databaseName;
}
private String getDatabaseName() {
try { try {
String databaseProductName = JdbcUtils.extractDatabaseMetaData( String databaseProductName = JdbcUtils.extractDatabaseMetaData(
this.dataSource, "getDatabaseProductName").toString(); this.dataSource, "getDatabaseProductName").toString();
@ -81,7 +78,7 @@ public abstract class AbstractDatabaseInitializer {
if (databaseDriver == DatabaseDriver.UNKNOWN) { if (databaseDriver == DatabaseDriver.UNKNOWN) {
throw new IllegalStateException("Unable to detect database type"); throw new IllegalStateException("Unable to detect database type");
} }
return databaseDriver.toString().toLowerCase(); return databaseDriver.getId();
} }
catch (MetaDataAccessException ex) { catch (MetaDataAccessException ex) {
throw new IllegalStateException("Unable to detect database type", ex); throw new IllegalStateException("Unable to detect database type", ex);

@ -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"); * 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,7 @@ import org.springframework.util.Assert;
*/ */
public class BatchDatabaseInitializer extends AbstractDatabaseInitializer { public class BatchDatabaseInitializer extends AbstractDatabaseInitializer {
private BatchProperties properties; private final BatchProperties properties;
public BatchDatabaseInitializer(DataSource dataSource, public BatchDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader, BatchProperties properties) { ResourceLoader resourceLoader, BatchProperties properties) {
@ -50,7 +50,8 @@ public class BatchDatabaseInitializer extends AbstractDatabaseInitializer {
} }
@Override @Override
protected String customizeDatabaseName(String databaseName) { protected String getDatabaseName() {
String databaseName = super.getDatabaseName();
if ("oracle".equals(databaseName)) { if ("oracle".equals(databaseName)) {
return "oracle10g"; return "oracle10g";
} }

@ -30,7 +30,7 @@ import org.springframework.util.Assert;
*/ */
public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer { public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer {
private SessionProperties.Jdbc properties; private final SessionProperties.Jdbc properties;
public JdbcSessionDatabaseInitializer(DataSource dataSource, public JdbcSessionDatabaseInitializer(DataSource dataSource,
ResourceLoader resourceLoader, SessionProperties properties) { ResourceLoader resourceLoader, SessionProperties properties) {

@ -33,77 +33,77 @@ public enum DatabaseDriver {
/** /**
* Unknown type. * Unknown type.
*/ */
UNKNOWN(null, null), UNKNOWN("unknown", null, null),
/** /**
* Apache Derby. * Apache Derby.
*/ */
DERBY("Apache Derby", "org.apache.derby.jdbc.EmbeddedDriver", DERBY("derby", "Apache Derby", "org.apache.derby.jdbc.EmbeddedDriver",
"org.apache.derby.jdbc.EmbeddedXADataSource", "org.apache.derby.jdbc.EmbeddedXADataSource",
"SELECT 1 FROM SYSIBM.SYSDUMMY1"), "SELECT 1 FROM SYSIBM.SYSDUMMY1"),
/** /**
* H2. * 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. * HyperSQL DataBase.
*/ */
HSQLDB("HSQL Database Engine", "org.hsqldb.jdbc.JDBCDriver", HSQLDB("hsqldb", "HSQL Database Engine", "org.hsqldb.jdbc.JDBCDriver",
"org.hsqldb.jdbc.pool.JDBCXADataSource", "org.hsqldb.jdbc.pool.JDBCXADataSource",
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS"), "SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS"),
/** /**
* SQL Lite. * SQL Lite.
*/ */
SQLITE("SQLite", "org.sqlite.JDBC"), SQLITE("sqlite", "SQLite", "org.sqlite.JDBC"),
/** /**
* MySQL. * MySQL.
*/ */
MYSQL("MySQL", "com.mysql.jdbc.Driver", MYSQL("mysql", "MySQL", "com.mysql.jdbc.Driver",
"com.mysql.jdbc.jdbc2.optional.MysqlXADataSource", "SELECT 1"), "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource", "SELECT 1"),
/** /**
* Maria DB. * Maria DB.
*/ */
MARIADB("MySQL", "org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource", MARIADB("maridb", "MySQL", "org.mariadb.jdbc.Driver",
"SELECT 1"), "org.mariadb.jdbc.MariaDbDataSource", "SELECT 1"),
/** /**
* Google App Engine. * Google App Engine.
*/ */
GAE(null, "com.google.appengine.api.rdbms.AppEngineDriver"), GAE("gae", null, "com.google.appengine.api.rdbms.AppEngineDriver"),
/** /**
* Oracle. * Oracle.
*/ */
ORACLE("Oracle", "oracle.jdbc.OracleDriver", ORACLE("oracle", "Oracle", "oracle.jdbc.OracleDriver",
"oracle.jdbc.xa.client.OracleXADataSource", "SELECT 'Hello' from DUAL"), "oracle.jdbc.xa.client.OracleXADataSource", "SELECT 'Hello' from DUAL"),
/** /**
* Postgres. * Postgres.
*/ */
POSTGRESQL("PostgreSQL", "org.postgresql.Driver", "org.postgresql.xa.PGXADataSource", POSTGRESQL("postgresql", "PostgreSQL", "org.postgresql.Driver",
"SELECT 1"), "org.postgresql.xa.PGXADataSource", "SELECT 1"),
/** /**
* jTDS. As it can be used for several databases, there isn't a single product name we * jTDS. As it can be used for several databases, there isn't a single product name we
* could rely on. * could rely on.
*/ */
JTDS(null, "net.sourceforge.jtds.jdbc.Driver"), JTDS("jtds", null, "net.sourceforge.jtds.jdbc.Driver"),
/** /**
* SQL Server. * 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"), "com.microsoft.sqlserver.jdbc.SQLServerXADataSource", "SELECT 1"),
/** /**
* Firebird. * Firebird.
*/ */
FIREBIRD("Firebird", "org.firebirdsql.jdbc.FBDriver", FIREBIRD("firebird", "Firebird", "org.firebirdsql.jdbc.FBDriver",
"org.firebirdsql.pool.FBConnectionPoolDataSource", "org.firebirdsql.pool.FBConnectionPoolDataSource",
"SELECT 1 FROM RDB$DATABASE") { "SELECT 1 FROM RDB$DATABASE") {
@ -117,7 +117,7 @@ public enum DatabaseDriver {
/** /**
* DB2 Server. * 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") { "SELECT 1 FROM SYSIBM.SYSDUMMY1") {
@Override @Override
@ -130,7 +130,7 @@ public enum DatabaseDriver {
/** /**
* DB2 AS400 Server. * 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", "com.ibm.as400.access.AS400JDBCXADataSource",
"SELECT 1 FROM SYSIBM.SYSDUMMY1") { "SELECT 1 FROM SYSIBM.SYSDUMMY1") {
@ -144,14 +144,16 @@ public enum DatabaseDriver {
/** /**
* Teradata. * Teradata.
*/ */
TERADATA("Teradata", "com.teradata.jdbc.TeraDriver"), TERADATA("teradata", "Teradata", "com.teradata.jdbc.TeraDriver"),
/** /**
* Informix. * Informix.
*/ */
INFORMIX("Informix Dynamic Server", "com.informix.jdbc.IfxDriver", null, INFORMIX("informix", "Informix Dynamic Server", "com.informix.jdbc.IfxDriver", null,
"select count(*) from systables"); "select count(*) from systables");
private final String id;
private final String productName; private final String productName;
private final String driverClassName; private final String driverClassName;
@ -160,22 +162,32 @@ public enum DatabaseDriver {
private final String validationQuery; private final String validationQuery;
DatabaseDriver(String name, String driverClassName) { DatabaseDriver(String id, String name, String driverClassName) {
this(name, driverClassName, null); this(id, name, driverClassName, null);
} }
DatabaseDriver(String name, String driverClassName, String xaDataSourceClassName) { DatabaseDriver(String id, String name, String driverClassName,
this(name, driverClassName, xaDataSourceClassName, null); String xaDataSourceClassName) {
this(id, name, driverClassName, xaDataSourceClassName, null);
} }
DatabaseDriver(String productName, String driverClassName, DatabaseDriver(String id, String productName, String driverClassName,
String xaDataSourceClassName, String validationQuery) { String xaDataSourceClassName, String validationQuery) {
this.id = id;
this.productName = productName; this.productName = productName;
this.driverClassName = driverClassName; this.driverClassName = driverClassName;
this.xaDataSourceClassName = xaDataSourceClassName; this.xaDataSourceClassName = xaDataSourceClassName;
this.validationQuery = validationQuery; this.validationQuery = validationQuery;
} }
/**
* Return the identifier of this driver.
* @return the identifier
*/
public String getId() {
return this.id;
}
protected boolean matchProductName(String productName) { protected boolean matchProductName(String productName) {
return this.productName != null && this.productName.equalsIgnoreCase(productName); return this.productName != null && this.productName.equalsIgnoreCase(productName);
} }

Loading…
Cancel
Save