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