KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: InformixDialect.java,v 1.17 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.MappingException;
8 import org.hibernate.exception.ErrorCodeConverter;
9 import org.hibernate.exception.JDBCExceptionHelper;
10 import org.hibernate.exception.SQLExceptionConverter;
11 import org.hibernate.exception.TemplatedViolatedConstraintNameExtracter;
12 import org.hibernate.exception.ViolatedConstraintNameExtracter;
13 import org.hibernate.util.StringHelper;
14
15 /**
16  * Informix dialect.<br>
17  * <br>
18  * Seems to work with Informix Dynamic Server Version 7.31.UD3,
19  * Informix JDBC driver version 2.21JC3.
20  * @author Steve Molitor
21  */

22 public class InformixDialect extends Dialect {
23
24     /**
25      * Creates new <code>InformixDialect</code> instance. Sets up the JDBC /
26      * Informix type mappings.
27      */

28     public InformixDialect() {
29         super();
30
31         registerColumnType(Types.BIGINT, "int8");
32         registerColumnType(Types.BINARY, "byte");
33         registerColumnType(Types.BIT, "smallint"); // Informix doesn't have a bit type
34
registerColumnType(Types.CHAR, "char($l)");
35         registerColumnType(Types.DATE, "date");
36         registerColumnType(Types.DECIMAL, "decimal");
37         registerColumnType(Types.DOUBLE, "double");
38         registerColumnType(Types.FLOAT, "float");
39         registerColumnType(Types.INTEGER, "integer");
40         registerColumnType(Types.LONGVARBINARY, "blob"); // or BYTE
41
registerColumnType(Types.LONGVARCHAR, "clob"); // or TEXT?
42
registerColumnType(Types.NUMERIC, "decimal"); // or MONEY
43
registerColumnType(Types.REAL, "smallfloat");
44         registerColumnType(Types.SMALLINT, "smallint");
45         registerColumnType(Types.TIMESTAMP, "datetime year to fraction(5)");
46         registerColumnType(Types.TIME, "datetime hour to second");
47         registerColumnType(Types.TINYINT, "smallint");
48         registerColumnType(Types.VARBINARY, "byte");
49         registerColumnType(Types.VARCHAR, "varchar($l)");
50     }
51
52     public String JavaDoc getAddColumnString() {
53         return "add";
54     }
55
56     public boolean supportsIdentityColumns() {
57         return true;
58     }
59
60     public String JavaDoc getIdentitySelectString(String JavaDoc table, String JavaDoc column, int type)
61     throws MappingException {
62         return type==Types.BIGINT ?
63             "select dbinfo('serial8') from systables where tabid=1" :
64             "select dbinfo('sqlca.sqlerrd1') from systables where tabid=1";
65     }
66
67     public String JavaDoc getIdentityColumnString(int type) throws MappingException {
68         return type==Types.BIGINT ?
69             "serial8 not null" :
70             "serial not null";
71     }
72
73     public boolean hasDataTypeInIdentityColumn() {
74         return false;
75     }
76
77     /**
78      * The syntax used to add a foreign key constraint to a table.
79      * Informix constraint name must be at the end.
80      * @return String
81      */

82     public String JavaDoc getAddForeignKeyConstraintString(
83             String JavaDoc constraintName,
84             String JavaDoc[] foreignKey,
85             String JavaDoc referencedTable,
86             String JavaDoc[] primaryKey, boolean referencesPrimaryKey
87     ) {
88         StringBuffer JavaDoc result = new StringBuffer JavaDoc(30);
89         
90         result.append(" add constraint ")
91             .append(" foreign key (")
92             .append( StringHelper.join(", ", foreignKey) )
93             .append(") references ")
94             .append(referencedTable);
95         
96         if(!referencesPrimaryKey) {
97             result.append(" (")
98                .append( StringHelper.join(", ", primaryKey) )
99                .append(')');
100         }
101
102         result.append(" constraint ").append(constraintName);
103             
104             return result.toString();
105     }
106
107     /**
108      * The syntax used to add a primary key constraint to a table.
109      * Informix constraint name must be at the end.
110      * @return String
111      */

112     public String JavaDoc getAddPrimaryKeyConstraintString(String JavaDoc constraintName) {
113         return " add constraint primary key constraint " + constraintName + " ";
114     }
115
116     public String JavaDoc getCreateSequenceString(String JavaDoc sequenceName) {
117         return "create sequence " + sequenceName;
118     }
119     public String JavaDoc getDropSequenceString(String JavaDoc sequenceName) {
120         return "drop sequence " + sequenceName + " restrict";
121     }
122
123     public String JavaDoc getSequenceNextValString(String JavaDoc sequenceName) {
124         return "select " + getSelectSequenceNextValString( sequenceName ) + " from systables where tabid=1";
125     }
126
127     public String JavaDoc getSelectSequenceNextValString(String JavaDoc sequenceName) {
128         return sequenceName + ".nextval";
129     }
130
131     public boolean supportsSequences() {
132         return true;
133     }
134
135     public boolean supportsLimit() {
136         return true;
137     }
138
139     public boolean useMaxForLimit() {
140         return true;
141     }
142
143     public boolean supportsLimitOffset() {
144         return false;
145     }
146
147     public String JavaDoc getLimitString(String JavaDoc querySelect, int offset, int limit) {
148         if (offset>0) throw new UnsupportedOperationException JavaDoc("informix has no offset");
149         return new StringBuffer JavaDoc( querySelect.length()+8 )
150             .append(querySelect)
151             .insert( querySelect.toLowerCase().indexOf( "select" ) + 6, " first " + limit )
152             .toString();
153     }
154
155     public boolean supportsVariableLimit() {
156         return false;
157     }
158
159     public SQLExceptionConverter buildSQLExceptionConverter() {
160         return new ExceptionConverter( getViolatedConstraintNameExtracter() );
161     }
162
163     public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
164         return EXTRACTER;
165     }
166
167     private static class ExceptionConverter extends ErrorCodeConverter {
168         private int[] sqlGrammarCodes = new int[]{};
169         private int[] integrityViolationCodes = new int[]{-239, -268, -691, -692};
170
171         public ExceptionConverter(ViolatedConstraintNameExtracter anExtracter) {
172             super( anExtracter );
173         }
174
175         protected int[] getSQLGrammarErrorCodes() {
176             return this.sqlGrammarCodes;
177         }
178
179         protected int[] getIntegrityViolationErrorCodes() {
180             return this.integrityViolationCodes;
181         }
182     }
183
184     private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
185
186         /**
187          * Extract the name of the violated constraint from the given SQLException.
188          *
189          * @param sqle The exception that was the result of the constraint violation.
190          * @return The extracted constraint name.
191          */

192         public String JavaDoc extractConstraintName(SQLException JavaDoc sqle) {
193             String JavaDoc constraintName = null;
194             
195             int errorCode = JDBCExceptionHelper.extractErrorCode(sqle);
196             if ( errorCode == -268 ) {
197                 constraintName = extractUsingTemplate( "Unique constraint (", ") violated.", sqle.getMessage() );
198             }
199             else if ( errorCode == -691 ) {
200                 constraintName = extractUsingTemplate( "Missing key in referenced table for referential constraint (", ").", sqle.getMessage() );
201             }
202             else if ( errorCode == -692 ) {
203                 constraintName = extractUsingTemplate( "Key value for constraint (", ") is still being referenced.", sqle.getMessage() );
204             }
205             
206             if (constraintName != null) {
207                 // strip table-owner because Informix always returns constraint names as "<table-owner>.<constraint-name>"
208
int i = constraintName.indexOf('.');
209                 if (i != -1) {
210                     constraintName = constraintName.substring(i + 1);
211                 }
212             }
213
214             return constraintName;
215         }
216
217     };
218
219 }
Popular Tags