1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.dao.db.DBFactory;
27  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.util.PropsValues;
31  
32  import org.hibernate.dialect.DB2Dialect;
33  import org.hibernate.dialect.DerbyDialect;
34  import org.hibernate.dialect.Dialect;
35  import org.hibernate.dialect.FirebirdDialect;
36  import org.hibernate.dialect.HSQLDialect;
37  import org.hibernate.dialect.InformixDialect;
38  import org.hibernate.dialect.IngresDialect;
39  import org.hibernate.dialect.InterbaseDialect;
40  import org.hibernate.dialect.JDataStoreDialect;
41  import org.hibernate.dialect.MySQLDialect;
42  import org.hibernate.dialect.Oracle10gDialect;
43  import org.hibernate.dialect.Oracle8iDialect;
44  import org.hibernate.dialect.Oracle9Dialect;
45  import org.hibernate.dialect.Oracle9iDialect;
46  import org.hibernate.dialect.OracleDialect;
47  import org.hibernate.dialect.PostgreSQLDialect;
48  import org.hibernate.dialect.SAPDBDialect;
49  import org.hibernate.dialect.SQLServerDialect;
50  import org.hibernate.dialect.SybaseDialect;
51  
52  /**
53   * <a href="DBFactoryImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Alexander Chow
56   * @author Brian Wing Shun Chan
57   */
58  public class DBFactoryImpl implements DBFactory {
59  
60      public DB getDB() {
61          if (_db == null) {
62              try {
63                  if (_log.isInfoEnabled()) {
64                      _log.info("Using dialect " + PropsValues.HIBERNATE_DIALECT);
65                  }
66  
67                  Dialect dialect = (Dialect)Class.forName(
68                      PropsValues.HIBERNATE_DIALECT).newInstance();
69  
70                  DBFactoryUtil.setDB(dialect);
71              }
72              catch (Exception e) {
73                  _log.error(e, e);
74              }
75          }
76  
77          return _db;
78      }
79  
80      public DB getDB(Object dialect) {
81          DB db = null;
82  
83          if (dialect instanceof DB2Dialect) {
84              if (dialect instanceof DerbyDialect) {
85                  db = DerbyDB.getInstance();
86              }
87              else {
88                  db = DB2DB.getInstance();
89              }
90          }
91          else if (dialect instanceof HSQLDialect) {
92              db = HypersonicDB.getInstance();
93          }
94          else if (dialect instanceof InformixDialect) {
95              db = InformixDB.getInstance();
96          }
97          else if (dialect instanceof IngresDialect) {
98              db = IngresDB.getInstance();
99          }
100         else if (dialect instanceof InterbaseDialect) {
101             if (dialect instanceof FirebirdDialect) {
102                 db = FirebirdDB.getInstance();
103             }
104             else {
105                 db = InterBaseDB.getInstance();
106             }
107         }
108         else if (dialect instanceof JDataStoreDialect) {
109             db = JDataStoreDB.getInstance();
110         }
111         else if (dialect instanceof MySQLDialect) {
112             db = MySQLDB.getInstance();
113         }
114         else if (dialect instanceof OracleDialect ||
115                  dialect instanceof Oracle8iDialect ||
116                  dialect instanceof Oracle9Dialect ||
117                  dialect instanceof Oracle9iDialect ||
118                  dialect instanceof Oracle10gDialect) {
119 
120             db = OracleDB.getInstance();
121         }
122         else if (dialect instanceof PostgreSQLDialect) {
123             db = PostgreSQLDB.getInstance();
124         }
125         else if (dialect instanceof SAPDBDialect) {
126             db = SAPDB.getInstance();
127         }
128         else if (dialect instanceof SybaseDialect) {
129             if (dialect instanceof SQLServerDialect) {
130                 db = SQLServerDB.getInstance();
131             }
132             else {
133                 db = SybaseDB.getInstance();
134             }
135         }
136 
137         return db;
138     }
139 
140     public DB getDB(String type) {
141         DB db = null;
142 
143         if (type.equals(DB.TYPE_DB2)) {
144             db = DB2DB.getInstance();
145         }
146         else if (type.equals(DB.TYPE_DERBY)) {
147             db = DerbyDB.getInstance();
148         }
149         else if (type.equals(DB.TYPE_FIREBIRD)) {
150             db = FirebirdDB.getInstance();
151         }
152         else if (type.equals(DB.TYPE_HYPERSONIC)) {
153             db = HypersonicDB.getInstance();
154         }
155         else if (type.equals(DB.TYPE_INFORMIX)) {
156             db = InformixDB.getInstance();
157         }
158         else if (type.equals(DB.TYPE_INGRES)) {
159             db = IngresDB.getInstance();
160         }
161         else if (type.equals(DB.TYPE_INTERBASE)) {
162             db = InterBaseDB.getInstance();
163         }
164         else if (type.equals(DB.TYPE_JDATASTORE)) {
165             db = JDataStoreDB.getInstance();
166         }
167         else if (type.equals(DB.TYPE_MYSQL)) {
168             db = MySQLDB.getInstance();
169         }
170         else if (type.equals(DB.TYPE_ORACLE)) {
171             db = OracleDB.getInstance();
172         }
173         else if (type.equals(DB.TYPE_POSTGRESQL)) {
174             db = PostgreSQLDB.getInstance();
175         }
176         else if (type.equals(DB.TYPE_SAP)) {
177             db = SAPDB.getInstance();
178         }
179         else if (type.equals(DB.TYPE_SQLSERVER)) {
180             db = SQLServerDB.getInstance();
181         }
182         else if (type.equals(DB.TYPE_SYBASE)) {
183             db = SybaseDB.getInstance();
184         }
185 
186         return db;
187     }
188 
189     public void setDB(Object dialect) {
190         if (_db == null) {
191             _db = getDB(dialect);
192         }
193     }
194 
195     public void setDB(String type) {
196         if (_db == null) {
197             _db = getDB(type);
198         }
199     }
200 
201     private static Log _log = LogFactoryUtil.getLog(DBFactoryImpl.class);
202 
203     private static DB _db;
204 
205 }