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.HSQL; |
22 | import static org.springframework.batch.support.DatabaseType.H2; |
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.HashMap; |
30 | import java.util.Map; |
31 | |
32 | import javax.sql.DataSource; |
33 | |
34 | import org.springframework.batch.item.database.PagingQueryProvider; |
35 | import org.springframework.batch.support.DatabaseType; |
36 | import org.springframework.beans.factory.FactoryBean; |
37 | import org.springframework.jdbc.support.MetaDataAccessException; |
38 | import org.springframework.util.Assert; |
39 | import org.springframework.util.StringUtils; |
40 | |
41 | /** |
42 | * Factory bean for {@link PagingQueryProvider} interface. The database type |
43 | * will be determined from the data source if not provided explicitly. Valid |
44 | * types are given by the {@link DatabaseType} enum. |
45 | * |
46 | * @author Dave Syer |
47 | */ |
48 | public class SqlPagingQueryProviderFactoryBean implements FactoryBean { |
49 | |
50 | private DataSource dataSource; |
51 | |
52 | private String databaseType; |
53 | |
54 | private String fromClause; |
55 | |
56 | private String whereClause; |
57 | |
58 | private String selectClause; |
59 | |
60 | private String sortKey; |
61 | |
62 | private boolean ascending = true; |
63 | |
64 | private Map<DatabaseType, AbstractSqlPagingQueryProvider> providers = new HashMap<DatabaseType, AbstractSqlPagingQueryProvider>(); |
65 | |
66 | |
67 | { |
68 | providers.put(DB2, new Db2PagingQueryProvider()); |
69 | providers.put(DB2ZOS, new Db2PagingQueryProvider()); |
70 | providers.put(DERBY,new DerbyPagingQueryProvider()); |
71 | providers.put(HSQL,new HsqlPagingQueryProvider()); |
72 | providers.put(H2,new H2PagingQueryProvider()); |
73 | providers.put(MYSQL,new MySqlPagingQueryProvider()); |
74 | providers.put(ORACLE,new OraclePagingQueryProvider()); |
75 | providers.put(POSTGRES,new PostgresPagingQueryProvider()); |
76 | providers.put(SQLSERVER,new SqlServerPagingQueryProvider()); |
77 | providers.put(SYBASE,new SybasePagingQueryProvider()); |
78 | } |
79 | |
80 | /** |
81 | * @param databaseType the databaseType to set |
82 | */ |
83 | public void setDatabaseType(String databaseType) { |
84 | this.databaseType = databaseType; |
85 | } |
86 | |
87 | /** |
88 | * @param dataSource the dataSource to set |
89 | */ |
90 | public void setDataSource(DataSource dataSource) { |
91 | this.dataSource = dataSource; |
92 | } |
93 | |
94 | /** |
95 | * @param fromClause the fromClause to set |
96 | */ |
97 | public void setFromClause(String fromClause) { |
98 | this.fromClause = fromClause; |
99 | } |
100 | |
101 | /** |
102 | * @param whereClause the whereClause to set |
103 | */ |
104 | public void setWhereClause(String whereClause) { |
105 | this.whereClause = whereClause; |
106 | } |
107 | |
108 | /** |
109 | * @param selectClause the selectClause to set |
110 | */ |
111 | public void setSelectClause(String selectClause) { |
112 | this.selectClause = selectClause; |
113 | } |
114 | |
115 | /** |
116 | * @param sortKey the sortKey to set |
117 | */ |
118 | public void setSortKey(String sortKey) { |
119 | this.sortKey = sortKey; |
120 | } |
121 | |
122 | /** |
123 | * @param ascending |
124 | */ |
125 | public void setAscending(boolean ascending) { |
126 | this.ascending = ascending; |
127 | } |
128 | |
129 | /** |
130 | * Get a {@link PagingQueryProvider} instance using the provided properties |
131 | * and appropriate for the given database type. |
132 | * |
133 | * @see FactoryBean#getObject() |
134 | */ |
135 | public Object getObject() throws Exception { |
136 | |
137 | DatabaseType type; |
138 | try { |
139 | type = databaseType != null ? DatabaseType.valueOf(databaseType.toUpperCase()) : DatabaseType |
140 | .fromMetaData(dataSource); |
141 | } |
142 | catch (MetaDataAccessException e) { |
143 | throw new IllegalArgumentException( |
144 | "Could not inspect meta data for database type. You have to supply it explicitly.", e); |
145 | } |
146 | |
147 | AbstractSqlPagingQueryProvider provider = providers.get(type); |
148 | Assert.state(provider!=null, "Should not happen: missing PagingQueryProvider for DatabaseType="+type); |
149 | |
150 | provider.setFromClause(fromClause); |
151 | provider.setWhereClause(whereClause); |
152 | provider.setSortKey(sortKey); |
153 | provider.setAscending(ascending); |
154 | if (StringUtils.hasText(selectClause)) { |
155 | provider.setSelectClause(selectClause); |
156 | } |
157 | |
158 | provider.init(dataSource); |
159 | |
160 | return provider; |
161 | |
162 | } |
163 | |
164 | /** |
165 | * Always returns {@link PagingQueryProvider}. |
166 | * |
167 | * @see FactoryBean#getObjectType() |
168 | */ |
169 | public Class<PagingQueryProvider> getObjectType() { |
170 | return PagingQueryProvider.class; |
171 | } |
172 | |
173 | /** |
174 | * Always returns true. |
175 | * @see FactoryBean#isSingleton() |
176 | */ |
177 | public boolean isSingleton() { |
178 | return true; |
179 | } |
180 | |
181 | } |