1
//$Id: PostgreSQLDialect.java 11366 2007-03-29 13:26:25Z steve.ebersole@jboss.com $
2
package org.hibernate.dialect;
5
import java.sql.SQLException;
7
import org.hibernate.Hibernate;
8
import org.hibernate.exception.ViolatedConstraintNameExtracter;
9
import org.hibernate.exception.TemplatedViolatedConstraintNameExtracter;
10
import org.hibernate.exception.JDBCExceptionHelper;
11
import org.hibernate.cfg.Environment;
12
import org.hibernate.dialect.function.NoArgSQLFunction;
13
import org.hibernate.dialect.function.PositionSubstringFunction;
14
import org.hibernate.dialect.function.SQLFunctionTemplate;
15
import org.hibernate.dialect.function.StandardSQLFunction;
16
import org.hibernate.dialect.function.VarArgsSQLFunction;
17
import org.hibernate.id.SequenceGenerator;
20
* An SQL dialect for Postgres
23
public class PostgreSQLDialect extends Dialect {
25
public PostgreSQLDialect() {
27
registerColumnType( Types.BIT, "bool" );
28
registerColumnType( Types.BIGINT, "int8" );
29
registerColumnType( Types.SMALLINT, "int2" );
30
registerColumnType( Types.TINYINT, "int2" );
31
registerColumnType( Types.INTEGER, "int4" );
32
registerColumnType( Types.CHAR, "char(1)" );
33
registerColumnType( Types.VARCHAR, "varchar($l)" );
34
registerColumnType( Types.FLOAT, "float4" );
35
registerColumnType( Types.DOUBLE, "float8" );
36
registerColumnType( Types.DATE, "date" );
37
registerColumnType( Types.TIME, "time" );
38
registerColumnType( Types.TIMESTAMP, "timestamp" );
39
registerColumnType( Types.VARBINARY, "bytea" );
40
registerColumnType( Types.CLOB, "text" );
41
registerColumnType( Types.BLOB, "oid" );
42
registerColumnType( Types.NUMERIC, "numeric($p, $s)" );
44
registerFunction( "abs", new StandardSQLFunction("abs") );
45
registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );
47
registerFunction( "acos", new StandardSQLFunction("acos", Hibernate.DOUBLE) );
48
registerFunction( "asin", new StandardSQLFunction("asin", Hibernate.DOUBLE) );
49
registerFunction( "atan", new StandardSQLFunction("atan", Hibernate.DOUBLE) );
50
registerFunction( "cos", new StandardSQLFunction("cos", Hibernate.DOUBLE) );
51
registerFunction( "cot", new StandardSQLFunction("cot", Hibernate.DOUBLE) );
52
registerFunction( "exp", new StandardSQLFunction("exp", Hibernate.DOUBLE) );
53
registerFunction( "ln", new StandardSQLFunction("ln", Hibernate.DOUBLE) );
54
registerFunction( "log", new StandardSQLFunction("log", Hibernate.DOUBLE) );
55
registerFunction( "sin", new StandardSQLFunction("sin", Hibernate.DOUBLE) );
56
registerFunction( "sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE) );
57
registerFunction( "cbrt", new StandardSQLFunction("cbrt", Hibernate.DOUBLE) );
58
registerFunction( "tan", new StandardSQLFunction("tan", Hibernate.DOUBLE) );
59
registerFunction( "radians", new StandardSQLFunction("radians", Hibernate.DOUBLE) );
60
registerFunction( "degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE) );
62
registerFunction( "stddev", new StandardSQLFunction("stddev", Hibernate.DOUBLE) );
63
registerFunction( "variance", new StandardSQLFunction("variance", Hibernate.DOUBLE) );
65
registerFunction( "random", new NoArgSQLFunction("random", Hibernate.DOUBLE) );
67
registerFunction( "round", new StandardSQLFunction("round") );
68
registerFunction( "trunc", new StandardSQLFunction("trunc") );
69
registerFunction( "ceil", new StandardSQLFunction("ceil") );
70
registerFunction( "floor", new StandardSQLFunction("floor") );
72
registerFunction( "chr", new StandardSQLFunction("chr", Hibernate.CHARACTER) );
73
registerFunction( "lower", new StandardSQLFunction("lower") );
74
registerFunction( "upper", new StandardSQLFunction("upper") );
75
registerFunction( "substr", new StandardSQLFunction("substr", Hibernate.STRING) );
76
registerFunction( "initcap", new StandardSQLFunction("initcap") );
77
registerFunction( "to_ascii", new StandardSQLFunction("to_ascii") );
78
registerFunction( "quote_ident", new StandardSQLFunction("quote_ident", Hibernate.STRING) );
79
registerFunction( "quote_literal", new StandardSQLFunction("quote_literal", Hibernate.STRING) );
80
registerFunction( "md5", new StandardSQLFunction("md5") );
81
registerFunction( "ascii", new StandardSQLFunction("ascii", Hibernate.INTEGER) );
82
registerFunction( "length", new StandardSQLFunction("length", Hibernate.LONG) );
83
registerFunction( "char_length", new StandardSQLFunction("char_length", Hibernate.LONG) );
84
registerFunction( "bit_length", new StandardSQLFunction("bit_length", Hibernate.LONG) );
85
registerFunction( "octet_length", new StandardSQLFunction("octet_length", Hibernate.LONG) );
87
registerFunction( "current_date", new NoArgSQLFunction("current_date", Hibernate.DATE, false) );
88
registerFunction( "current_time", new NoArgSQLFunction("current_time", Hibernate.TIME, false) );
89
registerFunction( "current_timestamp", new NoArgSQLFunction("current_timestamp", Hibernate.TIMESTAMP, false) );
90
registerFunction( "localtime", new NoArgSQLFunction("localtime", Hibernate.TIME, false) );
91
registerFunction( "localtimestamp", new NoArgSQLFunction("localtimestamp", Hibernate.TIMESTAMP, false) );
92
registerFunction( "now", new NoArgSQLFunction("now", Hibernate.TIMESTAMP) );
93
registerFunction( "timeofday", new NoArgSQLFunction("timeofday", Hibernate.STRING) );
94
registerFunction( "age", new StandardSQLFunction("age") );
96
registerFunction( "current_user", new NoArgSQLFunction("current_user", Hibernate.STRING, false) );
97
registerFunction( "session_user", new NoArgSQLFunction("session_user", Hibernate.STRING, false) );
98
registerFunction( "user", new NoArgSQLFunction("user", Hibernate.STRING, false) );
99
registerFunction( "current_database", new NoArgSQLFunction("current_database", Hibernate.STRING, true) );
100
registerFunction( "current_schema", new NoArgSQLFunction("current_schema", Hibernate.STRING, true) );
102
registerFunction( "to_char", new StandardSQLFunction("to_char", Hibernate.STRING) );
103
registerFunction( "to_date", new StandardSQLFunction("to_date", Hibernate.DATE) );
104
registerFunction( "to_timestamp", new StandardSQLFunction("to_timestamp", Hibernate.TIMESTAMP) );
105
registerFunction( "to_number", new StandardSQLFunction("to_number", Hibernate.BIG_DECIMAL) );
107
registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","||",")" ) );
109
registerFunction( "locate", new PositionSubstringFunction() );
111
registerFunction( "str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as varchar)") );
113
getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
116
public String getAddColumnString() {
120
public String getSequenceNextValString(String sequenceName) {
121
return "select " + getSelectSequenceNextValString( sequenceName );
124
public String getSelectSequenceNextValString(String sequenceName) {
125
return "nextval ('" + sequenceName + "')";
128
public String getCreateSequenceString(String sequenceName) {
129
return "create sequence " + sequenceName; //starts with 1, implicitly
132
public String getDropSequenceString(String sequenceName) {
133
return "drop sequence " + sequenceName;
136
public String getCascadeConstraintsString() {
137
return "";//" cascade";
139
public boolean dropConstraints() {
143
public boolean supportsSequences() {
147
public String getQuerySequencesString() {
148
return "select relname from pg_class where relkind='S'";
151
public boolean supportsLimit() {
155
public String getLimitString(String sql, boolean hasOffset) {
156
return new StringBuffer( sql.length()+20 )
158
.append(hasOffset ? " limit ? offset ?" : " limit ?")
162
public boolean bindLimitParametersInReverseOrder() {
166
public boolean supportsIdentityColumns() {
170
public String getForUpdateString(String aliases) {
171
return getForUpdateString() + " of " + aliases;
174
public String getIdentitySelectString(String table, String column, int type) {
175
return new StringBuffer().append("select currval('")
183
public String getIdentityColumnString(int type) {
184
return type==Types.BIGINT ?
185
"bigserial not null" :
189
public boolean hasDataTypeInIdentityColumn() {
193
public String getNoColumnsInsertString() {
194
return "default values";
197
public Class getNativeIdentifierGeneratorClass() {
198
return SequenceGenerator.class;
201
public boolean supportsOuterJoinForUpdate() {
205
public boolean useInputStreamToInsertBlob() {
209
public boolean supportsUnionAll() {
214
* Workaround for postgres bug #1453
216
public String getSelectClauseNullString(int sqlType) {
217
String typeName = getTypeName(sqlType, 1, 1, 0);
218
//trim off the length/precision/scale
219
int loc = typeName.indexOf('(');
221
typeName = typeName.substring(0, loc);
223
return "null::" + typeName;
226
public boolean supportsCommentOn() {
230
public boolean supportsTemporaryTables() {
234
public String getCreateTemporaryTableString() {
235
return "create local temporary table";
238
public String getCreateTemporaryTablePostfix() {
239
return "on commit drop";
242
/*public boolean dropTemporaryTableAfterUse() {
243
//we have to, because postgres sets current tx
244
//to rollback only after a failed create table
248
public boolean supportsCurrentTimestampSelection() {
252
public boolean isCurrentTimestampSelectStringCallable() {
256
public String getCurrentTimestampSelectString() {
257
return "select now()";
260
public String toBooleanValueString(boolean bool) {
261
return bool ? "true" : "false";
264
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
269
* Constraint-name extractor for Postgres contraint violation exceptions.
270
* Orginally contributed by Denny Bartelt.
272
private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
273
public String extractConstraintName(SQLException sqle) {
275
int sqlState = Integer.valueOf( JDBCExceptionHelper.extractSqlState(sqle)).intValue();
278
case 23514: return extractUsingTemplate("violates check constraint \"","\"", sqle.getMessage());
280
case 23505: return extractUsingTemplate("violates unique constraint \"","\"", sqle.getMessage());
281
// FOREIGN KEY VIOLATION
282
case 23503: return extractUsingTemplate("violates foreign key constraint \"","\"", sqle.getMessage());
283
// NOT NULL VIOLATION
284
case 23502: return extractUsingTemplate("null value in column \"","\" violates not-null constraint", sqle.getMessage());
285
// TODO: RESTRICT VIOLATION
286
case 23001: return null;
288
default: return null;
290
} catch (NumberFormatException nfe) {
297
// Overridden informational metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299
// seems to not really...
300
// public boolean supportsRowValueConstructorSyntax() {
304
public boolean supportsEmptyInList() {
308
public boolean supportsExpectedLobUsagePattern() {
309
// PostgreSQL seems to have spotty LOB suppport