Project

General

Profile

Download (8.49 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.database;
11

    
12
import java.sql.DatabaseMetaData;
13
import java.sql.SQLException;
14
import java.util.ArrayList;
15
import java.util.List;
16

    
17
import javax.sql.DataSource;
18

    
19
import org.apache.log4j.Logger;
20
import org.hibernate.dialect.Dialect;
21

    
22
import eu.etaxonomy.cdm.database.types.H2DatabaseType;
23
import eu.etaxonomy.cdm.database.types.HSqlDbDatabaseType;
24
import eu.etaxonomy.cdm.database.types.IDatabaseType;
25
import eu.etaxonomy.cdm.database.types.MariaDbDatabaseType;
26
import eu.etaxonomy.cdm.database.types.MySQLDatabaseType;
27
import eu.etaxonomy.cdm.database.types.OdbcDatabaseType;
28
import eu.etaxonomy.cdm.database.types.OracleDatabaseType;
29
import eu.etaxonomy.cdm.database.types.PostgreSQLDatabaseType;
30
import eu.etaxonomy.cdm.database.types.SqlServer2005DatabaseType;
31
import eu.etaxonomy.cdm.database.types.SqlServer2008DatabaseType;
32
import eu.etaxonomy.cdm.database.types.SqlServer2012DatabaseType;
33
import eu.etaxonomy.cdm.database.types.SybaseDatabaseType;
34

    
35
/**
36
 * @author a.mueller
37
 *
38
 */
39
public enum DatabaseTypeEnum {
40
	HSqlDb(1),
41
	MySQL(2),
42
	ODBC(3),
43
	PostgreSQL(4),
44
	Oracle(5),
45
	//SqlServer2000(6),
46
	SqlServer2005(7),
47
	Sybase(8),
48
	H2(9),
49
	SqlServer2008(10),
50
    SqlServer2012(11),
51
    MariaDB(12),  //not yet tested
52
    ;
53

    
54
//	/**
55
//	 *
56
//	 */
57
//	private static final String P6SPY_DRIVER_CLASS_NAME = "com.p6spy.engine.spy.P6SpyDriver";
58

    
59
	/**
60
	 * Constructor
61
	 * @param i
62
	 */
63
	private DatabaseTypeEnum(int i) {
64
		switch(i)
65
        {
66
        	case 1:
67
        		this.dbType = new HSqlDbDatabaseType(); break;
68
        	case 2:
69
        		this.dbType = new MySQLDatabaseType(); break;
70
        	case 3:
71
        		this.dbType = new OdbcDatabaseType(); break;
72
        	case 4:
73
            	this.dbType = new PostgreSQLDatabaseType(); break;
74
        	case 5:
75
             	this.dbType = new OracleDatabaseType(); break;
76
//            case 6:
77
//            	this.dbType = new SqlServer2000DatabaseType(); break;
78
            case 7:
79
            	this.dbType = new SqlServer2005DatabaseType(); break;
80
            case 8:
81
            	this.dbType = new SybaseDatabaseType(); break;
82
            case 9:
83
            	this.dbType = new H2DatabaseType(); break;
84
            case 10:
85
                this.dbType = new SqlServer2008DatabaseType(); break;
86
            case 11:
87
                this.dbType = new SqlServer2012DatabaseType(); break;
88
            case 12:
89
                this.dbType = new MariaDbDatabaseType(); break;
90
            default:
91
                throw new RuntimeException("Database type not handled");
92
        }
93
	}
94

    
95
	public IDatabaseType getDatabaseType(){
96
		return dbType;
97
	}
98

    
99
 	//Logger
100
	private static final Logger logger = Logger.getLogger(DatabaseTypeEnum.class);
101
	protected IDatabaseType dbType;
102

    
103

    
104
    /**
105
     * @return
106
     */
107
    public String getName(){
108
    	return dbType.getName();
109
    }
110

    
111
	/**
112
	 * @return
113
	 */
114
	public String getDriverClassName(){
115
//		if(useP6Spy){
116
//			return P6SPY_DRIVER_CLASS_NAME;
117
//
118
//		} else {
119
			return dbType.getClassString();
120
//		}
121
	}
122

    
123
	/**
124
	 * Returns the DataSource class that the datasource needs to create a spring bean
125
	 * @return the DataSource class
126
	 */
127
	public Class<? extends DataSource> getDataSourceClass(){
128
		return dbType.getDataSourceClass();
129
	}
130

    
131
	/**
132
	 * @return
133
	 */
134
	public String getUrl(){
135
		return dbType.getUrlString();
136
	}
137

    
138
	/**
139
	 * @return
140
	 */
141
	public String getHibernateDialectCanonicalName(){
142
		return dbType.getHibernateDialectCanonicalName();
143
	}
144

    
145
    /**
146
     * @return
147
     */
148
    public int getDefaultPort(){
149
    	return dbType.getDefaultPort();
150
    }
151

    
152
	/**
153
     * returns the connection string
154
     * @param server the server, e.g. IP-Address
155
     * @param database the database name on the server (e.g. "testDB")
156
     * @param port the port number
157
     * @return the connection string
158
     */
159
    public String getConnectionString(ICdmDataSource cdmDataSource){
160
    	String result = dbType.getConnectionString(cdmDataSource);
161
    	logger.debug("Connection String: " + result);
162
        return result;
163
    }
164

    
165
    /**
166
     * Returns the {@link Dialect hibernate dialect} used for this database type.
167
	 * @return hibernate dialect
168
	 */
169
    public Dialect getHibernateDialect(){
170
    	Dialect result = dbType.getHibernateDialect();
171
    	return result;
172
    }
173

    
174
	/**
175
     * Returns the Name of the initialization method to be used when a hibernate datasource is created for this database
176
	 * @return String name of the init method
177
	 */
178
    public String getInitMethod(){
179
    	String result = dbType.getInitMethod();
180
    	logger.debug("InitMethod: " + result);
181
        return result;
182
    }
183

    
184
	/**
185
	 * Returns the Name of the destroying method to be used when a hibernate datasource representing this database is destroyed
186
	 * @return String name of the destroy method
187
	 */
188
    public String getDestroyMethod(){
189
    	String result = dbType.getDestroyMethod();
190
    	logger.debug("DestroyMethod: " + result);
191
        return result;
192
    }
193

    
194
    /**
195
     * Returns a List of all available DatabaseEnums.
196
     * @return List of DatabaseEnums
197
     */
198
    public static List<DatabaseTypeEnum> getAllTypes(){
199
    	List<DatabaseTypeEnum> result = new ArrayList<>();
200
    	for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
201
    		result.add(dbEnum);
202
    	}
203
    	return result;
204
    }
205

    
206
    /**
207
     * Returns the DatabaseTypeEnum to a given DriverClass
208
     * @param strDriverClass
209
     * @return the according DatabaseTypeEnum. Null if the driver class does not exist.
210
     */
211
    public static DatabaseTypeEnum byDriverClass(String strDriverClass){
212
    	if (strDriverClass == null){
213
    	    return null;
214
    	}
215
        for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
216
    		if (dbEnum.getDriverClassName().equals(strDriverClass)){
217
    			return dbEnum;
218
    		}
219
    	}
220
    	logger.info("Unknown driver class: " + strDriverClass);
221
    	return null;
222
    }
223

    
224
    /**
225
     * @param metaData
226
     * @return
227
     */
228
    public static DatabaseTypeEnum byDatabaseMetaData(DatabaseMetaData metaData) {
229
        if (metaData == null){
230
            return null;
231
        }
232

    
233
        //driver
234
        String driver = null;
235
        try {
236
            driver = metaData.getDriverName();
237
        } catch (SQLException e) {
238
            //do nothing
239
        }
240
        DatabaseTypeEnum result = byDriverClass(driver);
241
        if (result != null){
242
            return result;
243
        }
244

    
245
        //product
246
        String product = null;
247
        try {
248
            product = metaData.getDatabaseProductName();
249
        } catch (SQLException e) {
250
            //do nothing
251
        }
252
        if (product == null){
253
            return null;
254
        }
255
        if (product.toLowerCase().matches("\\.*mysql\\.*")){
256
            return MySQL;
257
        }else if (product.toLowerCase().matches("\\.*mariadb\\.*")) {
258
            return MariaDB;
259
        }else if (product.toLowerCase().matches("\\.*hsqldb\\.*")) {
260
            return HSqlDb;
261
        }else if (product.toLowerCase().matches("\\.*oracle\\.*")) {
262
            return Oracle;
263
        }else if (product.toLowerCase().matches("\\.*sybase\\.*")) {
264
            return Sybase;
265
        }else if (product.toLowerCase().matches("\\.*odbc\\.*")) {
266
            return ODBC;
267
        }else if (product.toLowerCase().matches("\\.*postgresql\\.*")) {
268
            return PostgreSQL;
269
        }else if (product.toLowerCase().matches("\\.*sqlserver\\.*")) {
270
            //TODO we need to distinguish versions here once we have sql server 2008 database enum
271
//            metaData.getDatabaseProductVersion()
272
            return SqlServer2005;
273
            //XX
274
        }else if (product.toLowerCase().matches("\\.*h2\\.*")) {
275
            return H2;
276
        }
277
        return null;
278
    }
279

    
280
    /**
281
     * Returns the database type evaluating the connection string.
282
     * To retrieves parts of the connection string (e.g. database name),
283
     * get the database type from the enum and call e.g.
284
     * type.getDatabaseType().getDatabaseNameByConnectionString(url);
285
     *
286
     * @param url the connection string
287
     * @return the database type
288
     */
289
    public static DatabaseTypeEnum byConnectionString(String url) {
290
        if (url == null){
291
            return null;
292
        }
293
        for (DatabaseTypeEnum type : values()){
294
            if (url.startsWith(type.getUrl())){
295
                type.getDatabaseType().getDatabaseNameByConnectionString(url);
296
                return type;
297
            }
298
        }
299
        return null;
300
    }
301

    
302

    
303
}
304

    
(9-9/20)