/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.SQLException;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.MappingException;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.exception.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.ViolatedConstraintNameExtracter;
import org.hibernate.sql.ANSICaseFragment;
import org.hibernate.sql.ANSIJoinFragment;
import org.hibernate.sql.CaseFragment;
import org.hibernate.sql.JoinFragment;
import org.hibernate.util.StringHelper;
/**
* Informix dialect.
*
* Seems to work with Informix Dynamic Server Version 7.31.UD3,
* Informix JDBC driver version 2.21JC3.
* @author Steve Molitor
*/
public class InformixDialect extends Dialect {
/**
* Creates new InformixDialect instance. Sets up the JDBC /
* Informix type mappings.
*/
public InformixDialect() {
super();
registerCharacterTypeMappings();
registerNumericTypeMappings();
registerDateTimeTypeMappings();
registerBinaryTypeMappings();
registerFunctions();
}
protected void registerCharacterTypeMappings()
{
registerColumnType(Types.CHAR, "char($l)");
registerColumnType(Types.VARCHAR, "varchar($l)");
registerColumnType(Types.VARCHAR, 255, "varchar($l)");
registerColumnType(Types.VARCHAR, 32739, "lvarchar($l)");
// Prefer Smart-LOB types (CLOB and BLOB) over LOB types (TEXT and BYTE)
registerColumnType(Types.LONGVARCHAR, "clob"); // or TEXT?
registerColumnType(Types.CLOB, "clob");
}
protected void registerNumericTypeMappings()
{
registerColumnType(Types.BIT, "smallint"); // Informix doesn't have a bit type
registerColumnType(Types.TINYINT, "smallint");
registerColumnType(Types.SMALLINT, "smallint");
registerColumnType(Types.INTEGER, "integer");
// Prefer bigint over int8 (conserves space, more standard)
registerColumnType(Types.BIGINT, "bigint"); // previously int8
registerColumnType(Types.FLOAT, "smallfloat");
registerColumnType(Types.REAL, "smallfloat");
registerColumnType(Types.DOUBLE, "float");
registerColumnType(Types.NUMERIC, "decimal"); // or MONEY
registerColumnType(Types.DECIMAL, "decimal");
}
protected void registerDateTimeTypeMappings()
{
registerColumnType(Types.DATE, "date");
registerColumnType(Types.TIME, "datetime hour to second");
registerColumnType(Types.TIMESTAMP, "datetime year to fraction(5)");
}
protected void registerBinaryTypeMappings() {
registerColumnType(Types.BOOLEAN, "boolean");
registerColumnType(Types.BINARY, "byte");
// Prefer Smart-LOB types (CLOB and BLOB) over LOB types (TEXT and BYTE)
registerColumnType(Types.VARBINARY, "blob");
registerColumnType(Types.LONGVARBINARY, "blob"); // or BYTE
registerColumnType(Types.BLOB, "blob");
}
protected void registerFunctions()
{
registerFunction( "concat", new VarArgsSQLFunction(Hibernate.STRING, "(", "||", ")" ) );
}
// IDENTITY support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Informix supports identity columns through the SERIAL and SERIAL8 types.
* Informix also supports sequences to generated identity values. Hibernate
* iterates through strategies, picking the first that returns true. To ensure
* sequences are used, report false here.
*/
public boolean supportsIdentityColumns() {
return false;
}
public boolean hasDataTypeInIdentityColumn() {
return false;
}
public String getIdentitySelectString(String table, String column, int type)
throws MappingException {
return type==Types.BIGINT ?
"select dbinfo('bigserial') from systables where tabid=1" :
"select dbinfo('sqlca.sqlerrd1') from systables where tabid=1";
}
public String getIdentityColumnString(int type) throws MappingException {
//return "generated by default as identity"; //not null ... (start with 1) is implied
return type==Types.BIGINT ?
"bigserial not null" :
"serial not null";
}
public String getIdentityInsertString() {
return "0";
}
// SEQUENCE support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public boolean supportsSequences() {
return true;
}
public boolean supportsPooledSequences() {
return true;
}
public String getSequenceNextValString(String sequenceName) {
return "select " + getSelectSequenceNextValString( sequenceName ) + " from systables where tabid=1";
}
public String getSelectSequenceNextValString(String sequenceName) {
return sequenceName + ".nextval";
}
public String getCreateSequenceString(String sequenceName) {
return "create sequence " + sequenceName;
}
/**
* Informix, as of 11.70, does not support the restrict or cascade clauses
* being applied when dropping a sequence
*/
public String getDropSequenceString(String sequenceName) {
return "drop sequence " + sequenceName;
}
/**
* Informix treats sequences like a table from the standpoint of naming.
* Therefore, to retrieve the sequence name we must perform a join between
* systables and syssequences on the {@code}tabid column.
*/
public String getQuerySequencesString() {
return "select systables.tabname from systables,syssequences where systables.tabid = syssequences.tabid";
}
// GUID support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
* Informix does not have built-in support for this type of operation.
* However, stored-procedures, ideally C-UDRs, can be used to make this happen.
* Jacques Roy has authored a developerWorks article on this.
*/
// limit/offset support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public boolean supportsLimit() {
return true;
}
public boolean supportsLimitOffset() {
return true;
}
public boolean supportsVariableLimit() {
return false;
}
/**
* Previously incorrectly claimed that Informix Dynamic Server (IDS) required using the maximum row number for the limit.
* Overriding explicitly so that user's of this dialect are aware (even though 'false' is the default inherited from Dialect).
*/
public boolean useMaxForLimit() {
return false;
}
public String getLimitString(String query, int offset, int limit) {
/* SQL Syntax:
* SELECT FIRST ...
* SELECT SKIP FIRST ...
*/
if (offset < 0 || limit < 0)
{
throw new IllegalArgumentException("Cannot perform limit query with negative limit and/or offset value(s)");
}
StringBuffer limitQuery = new StringBuffer(query.length() + 10);
limitQuery.append(query);
int indexOfEndOfSelect = query.toLowerCase().indexOf("select") + 6;
if (offset == 0)
{
limitQuery.insert(indexOfEndOfSelect, " first " + limit);
} else {
limitQuery.insert(indexOfEndOfSelect, " skip " + offset + " first " + limit);
}
return limitQuery.toString();
}
// temporary table support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public boolean supportsTemporaryTables() {
return true;
}
public String getCreateTemporaryTableString() {
return "create temp table";
}
// callable statement support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// current timestamp support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public boolean supportsCurrentTimestampSelection() {
return true;
}
public boolean isCurrentTimestampSelectStringCallable() {
return false;
}
public String getCurrentTimestampSelectString() {
return "select distinct current timestamp from informix.systables";
}
// SQLException support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER;
}
private static 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;
int errorCode = JDBCExceptionHelper.extractErrorCode(sqle);
if ( errorCode == -268 ) {
constraintName = extractUsingTemplate( "Unique constraint (", ") violated.", sqle.getMessage() );
}
else if ( errorCode == -691 ) {
constraintName = extractUsingTemplate( "Missing key in referenced table for referential constraint (", ").", sqle.getMessage() );
}
else if ( errorCode == -692 ) {
constraintName = extractUsingTemplate( "Key value for constraint (", ") is still being referenced.", sqle.getMessage() );
}
if (constraintName != null) {
// strip table-owner because Informix always returns constraint names as "."
int i = constraintName.indexOf('.');
if (i != -1) {
constraintName = constraintName.substring(i + 1);
}
}
return constraintName;
}
};
// union subclass support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Does this dialect support UNION ALL, which is generally a faster
* variant of UNION?
*
* @return True if UNION ALL is supported; false otherwise.
*/
public boolean supportsUnionAll() {
return true;
}
// miscellaneous support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Create a {@link org.hibernate.sql.JoinFragment} strategy responsible
* for handling this dialect's variations in how joins are handled.
*
* @return This dialect's {@link org.hibernate.sql.JoinFragment} strategy.
*/
public JoinFragment createOuterJoinFragment() {
return new ANSIJoinFragment();
}
/**
* Create a {@link org.hibernate.sql.CaseFragment} strategy responsible
* for handling this dialect's variations in how CASE statements are
* handled.
*
* @return This dialect's {@link org.hibernate.sql.CaseFragment} strategy.
*/
public CaseFragment createCaseFragment() {
return new ANSICaseFragment();
}
/**
* The fragment used to insert a row without specifying any column values.
* Informix does not support this concept at present.
*
* @return The appropriate empty values clause.
*/
public String getNoColumnsInsertString() {
return "values (0)";
}
/**
* The SQL literal value to which this database maps boolean values.
* Informix uses "t" and "f" rather than "0" and "1" as the string
* representation of boolean values.
*
* @param bool
* The boolean value
* @return The appropriate SQL literal.
*/
@Override
public String toBooleanValueString(boolean bool) {
return bool ? "t" : "f";
}
// DDL support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public String getAddColumnString() {
return "add";
}
/**
* The syntax used to add a foreign key constraint to a table.
* Informix constraint name must be at the end.
*
* @return String
*/
public String getAddForeignKeyConstraintString(
String constraintName,
String[] foreignKey,
String referencedTable,
String[] primaryKey,
boolean referencesPrimaryKey) {
StringBuffer result = new StringBuffer( 30 )
.append( " add constraint " ) /* The extra space is not needed here, other DBMSs place the constraintName here */
.append( " foreign key (" )
.append( StringHelper.join( ", ", foreignKey ) )
.append( ") references " )
.append( referencedTable );
if ( !referencesPrimaryKey ) {
result.append( " (" )
.append( StringHelper.join( ", ", primaryKey ) )
.append( ')' );
}
result.append( " constraint " ).append( constraintName );
return result.toString();
}
/**
* The syntax used to add a primary key constraint to a table.
* Informix constraint name must be at the end.
*
* @return String
*/
public String getAddPrimaryKeyConstraintString(String constraintName) {
return " add constraint primary key constraint " + constraintName + " ";
}
/**
* Informix 11.70 supports the "if exists" clause
* when dropping a table before the table name.
*/
public boolean supportsIfExistsBeforeTableName() {
return true;
}
}