KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: HSQLDialect.java,v 1.29 2005/07/07 02:39:10 steveebersole Exp $
2
package org.hibernate.dialect;
3
4 import java.sql.SQLException JavaDoc;
5 import java.sql.Types JavaDoc;
6
7 import org.hibernate.Hibernate;
8 import org.hibernate.cfg.Environment;
9 import org.hibernate.dialect.function.NoArgSQLFunction;
10 import org.hibernate.dialect.function.StandardSQLFunction;
11 import org.hibernate.dialect.function.VarArgsSQLFunction;
12 import org.hibernate.exception.ErrorCodeConverter;
13 import org.hibernate.exception.JDBCExceptionHelper;
14 import org.hibernate.exception.SQLExceptionConverter;
15 import org.hibernate.exception.TemplatedViolatedConstraintNameExtracter;
16 import org.hibernate.exception.ViolatedConstraintNameExtracter;
17
18 /**
19  * An SQL dialect compatible with HSQLDB (Hypersonic SQL).
20  *
21  * @author Christoph Sturm, Phillip Baird
22  */

23 public class HSQLDialect extends Dialect {
24
25     public HSQLDialect() {
26         super();
27         registerColumnType( Types.BIGINT, "bigint" );
28         registerColumnType( Types.BINARY, "binary" );
29         registerColumnType( Types.BIT, "bit" );
30         registerColumnType( Types.CHAR, "char(1)" );
31         registerColumnType( Types.DATE, "date" );
32         registerColumnType( Types.DECIMAL, "decimal" );
33         registerColumnType( Types.DOUBLE, "double" );
34         registerColumnType( Types.FLOAT, "float" );
35         registerColumnType( Types.INTEGER, "integer" );
36         registerColumnType( Types.LONGVARBINARY, "longvarbinary" );
37         registerColumnType( Types.LONGVARCHAR, "longvarchar" );
38         registerColumnType( Types.SMALLINT, "smallint" );
39         registerColumnType( Types.TINYINT, "tinyint" );
40         registerColumnType( Types.TIME, "time" );
41         registerColumnType( Types.TIMESTAMP, "timestamp" );
42         registerColumnType( Types.VARCHAR, "varchar($l)" );
43         registerColumnType( Types.VARBINARY, "varbinary($l)" );
44         registerColumnType( Types.NUMERIC, "numeric" );
45         //HSQL has no Blob/Clob support .... but just put these here for now!
46
registerColumnType( Types.BLOB, "longvarbinary" );
47         registerColumnType( Types.CLOB, "longvarchar" );
48
49         registerFunction( "ascii", new StandardSQLFunction( "ascii", Hibernate.INTEGER ) );
50         registerFunction( "char", new StandardSQLFunction( "char", Hibernate.CHARACTER ) );
51         registerFunction( "length", new StandardSQLFunction( "length", Hibernate.LONG ) );
52         registerFunction( "lower", new StandardSQLFunction("lower") );
53         registerFunction( "upper", new StandardSQLFunction("upper") );
54         registerFunction( "lcase", new StandardSQLFunction("lcase") );
55         registerFunction( "ucase", new StandardSQLFunction("ucase") );
56         registerFunction( "soundex", new StandardSQLFunction( "soundex", Hibernate.STRING ) );
57         registerFunction( "ltrim", new StandardSQLFunction("ltrim") );
58         registerFunction( "rtrim", new StandardSQLFunction("rtrim") );
59         registerFunction( "reverse", new StandardSQLFunction("reverse") );
60         registerFunction( "space", new StandardSQLFunction( "space", Hibernate.STRING ) );
61         registerFunction( "rawtohex", new StandardSQLFunction("rawtohex") );
62         registerFunction( "hextoraw", new StandardSQLFunction("hextoraw") );
63
64         registerFunction( "user", new NoArgSQLFunction( "user", Hibernate.STRING ) );
65         registerFunction( "database", new NoArgSQLFunction( "database", Hibernate.STRING ) );
66
67         registerFunction( "current_date", new NoArgSQLFunction( "current_date", Hibernate.DATE, false ) );
68         registerFunction( "curdate", new NoArgSQLFunction( "curdate", Hibernate.DATE ) );
69         registerFunction( "current_timestamp", new NoArgSQLFunction( "current_timestamp", Hibernate.TIMESTAMP, false ) );
70         registerFunction( "now", new NoArgSQLFunction( "now", Hibernate.TIMESTAMP ) );
71         registerFunction( "current_time", new NoArgSQLFunction( "current_time", Hibernate.TIME, false ) );
72         registerFunction( "curtime", new NoArgSQLFunction( "curtime", Hibernate.TIME ) );
73         registerFunction( "day", new StandardSQLFunction( "day", Hibernate.INTEGER ) );
74         registerFunction( "dayofweek", new StandardSQLFunction( "dayofweek", Hibernate.INTEGER ) );
75         registerFunction( "dayofyear", new StandardSQLFunction( "dayofyear", Hibernate.INTEGER ) );
76         registerFunction( "dayofmonth", new StandardSQLFunction( "dayofmonth", Hibernate.INTEGER ) );
77         registerFunction( "month", new StandardSQLFunction( "month", Hibernate.INTEGER ) );
78         registerFunction( "year", new StandardSQLFunction( "year", Hibernate.INTEGER ) );
79         registerFunction( "week", new StandardSQLFunction( "week", Hibernate.INTEGER ) );
80         registerFunction( "quater", new StandardSQLFunction( "quater", Hibernate.INTEGER ) );
81         registerFunction( "hour", new StandardSQLFunction( "hour", Hibernate.INTEGER ) );
82         registerFunction( "minute", new StandardSQLFunction( "minute", Hibernate.INTEGER ) );
83         registerFunction( "second", new StandardSQLFunction( "second", Hibernate.INTEGER ) );
84         registerFunction( "dayname", new StandardSQLFunction( "dayname", Hibernate.STRING ) );
85         registerFunction( "monthname", new StandardSQLFunction( "monthname", Hibernate.STRING ) );
86
87         registerFunction( "abs", new StandardSQLFunction("abs") );
88         registerFunction( "sign", new StandardSQLFunction( "sign", Hibernate.INTEGER ) );
89
90         registerFunction( "acos", new StandardSQLFunction( "acos", Hibernate.DOUBLE ) );
91         registerFunction( "asin", new StandardSQLFunction( "asin", Hibernate.DOUBLE ) );
92         registerFunction( "atan", new StandardSQLFunction( "atan", Hibernate.DOUBLE ) );
93         registerFunction( "cos", new StandardSQLFunction( "cos", Hibernate.DOUBLE ) );
94         registerFunction( "cot", new StandardSQLFunction( "cot", Hibernate.DOUBLE ) );
95         registerFunction( "exp", new StandardSQLFunction( "exp", Hibernate.DOUBLE ) );
96         registerFunction( "log", new StandardSQLFunction( "log", Hibernate.DOUBLE ) );
97         registerFunction( "log10", new StandardSQLFunction( "log10", Hibernate.DOUBLE ) );
98         registerFunction( "sin", new StandardSQLFunction( "sin", Hibernate.DOUBLE ) );
99         registerFunction( "sqrt", new StandardSQLFunction( "sqrt", Hibernate.DOUBLE ) );
100         registerFunction( "tan", new StandardSQLFunction( "tan", Hibernate.DOUBLE ) );
101         registerFunction( "pi", new NoArgSQLFunction( "pi", Hibernate.DOUBLE ) );
102         registerFunction( "rand", new StandardSQLFunction( "rand", Hibernate.FLOAT ) );
103
104         registerFunction( "radians", new StandardSQLFunction( "radians", Hibernate.DOUBLE ) );
105         registerFunction( "degrees", new StandardSQLFunction( "degrees", Hibernate.DOUBLE ) );
106         registerFunction( "roundmagic", new StandardSQLFunction("roundmagic") );
107
108         registerFunction( "ceiling", new StandardSQLFunction("ceiling") );
109         registerFunction( "floor", new StandardSQLFunction("floor") );
110
111         // Multi-param dialect functions...
112
registerFunction( "mod", new StandardSQLFunction( "mod", Hibernate.INTEGER ) );
113
114         // function templates
115
registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","||",")" ) );
116
117         getDefaultProperties().setProperty( Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE );
118     }
119
120     public String JavaDoc getAddColumnString() {
121         return "add column";
122     }
123
124     public boolean supportsIdentityColumns() {
125         return true;
126     }
127
128     public String JavaDoc getIdentityColumnString() {
129         return "generated by default as identity (start with 1)"; //not null is implicit
130
}
131
132     public String JavaDoc getIdentitySelectString() {
133         return "call identity()";
134     }
135
136     public String JavaDoc getIdentityInsertString() {
137         return "null";
138     }
139
140     public String JavaDoc getForUpdateString() {
141         return "";
142     }
143
144     /**
145      * Not supported in 1.7.1 (1.7.2 only)
146      */

147     public boolean supportsUnique() {
148         return false;
149     }
150
151     public boolean supportsLimit() {
152         return true;
153     }
154
155     public String JavaDoc getLimitString(String JavaDoc sql, boolean hasOffset) {
156         return new StringBuffer JavaDoc( sql.length() + 10 )
157                 .append( sql )
158                 .insert( sql.toLowerCase().indexOf( "select" ) + 6, hasOffset ? " limit ? ?" : " top ?" )
159                 .toString();
160     }
161
162     /*public CaseFragment createCaseFragment() {
163         return new HSQLCaseFragment();
164     }*/

165
166     public boolean bindLimitParametersFirst() {
167         return true;
168     }
169
170     public boolean supportsIfExistsAfterTableName() {
171         return true;
172     }
173
174     public boolean supportsColumnCheck() {
175         return false;
176     }
177
178     public String JavaDoc[] getCreateSequenceStrings(String JavaDoc sequenceName) {
179         return new String JavaDoc[]{
180             "create table dual_" + sequenceName + " (zero integer)",
181             "insert into dual_" + sequenceName + " values (0)",
182             "create sequence " + sequenceName + " start with 1"
183         };
184     }
185
186     public String JavaDoc[] getDropSequenceStrings(String JavaDoc sequenceName) {
187         return new String JavaDoc[]{
188             "drop table dual_" + sequenceName + " if exists",
189             "drop sequence " + sequenceName
190         };
191     }
192
193     public String JavaDoc getSequenceNextValString(String JavaDoc sequenceName) {
194         return "select " + getSelectSequenceNextValString( sequenceName ) + " from dual_" + sequenceName;
195     }
196
197     public String JavaDoc getSelectSequenceNextValString(String JavaDoc sequenceName) {
198         return "next value for " + sequenceName;
199     }
200
201     public boolean supportsSequences() {
202         return true;
203     }
204
205     public String JavaDoc getQuerySequencesString() {
206         return "select sequence_name from system_sequences";
207     }
208
209     /**
210      * Build an instance of the SQLExceptionConverter preferred by this dialect for
211      * converting SQLExceptions into Hibernate's JDBCException hierarchy. The default
212      * Dialect implementation simply returns a converter based on X/Open SQLState codes.
213      * <p/>
214      * It is strongly recommended that specific Dialect implementations override this
215      * method, since interpretation of a SQL error is much more accurate when based on
216      * the ErrorCode rather than the SQLState. Unfortunately, the ErrorCode is a vendor-
217      * specific approach.
218      *
219      * @return The Dialect's preferred SQLExceptionConverter.
220      */

221     public SQLExceptionConverter buildSQLExceptionConverter() {
222         return new ExceptionConverter( getViolatedConstraintNameExtracter() );
223     }
224
225     private static class ExceptionConverter extends ErrorCodeConverter {
226         private int[] sqlGrammarCodes = new int[]{-22, -28};
227         private int[] integrityViolationCodes = new int[]{-8, -9, -177, -104};
228
229         public ExceptionConverter(ViolatedConstraintNameExtracter extracter) {
230             super( extracter );
231         }
232
233         protected int[] getSQLGrammarErrorCodes() {
234             return sqlGrammarCodes;
235         }
236
237         protected int[] getIntegrityViolationErrorCodes() {
238             return integrityViolationCodes;
239         }
240     }
241
242     public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
243         return EXTRACTER;
244     }
245
246     private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
247
248         /**
249          * Extract the name of the violated constraint from the given SQLException.
250          *
251          * @param sqle The exception that was the result of the constraint violation.
252          * @return The extracted constraint name.
253          */

254         public String JavaDoc extractConstraintName(SQLException JavaDoc sqle) {
255             String JavaDoc constraintName = null;
256             
257             int errorCode = JDBCExceptionHelper.extractErrorCode(sqle);
258
259             if ( errorCode == -8 ) {
260                 constraintName = extractUsingTemplate( "Integrity constraint violation ", " table:", sqle.getMessage() );
261             }
262             else if ( errorCode == -9 ) {
263                 constraintName = extractUsingTemplate( "Violation of unique index: ", " in statement [", sqle.getMessage() );
264             }
265             else if ( errorCode == -104 ) {
266                 constraintName = extractUsingTemplate( "Unique constraint violation: ", " in statement [", sqle.getMessage() );
267             }
268             else if ( errorCode == -177 ) {
269                 constraintName = extractUsingTemplate( "Integrity constraint violation - no parent ", " table:", sqle.getMessage() );
270             }
271
272             return constraintName;
273         }
274
275     };
276
277     public boolean supportsTemporaryTables() {
278         return true;
279     }
280
281     public String JavaDoc getTemporaryTableCreationCommand() {
282         // :) Not really a temp table; just taking advantage of the
283
// fact that it is a single user db...
284
return "create table";
285     }
286
287 }
288
Popular Tags