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 static org.springframework.batch.support.DatabaseType.DB2; |
19 | import static org.springframework.batch.support.DatabaseType.DB2ZOS; |
20 | import static org.springframework.batch.support.DatabaseType.DERBY; |
21 | import static org.springframework.batch.support.DatabaseType.H2; |
22 | import static org.springframework.batch.support.DatabaseType.HSQL; |
23 | import static org.springframework.batch.support.DatabaseType.MYSQL; |
24 | import static org.springframework.batch.support.DatabaseType.ORACLE; |
25 | import static org.springframework.batch.support.DatabaseType.POSTGRES; |
26 | import static org.springframework.batch.support.DatabaseType.SQLSERVER; |
27 | import static org.springframework.batch.support.DatabaseType.SYBASE; |
28 | |
29 | import java.util.ArrayList; |
30 | import java.util.List; |
31 | |
32 | import javax.sql.DataSource; |
33 | |
34 | import org.springframework.batch.support.DatabaseType; |
35 | import org.springframework.jdbc.support.incrementer.DB2MainframeSequenceMaxValueIncrementer; |
36 | import org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer; |
37 | import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; |
38 | import org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer; |
39 | import org.springframework.jdbc.support.incrementer.H2SequenceMaxValueIncrementer; |
40 | import org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer; |
41 | import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer; |
42 | import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer; |
43 | import org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer; |
44 | import org.springframework.jdbc.support.incrementer.SqlServerMaxValueIncrementer; |
45 | import org.springframework.jdbc.support.incrementer.SybaseMaxValueIncrementer; |
46 | |
47 | /** |
48 | * Default implementation of the {@link DataFieldMaxValueIncrementerFactory} |
49 | * interface. Valid database types are given by the {@link DatabaseType} enum. |
50 | * |
51 | * @author Lucas Ward |
52 | * @see DatabaseType |
53 | */ |
54 | public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxValueIncrementerFactory { |
55 | |
56 | private DataSource dataSource; |
57 | |
58 | private String incrementerColumnName = "ID"; |
59 | |
60 | /** |
61 | * Public setter for the column name (defaults to "ID") in the incrementer. |
62 | * Only used by some platforms (Derby, HSQL, MySQL, SQL Server and Sybase), |
63 | * and should be fine for use with Spring Batch meta data as long as the |
64 | * default batch schema hasn't been changed. |
65 | * |
66 | * @param incrementerColumnName the primary key column name to set |
67 | */ |
68 | public void setIncrementerColumnName(String incrementerColumnName) { |
69 | this.incrementerColumnName = incrementerColumnName; |
70 | } |
71 | |
72 | public DefaultDataFieldMaxValueIncrementerFactory(DataSource dataSource) { |
73 | this.dataSource = dataSource; |
74 | } |
75 | |
76 | @Override |
77 | public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) { |
78 | DatabaseType databaseType = DatabaseType.valueOf(incrementerType.toUpperCase()); |
79 | |
80 | if (databaseType == DB2) { |
81 | return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName); |
82 | } |
83 | else if (databaseType == DB2ZOS) { |
84 | return new DB2MainframeSequenceMaxValueIncrementer(dataSource, incrementerName); |
85 | } |
86 | else if (databaseType == DERBY) { |
87 | return new DerbyMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
88 | } |
89 | else if (databaseType == HSQL) { |
90 | return new HsqlMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
91 | } |
92 | else if (databaseType == H2) { |
93 | return new H2SequenceMaxValueIncrementer(dataSource, incrementerName); |
94 | } |
95 | else if (databaseType == MYSQL) { |
96 | return new MySQLMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
97 | } |
98 | else if (databaseType == ORACLE) { |
99 | return new OracleSequenceMaxValueIncrementer(dataSource, incrementerName); |
100 | } |
101 | else if (databaseType == POSTGRES) { |
102 | return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName); |
103 | } |
104 | else if (databaseType == SQLSERVER) { |
105 | return new SqlServerMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
106 | } |
107 | else if (databaseType == SYBASE) { |
108 | return new SybaseMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName); |
109 | } |
110 | throw new IllegalArgumentException("databaseType argument was not on the approved list"); |
111 | |
112 | } |
113 | |
114 | @Override |
115 | public boolean isSupportedIncrementerType(String incrementerType) { |
116 | for (DatabaseType type : DatabaseType.values()) { |
117 | if (type.name().equals(incrementerType.toUpperCase())) { |
118 | return true; |
119 | } |
120 | } |
121 | |
122 | return false; |
123 | } |
124 | |
125 | @Override |
126 | public String[] getSupportedIncrementerTypes() { |
127 | |
128 | List<String> types = new ArrayList<String>(); |
129 | |
130 | for (DatabaseType type : DatabaseType.values()) { |
131 | types.add(type.name()); |
132 | } |
133 | |
134 | return types.toArray(new String[types.size()]); |
135 | } |
136 | } |