From 26aee69c6ab0511bf87ad43c95493f2a57cf0fcb Mon Sep 17 00:00:00 2001 From: dsyer Date: Fri, 27 Jun 2008 14:14:28 +0000 Subject: [PATCH] Add setter for primary key column name (in case someone changed the schema). --- ...ltDataFieldMaxValueIncrementerFactory.java | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/DefaultDataFieldMaxValueIncrementerFactory.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/DefaultDataFieldMaxValueIncrementerFactory.java index 5f18a9fa5..539e08d10 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/DefaultDataFieldMaxValueIncrementerFactory.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/support/DefaultDataFieldMaxValueIncrementerFactory.java @@ -26,8 +26,8 @@ import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrem import org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer; /** - * Default implementation of the {@link DataFieldMaxValueIncrementerFactory} interface. Valid types - * are: + * Default implementation of the {@link DataFieldMaxValueIncrementerFactory} + * interface. Valid types are: * * Valid values are: * @@ -59,6 +59,20 @@ public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxV private DataSource dataSource; + private String primaryKeyColumnName = "id"; + + /** + * Public setter for the primary key column name (defaults to "id"). Only + * used by some platforms (Derby, HSQL, MySQL), and should be fine for use + * with Spring Batch meta data as long as the default batch schema hasn't + * been changed. + * + * @param primaryKeyColumnName the primary key column name to set + */ + public void setPrimaryKeyColumnName(String primaryKeyColumnName) { + this.primaryKeyColumnName = primaryKeyColumnName; + } + public DefaultDataFieldMaxValueIncrementerFactory(DataSource dataSource) { this.dataSource = dataSource; } @@ -66,15 +80,20 @@ public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxV public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) { if (DB_TYPE_DB2.equals(incrementerType)) { return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName); - } else if (DB_TYPE_DERBY.equals(incrementerType)) { - return new DerbyMaxValueIncrementer(dataSource, incrementerName, "id"); - } else if (DB_TYPE_HSQL.equals(incrementerType)) { - return new HsqlMaxValueIncrementer(dataSource, incrementerName, "id"); - } else if (DB_TYPE_MYSQL.equals(incrementerType)) { - return new MySQLMaxValueIncrementer(dataSource, incrementerName, "id"); - } else if (DB_TYPE_ORACLE.equals(incrementerType)) { + } + else if (DB_TYPE_DERBY.equals(incrementerType)) { + return new DerbyMaxValueIncrementer(dataSource, incrementerName, primaryKeyColumnName); + } + else if (DB_TYPE_HSQL.equals(incrementerType)) { + return new HsqlMaxValueIncrementer(dataSource, incrementerName, primaryKeyColumnName); + } + else if (DB_TYPE_MYSQL.equals(incrementerType)) { + return new MySQLMaxValueIncrementer(dataSource, incrementerName, primaryKeyColumnName); + } + else if (DB_TYPE_ORACLE.equals(incrementerType)) { return new OracleSequenceMaxValueIncrementer(dataSource, incrementerName); - } else if (DB_TYPE_POSTGRES.equals(incrementerType)) { + } + else if (DB_TYPE_POSTGRES.equals(incrementerType)) { return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName); } throw new IllegalArgumentException("databaseType argument was not on the approved list"); @@ -83,11 +102,12 @@ public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxV public boolean isSupportedIncrementerType(String incrementerType) { if (!DB_TYPE_DB2.equals(incrementerType) && !DB_TYPE_DERBY.equals(incrementerType) - && !DB_TYPE_HSQL.equals(incrementerType) && !DB_TYPE_MYSQL.equals(incrementerType) - && !DB_TYPE_ORACLE.equals(incrementerType) && !DB_TYPE_POSTGRES.equals(incrementerType)) { + && !DB_TYPE_HSQL.equals(incrementerType) && !DB_TYPE_MYSQL.equals(incrementerType) + && !DB_TYPE_ORACLE.equals(incrementerType) && !DB_TYPE_POSTGRES.equals(incrementerType)) { return false; - } else { + } + else { return true; } }