1 | /* |
2 | * Copyright 2006-2007 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 | |
17 | package org.springframework.batch.support; |
18 | |
19 | import org.springframework.jdbc.support.JdbcUtils; |
20 | import org.springframework.jdbc.support.MetaDataAccessException; |
21 | import org.springframework.util.StringUtils; |
22 | |
23 | import javax.sql.DataSource; |
24 | import java.util.HashMap; |
25 | import java.util.Map; |
26 | |
27 | |
28 | /** |
29 | * Enum representing a database type, such as DB2 or oracle. The type also |
30 | * contains a product name, which is expected to be the same as the product name |
31 | * provided by the database driver's metadata. |
32 | * |
33 | * @author Lucas Ward |
34 | * @since 2.0 |
35 | */ |
36 | public enum DatabaseType { |
37 | |
38 | DERBY("Apache Derby"), |
39 | DB2("DB2"), |
40 | DB2ZOS("DB2ZOS"), |
41 | HSQL("HSQL Database Engine"), |
42 | SQLSERVER("Microsoft SQL Server"), |
43 | MYSQL("MySQL"), |
44 | ORACLE("Oracle"), |
45 | POSTGRES("PostgreSQL"), |
46 | SYBASE("Sybase"), H2("H2"); |
47 | |
48 | private static final Map<String, DatabaseType> nameMap; |
49 | |
50 | static{ |
51 | nameMap = new HashMap<String, DatabaseType>(); |
52 | for(DatabaseType type: values()){ |
53 | nameMap.put(type.getProductName(), type); |
54 | } |
55 | } |
56 | //A description is necessary due to the nature of database descriptions |
57 | //in metadata. |
58 | private final String productName; |
59 | |
60 | private DatabaseType(String productName) { |
61 | this.productName = productName; |
62 | } |
63 | |
64 | public String getProductName() { |
65 | return productName; |
66 | } |
67 | |
68 | /** |
69 | * Static method to obtain a DatabaseType from the provided product name. |
70 | * |
71 | * @param productName |
72 | * @return DatabaseType for given product name. |
73 | * @throws IllegalArgumentException if none is found. |
74 | */ |
75 | public static DatabaseType fromProductName(String productName){ |
76 | if(!nameMap.containsKey(productName)){ |
77 | throw new IllegalArgumentException("DatabaseType not found for product name: [" + |
78 | productName + "]"); |
79 | } |
80 | else{ |
81 | return nameMap.get(productName); |
82 | } |
83 | } |
84 | |
85 | /** |
86 | * Convenience method that pulls a database product name from the DataSource's metadata. |
87 | * |
88 | * @param dataSource |
89 | * @return DatabaseType |
90 | * @throws MetaDataAccessException |
91 | */ |
92 | public static DatabaseType fromMetaData(DataSource dataSource) throws MetaDataAccessException { |
93 | String databaseProductName = |
94 | JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName").toString(); |
95 | if (StringUtils.hasText(databaseProductName) && !databaseProductName.equals("DB2/Linux") && databaseProductName.startsWith("DB2")) { |
96 | String databaseProductVersion = |
97 | JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductVersion").toString(); |
98 | if (!databaseProductVersion.startsWith("SQL")) { |
99 | databaseProductName = "DB2ZOS"; |
100 | } |
101 | else { |
102 | databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName); |
103 | } |
104 | } |
105 | else { |
106 | databaseProductName = JdbcUtils.commonDatabaseName(databaseProductName); |
107 | } |
108 | return fromProductName(databaseProductName); |
109 | } |
110 | } |