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.DB2SequenceMaxValueIncrementer; |
21 | import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; |
22 | import org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer; |
23 | import org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer; |
24 | import org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer; |
25 | import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer; |
26 | import org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer; |
27 | |
28 | /** |
29 | * Default implementation of the {@link DataFieldMaxValueIncrementerFactory} interface. Valid types |
30 | * are: |
31 | * |
32 | * Valid values are: |
33 | * |
34 | * <ul> |
35 | * <li>db2</li> |
36 | * <li>derby</li> |
37 | * <li>hsql</li> |
38 | * <li>mysql</li> |
39 | * <li>oracle</li> |
40 | * <li>postgres</li> |
41 | * </ul> |
42 | * |
43 | * @author Lucas Ward |
44 | * |
45 | */ |
46 | public class DefaultDataFieldMaxValueIncrementerFactory implements DataFieldMaxValueIncrementerFactory { |
47 | |
48 | static final String DB_TYPE_DB2 = "db2"; |
49 | |
50 | static final String DB_TYPE_DERBY = "derby"; |
51 | |
52 | static final String DB_TYPE_HSQL = "hsql"; |
53 | |
54 | static final String DB_TYPE_MYSQL = "mysql"; |
55 | |
56 | static final String DB_TYPE_ORACLE = "oracle"; |
57 | |
58 | static final String DB_TYPE_POSTGRES = "postgres"; |
59 | |
60 | private DataSource dataSource; |
61 | |
62 | public DefaultDataFieldMaxValueIncrementerFactory(DataSource dataSource) { |
63 | this.dataSource = dataSource; |
64 | } |
65 | |
66 | public DataFieldMaxValueIncrementer getIncrementer(String incrementerType, String incrementerName) { |
67 | if (DB_TYPE_DB2.equals(incrementerType)) { |
68 | return new DB2SequenceMaxValueIncrementer(dataSource, incrementerName); |
69 | } else if (DB_TYPE_DERBY.equals(incrementerType)) { |
70 | return new DerbyMaxValueIncrementer(dataSource, incrementerName, "id"); |
71 | } else if (DB_TYPE_HSQL.equals(incrementerType)) { |
72 | return new HsqlMaxValueIncrementer(dataSource, incrementerName, "id"); |
73 | } else if (DB_TYPE_MYSQL.equals(incrementerType)) { |
74 | return new MySQLMaxValueIncrementer(dataSource, incrementerName, "id"); |
75 | } else if (DB_TYPE_ORACLE.equals(incrementerType)) { |
76 | return new OracleSequenceMaxValueIncrementer(dataSource, incrementerName); |
77 | } else if (DB_TYPE_POSTGRES.equals(incrementerType)) { |
78 | return new PostgreSQLSequenceMaxValueIncrementer(dataSource, incrementerName); |
79 | } |
80 | throw new IllegalArgumentException("databaseType argument was not on the approved list"); |
81 | |
82 | } |
83 | |
84 | public boolean isSupportedIncrementerType(String incrementerType) { |
85 | if (!DB_TYPE_DB2.equals(incrementerType) && !DB_TYPE_DERBY.equals(incrementerType) |
86 | && !DB_TYPE_HSQL.equals(incrementerType) && !DB_TYPE_MYSQL.equals(incrementerType) |
87 | && !DB_TYPE_ORACLE.equals(incrementerType) && !DB_TYPE_POSTGRES.equals(incrementerType)) { |
88 | |
89 | return false; |
90 | } else { |
91 | return true; |
92 | } |
93 | } |
94 | |
95 | public String[] getSupportedIncrementerTypes() { |
96 | return new String[] { DB_TYPE_DB2, DB_TYPE_DERBY, DB_TYPE_HSQL, DB_TYPE_MYSQL, DB_TYPE_ORACLE, DB_TYPE_POSTGRES }; |
97 | } |
98 | } |