1   /**
2    * Copyright (c) 2000-2007 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.spring.hibernate;
24  
25  import com.liferay.util.dao.DataAccess;
26  
27  import java.sql.CallableStatement;
28  import java.sql.Connection;
29  import java.sql.DatabaseMetaData;
30  import java.sql.ResultSet;
31  import java.sql.SQLException;
32  
33  import java.util.Enumeration;
34  import java.util.Map;
35  import java.util.Properties;
36  import java.util.Set;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  import org.hibernate.HibernateException;
42  import org.hibernate.LockMode;
43  import org.hibernate.MappingException;
44  import org.hibernate.dialect.DB2400Dialect;
45  import org.hibernate.dialect.Dialect;
46  import org.hibernate.dialect.DialectFactory;
47  import org.hibernate.dialect.SybaseDialect;
48  import org.hibernate.dialect.lock.LockingStrategy;
49  import org.hibernate.exception.SQLExceptionConverter;
50  import org.hibernate.exception.ViolatedConstraintNameExtracter;
51  import org.hibernate.persister.entity.Lockable;
52  import org.hibernate.sql.CaseFragment;
53  import org.hibernate.sql.JoinFragment;
54  
55  /**
56   * <a href="DynamicDialect.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Bruno Farache
60   *
61   */
62  public class DynamicDialect extends Dialect {
63  
64      public DynamicDialect() {
65  
66          // Instantiate the proper dialect
67  
68          Connection con = null;
69  
70          try {
71              con = HibernateUtil.getConnection();
72  
73              DatabaseMetaData metaData = con.getMetaData();
74  
75              String dbName = metaData.getDatabaseProductName();
76              int dbMajorVersion = metaData.getDatabaseMajorVersion();
77  
78              if (_log.isInfoEnabled()) {
79                  _log.info(
80                      "Determining dialect for " + dbName + " " + dbMajorVersion);
81              }
82  
83              if (dbName.startsWith("HSQL")) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn(
86                          "Liferay is configured to use Hypersonic as its " +
87                              "database. Do NOT use Hypersonic in production. " +
88                                  "Hypersonic is an embedded database useful " +
89                                      "for development and demo'ing purposes.");
90                  }
91              }
92  
93              if (dbName.equals("ASE") && (dbMajorVersion == 15)) {
94                  _dialect = new SybaseDialect();
95              }
96              else {
97                  _dialect = DialectFactory.determineDialect(
98                      dbName, dbMajorVersion);
99              }
100 
101             if (_log.isInfoEnabled()) {
102                 _log.info("Using dialect " + _dialect.getClass().getName());
103             }
104         }
105         catch (Exception e) {
106             String msg = e.getMessage();
107 
108             if (msg.indexOf("explicitly set for database: DB2") != -1) {
109                 _dialect = new DB2400Dialect();
110 
111                 if (_log.isWarnEnabled()) {
112                     _log.warn(
113                         "DB2400Dialect was dynamically chosen as the " +
114                             "Hibernate dialect for DB2. This can be " +
115                                 "overriden in portal.properties");
116                 }
117             }
118             else {
119                 _log.error(e, e);
120             }
121         }
122         finally {
123             DataAccess.cleanUp(con);
124         }
125 
126         if (_dialect == null) {
127             throw new RuntimeException("No dialect found");
128         }
129 
130         // Synchorize default properties
131 
132         Properties dynamicDefaultProps = getDefaultProperties();
133         Properties dialectDefaultProps = _dialect.getDefaultProperties();
134 
135         dynamicDefaultProps.clear();
136 
137         Enumeration enu = dialectDefaultProps.propertyNames();
138 
139         while (enu.hasMoreElements()) {
140             String key = (String)enu.nextElement();
141             String value = dialectDefaultProps.getProperty(key);
142 
143             dynamicDefaultProps.setProperty(key, value);
144         }
145     }
146 
147     public Dialect getWrappedDialect() {
148         return _dialect;
149     }
150 
151     public String appendIdentitySelectToInsert(String insertSQL) {
152         return _dialect.appendIdentitySelectToInsert(insertSQL);
153     }
154 
155     public String appendLockHint(LockMode mode, String tableName) {
156         return _dialect.appendLockHint(mode, tableName);
157     }
158 
159     public String applyLocksToSql(
160         String sql, Map aliasedLockModes, Map keyColumnNames) {
161 
162         return _dialect.applyLocksToSql(sql, aliasedLockModes, keyColumnNames);
163     }
164 
165     public boolean areStringComparisonsCaseInsensitive() {
166         return _dialect.areStringComparisonsCaseInsensitive();
167     }
168 
169     public boolean bindLimitParametersFirst() {
170         return _dialect.bindLimitParametersFirst();
171     }
172 
173     public boolean bindLimitParametersInReverseOrder() {
174         return _dialect.bindLimitParametersInReverseOrder();
175     }
176 
177     public SQLExceptionConverter buildSQLExceptionConverter() {
178         return _dialect.buildSQLExceptionConverter();
179     }
180 
181     public char closeQuote() {
182         return _dialect.closeQuote();
183     }
184 
185     public CaseFragment createCaseFragment() {
186         return _dialect.createCaseFragment();
187     }
188 
189     public JoinFragment createOuterJoinFragment() {
190         return _dialect.createOuterJoinFragment();
191     }
192 
193     public boolean doesReadCommittedCauseWritersToBlockReaders() {
194         return _dialect.doesReadCommittedCauseWritersToBlockReaders();
195     }
196 
197     public boolean doesRepeatableReadCauseReadersToBlockWriters() {
198         return _dialect.doesRepeatableReadCauseReadersToBlockWriters();
199     }
200 
201     public boolean dropConstraints() {
202         return _dialect.dropConstraints();
203     }
204 
205     public boolean dropTemporaryTableAfterUse() {
206         return _dialect.dropTemporaryTableAfterUse();
207     }
208 
209     public boolean forUpdateOfColumns() {
210         return _dialect.forUpdateOfColumns();
211     }
212 
213     public String generateTemporaryTableName(String baseTableName) {
214         return _dialect.generateTemporaryTableName(baseTableName);
215     }
216 
217     public String getAddColumnString() {
218         return _dialect.getAddColumnString();
219     }
220 
221     public String getAddForeignKeyConstraintString(
222         String constraintName, String[] foreignKey, String referencedTable,
223         String[] primaryKey, boolean referencesPrimaryKey) {
224 
225         return _dialect.getAddForeignKeyConstraintString(
226             constraintName, foreignKey, referencedTable, primaryKey,
227             referencesPrimaryKey);
228     }
229 
230     public String getAddPrimaryKeyConstraintString(String constraintName) {
231         return _dialect.getAddPrimaryKeyConstraintString(constraintName);
232     }
233 
234     public String getCascadeConstraintsString() {
235         return _dialect.getCascadeConstraintsString();
236     }
237 
238     public String getCastTypeName(int code) {
239         return _dialect.getCastTypeName(code);
240     }
241 
242     public String getColumnComment(String comment) {
243         return _dialect.getColumnComment(comment);
244     }
245 
246     public String getCreateMultisetTableString() {
247         return _dialect.getCreateMultisetTableString();
248     }
249 
250     /**
251      * @deprecated
252      */
253     public String[] getCreateSequenceStrings(String sequenceName)
254         throws MappingException {
255 
256         return _dialect.getCreateSequenceStrings(sequenceName);
257     }
258 
259     public String[] getCreateSequenceStrings(
260             String sequenceName, int initialValue, int incrementSize)
261         throws MappingException {
262 
263         return _dialect.getCreateSequenceStrings(
264             sequenceName, initialValue, incrementSize);
265     }
266 
267     public String getCreateTableString() {
268         return _dialect.getCreateTableString();
269     }
270 
271     public String getCreateTemporaryTablePostfix() {
272         return _dialect.getCreateTemporaryTablePostfix();
273     }
274 
275     public String getCreateTemporaryTableString() {
276         return _dialect.getCreateTemporaryTableString();
277     }
278 
279     public String getCurrentTimestampSelectString() {
280         return _dialect.getCurrentTimestampSelectString();
281     }
282 
283     public String getCurrentTimestampSQLFunctionName() {
284         return _dialect.getCurrentTimestampSQLFunctionName();
285     }
286 
287     public String getDropForeignKeyString() {
288         return _dialect.getDropForeignKeyString();
289     }
290 
291     public String[] getDropSequenceStrings(String sequenceName)
292         throws MappingException {
293 
294         return _dialect.getDropSequenceStrings(sequenceName);
295     }
296 
297     public String getForUpdateNowaitString() {
298         return _dialect.getForUpdateNowaitString();
299     }
300 
301     public String getForUpdateNowaitString(String aliases) {
302         return _dialect.getForUpdateNowaitString(aliases);
303     }
304 
305     public String getForUpdateString() {
306         return _dialect.getForUpdateString();
307     }
308 
309     public String getForUpdateString(LockMode lockMode) {
310         return _dialect.getForUpdateString(lockMode);
311     }
312 
313     public String getForUpdateString(String aliases) {
314         return _dialect.getForUpdateString(aliases);
315     }
316 
317     public String getHibernateTypeName(int code) throws HibernateException {
318         return _dialect.getHibernateTypeName(code);
319     }
320 
321     public String getHibernateTypeName(
322             int code, int length, int precision, int scale)
323         throws HibernateException {
324 
325         return _dialect.getHibernateTypeName(code, length, precision, scale);
326     }
327 
328     public String getIdentityColumnString(int type) throws MappingException {
329         return _dialect.getIdentityColumnString(type);
330     }
331 
332     public String getIdentityInsertString() {
333         return _dialect.getIdentityInsertString();
334     }
335 
336     public String getIdentitySelectString(String table, String column, int type)
337         throws MappingException {
338 
339         return _dialect.getIdentitySelectString(table, column, type);
340     }
341 
342     public Set getKeywords() {
343         return _dialect.getKeywords();
344     }
345 
346     public String getLimitString(String querySelect, int hasOffset, int limit) {
347         return _dialect.getLimitString(querySelect, hasOffset, limit);
348     }
349 
350     public LockingStrategy getLockingStrategy(
351         Lockable lockable, LockMode lockMode) {
352 
353         return _dialect.getLockingStrategy(lockable, lockMode);
354     }
355 
356     public String getLowercaseFunction() {
357         return _dialect.getLowercaseFunction();
358     }
359 
360     public int getMaxAliasLength() {
361         return _dialect.getMaxAliasLength();
362     }
363 
364     public Class getNativeIdentifierGeneratorClass() {
365         return _dialect.getNativeIdentifierGeneratorClass();
366     }
367 
368     public String getNoColumnsInsertString() {
369         return _dialect.getNoColumnsInsertString();
370     }
371 
372     public String getNullColumnString() {
373         return _dialect.getNullColumnString();
374     }
375 
376     public String getQuerySequencesString() {
377         return _dialect.getQuerySequencesString();
378     }
379 
380     public ResultSet getResultSet(CallableStatement ps) throws SQLException {
381         return _dialect.getResultSet(ps);
382     }
383 
384     public String getSelectClauseNullString(int sqlType) {
385         return _dialect.getSelectClauseNullString(sqlType);
386     }
387 
388     public String getSelectGUIDString() {
389         return _dialect.getSelectGUIDString();
390     }
391 
392     public String getSelectSequenceNextValString(String sequenceName)
393         throws MappingException {
394 
395         return _dialect.getSelectSequenceNextValString(sequenceName);
396     }
397 
398     public String getSequenceNextValString(String sequenceName)
399         throws MappingException {
400 
401         return _dialect.getSequenceNextValString(sequenceName);
402     }
403 
404     public String getTableComment(String comment) {
405         return _dialect.getTableComment(comment);
406     }
407 
408     public String getTableTypeString() {
409         return _dialect.getTableTypeString();
410     }
411 
412     public String getTypeName(int code) throws HibernateException {
413         return _dialect.getTypeName(code);
414     }
415 
416     public String getTypeName(int code, int length, int precision, int scale)
417         throws HibernateException {
418 
419         return _dialect.getTypeName(code, length, precision, scale);
420     }
421 
422     public ViolatedConstraintNameExtracter
423         getViolatedConstraintNameExtracter() {
424 
425         return _dialect.getViolatedConstraintNameExtracter();
426     }
427 
428     public boolean hasAlterTable() {
429         return _dialect.hasAlterTable();
430     }
431 
432     public boolean hasDataTypeInIdentityColumn() {
433         return _dialect.hasDataTypeInIdentityColumn();
434     }
435 
436     public boolean hasSelfReferentialForeignKeyBug() {
437         return _dialect.hasSelfReferentialForeignKeyBug();
438     }
439 
440     public boolean isCurrentTimestampSelectStringCallable() {
441         return _dialect.isCurrentTimestampSelectStringCallable();
442     }
443 
444     public char openQuote() {
445         return _dialect.openQuote();
446     }
447 
448     public Boolean performTemporaryTableDDLInIsolation() {
449         return _dialect.performTemporaryTableDDLInIsolation();
450     }
451 
452     public boolean qualifyIndexName() {
453         return _dialect.qualifyIndexName();
454     }
455 
456     public int registerResultSetOutParameter(
457             CallableStatement statement, int col)
458         throws SQLException {
459 
460         return _dialect.registerResultSetOutParameter(statement, col);
461     }
462 
463     public boolean supportsBindAsCallableArgument() {
464         return _dialect.supportsBindAsCallableArgument();
465     }
466 
467     public boolean supportsCascadeDelete() {
468         return _dialect.supportsCascadeDelete();
469     }
470 
471     public boolean supportsCircularCascadeDeleteConstraints() {
472         return _dialect.supportsCircularCascadeDeleteConstraints();
473     }
474 
475     public boolean supportsColumnCheck() {
476         return _dialect.supportsColumnCheck();
477     }
478 
479     public boolean supportsCommentOn() {
480         return _dialect.supportsCommentOn();
481     }
482 
483     public boolean supportsCurrentTimestampSelection() {
484         return _dialect.supportsCurrentTimestampSelection();
485     }
486 
487     public boolean supportsEmptyInList() {
488         return _dialect.supportsEmptyInList();
489     }
490 
491     public boolean supportsExistsInSelect() {
492         return _dialect.supportsExistsInSelect();
493     }
494 
495     public boolean supportsExpectedLobUsagePattern() {
496         return _dialect.supportsExpectedLobUsagePattern();
497     }
498 
499     public boolean supportsIdentityColumns() {
500         return _dialect.supportsIdentityColumns();
501     }
502 
503     public boolean supportsIfExistsAfterTableName() {
504         return _dialect.supportsIfExistsAfterTableName();
505     }
506 
507     public boolean supportsIfExistsBeforeTableName() {
508         return _dialect.supportsIfExistsBeforeTableName();
509     }
510 
511     public boolean supportsInsertSelectIdentity() {
512         return _dialect.supportsInsertSelectIdentity();
513     }
514 
515     public boolean supportsLimit() {
516         return _dialect.supportsLimit();
517     }
518 
519     public boolean supportsLimitOffset() {
520         return _dialect.supportsLimitOffset();
521     }
522 
523     public boolean supportsLobValueChangePropogation() {
524         return _dialect.supportsLobValueChangePropogation();
525     }
526 
527     public boolean supportsNotNullUnique() {
528         return _dialect.supportsNotNullUnique();
529     }
530 
531     public boolean supportsOuterJoinForUpdate() {
532         return _dialect.supportsOuterJoinForUpdate();
533     }
534 
535     public boolean supportsParametersInInsertSelect() {
536         return _dialect.supportsParametersInInsertSelect();
537     }
538 
539     public boolean supportsPooledSequences() {
540         return _dialect.supportsPooledSequences();
541     }
542 
543     public boolean supportsResultSetPositionQueryMethodsOnForwardOnlyCursor() {
544         return _dialect.
545             supportsResultSetPositionQueryMethodsOnForwardOnlyCursor();
546     }
547 
548     public boolean supportsRowValueConstructorSyntax() {
549         return _dialect.supportsRowValueConstructorSyntax();
550     }
551 
552     public boolean supportsRowValueConstructorSyntaxInInList() {
553         return _dialect.supportsRowValueConstructorSyntaxInInList();
554     }
555 
556     public boolean supportsSequences() {
557         return _dialect.supportsSequences();
558     }
559 
560     public boolean supportsSubqueryOnMutatingTable() {
561         return _dialect.supportsSubqueryOnMutatingTable();
562     }
563 
564     public boolean supportsSubselectAsInPredicateLHS() {
565         return _dialect.supportsSubselectAsInPredicateLHS();
566     }
567 
568     public boolean supportsTableCheck() {
569         return _dialect.supportsTableCheck();
570     }
571 
572     public boolean supportsTemporaryTables() {
573         return _dialect.supportsTemporaryTables();
574     }
575 
576     public boolean supportsUnboundedLobLocatorMaterialization() {
577         return _dialect.supportsUnboundedLobLocatorMaterialization();
578     }
579 
580     public boolean supportsUnionAll() {
581         return _dialect.supportsUnionAll();
582     }
583 
584     public boolean supportsUnique() {
585         return _dialect.supportsUnique();
586     }
587 
588     public boolean supportsUniqueConstraintInCreateAlterTable() {
589         return _dialect.supportsUniqueConstraintInCreateAlterTable();
590     }
591 
592     public boolean supportsVariableLimit() {
593         return _dialect.supportsVariableLimit();
594     }
595 
596     public String toBooleanValueString(boolean bool) {
597         return _dialect.toBooleanValueString(bool);
598     }
599 
600     public String toString() {
601         if (_dialect != null) {
602             return _dialect.toString();
603         }
604         else {
605             return null;
606         }
607     }
608 
609     public String transformSelectString(String select) {
610         return _dialect.transformSelectString(select);
611     }
612 
613     public boolean useInputStreamToInsertBlob() {
614         return _dialect.useInputStreamToInsertBlob();
615     }
616 
617     public boolean useMaxForLimit() {
618         return _dialect.useMaxForLimit();
619     }
620 
621     private static Log _log = LogFactory.getLog(DynamicDialect.class);
622 
623     private Dialect _dialect;
624 
625 }