//$Id: PostgreSQLDialect.java 11367 2007-03-29 13:26:40Z steve.ebersole@jboss.com $ package org.hibernate.dialect; import java.sql.Types; import java.sql.SQLException; import org.hibernate.Hibernate; import org.hibernate.exception.ViolatedConstraintNameExtracter; import org.hibernate.exception.TemplatedViolatedConstraintNameExtracter; import org.hibernate.exception.JDBCExceptionHelper; import org.hibernate.cfg.Environment; import org.hibernate.dialect.function.NoArgSQLFunction; import org.hibernate.dialect.function.PositionSubstringFunction; import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.VarArgsSQLFunction; import org.hibernate.id.SequenceGenerator; /** * An SQL dialect for Postgres * @author Gavin King * * Dialect reorganized to follow sequence in base Dialect, few * additions and corrections from earlier PostgreSQLDialect * * This is the base class for PostgreSQL 8.x versions * PostgreSQL 8.0 and 8.1 can use the unmodified, later releases provide * further extensions and so are provided as separate Dialects * simon@2ndquadrant.com 12 Nov 2007 */ public class PostgreSQL8Dialect extends Dialect { public PostgreSQL8Dialect() { super(); getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE); // database type mapping support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ registerColumnType( Types.BIT, "bool" ); registerColumnType( Types.BIGINT, "int8" ); registerColumnType( Types.SMALLINT, "int2" ); registerColumnType( Types.TINYINT, "int2" ); registerColumnType( Types.INTEGER, "int4" ); registerColumnType( Types.CHAR, "char(1)" ); registerColumnType( Types.VARCHAR, "varchar($l)" ); registerColumnType( Types.FLOAT, "float4" ); registerColumnType( Types.DOUBLE, "float8" ); registerColumnType( Types.DATE, "date" ); registerColumnType( Types.TIME, "time" ); registerColumnType( Types.TIMESTAMP, "timestamp" ); registerColumnType( Types.VARBINARY, "bytea" ); registerColumnType( Types.CLOB, "text" ); registerColumnType( Types.BLOB, "oid" ); registerColumnType( Types.NUMERIC, "numeric($p, $s)" ); // function support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ registerFunction( "abs", new StandardSQLFunction("abs") ); registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) ); registerFunction( "acos", new StandardSQLFunction("acos", Hibernate.DOUBLE) ); registerFunction( "asin", new StandardSQLFunction("asin", Hibernate.DOUBLE) ); registerFunction( "atan", new StandardSQLFunction("atan", Hibernate.DOUBLE) ); registerFunction( "cos", new StandardSQLFunction("cos", Hibernate.DOUBLE) ); registerFunction( "cot", new StandardSQLFunction("cot", Hibernate.DOUBLE) ); registerFunction( "exp", new StandardSQLFunction("exp", Hibernate.DOUBLE) ); registerFunction( "ln", new StandardSQLFunction("ln", Hibernate.DOUBLE) ); registerFunction( "log", new StandardSQLFunction("log", Hibernate.DOUBLE) ); registerFunction( "sin", new StandardSQLFunction("sin", Hibernate.DOUBLE) ); registerFunction( "sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE) ); registerFunction( "cbrt", new StandardSQLFunction("cbrt", Hibernate.DOUBLE) ); registerFunction( "tan", new StandardSQLFunction("tan", Hibernate.DOUBLE) ); registerFunction( "radians", new StandardSQLFunction("radians", Hibernate.DOUBLE) ); registerFunction( "degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE) ); registerFunction( "stddev", new StandardSQLFunction("stddev", Hibernate.DOUBLE) ); registerFunction( "variance", new StandardSQLFunction("variance", Hibernate.DOUBLE) ); registerFunction( "random", new NoArgSQLFunction("random", Hibernate.DOUBLE) ); registerFunction( "round", new StandardSQLFunction("round") ); registerFunction( "trunc", new StandardSQLFunction("trunc") ); registerFunction( "ceil", new StandardSQLFunction("ceil") ); registerFunction( "floor", new StandardSQLFunction("floor") ); registerFunction( "chr", new StandardSQLFunction("chr", Hibernate.CHARACTER) ); registerFunction( "lower", new StandardSQLFunction("lower") ); registerFunction( "upper", new StandardSQLFunction("upper") ); registerFunction( "substr", new StandardSQLFunction("substr", Hibernate.STRING) ); registerFunction( "initcap", new StandardSQLFunction("initcap") ); registerFunction( "to_ascii", new StandardSQLFunction("to_ascii") ); registerFunction( "quote_ident", new StandardSQLFunction("quote_ident", Hibernate.STRING) ); registerFunction( "quote_literal", new StandardSQLFunction("quote_literal", Hibernate.STRING) ); registerFunction( "md5", new StandardSQLFunction("md5") ); registerFunction( "ascii", new StandardSQLFunction("ascii", Hibernate.INTEGER) ); registerFunction( "length", new StandardSQLFunction("length", Hibernate.LONG) ); registerFunction( "char_length", new StandardSQLFunction("char_length", Hibernate.LONG) ); registerFunction( "bit_length", new StandardSQLFunction("bit_length", Hibernate.LONG) ); registerFunction( "octet_length", new StandardSQLFunction("octet_length", Hibernate.LONG) ); registerFunction( "current_date", new NoArgSQLFunction("current_date", Hibernate.DATE, false) ); registerFunction( "current_time", new NoArgSQLFunction("current_time", Hibernate.TIME, false) ); registerFunction( "current_timestamp", new NoArgSQLFunction("current_timestamp", Hibernate.TIMESTAMP, false) ); registerFunction( "localtime", new NoArgSQLFunction("localtime", Hibernate.TIME, false) ); registerFunction( "localtimestamp", new NoArgSQLFunction("localtimestamp", Hibernate.TIMESTAMP, false) ); registerFunction( "now", new NoArgSQLFunction("now", Hibernate.TIMESTAMP) ); registerFunction( "timeofday", new NoArgSQLFunction("timeofday", Hibernate.STRING) ); registerFunction( "age", new StandardSQLFunction("age") ); registerFunction( "current_user", new NoArgSQLFunction("current_user", Hibernate.STRING, false) ); registerFunction( "session_user", new NoArgSQLFunction("session_user", Hibernate.STRING, false) ); registerFunction( "user", new NoArgSQLFunction("user", Hibernate.STRING, false) ); registerFunction( "current_database", new NoArgSQLFunction("current_database", Hibernate.STRING, true) ); registerFunction( "current_schema", new NoArgSQLFunction("current_schema", Hibernate.STRING, true) ); registerFunction( "to_char", new StandardSQLFunction("to_char", Hibernate.STRING) ); registerFunction( "to_date", new StandardSQLFunction("to_date", Hibernate.DATE) ); registerFunction( "to_timestamp", new StandardSQLFunction("to_timestamp", Hibernate.TIMESTAMP) ); registerFunction( "to_number", new StandardSQLFunction("to_number", Hibernate.BIG_DECIMAL) ); registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","||",")" ) ); registerFunction( "locate", new PositionSubstringFunction() ); registerFunction( "str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as varchar)") ); // keyword support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } // native identifier generatiion ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public Class getNativeIdentifierGeneratorClass() { return SequenceGenerator.class; } // IDENTITY support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsIdentityColumns() { return true; } public boolean hasDataTypeInIdentityColumn() { return false; } public String getIdentitySelectString(String table, String column, int type) { return new StringBuffer().append("select currval('") .append(table) .append('_') .append(column) .append("_seq')") .toString(); } public String getIdentityColumnString(int type) { return type==Types.BIGINT ? "bigserial not null" : "serial not null"; } // SEQUENCE support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsSequences() { return true; } public boolean supportsPooledSequences() { return true; } public String getSequenceNextValString(String sequenceName) { return "select " + getSelectSequenceNextValString( sequenceName ); } public String getSelectSequenceNextValString(String sequenceName) { return "nextval ('" + sequenceName + "')"; } public String getCreateSequenceString(String sequenceName) { return "create sequence " + sequenceName; //starts with 1, implicitly } public String getDropSequenceString(String sequenceName) { return "drop sequence " + sequenceName; } public String getQuerySequencesString() { return "select relname from pg_class where relkind='S'"; } // GUID support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // limit/offset support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsLimit() { return true; } public boolean bindLimitParametersInReverseOrder() { return true; } public String getLimitString(String sql, boolean hasOffset) { return new StringBuffer( sql.length()+20 ) .append(sql) .append(hasOffset ? " limit ? offset ?" : " limit ?") .toString(); } // lock acquisition support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsOuterJoinForUpdate() { return false; } // PostgreSQL 8.1 supports LOCK ... NO WAIT, just not FOR UPDATE NOWAIT // Leave NOWAIT support at the default for 8.0 and 8.1 // table support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // temporary table support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsTemporaryTables() { return true; } public String getCreateTemporaryTableString() { return "create temporary table"; } public String getCreateTemporaryTablePostfix() { return "on commit drop"; } // callable statement support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // current timestamp support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsCurrentTimestampSelection() { return true; } public boolean isCurrentTimestampSelectStringCallable() { return false; } public String getCurrentTimestampSelectString() { return "select now()"; } // SQLException support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Constraint-name extractor for Postgres contraint violation exceptions. * Orginally contributed by Denny Bartelt. */ private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() { public String extractConstraintName(SQLException sqle) { try { int sqlState = Integer.valueOf( JDBCExceptionHelper.extractSqlState(sqle)).intValue(); switch (sqlState) { // CHECK VIOLATION case 23514: return extractUsingTemplate("violates check constraint \"","\"", sqle.getMessage()); // UNIQUE VIOLATION case 23505: return extractUsingTemplate("violates unique constraint \"","\"", sqle.getMessage()); // FOREIGN KEY VIOLATION case 23503: return extractUsingTemplate("violates foreign key constraint \"","\"", sqle.getMessage()); // NOT NULL VIOLATION case 23502: return extractUsingTemplate("null value in column \"","\" violates not-null constraint", sqle.getMessage()); // TODO: RESTRICT VIOLATION case 23001: return null; // ALL OTHER default: return null; } } catch (NumberFormatException nfe) { return null; } } }; // union subclass support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Workaround for postgres bug #1453 */ public String getSelectClauseNullString(int sqlType) { String typeName = getTypeName(sqlType, 1, 1, 0); //trim off the length/precision/scale int loc = typeName.indexOf('('); if (loc>-1) { typeName = typeName.substring(0, loc); } return "null::" + typeName; } public boolean supportsUnionAll() { return true; } // miscellaneous support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public String getNoColumnsInsertString() { return "default values"; } public String toBooleanValueString(boolean bool) { return bool ? "true" : "false"; } // identifier quoting support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // DDL support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public String getAddColumnString() { return "add column"; } public boolean supportsCommentOn() { return true; } public boolean supportsIfExistsBeforeTableName() { return false; } public String getCascadeConstraintsString() { return "";//" cascade"; } // Informational metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public boolean supportsEmptyInList() { return false; } public boolean supportsRowValueConstructorSyntax() { return false; } public boolean useInputStreamToInsertBlob() { return false; } public boolean supportsExpectedLobUsagePattern() { // seems to have spotty LOB suppport return false; } }