Project

General

Profile

Download (7.6 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.SybaseDatabaseType;
31

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

    
48
	/**
49
	 *
50
	 */
51
	private static final String P6SPY_DRIVER_CLASS_NAME = "com.p6spy.engine.spy.P6SpyDriver";
52
	private boolean useP6Spy = false;
53

    
54

    
55
	/**
56
	 * @return the useP6Spy
57
	 */
58
	public boolean isUseP6Spy() {
59
		return useP6Spy;
60
	}
61

    
62
	/**
63
	 * @param useP6Spy the useP6Spy to set
64
	 */
65
	public void setUseP6Spy(boolean useP6Spy) {
66
		this.useP6Spy = useP6Spy;
67
	}
68

    
69
	/**
70
	 * Constructor
71
	 * @param i
72
	 */
73
	private DatabaseTypeEnum(int i) {
74
		switch(i)
75
        {
76
        	case 1:
77
        		this.dbType = new HSqlDbDatabaseType(); break;
78
        	case 2:
79
        		this.dbType = new MySQLDatabaseType(); break;
80
        	case 3:
81
        		this.dbType = new OdbcDatabaseType(); break;
82
        	case 4:
83
            	this.dbType = new PostgreSQLDatabaseType(); break;
84
        	case 5:
85
             	this.dbType = new OracleDatabaseType(); break;
86
//            case 6:
87
//            	this.dbType = new SqlServer2000DatabaseType(); break;
88
            case 7:
89
            	this.dbType = new SqlServer2005DatabaseType(); break;
90
            case 8:
91
            	this.dbType = new SybaseDatabaseType(); break;
92
            case 9:
93
            	this.dbType = new H2DatabaseType(); break;
94
            default:
95
                //TODO Exception
96
        }
97
	}
98

    
99
	public IDatabaseType getDatabaseType(){
100
		return dbType;
101
	}
102

    
103
 	//Logger
104
	private static final Logger logger = Logger.getLogger(DatabaseTypeEnum.class);
105
	protected IDatabaseType dbType;
106

    
107

    
108
    /**
109
     * @return
110
     */
111
    public String getName(){
112
    	return dbType.getName();
113
    }
114

    
115
	/**
116
	 * @return
117
	 */
118
	public String getDriverClassName(){
119
		if(useP6Spy){
120
			return P6SPY_DRIVER_CLASS_NAME;
121

    
122
		} else {
123
			return dbType.getClassString();
124
		}
125
	}
126

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

    
135
	/**
136
	 * @return
137
	 */
138
	public String getUrl(){
139
		return dbType.getUrlString();
140
	}
141

    
142
	/**
143
	 * @return
144
	 */
145
	public String getHibernateDialectCanonicalName(){
146
		return dbType.getHibernateDialectCanonicalName();
147
	}
148

    
149
    /**
150
     * @return
151
     */
152
    public int getDefaultPort(){
153
    	return dbType.getDefaultPort();
154
    }
155

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

    
169
    /**
170
     * Returns the {@link Dialect hibernate dialect} used for this database type.
171
	 * @return hibernate dialect
172
	 */
173
    public Dialect getHibernateDialect(){
174
    	Dialect result = dbType.getHibernateDialect();
175
    	return result;
176
    }
177

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

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

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

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

    
228
    /**
229
     * @param metaData
230
     * @return
231
     */
232
    public static DatabaseTypeEnum byDatabaseMetaData(DatabaseMetaData metaData) {
233
        if (metaData == null){
234
            return null;
235
        }
236

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

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

    
281

    
282

    
283

    
284
}
285

    
(9-9/20)