package org.hibernate.dialect;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.type.StandardBasicTypes;
public class TeradataDialect extends Dialect {
private static final int PARAM_LIST_SIZE_LIMIT = 1024;
public TeradataDialect() {
super();
registerColumnType( Types.NUMERIC, "NUMERIC($p,$s)" );
registerColumnType( Types.DOUBLE, "DOUBLE PRECISION" );
registerColumnType( Types.BIGINT, "NUMERIC(18,0)" );
registerColumnType( Types.BIT, "BYTEINT" );
registerColumnType( Types.TINYINT, "BYTEINT" );
registerColumnType( Types.VARBINARY, "VARBYTE($l)" );
registerColumnType( Types.BINARY, "BYTEINT" );
registerColumnType( Types.LONGVARCHAR, "LONG VARCHAR" );
registerColumnType( Types.CHAR, "CHAR(1)" );
registerColumnType( Types.DECIMAL, "DECIMAL" );
registerColumnType( Types.INTEGER, "INTEGER" );
registerColumnType( Types.SMALLINT, "SMALLINT" );
registerColumnType( Types.FLOAT, "FLOAT" );
registerColumnType( Types.VARCHAR, "VARCHAR($l)" );
registerColumnType( Types.DATE, "DATE" );
registerColumnType( Types.TIME, "TIME" );
registerColumnType( Types.TIMESTAMP, "TIMESTAMP" );
registerColumnType( Types.BOOLEAN, "BYTEINT" );
registerColumnType( Types.BLOB, "BLOB" );
registerColumnType( Types.CLOB, "CLOB" );
registerFunction( "year", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "extract(year from ?1)" ) );
registerFunction( "length", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "character_length(?1)" ) );
registerFunction( "concat", new VarArgsSQLFunction( StandardBasicTypes.STRING, "(", "||", ")" ) );
registerFunction( "substring", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substring(?1 from ?2 for ?3)" ) );
registerFunction( "locate", new SQLFunctionTemplate( StandardBasicTypes.STRING, "position(?1 in ?2)" ) );
registerFunction( "mod", new SQLFunctionTemplate( StandardBasicTypes.STRING, "?1 mod ?2" ) );
registerFunction( "str", new SQLFunctionTemplate( StandardBasicTypes.STRING, "cast(?1 as varchar(255))" ) );
registerFunction(
"bit_length", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "octet_length(cast(?1 as char))*4" )
);
registerFunction( "current_timestamp", new SQLFunctionTemplate( StandardBasicTypes.TIMESTAMP, "current_timestamp" ) );
registerFunction( "current_time", new SQLFunctionTemplate( StandardBasicTypes.TIMESTAMP, "current_time" ) );
registerFunction( "current_date", new SQLFunctionTemplate( StandardBasicTypes.TIMESTAMP, "current_date" ) );
registerKeyword( "password" );
registerKeyword( "type" );
registerKeyword( "title" );
registerKeyword( "year" );
registerKeyword( "month" );
registerKeyword( "summary" );
registerKeyword( "alias" );
registerKeyword( "value" );
registerKeyword( "first" );
registerKeyword( "role" );
registerKeyword( "account" );
registerKeyword( "class" );
getDefaultProperties().setProperty( Environment.USE_STREAMS_FOR_BINARY, "false" );
getDefaultProperties().setProperty( Environment.STATEMENT_BATCH_SIZE, NO_BATCH );
}
@Override
public String getForUpdateString() {
return "";
}
@Override
public boolean supportsIdentityColumns() {
return false;
}
@Override
public boolean supportsSequences() {
return false;
}
@Override
public String getAddColumnString() {
return "Add Column";
}
@Override
public boolean supportsTemporaryTables() {
return true;
}
@Override
public String getCreateTemporaryTableString() {
return "create global temporary table";
}
@Override
public String getCreateTemporaryTablePostfix() {
return " on commit preserve rows";
}
@Override
public Boolean performTemporaryTableDDLInIsolation() {
return Boolean.TRUE;
}
@Override
public boolean dropTemporaryTableAfterUse() {
return false;
}
@Override
public String getTypeName(int code, long length, int precision, int scale) throws HibernateException {
final float f = precision > 0 ? (float) scale / (float) precision : 0;
final int p = ( precision > 18 ? 18 : precision );
final int s = ( precision > 18 ? (int) ( 18.0 * f ) : ( scale > 18 ? 18 : scale ) );
return super.getTypeName( code, length, p, s );
}
@Override
public boolean supportsCascadeDelete() {
return false;
}
@Override
public boolean supportsCircularCascadeDeleteConstraints() {
return false;
}
@Override
public boolean areStringComparisonsCaseInsensitive() {
return true;
}
@Override
public boolean supportsEmptyInList() {
return false;
}
@Override
public String getSelectClauseNullString(int sqlType) {
String v = "null";
switch ( sqlType ) {
case Types.BIT:
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
case Types.BIGINT:
case Types.FLOAT:
case Types.REAL:
case Types.DOUBLE:
case Types.NUMERIC:
case Types.DECIMAL:
v = "cast(null as decimal)";
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
v = "cast(null as varchar(255))";
break;
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
v = "cast(null as timestamp)";
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
case Types.NULL:
case Types.OTHER:
case Types.JAVA_OBJECT:
case Types.DISTINCT:
case Types.STRUCT:
case Types.ARRAY:
case Types.BLOB:
case Types.CLOB:
case Types.REF:
case Types.DATALINK:
case Types.BOOLEAN:
break;
default:
break;
}
return v;
}
@Override
public String getCreateMultisetTableString() {
return "create multiset table ";
}
@Override
public boolean supportsLobValueChangePropogation() {
return false;
}
@Override
public boolean doesReadCommittedCauseWritersToBlockReaders() {
return true;
}
@Override
public boolean doesRepeatableReadCauseReadersToBlockWriters() {
return true;
}
@Override
public boolean supportsBindAsCallableArgument() {
return false;
}
@Override
public int getInExpressionCountLimit() {
return PARAM_LIST_SIZE_LIMIT;
}
}