1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.db;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactory;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.util.PropsValues;
22  
23  import org.hibernate.dialect.DB2Dialect;
24  import org.hibernate.dialect.DerbyDialect;
25  import org.hibernate.dialect.Dialect;
26  import org.hibernate.dialect.FirebirdDialect;
27  import org.hibernate.dialect.HSQLDialect;
28  import org.hibernate.dialect.InformixDialect;
29  import org.hibernate.dialect.IngresDialect;
30  import org.hibernate.dialect.InterbaseDialect;
31  import org.hibernate.dialect.JDataStoreDialect;
32  import org.hibernate.dialect.MySQLDialect;
33  import org.hibernate.dialect.Oracle8iDialect;
34  import org.hibernate.dialect.Oracle9Dialect;
35  import org.hibernate.dialect.PostgreSQLDialect;
36  import org.hibernate.dialect.SAPDBDialect;
37  import org.hibernate.dialect.SQLServerDialect;
38  import org.hibernate.dialect.Sybase11Dialect;
39  import org.hibernate.dialect.SybaseASE15Dialect;
40  import org.hibernate.dialect.SybaseAnywhereDialect;
41  import org.hibernate.dialect.SybaseDialect;
42  
43  /**
44   * <a href="DBFactoryImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Alexander Chow
47   * @author Brian Wing Shun Chan
48   */
49  @SuppressWarnings("deprecation")
50  public class DBFactoryImpl implements DBFactory {
51  
52      public DB getDB() {
53          if (_db == null) {
54              try {
55                  if (_log.isInfoEnabled()) {
56                      _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
57                  }
58  
59                  Dialect dialect = (Dialect)Class.forName(
60                      PropsValues.HIBERNATE_DIALECT).newInstance();
61  
62                  setDB(dialect);
63              }
64              catch (Exception e) {
65                  _log.error(e, e);
66              }
67          }
68  
69          return _db;
70      }
71  
72      public DB getDB(Object dialect) {
73          DB db = null;
74  
75          if (dialect instanceof DB2Dialect) {
76              if (dialect instanceof DerbyDialect) {
77                  db = DerbyDB.getInstance();
78              }
79              else {
80                  db = DB2DB.getInstance();
81              }
82          }
83          else if (dialect instanceof HSQLDialect) {
84              db = HypersonicDB.getInstance();
85          }
86          else if (dialect instanceof InformixDialect) {
87              db = InformixDB.getInstance();
88          }
89          else if (dialect instanceof IngresDialect) {
90              db = IngresDB.getInstance();
91          }
92          else if (dialect instanceof InterbaseDialect) {
93              if (dialect instanceof FirebirdDialect) {
94                  db = FirebirdDB.getInstance();
95              }
96              else {
97                  db = InterBaseDB.getInstance();
98              }
99          }
100         else if (dialect instanceof JDataStoreDialect) {
101             db = JDataStoreDB.getInstance();
102         }
103         else if (dialect instanceof MySQLDialect) {
104             db = MySQLDB.getInstance();
105         }
106         else if (dialect instanceof Oracle8iDialect ||
107                  dialect instanceof Oracle9Dialect) {
108 
109             db = OracleDB.getInstance();
110         }
111         else if (dialect instanceof PostgreSQLDialect) {
112             db = PostgreSQLDB.getInstance();
113         }
114         else if (dialect instanceof SAPDBDialect) {
115             db = SAPDB.getInstance();
116         }
117         else if (dialect instanceof SQLServerDialect) {
118             db = SQLServerDB.getInstance();
119         }
120         else if (dialect instanceof SybaseDialect ||
121                  dialect instanceof Sybase11Dialect ||
122                  dialect instanceof SybaseAnywhereDialect ||
123                  dialect instanceof SybaseASE15Dialect) {
124 
125             db = SybaseDB.getInstance();
126         }
127 
128         return db;
129     }
130 
131     public DB getDB(String type) {
132         DB db = null;
133 
134         if (type.equals(DB.TYPE_DB2)) {
135             db = DB2DB.getInstance();
136         }
137         else if (type.equals(DB.TYPE_DERBY)) {
138             db = DerbyDB.getInstance();
139         }
140         else if (type.equals(DB.TYPE_FIREBIRD)) {
141             db = FirebirdDB.getInstance();
142         }
143         else if (type.equals(DB.TYPE_HYPERSONIC)) {
144             db = HypersonicDB.getInstance();
145         }
146         else if (type.equals(DB.TYPE_INFORMIX)) {
147             db = InformixDB.getInstance();
148         }
149         else if (type.equals(DB.TYPE_INGRES)) {
150             db = IngresDB.getInstance();
151         }
152         else if (type.equals(DB.TYPE_INTERBASE)) {
153             db = InterBaseDB.getInstance();
154         }
155         else if (type.equals(DB.TYPE_JDATASTORE)) {
156             db = JDataStoreDB.getInstance();
157         }
158         else if (type.equals(DB.TYPE_MYSQL)) {
159             db = MySQLDB.getInstance();
160         }
161         else if (type.equals(DB.TYPE_ORACLE)) {
162             db = OracleDB.getInstance();
163         }
164         else if (type.equals(DB.TYPE_POSTGRESQL)) {
165             db = PostgreSQLDB.getInstance();
166         }
167         else if (type.equals(DB.TYPE_SAP)) {
168             db = SAPDB.getInstance();
169         }
170         else if (type.equals(DB.TYPE_SQLSERVER)) {
171             db = SQLServerDB.getInstance();
172         }
173         else if (type.equals(DB.TYPE_SYBASE)) {
174             db = SybaseDB.getInstance();
175         }
176 
177         return db;
178     }
179 
180     public void setDB(Object dialect) {
181         if (_db == null) {
182             _db = getDB(dialect);
183 
184             if (_db == null) {
185                 _log.error(
186                     "No DB implementation exists for " +
187                         dialect.getClass().getName());
188             }
189             else {
190                 if (_log.isDebugEnabled()) {
191                     _log.debug(
192                         "Using DB implementation " + _db.getClass().getName() +
193                             " for " + dialect.getClass().getName());
194                 }
195             }
196         }
197     }
198 
199     public void setDB(String type) {
200         if (_db == null) {
201             _db = getDB(type);
202 
203             if (_db == null) {
204                 _log.error("No DB implementation exists for " + type);
205             }
206             else {
207                 if (_log.isDebugEnabled()) {
208                     _log.debug(
209                         "Using DB implementation " + _db.getClass().getName() +
210                             " for " + type);
211                 }
212             }
213         }
214     }
215 
216     private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
217 
218     private static DB _db;
219 
220 }