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