1 56 package org.objectstyle.cayenne.dba.firebird; 57 58 import java.util.Iterator ; 59 60 import org.objectstyle.cayenne.CayenneRuntimeException; 61 import org.objectstyle.cayenne.access.types.CharType; 62 import org.objectstyle.cayenne.access.types.ExtendedTypeMap; 63 import org.objectstyle.cayenne.dba.JdbcAdapter; 64 import org.objectstyle.cayenne.dba.PkGenerator; 65 import org.objectstyle.cayenne.dba.TypesMapping; 66 import org.objectstyle.cayenne.map.DbAttribute; 67 import org.objectstyle.cayenne.map.DbEntity; 68 import org.objectstyle.cayenne.map.DbRelationship; 69 import org.objectstyle.cayenne.map.DerivedDbEntity; 70 71 86 public class FirebirdAdapter extends JdbcAdapter { 87 88 public FirebirdAdapter() { 89 super(); 90 } 91 92 public String createTable(DbEntity ent) { 93 if (ent instanceof DerivedDbEntity) { 94 throw new CayenneRuntimeException( 95 "Can't create table for derived DbEntity '" + ent.getName() + "'."); 96 } 97 98 StringBuffer buf = new StringBuffer (); 99 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" ("); 100 101 Iterator it = ent.getAttributes().iterator(); 103 boolean first = true; 104 while (it.hasNext()) { 105 if (first) { 106 first = false; 107 } 108 else { 109 buf.append(", "); 110 } 111 112 DbAttribute at = (DbAttribute) it.next(); 113 114 if (at.getType() == TypesMapping.NOT_DEFINED) { 116 throw new CayenneRuntimeException( 117 "Undefined type for attribute '" 118 + ent.getFullyQualifiedName() 119 + "." 120 + at.getName() 121 + "'."); 122 } 123 124 String [] types = externalTypesForJdbcType(at.getType()); 125 if (types == null || types.length == 0) { 126 throw new CayenneRuntimeException( 127 "Undefined type for attribute '" 128 + ent.getFullyQualifiedName() 129 + "." 130 + at.getName() 131 + "': " 132 + at.getType()); 133 } 134 135 String type = types[0]; 136 buf.append(at.getName()).append(' ').append(type); 137 138 if (typeSupportsLength(at.getType())) { 140 int len = at.getMaxLength(); 141 int prec = TypesMapping.isDecimal(at.getType()) ? at.getPrecision() : -1; 142 143 if (type.compareTo("double precision") == 0) len = 0; 145 146 if (prec > len) { 148 prec = -1; 149 } 150 151 if (len > 0) { 152 buf.append('(').append(len); 153 154 if (prec >= 0) { 155 buf.append(", ").append(prec); 156 } 157 buf.append(')'); 158 } 159 } 160 161 if (at.isMandatory()) { 162 buf.append(" NOT NULL"); 163 } 164 165 } 166 167 Iterator pkit = ent.getPrimaryKey().iterator(); 169 if (pkit.hasNext()) { 170 if (first) 171 first = false; 172 else 173 buf.append(", "); 174 175 buf.append("PRIMARY KEY ("); 176 boolean firstPk = true; 177 while (pkit.hasNext()) { 178 if (firstPk) 179 firstPk = false; 180 else 181 buf.append(", "); 182 183 DbAttribute at = (DbAttribute) pkit.next(); 184 buf.append(at.getName()); 185 } 186 buf.append(')'); 187 } 188 buf.append(");"); return buf.toString(); 190 } 191 192 private boolean typeSupportsLength(int type) { 193 String [] externalTypes = externalTypesForJdbcType(type); 195 if (externalTypes != null && externalTypes.length > 0) { 196 for (int i = 0; i < externalTypes.length; i++) { 197 if ("blob sub_type 0".equalsIgnoreCase(externalTypes[i])) { 198 return false; 199 } 200 } 201 } 202 203 return TypesMapping.supportsLength(type); 204 } 205 206 210 public String createFkConstraint(DbRelationship rel) { 211 return super.createFkConstraint(rel) + ";"; 213 } 214 215 public String dropTable(DbEntity ent) { 216 return super.dropTable(ent) + ";"; 218 } 219 220 protected void configureExtendedTypes(ExtendedTypeMap map) { 221 super.configureExtendedTypes(map); 222 map.registerType(new CharType(true, false)); 223 } 224 225 228 protected PkGenerator createPkGenerator() { 229 return new FirebirdPkGenerator(); 230 } 231 232 236 } 237 | Popular Tags |