1 package org.hibernate.dialect; 3 4 import java.sql.SQLException ; 5 import java.sql.Types ; 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 22 public class InformixDialect extends Dialect { 23 24 28 public InformixDialect() { 29 super(); 30 31 registerColumnType(Types.BIGINT, "int8"); 32 registerColumnType(Types.BINARY, "byte"); 33 registerColumnType(Types.BIT, "smallint"); 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"); registerColumnType(Types.LONGVARCHAR, "clob"); registerColumnType(Types.NUMERIC, "decimal"); 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 getAddColumnString() { 53 return "add"; 54 } 55 56 public boolean supportsIdentityColumns() { 57 return true; 58 } 59 60 public String getIdentitySelectString(String table, String 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 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 82 public String getAddForeignKeyConstraintString( 83 String constraintName, 84 String [] foreignKey, 85 String referencedTable, 86 String [] primaryKey, boolean referencesPrimaryKey 87 ) { 88 StringBuffer result = new StringBuffer (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 112 public String getAddPrimaryKeyConstraintString(String constraintName) { 113 return " add constraint primary key constraint " + constraintName + " "; 114 } 115 116 public String getCreateSequenceString(String sequenceName) { 117 return "create sequence " + sequenceName; 118 } 119 public String getDropSequenceString(String sequenceName) { 120 return "drop sequence " + sequenceName + " restrict"; 121 } 122 123 public String getSequenceNextValString(String sequenceName) { 124 return "select " + getSelectSequenceNextValString( sequenceName ) + " from systables where tabid=1"; 125 } 126 127 public String getSelectSequenceNextValString(String 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 getLimitString(String querySelect, int offset, int limit) { 148 if (offset>0) throw new UnsupportedOperationException ("informix has no offset"); 149 return new StringBuffer ( 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 192 public String extractConstraintName(SQLException sqle) { 193 String 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 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 |