Project

General

Profile

Download (8.3 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.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
import org.hibernate.dialect.Dialect;
22

    
23
import eu.etaxonomy.cdm.database.types.H2DatabaseType;
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
	MySQL(1),
41
	ODBC(2),
42
	PostgreSQL(3),
43
	Oracle(4),
44
	//SqlServer2000(5),
45
	SqlServer2005(6),
46
	Sybase(7),
47
	H2(8),
48
	SqlServer2008(9),
49
    SqlServer2012(10),
50
    MariaDB(11),  //not yet tested
51
    ;
52

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

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

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

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

    
100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
297

    
298
}
299

    
(10-10/21)