1 21 package oracle.toplink.essentials.tools.schemaframework; 23 24 import java.io.*; 25 import oracle.toplink.essentials.internal.helper.*; 26 import oracle.toplink.essentials.internal.databaseaccess.*; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.internal.sessions.AbstractSession; 29 30 41 public class FieldDefinition implements Serializable, Cloneable { 42 protected String name; 43 protected Class type; 44 protected String typeName; 45 protected int size; 46 protected int subSize; 47 protected boolean shouldAllowNull; 48 protected boolean isIdentity; 49 protected boolean isPrimaryKey; 50 protected boolean isUnique; 51 protected String additional; 52 protected String constraint; 53 protected String foreignKeyFieldName; 54 55 public FieldDefinition() { 56 this.name = ""; 57 this.size = 0; 58 this.subSize = 0; 59 this.shouldAllowNull = true; 60 this.isIdentity = false; 61 this.isPrimaryKey = false; 62 this.isUnique = false; 63 } 64 65 public FieldDefinition(String name, Class type) { 66 this.name = name; 67 this.type = type; 68 this.size = 0; 69 this.subSize = 0; 70 shouldAllowNull = true; 71 isIdentity = false; 72 isPrimaryKey = false; 73 isUnique = false; 74 } 75 76 public FieldDefinition(String name, Class type, int size) { 77 this(); 78 this.name = name; 79 this.type = type; 80 this.size = size; 81 } 82 83 public FieldDefinition(String name, Class type, int size, int subSize) { 84 this(); 85 this.name = name; 86 this.type = type; 87 this.size = size; 88 this.subSize = subSize; 89 } 90 91 public FieldDefinition(String name, String typeName) { 92 this(); 93 this.name = name; 94 this.typeName = typeName; 95 } 96 97 101 public void appendDBString(Writer writer, AbstractSession session, TableDefinition table) throws ValidationException { 102 FieldTypeDefinition fieldType; 103 if (getType() != null) { 104 fieldType = session.getPlatform().getFieldTypeDefinition(getType()); 105 if (fieldType == null) { 106 throw ValidationException.javaTypeIsNotAValidDatabaseType(getType()); 107 } 108 } else { 109 fieldType = new FieldTypeDefinition(getTypeName()); 110 } 111 try { 112 writer.write(getName()); 113 writer.write(" "); 114 String qualifiedName = table.getFullName() + '.' + getName(); 115 session.getPlatform().printFieldTypeSize(writer, this, fieldType, session, qualifiedName); 116 session.getPlatform().printFieldUnique(writer, isUnique(), session, qualifiedName); 117 if (isIdentity()) { 118 String name = table.getFullName() + '.' + getName(); 119 session.getPlatform().printFieldIdentityClause(writer, session, name); 120 } 121 if (shouldAllowNull() && fieldType.shouldAllowNull()) { 122 session.getPlatform().printFieldNullClause(writer); 123 } else { 124 session.getPlatform().printFieldNotNullClause(writer); 125 } 126 if (getConstraint() != null) { 127 writer.write(" " + getConstraint()); 128 } 129 if (getAdditional() != null) { 130 writer.write(" " + getAdditional()); 131 } 132 } catch (IOException ioException) { 133 throw ValidationException.fileError(ioException); 134 } 135 } 136 137 142 public void appendTypeString(Writer writer, AbstractSession session) throws ValidationException { 143 FieldTypeDefinition fieldType; 144 if (getType() != null) { 145 fieldType = session.getPlatform().getFieldTypeDefinition(getType()); 146 if (fieldType == null) { 147 throw ValidationException.javaTypeIsNotAValidDatabaseType(getType()); 148 } 149 } else { 150 fieldType = new FieldTypeDefinition(getTypeName()); 151 } 152 try { 153 writer.write(getName()); 154 writer.write(" "); 155 writer.write(fieldType.getName()); 156 if ((fieldType.isSizeAllowed()) && ((getSize() != 0) || (fieldType.isSizeRequired()))) { 157 writer.write("("); 158 if (getSize() == 0) { 159 writer.write(new Integer (fieldType.getDefaultSize()).toString()); 160 } else { 161 writer.write(new Integer (getSize()).toString()); 162 } 163 if (getSubSize() != 0) { 164 writer.write(","); 165 writer.write(new Integer (getSubSize()).toString()); 166 } else if (fieldType.getDefaultSubSize() != 0) { 167 writer.write(","); 168 writer.write(new Integer (fieldType.getDefaultSubSize()).toString()); 169 } 170 writer.write(")"); 171 } 172 if (getAdditional() != null) { 173 writer.write(" " + getAdditional()); 174 } 175 } catch (IOException ioException) { 176 throw ValidationException.fileError(ioException); 177 } 178 } 179 180 183 public Object clone() { 184 try { 185 return super.clone(); 186 } catch (CloneNotSupportedException impossible) { 187 return null; 188 } 189 } 190 191 195 public String getAdditional() { 196 return additional; 197 } 198 199 204 public String getConstraint() { 205 return constraint; 206 } 207 208 public String getForeignKeyFieldName() { 209 return foreignKeyFieldName; 210 } 211 212 216 public String getName() { 217 return name; 218 } 219 220 224 public int getSize() { 225 return size; 226 } 227 228 233 public int getSubSize() { 234 return subSize; 235 } 236 237 242 public Class getType() { 243 return type; 244 } 245 246 251 public String getTypeName() { 252 return typeName; 253 } 254 255 261 public boolean isIdentity() { 262 return isIdentity; 263 } 264 265 270 public boolean isPrimaryKey() { 271 return isPrimaryKey; 272 } 273 274 278 public boolean isUnique() { 279 return isUnique; 280 } 281 282 286 public void setAdditional(String string) { 287 additional = string; 288 } 289 290 295 public void setConstraint(String string) { 296 constraint = string; 297 } 298 299 public void setForeignKeyFieldName(String foreignKeyFieldName) { 300 this.foreignKeyFieldName = foreignKeyFieldName; 301 } 302 303 309 public void setIsIdentity(boolean value) { 310 isIdentity = value; 311 if (value) { 312 setShouldAllowNull(false); 313 } 314 } 315 316 321 public void setIsPrimaryKey(boolean value) { 322 isPrimaryKey = value; 323 if (value) { 324 setShouldAllowNull(false); 325 } 326 } 327 328 332 public void setName(String name) { 333 this.name = name; 334 } 335 336 340 public void setShouldAllowNull(boolean value) { 341 shouldAllowNull = value; 342 } 343 344 348 public void setSize(int size) { 349 this.size = size; 350 } 351 352 357 public void setSubSize(int subSize) { 358 this.subSize = subSize; 359 } 360 361 366 public void setType(Class type) { 367 this.type = type; 368 } 369 370 375 public void setTypeName(String typeName) { 376 this.typeName = typeName; 377 } 378 379 383 public void setUnique(boolean value) { 384 isUnique = value; 385 } 386 387 391 public boolean shouldAllowNull() { 392 return shouldAllowNull; 393 } 394 395 public String toString() { 396 return Helper.getShortClassName(getClass()) + "(" + getName() + "(" + getType() + "))"; 397 } 398 } 399 | Popular Tags |