/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.dialect;
import java.sql.Types;
//import org.hibernate.JDBCException;
//import org.hibernate.PessimisticLockException;
import org.hibernate.cfg.AvailableSettings;
//import org.hibernate.exception.ConstraintViolationException;
//import org.hibernate.exception.LockAcquisitionException;
//import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
//import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
//import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
//import org.hibernate.internal.CoreMessageLogger;
//import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.ReflectHelper;
//import org.jboss.logging.Logger;
/**
* A dialect compatible with the H2 database.
*
* @author Thomas Mueller
*/
public class H2Dialect extends Dialect {
// private static final CoreMessageLogger LOG = Logger.getMessageLogger(
// CoreMessageLogger.class,
// H2Dialect.class.getName()
// );
private final String querySequenceString;
/**
* Constructs a H2Dialect
*/
public H2Dialect() {
super();
String querySequenceString = "select sequence_name from information_schema.sequences";
try {
// HHH-2300
final Class h2ConstantsClass = ReflectHelper.classForName( "org.h2.engine.Constants" );
final int majorVersion = (Integer) h2ConstantsClass.getDeclaredField( "VERSION_MAJOR" ).get( null );
final int minorVersion = (Integer) h2ConstantsClass.getDeclaredField( "VERSION_MINOR" ).get( null );
final int buildId = (Integer) h2ConstantsClass.getDeclaredField( "BUILD_ID" ).get( null );
if ( buildId < 32 ) {
querySequenceString = "select name from information_schema.sequences";
}
if ( ! ( majorVersion > 1 || minorVersion > 2 || buildId >= 139 ) ) {
// LOG.unsupportedMultiTableBulkHqlJpaql( majorVersion, minorVersion, buildId );
}
}
catch ( Exception e ) {
// probably H2 not in the classpath, though in certain app server environments it might just mean we are
// not using the correct classloader
// LOG.undeterminedH2Version();
}
this.querySequenceString = querySequenceString;
registerColumnType( Types.BOOLEAN, "boolean" );
registerColumnType( Types.BIGINT, "bigint" );
registerColumnType( Types.BINARY, "binary" );
registerColumnType( Types.BIT, "boolean" );
registerColumnType( Types.CHAR, "char($l)" );
registerColumnType( Types.DATE, "date" );
registerColumnType( Types.DECIMAL, "decimal($p,$s)" );
registerColumnType( Types.NUMERIC, "decimal($p,$s)" );
registerColumnType( Types.DOUBLE, "double" );
registerColumnType( Types.FLOAT, "float" );
registerColumnType( Types.INTEGER, "integer" );
registerColumnType( Types.LONGVARBINARY, "longvarbinary" );
registerColumnType( Types.LONGVARCHAR, "longvarchar" );
registerColumnType( Types.REAL, "real" );
registerColumnType( Types.SMALLINT, "smallint" );
registerColumnType( Types.TINYINT, "tinyint" );
registerColumnType( Types.TIME, "time" );
registerColumnType( Types.TIMESTAMP, "timestamp" );
registerColumnType( Types.VARCHAR, "varchar($l)" );
registerColumnType( Types.VARBINARY, "binary($l)" );
registerColumnType( Types.BLOB, "blob" );
registerColumnType( Types.CLOB, "clob" );
// select topic, syntax from information_schema.help
// where section like 'Function%' order by section, topic
//
// see also -> http://www.h2database.com/html/functions.html
//
getDefaultProperties().setProperty( AvailableSettings.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE );
// http://code.google.com/p/h2database/issues/detail?id=235
getDefaultProperties().setProperty( AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, "true" );
}
@Override
public String getAddColumnString() {
return "add column";
}
@Override
public boolean supportsIdentityColumns() {
return true;
}
@Override
public String getIdentityColumnString() {
// not null is implicit
return "generated by default as identity";
}
@Override
public String getIdentitySelectString() {
return "call identity()";
}
@Override
public String getIdentityInsertString() {
return "null";
}
@Override
public String getForUpdateString() {
return " for update";
}
@Override
public boolean supportsLimit() {
return true;
}
@Override
public String getLimitString(String sql, boolean hasOffset) {
return sql + (hasOffset ? " limit ? offset ?" : " limit ?");
}
@Override
public boolean bindLimitParametersInReverseOrder() {
return true;
}
@Override
public boolean bindLimitParametersFirst() {
return false;
}
@Override
public boolean supportsIfExistsAfterTableName() {
return true;
}
@Override
public boolean supportsIfExistsAfterConstraintName() {
return true;
}
@Override
public boolean supportsSequences() {
return true;
}
@Override
public boolean supportsPooledSequences() {
return true;
}
@Override
public String getCreateSequenceString(String sequenceName) {
return "create sequence " + sequenceName;
}
@Override
public String getDropSequenceString(String sequenceName) {
return "drop sequence " + sequenceName;
}
@Override
public String getSelectSequenceNextValString(String sequenceName) {
return "next value for " + sequenceName;
}
@Override
public String getSequenceNextValString(String sequenceName) {
return "call next value for " + sequenceName;
}
@Override
public String getQuerySequencesString() {
return querySequenceString;
}
// @Override
// public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
// return EXTRACTER;
// }
//
// private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
// /**
// * Extract the name of the violated constraint from the given SQLException.
// *
// * @param sqle The exception that was the result of the constraint violation.
// * @return The extracted constraint name.
// */
// public String extractConstraintName(SQLException sqle) {
// String constraintName = null;
// // 23000: Check constraint violation: {0}
// // 23001: Unique index or primary key violation: {0}
// if ( sqle.getSQLState().startsWith( "23" ) ) {
// final String message = sqle.getMessage();
// final int idx = message.indexOf( "violation: " );
// if ( idx > 0 ) {
// constraintName = message.substring( idx + "violation: ".length() );
// }
// }
// return constraintName;
// }
// };
//
// @Override
// public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
// SQLExceptionConversionDelegate delegate = super.buildSQLExceptionConversionDelegate();
// if (delegate == null) {
// delegate = new SQLExceptionConversionDelegate() {
// @Override
// public JDBCException convert(SQLException sqlException, String message, String sql) {
// final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
//
// if (40001 == errorCode) {
// // DEADLOCK DETECTED
// return new LockAcquisitionException(message, sqlException, sql);
// }
//
// if (50200 == errorCode) {
// // LOCK NOT AVAILABLE
// return new PessimisticLockException(message, sqlException, sql);
// }
//
// if ( 90006 == errorCode ) {
// // NULL not allowed for column [90006-145]
// final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
// return new ConstraintViolationException( message, sqlException, sql, constraintName );
// }
//
// return null;
// }
// };
// }
// return delegate;
// }
@Override
public boolean supportsTemporaryTables() {
return true;
}
@Override
public String getCreateTemporaryTableString() {
return "create cached local temporary table if not exists";
}
@Override
public String getCreateTemporaryTablePostfix() {
// actually 2 different options are specified here:
// 1) [on commit drop] - says to drop the table on transaction commit
// 2) [transactional] - says to not perform an implicit commit of any current transaction
return "on commit drop transactional";
}
@Override
public Boolean performTemporaryTableDDLInIsolation() {
// explicitly create the table using the same connection and transaction
return Boolean.FALSE;
}
@Override
public boolean dropTemporaryTableAfterUse() {
return false;
}
@Override
public boolean supportsCurrentTimestampSelection() {
return true;
}
@Override
public boolean isCurrentTimestampSelectStringCallable() {
return false;
}
@Override
public String getCurrentTimestampSelectString() {
return "call current_timestamp()";
}
@Override
public boolean supportsUnionAll() {
return true;
}
// Overridden informational metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public boolean supportsLobValueChangePropogation() {
return false;
}
@Override
public boolean supportsTupleDistinctCounts() {
return false;
}
@Override
public boolean doesReadCommittedCauseWritersToBlockReaders() {
// see http://groups.google.com/group/h2-database/browse_thread/thread/562d8a49e2dabe99?hl=en
return true;
}
@Override
public boolean supportsTuplesInSubqueries() {
return false;
}
}