KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > dialect > PostgreSQLDialect


1 //$Id: PostgreSQLDialect.java,v 1.25 2005/07/12 01:56:08 oneovthafew Exp $
2
package org.hibernate.dialect;
3
4 import java.sql.Types JavaDoc;
5
6 import org.hibernate.Hibernate;
7 import org.hibernate.cfg.Environment;
8 import org.hibernate.dialect.function.NoArgSQLFunction;
9 import org.hibernate.dialect.function.PositionSubstringFunction;
10 import org.hibernate.dialect.function.SQLFunctionTemplate;
11 import org.hibernate.dialect.function.StandardSQLFunction;
12 import org.hibernate.dialect.function.VarArgsSQLFunction;
13 import org.hibernate.id.SequenceGenerator;
14
15 /**
16  * An SQL dialect for Postgres
17  * @author Gavin King
18  */

19 public class PostgreSQLDialect extends Dialect {
20
21     public PostgreSQLDialect() {
22         super();
23         registerColumnType( Types.BIT, "bool" );
24         registerColumnType( Types.BIGINT, "int8" );
25         registerColumnType( Types.SMALLINT, "int2" );
26         registerColumnType( Types.TINYINT, "int2" );
27         registerColumnType( Types.INTEGER, "int4" );
28         registerColumnType( Types.CHAR, "char(1)" );
29         registerColumnType( Types.VARCHAR, "varchar($l)" );
30         registerColumnType( Types.FLOAT, "float4" );
31         registerColumnType( Types.DOUBLE, "float8" );
32         registerColumnType( Types.DATE, "date" );
33         registerColumnType( Types.TIME, "time" );
34         registerColumnType( Types.TIMESTAMP, "timestamp" );
35         registerColumnType( Types.VARBINARY, "bytea" );
36         registerColumnType( Types.CLOB, "text" );
37         registerColumnType( Types.BLOB, "oid" );
38         registerColumnType( Types.NUMERIC, "numeric($p, $s)" );
39
40         registerFunction( "abs", new StandardSQLFunction("abs") );
41         registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );
42
43         registerFunction( "acos", new StandardSQLFunction("acos", Hibernate.DOUBLE) );
44         registerFunction( "asin", new StandardSQLFunction("asin", Hibernate.DOUBLE) );
45         registerFunction( "atan", new StandardSQLFunction("atan", Hibernate.DOUBLE) );
46         registerFunction( "cos", new StandardSQLFunction("cos", Hibernate.DOUBLE) );
47         registerFunction( "cot", new StandardSQLFunction("cot", Hibernate.DOUBLE) );
48         registerFunction( "exp", new StandardSQLFunction("exp", Hibernate.DOUBLE) );
49         registerFunction( "ln", new StandardSQLFunction("ln", Hibernate.DOUBLE) );
50         registerFunction( "log", new StandardSQLFunction("log", Hibernate.DOUBLE) );
51         registerFunction( "sin", new StandardSQLFunction("sin", Hibernate.DOUBLE) );
52         registerFunction( "sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE) );
53         registerFunction( "cbrt", new StandardSQLFunction("cbrt", Hibernate.DOUBLE) );
54         registerFunction( "tan", new StandardSQLFunction("tan", Hibernate.DOUBLE) );
55         registerFunction( "radians", new StandardSQLFunction("radians", Hibernate.DOUBLE) );
56         registerFunction( "degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE) );
57
58         registerFunction( "stddev", new StandardSQLFunction("stddev", Hibernate.DOUBLE) );
59         registerFunction( "variance", new StandardSQLFunction("variance", Hibernate.DOUBLE) );
60
61         registerFunction( "random", new NoArgSQLFunction("random", Hibernate.DOUBLE) );
62
63         registerFunction( "round", new StandardSQLFunction("round") );
64         registerFunction( "trunc", new StandardSQLFunction("trunc") );
65         registerFunction( "ceil", new StandardSQLFunction("ceil") );
66         registerFunction( "floor", new StandardSQLFunction("floor") );
67
68         registerFunction( "chr", new StandardSQLFunction("chr", Hibernate.CHARACTER) );
69         registerFunction( "lower", new StandardSQLFunction("lower") );
70         registerFunction( "upper", new StandardSQLFunction("upper") );
71         registerFunction( "substr", new StandardSQLFunction("substr", Hibernate.STRING) );
72         registerFunction( "initcap", new StandardSQLFunction("initcap") );
73         registerFunction( "to_ascii", new StandardSQLFunction("to_ascii") );
74         registerFunction( "quote_ident", new StandardSQLFunction("quote_ident", Hibernate.STRING) );
75         registerFunction( "quote_literal", new StandardSQLFunction("quote_literal", Hibernate.STRING) );
76         registerFunction( "md5", new StandardSQLFunction("md5") );
77         registerFunction( "ascii", new StandardSQLFunction("ascii", Hibernate.INTEGER) );
78         registerFunction( "length", new StandardSQLFunction("length", Hibernate.LONG) );
79         registerFunction( "char_length", new StandardSQLFunction("char_length", Hibernate.LONG) );
80         registerFunction( "bit_length", new StandardSQLFunction("bit_length", Hibernate.LONG) );
81         registerFunction( "octet_length", new StandardSQLFunction("octet_length", Hibernate.LONG) );
82
83         registerFunction( "current_date", new NoArgSQLFunction("current_date", Hibernate.DATE, false) );
84         registerFunction( "current_time", new NoArgSQLFunction("current_time", Hibernate.TIME, false) );
85         registerFunction( "current_timestamp", new NoArgSQLFunction("current_timestamp", Hibernate.TIMESTAMP, false) );
86         registerFunction( "localtime", new NoArgSQLFunction("localtime", Hibernate.TIME, false) );
87         registerFunction( "localtimestamp", new NoArgSQLFunction("localtimestamp", Hibernate.TIMESTAMP, false) );
88         registerFunction( "now", new NoArgSQLFunction("now", Hibernate.TIMESTAMP) );
89         registerFunction( "timeofday", new NoArgSQLFunction("timeofday", Hibernate.STRING) );
90         registerFunction( "age", new StandardSQLFunction("age") );
91
92         registerFunction( "current_user", new NoArgSQLFunction("current_user", Hibernate.STRING, false) );
93         registerFunction( "session_user", new NoArgSQLFunction("session_user", Hibernate.STRING, false) );
94         registerFunction( "user", new NoArgSQLFunction("user", Hibernate.STRING, false) );
95         registerFunction( "current_database", new NoArgSQLFunction("current_database", Hibernate.STRING, true) );
96         registerFunction( "current_schema", new NoArgSQLFunction("current_schema", Hibernate.STRING, true) );
97
98         registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","||",")" ) );
99
100         registerFunction( "locate", new PositionSubstringFunction() );
101
102         registerFunction( "str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as varchar)") );
103
104         getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
105     }
106
107     public String JavaDoc getAddColumnString() {
108         return "add column";
109     }
110
111     public String JavaDoc getSequenceNextValString(String JavaDoc sequenceName) {
112         return "select " + getSelectSequenceNextValString( sequenceName );
113     }
114
115     public String JavaDoc getSelectSequenceNextValString(String JavaDoc sequenceName) {
116         return "nextval ('" + sequenceName + "')";
117     }
118
119     public String JavaDoc getCreateSequenceString(String JavaDoc sequenceName) {
120         return "create sequence " + sequenceName; //starts with 1, implicitly
121
}
122
123     public String JavaDoc getDropSequenceString(String JavaDoc sequenceName) {
124         return "drop sequence " + sequenceName;
125     }
126
127     public String JavaDoc getCascadeConstraintsString() {
128         return "";//" cascade";
129
}
130     public boolean dropConstraints() {
131         return true;
132     }
133
134     public boolean supportsSequences() {
135         return true;
136     }
137
138     public String JavaDoc getQuerySequencesString() {
139         return "select relname from pg_class where relkind='S'";
140     }
141
142     public boolean supportsLimit() {
143         return true;
144     }
145
146     public String JavaDoc getLimitString(String JavaDoc sql, boolean hasOffset) {
147         return new StringBuffer JavaDoc( sql.length()+20 )
148             .append(sql)
149             .append(hasOffset ? " limit ? offset ?" : " limit ?")
150             .toString();
151     }
152
153     public boolean bindLimitParametersInReverseOrder() {
154         return true;
155     }
156
157     public boolean supportsIdentityColumns() {
158         return true;
159     }
160
161     public String JavaDoc getForUpdateString(String JavaDoc aliases) {
162         return getForUpdateString() + " of " + aliases;
163     }
164
165     public String JavaDoc getIdentitySelectString(String JavaDoc table, String JavaDoc column, int type) {
166         return new StringBuffer JavaDoc().append("select currval('")
167             .append(table)
168             .append('_')
169             .append(column)
170             .append("_seq')")
171             .toString();
172     }
173
174     public String JavaDoc getIdentityColumnString(int type) {
175         return type==Types.BIGINT ?
176             "bigserial not null" :
177             "serial not null";
178     }
179
180     public boolean hasDataTypeInIdentityColumn() {
181         return false;
182     }
183
184     public String JavaDoc getNoColumnsInsertString() {
185         return "default values";
186     }
187
188     public Class JavaDoc getNativeIdentifierGeneratorClass() {
189         return SequenceGenerator.class;
190     }
191
192     public boolean supportsOuterJoinForUpdate() {
193         return false;
194     }
195     
196     public boolean useInputStreamToInsertBlob() {
197         return false;
198     }
199
200     public boolean supportsUnionAll() {
201         return true;
202     }
203
204     /**
205      * Workaround for postgres bug #1453
206      */

207     public String JavaDoc getSelectClauseNullString(int sqlType) {
208         String JavaDoc typeName = getTypeName(sqlType, 1, 1, 0);
209         //trim off the length/precision/scale
210
int loc = typeName.indexOf('(');
211         if (loc>-1) {
212             typeName = typeName.substring(0, loc);
213         }
214         return "null::" + typeName;
215     }
216
217     public boolean supportsCommentOn() {
218         return true;
219     }
220
221     public boolean supportsTemporaryTables() {
222         return true;
223     }
224
225     public String JavaDoc getTemporaryTableCreationCommand() {
226         return "create local temporary table";
227     }
228
229     public String JavaDoc getTemporaryTableCreationPostfix() {
230         return "on commit drop";
231     }
232
233     /*public boolean dropTemporaryTableAfterUse() {
234         //we have to, because postgres sets current tx
235         //to rollback only after a failed create table
236         return true;
237     }*/

238     
239 }
240
Popular Tags