| 1 | /* |
| 2 | * Copyright 2006-2008 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.item.database.support; |
| 17 | |
| 18 | import javax.sql.DataSource; |
| 19 | |
| 20 | import org.springframework.jdbc.support.incrementer.*; |
| 21 | |
| 22 | /** |
| 23 | * Default implementation of the {@link DataFieldMaxValueIncrementerFactory} |
| 24 | * interface. Valid types are: |
| 25 | * |
| 26 | * Valid values are: |
| 27 | * |
| 28 | * <ul> |
| 29 | * <li>db2</li> |
| 30 | * <li>db2zos</li> |
| 31 | * <li>derby</li> |
| 32 | * <li>hsql</li> |
| 33 | * <li>mysql</li> |
| 34 | * <li>oracle</li> |
| 35 | * <li>postgres</li> |
| 36 | * <li>sqlserver</li> |
| 37 | * <li>sybase</li> |
| 38 | * </ul> |
| 39 | * |
| 40 | * @author Lucas Ward |
| 41 | * |
| 42 | */ |
| 43 | public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxValueIncrementerFactory { |
| 44 | |
| 45 | static final String DB_TYPE_DB2 = "db2"; |
| 46 | |
| 47 | static final String DB_TYPE_DB2ZOS = "db2zos"; |
| 48 | |
| 49 | static final String DB_TYPE_DERBY = "derby"; |
| 50 | |
| 51 | static final String DB_TYPE_HSQL = "hsql"; |
| 52 | |
| 53 | static final String DB_TYPE_MYSQL = "mysql"; |
| 54 | |
| 55 | static final String DB_TYPE_ORACLE = "oracle"; |
| 56 | |
| 57 | static final String DB_TYPE_POSTGRES = "postgres"; |
| 58 | |
| 59 | static final String DB_TYPE_SQLSERVER = "sqlserver"; |
| 60 | |
| 61 | static final String DB_TYPE_SYBASE = "sybase"; |
| 62 | |
| 63 | private DataSource dataSource; |
| 64 | |
| 65 | private String incrementerColumnName = "ID"; |
| 66 | |
| 67 | /** |
| 68 | * Public setter for the column name (defaults to "ID") in the incrementer. |
| 69 | * Only used by some platforms (Derby, HSQL, MySQL, SQL Server and Sybase), |
| 70 | * and should be fine for use with Spring Batch meta data as long as the default |
| 71 | * batch schema hasn't been changed. |
| 72 | * |
| 73 | * @param incrementerColumnName the primary key column name to set |
| 74 | */ |
| 75 | public void setIncrementerColumnName(String incrementerColumnName) { |
| 76 | this.incrementerColumnName = incrementerColumnName; |
| 77 | } |
| 78 | |
| 79 | public DefaultDataFieldMaxValueIncrementerFactory(DataSource dataSource) { |
| 80 | this.dataSource = dataSource; |
| 81 | } |
| 82 | |
| 83 | public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) { |
| 84 | if (DB_TYPE_DB2.equals(incrementerType)) { |
| 85 | return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName); |
| 86 | } |
| 87 | else if (DB_TYPE_DB2ZOS.equals(incrementerType)) { |
| 88 | return new DB2MainframeSequenceMaxValueIncrementer(dataSource, incrementerName); |
| 89 | } |
| 90 | else if (DB_TYPE_DERBY.equals(incrementerType)) { |
| 91 | return new DerbyMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
| 92 | } |
| 93 | else if (DB_TYPE_HSQL.equals(incrementerType)) { |
| 94 | return new HsqlMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
| 95 | } |
| 96 | else if (DB_TYPE_MYSQL.equals(incrementerType)) { |
| 97 | return new MySQLMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
| 98 | } |
| 99 | else if (DB_TYPE_ORACLE.equals(incrementerType)) { |
| 100 | return new OracleSequenceMaxValueIncrementer(dataSource, incrementerName); |
| 101 | } |
| 102 | else if (DB_TYPE_POSTGRES.equals(incrementerType)) { |
| 103 | return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName); |
| 104 | } |
| 105 | else if (DB_TYPE_SQLSERVER.equals(incrementerType)) { |
| 106 | return new SqlServerMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
| 107 | } |
| 108 | else if (DB_TYPE_SYBASE.equals(incrementerType)) { |
| 109 | return new SybaseMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
| 110 | } |
| 111 | throw new IllegalArgumentException("databaseType argument was not on the approved list"); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | public boolean isSupportedIncrementerType(String incrementerType) { |
| 116 | if (!DB_TYPE_DB2.equals(incrementerType) |
| 117 | && !DB_TYPE_DB2ZOS.equals(incrementerType) |
| 118 | && !DB_TYPE_DERBY.equals(incrementerType) |
| 119 | && !DB_TYPE_HSQL.equals(incrementerType) |
| 120 | && !DB_TYPE_MYSQL.equals(incrementerType) |
| 121 | && !DB_TYPE_ORACLE.equals(incrementerType) |
| 122 | && !DB_TYPE_POSTGRES.equals(incrementerType) |
| 123 | && !DB_TYPE_SQLSERVER.equals(incrementerType) |
| 124 | && !DB_TYPE_SYBASE.equals(incrementerType)) { |
| 125 | return false; |
| 126 | } |
| 127 | else { |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public String[] getSupportedIncrementerTypes() { |
| 133 | return new String[] { DB_TYPE_DB2, DB_TYPE_DB2ZOS, DB_TYPE_DERBY, DB_TYPE_HSQL, DB_TYPE_MYSQL, |
| 134 | DB_TYPE_ORACLE, DB_TYPE_POSTGRES, DB_TYPE_SQLSERVER, DB_TYPE_SYBASE }; |
| 135 | } |
| 136 | } |