Project

General

Profile

Download (8.18 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.MySQLDatabaseType;
26
import eu.etaxonomy.cdm.database.types.OdbcDatabaseType;
27
import eu.etaxonomy.cdm.database.types.OracleDatabaseType;
28
import eu.etaxonomy.cdm.database.types.PostgreSQLDatabaseType;
29
import eu.etaxonomy.cdm.database.types.SqlServer2005DatabaseType;
30
import eu.etaxonomy.cdm.database.types.SqlServer2008DatabaseType;
31
import eu.etaxonomy.cdm.database.types.SqlServer2012DatabaseType;
32
import eu.etaxonomy.cdm.database.types.SybaseDatabaseType;
33

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

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

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

    
91
	public IDatabaseType getDatabaseType(){
92
		return dbType;
93
	}
94

    
95
 	//Logger
96
	private static final Logger logger = Logger.getLogger(DatabaseTypeEnum.class);
97
	protected IDatabaseType dbType;
98

    
99

    
100
    /**
101
     * @return
102
     */
103
    public String getName(){
104
    	return dbType.getName();
105
    }
106

    
107
	/**
108
	 * @return
109
	 */
110
	public String getDriverClassName(){
111
//		if(useP6Spy){
112
//			return P6SPY_DRIVER_CLASS_NAME;
113
//
114
//		} else {
115
			return dbType.getClassString();
116
//		}
117
	}
118

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

    
127
	/**
128
	 * @return
129
	 */
130
	public String getUrl(){
131
		return dbType.getUrlString();
132
	}
133

    
134
	/**
135
	 * @return
136
	 */
137
	public String getHibernateDialectCanonicalName(){
138
		return dbType.getHibernateDialectCanonicalName();
139
	}
140

    
141
    /**
142
     * @return
143
     */
144
    public int getDefaultPort(){
145
    	return dbType.getDefaultPort();
146
    }
147

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

    
161
    /**
162
     * Returns the {@link Dialect hibernate dialect} used for this database type.
163
	 * @return hibernate dialect
164
	 */
165
    public Dialect getHibernateDialect(){
166
    	Dialect result = dbType.getHibernateDialect();
167
    	return result;
168
    }
169

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

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

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

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

    
220
    /**
221
     * @param metaData
222
     * @return
223
     */
224
    public static DatabaseTypeEnum byDatabaseMetaData(DatabaseMetaData metaData) {
225
        if (metaData == null){
226
            return null;
227
        }
228

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

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

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

    
296

    
297
}
298

    
(9-9/20)