~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/dialect/PostgreSQLDialect.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: PostgreSQLDialect.java 11366 2007-03-29 13:26:25Z steve.ebersole@jboss.com $
 
2
package org.hibernate.dialect;
 
3
 
 
4
import java.sql.Types;
 
5
import java.sql.SQLException;
 
6
 
 
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;
 
18
 
 
19
/**
 
20
 * An SQL dialect for Postgres
 
21
 * @author Gavin King
 
22
 */
 
23
public class PostgreSQLDialect extends Dialect {
 
24
 
 
25
        public PostgreSQLDialect() {
 
26
                super();
 
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)" );
 
43
 
 
44
                registerFunction( "abs", new StandardSQLFunction("abs") );
 
45
                registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );
 
46
 
 
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) );
 
61
 
 
62
                registerFunction( "stddev", new StandardSQLFunction("stddev", Hibernate.DOUBLE) );
 
63
                registerFunction( "variance", new StandardSQLFunction("variance", Hibernate.DOUBLE) );
 
64
 
 
65
                registerFunction( "random", new NoArgSQLFunction("random", Hibernate.DOUBLE) );
 
66
 
 
67
                registerFunction( "round", new StandardSQLFunction("round") );
 
68
                registerFunction( "trunc", new StandardSQLFunction("trunc") );
 
69
                registerFunction( "ceil", new StandardSQLFunction("ceil") );
 
70
                registerFunction( "floor", new StandardSQLFunction("floor") );
 
71
 
 
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) );
 
86
 
 
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") );
 
95
 
 
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) );
 
101
                
 
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) );
 
106
 
 
107
                registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","||",")" ) );
 
108
 
 
109
                registerFunction( "locate", new PositionSubstringFunction() );
 
110
 
 
111
                registerFunction( "str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as varchar)") );
 
112
 
 
113
                getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
 
114
        }
 
115
 
 
116
        public String getAddColumnString() {
 
117
                return "add column";
 
118
        }
 
119
 
 
120
        public String getSequenceNextValString(String sequenceName) {
 
121
                return "select " + getSelectSequenceNextValString( sequenceName );
 
122
        }
 
123
 
 
124
        public String getSelectSequenceNextValString(String sequenceName) {
 
125
                return "nextval ('" + sequenceName + "')";
 
126
        }
 
127
 
 
128
        public String getCreateSequenceString(String sequenceName) {
 
129
                return "create sequence " + sequenceName; //starts with 1, implicitly
 
130
        }
 
131
 
 
132
        public String getDropSequenceString(String sequenceName) {
 
133
                return "drop sequence " + sequenceName;
 
134
        }
 
135
 
 
136
        public String getCascadeConstraintsString() {
 
137
                return "";//" cascade";
 
138
        }
 
139
        public boolean dropConstraints() {
 
140
                return true;
 
141
        }
 
142
 
 
143
        public boolean supportsSequences() {
 
144
                return true;
 
145
        }
 
146
 
 
147
        public String getQuerySequencesString() {
 
148
                return "select relname from pg_class where relkind='S'";
 
149
        }
 
150
 
 
151
        public boolean supportsLimit() {
 
152
                return true;
 
153
        }
 
154
 
 
155
        public String getLimitString(String sql, boolean hasOffset) {
 
156
                return new StringBuffer( sql.length()+20 )
 
157
                        .append(sql)
 
158
                        .append(hasOffset ? " limit ? offset ?" : " limit ?")
 
159
                        .toString();
 
160
        }
 
161
 
 
162
        public boolean bindLimitParametersInReverseOrder() {
 
163
                return true;
 
164
        }
 
165
 
 
166
        public boolean supportsIdentityColumns() {
 
167
                return true;
 
168
        }
 
169
 
 
170
        public String getForUpdateString(String aliases) {
 
171
                return getForUpdateString() + " of " + aliases;
 
172
        }
 
173
 
 
174
        public String getIdentitySelectString(String table, String column, int type) {
 
175
                return new StringBuffer().append("select currval('")
 
176
                        .append(table)
 
177
                        .append('_')
 
178
                        .append(column)
 
179
                        .append("_seq')")
 
180
                        .toString();
 
181
        }
 
182
 
 
183
        public String getIdentityColumnString(int type) {
 
184
                return type==Types.BIGINT ?
 
185
                        "bigserial not null" :
 
186
                        "serial not null";
 
187
        }
 
188
 
 
189
        public boolean hasDataTypeInIdentityColumn() {
 
190
                return false;
 
191
        }
 
192
 
 
193
        public String getNoColumnsInsertString() {
 
194
                return "default values";
 
195
        }
 
196
 
 
197
        public Class getNativeIdentifierGeneratorClass() {
 
198
                return SequenceGenerator.class;
 
199
        }
 
200
 
 
201
        public boolean supportsOuterJoinForUpdate() {
 
202
                return false;
 
203
        }
 
204
        
 
205
        public boolean useInputStreamToInsertBlob() {
 
206
                return false;
 
207
        }
 
208
 
 
209
        public boolean supportsUnionAll() {
 
210
                return true;
 
211
        }
 
212
 
 
213
        /**
 
214
         * Workaround for postgres bug #1453
 
215
         */
 
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('(');
 
220
                if (loc>-1) {
 
221
                        typeName = typeName.substring(0, loc);
 
222
                }
 
223
                return "null::" + typeName;
 
224
        }
 
225
 
 
226
        public boolean supportsCommentOn() {
 
227
                return true;
 
228
        }
 
229
 
 
230
        public boolean supportsTemporaryTables() {
 
231
                return true;
 
232
        }
 
233
 
 
234
        public String getCreateTemporaryTableString() {
 
235
                return "create local temporary table";
 
236
        }
 
237
 
 
238
        public String getCreateTemporaryTablePostfix() {
 
239
                return "on commit drop";
 
240
        }
 
241
 
 
242
        /*public boolean dropTemporaryTableAfterUse() {
 
243
                //we have to, because postgres sets current tx
 
244
                //to rollback only after a failed create table
 
245
                return true;
 
246
        }*/
 
247
 
 
248
        public boolean supportsCurrentTimestampSelection() {
 
249
                return true;
 
250
        }
 
251
 
 
252
        public boolean isCurrentTimestampSelectStringCallable() {
 
253
                return false;
 
254
        }
 
255
 
 
256
        public String getCurrentTimestampSelectString() {
 
257
                return "select now()";
 
258
        }
 
259
 
 
260
        public String toBooleanValueString(boolean bool) {
 
261
                return bool ? "true" : "false";
 
262
        }
 
263
 
 
264
        public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
 
265
                return EXTRACTER;
 
266
        }
 
267
 
 
268
        /**
 
269
         * Constraint-name extractor for Postgres contraint violation exceptions.
 
270
         * Orginally contributed by Denny Bartelt.
 
271
         */
 
272
        private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
 
273
                public String extractConstraintName(SQLException sqle) {
 
274
                        try {
 
275
                                int sqlState = Integer.valueOf( JDBCExceptionHelper.extractSqlState(sqle)).intValue();
 
276
                                switch (sqlState) {
 
277
                                        // CHECK VIOLATION
 
278
                                        case 23514: return extractUsingTemplate("violates check constraint \"","\"", sqle.getMessage());
 
279
                                        // UNIQUE VIOLATION
 
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;
 
287
                                        // ALL OTHER
 
288
                                        default: return null;
 
289
                                }
 
290
                        } catch (NumberFormatException nfe) {
 
291
                                return null;
 
292
                        }
 
293
                }
 
294
        };
 
295
 
 
296
 
 
297
        // Overridden informational metadata ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
298
 
 
299
// seems to not really...
 
300
//      public boolean supportsRowValueConstructorSyntax() {
 
301
//              return true;
 
302
//      }
 
303
 
 
304
        public boolean supportsEmptyInList() {
 
305
                return false;
 
306
        }
 
307
 
 
308
        public boolean supportsExpectedLobUsagePattern() {
 
309
                // PostgreSQL seems to have spotty LOB suppport
 
310
                return false;
 
311
        }
 
312
}