package org.hibernate.dialect;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Locale;
import org.hibernate.MappingException;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.dialect.unique.InformixUniqueDelegate;
import org.hibernate.dialect.unique.UniqueDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtracter;
import org.hibernate.exception.spi.ViolatedConstraintNameExtracter;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.type.StandardBasicTypes;
public class InformixDialect extends Dialect {
private final UniqueDelegate uniqueDelegate;
public InformixDialect() {
super();
registerColumnType( Types.BIGINT, "int8" );
registerColumnType( Types.BINARY, "byte" );
registerColumnType( Types.BIT, "smallint" );
registerColumnType( Types.CHAR, "char($l)" );
registerColumnType( Types.DATE, "date" );
registerColumnType( Types.DECIMAL, "decimal" );
registerColumnType( Types.DOUBLE, "float" );
registerColumnType( Types.FLOAT, "smallfloat" );
registerColumnType( Types.INTEGER, "integer" );
registerColumnType( Types.LONGVARBINARY, "blob" );
registerColumnType( Types.LONGVARCHAR, "clob" );
registerColumnType( Types.NUMERIC, "decimal" );
registerColumnType( Types.REAL, "smallfloat" );
registerColumnType( Types.SMALLINT, "smallint" );
registerColumnType( Types.TIMESTAMP, "datetime year to fraction(5)" );
registerColumnType( Types.TIME, "datetime hour to second" );
registerColumnType( Types.TINYINT, "smallint" );
registerColumnType( Types.VARBINARY, "byte" );
registerColumnType( Types.VARCHAR, "varchar($l)" );
registerColumnType( Types.VARCHAR, 255, "varchar($l)" );
registerColumnType( Types.VARCHAR, 32739, "lvarchar($l)" );
registerFunction( "concat", new VarArgsSQLFunction( StandardBasicTypes.STRING, "(", "||", ")" ) );
uniqueDelegate = new InformixUniqueDelegate( this );
}
@Override
public String getAddColumnString() {
return "add";
}
@Override
public boolean supportsIdentityColumns() {
return true;
}
@Override
public String getIdentitySelectString(String table, String column, int type)
throws MappingException {
return type == Types.BIGINT
? "select dbinfo('serial8') from informix.systables where tabid=1"
: "select dbinfo('sqlca.sqlerrd1') from informix.systables where tabid=1";
}
@Override
public String getIdentityColumnString(int type) throws MappingException {
return type == Types.BIGINT ?
"serial8 not null" :
"serial not null";
}
@Override
public boolean hasDataTypeInIdentityColumn() {
return false;
}
@Override
public String getAddForeignKeyConstraintString(
String constraintName,
String[] foreignKey,
String referencedTable,
String[] primaryKey,
boolean referencesPrimaryKey) {
final StringBuilder result = new StringBuilder( 30 )
.append( " add constraint " )
.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();
}
@Override
public String getAddPrimaryKeyConstraintString(String constraintName) {
return " add constraint primary key constraint " + constraintName + " ";
}
@Override
public String getCreateSequenceString(String sequenceName) {
return "create sequence " + sequenceName;
}
@Override
public String getDropSequenceString(String sequenceName) {
return "drop sequence " + sequenceName + " restrict";
}
@Override
public String getSequenceNextValString(String sequenceName) {
return "select " + getSelectSequenceNextValString( sequenceName ) + " from informix.systables where tabid=1";
}
@Override
public String getSelectSequenceNextValString(String sequenceName) {
return sequenceName + ".nextval";
}
@Override
public boolean supportsSequences() {
return true;
}
@Override
public boolean supportsPooledSequences() {
return true;
}
@Override
public String getQuerySequencesString() {
return "select tabname from informix.systables where tabtype='Q'";
}
@Override
public boolean supportsLimit() {
return true;
}
@Override
public boolean useMaxForLimit() {
return true;
}
@Override
public boolean supportsLimitOffset() {
return false;
}
@Override
public String getLimitString(String querySelect, int offset, int limit) {
if ( offset > 0 ) {
throw new UnsupportedOperationException( "query result offset is not supported" );
}
return new StringBuilder( querySelect.length() + 8 )
.append( querySelect )
.insert( querySelect.toLowerCase(Locale.ROOT).indexOf( "select" ) + 6, " first " + limit )
.toString();
}
@Override
public boolean supportsVariableLimit() {
return false;
}
@Override
public ViolatedConstraintNameExtracter () {
return EXTRACTER;
}
private static final ViolatedConstraintNameExtracter = new TemplatedViolatedConstraintNameExtracter() {
@Override
public String (SQLException sqle) {
String constraintName = null;
final 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 ) {
final int i = constraintName.indexOf( '.' );
if ( i != -1 ) {
constraintName = constraintName.substring( i + 1 );
}
}
return constraintName;
}
};
@Override
public boolean supportsCurrentTimestampSelection() {
return true;
}
@Override
public boolean isCurrentTimestampSelectStringCallable() {
return false;
}
@Override
public String getCurrentTimestampSelectString() {
return "select distinct current timestamp from informix.systables";
}
@Override
public boolean supportsTemporaryTables() {
return true;
}
@Override
public String getCreateTemporaryTableString() {
return "create temp table";
}
@Override
public String getCreateTemporaryTablePostfix() {
return "with no log";
}
@Override
public UniqueDelegate getUniqueDelegate() {
return uniqueDelegate;
}
}