1 21 package oracle.toplink.essentials.tools.schemaframework; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.internal.helper.*; 27 import oracle.toplink.essentials.internal.databaseaccess.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.sessions.AbstractSession; 30 31 36 public class TableDefinition extends DatabaseObjectDefinition { 37 protected Vector fields; 38 protected Vector foreignKeys; 39 protected Vector uniqueKeys; 40 protected String creationPrefix; 41 protected String creationSuffix; 42 private boolean createSQLFiles; 43 44 public TableDefinition() { 45 this.fields = new Vector(); 46 this.foreignKeys = new Vector(); 47 this.uniqueKeys = new Vector(); 48 this.creationPrefix = "CREATE TABLE "; 49 this.creationSuffix = ""; 50 } 51 52 57 public void addField(String fieldName, Class type) { 58 this.addField(new FieldDefinition(fieldName, type)); 59 } 60 61 66 public void addField(String fieldName, Class type, int fieldSize) { 67 this.addField(new FieldDefinition(fieldName, type, fieldSize)); 68 } 69 70 75 public void addField(String fieldName, Class type, int fieldSize, int fieldSubSize) { 76 this.addField(new FieldDefinition(fieldName, type, fieldSize, fieldSubSize)); 77 } 78 79 84 public void addField(String fieldName, String typeName) { 85 this.addField(new FieldDefinition(fieldName, typeName)); 86 } 87 88 92 public void addField(FieldDefinition field) { 93 this.getFields().addElement(field); 94 } 95 96 100 public void addForeignKeyConstraint(String name, String sourceField, String targetField, String targetTable) { 101 ForeignKeyConstraint foreignKey = new ForeignKeyConstraint(name, sourceField, targetField, targetTable); 102 addForeignKeyConstraint(foreignKey); 103 } 104 105 109 public void addUniqueKeyConstraint(String name, String sourceField) { 110 UniqueKeyConstraint uniqueKey = new UniqueKeyConstraint(name, sourceField); 111 addUniqueKeyConstraint(uniqueKey); 112 } 113 114 118 public void addForeignKeyConstraint(ForeignKeyConstraint foreignKey) { 119 getForeignKeys().addElement(foreignKey); 120 } 121 122 126 public void addUniqueKeyConstraint(UniqueKeyConstraint uniqueKey) { 127 getUniqueKeys().addElement(uniqueKey); 128 } 129 130 137 public void addIdentityField(String fieldName, Class type) { 138 FieldDefinition fieldDef = new FieldDefinition(fieldName, type); 139 fieldDef.setIsIdentity(true); 140 fieldDef.setIsPrimaryKey(true); 141 addField(fieldDef); 142 } 143 144 151 public void addIdentityField(String fieldName, Class type, int fieldSize) { 152 FieldDefinition fieldDef = new FieldDefinition(fieldName, type, fieldSize); 153 fieldDef.setIsIdentity(true); 154 fieldDef.setIsPrimaryKey(true); 155 addField(fieldDef); 156 } 157 158 164 public void addPrimaryKeyField(String fieldName, Class type) { 165 FieldDefinition fieldDef = new FieldDefinition(fieldName, type); 166 fieldDef.setIsPrimaryKey(true); 167 addField(fieldDef); 168 } 169 170 176 public void addPrimaryKeyField(String fieldName, Class type, int fieldSize) { 177 FieldDefinition fieldDef = new FieldDefinition(fieldName, type, fieldSize); 178 fieldDef.setIsPrimaryKey(true); 179 addField(fieldDef); 180 } 181 182 187 public Writer buildConstraintCreationWriter(AbstractSession session, ForeignKeyConstraint foreignKey, Writer writer) throws ValidationException { 188 try { 189 writer.write("ALTER TABLE " + getFullName()); 190 writer.write(" ADD CONSTRAINT "); 191 if (!session.getPlatform().shouldPrintConstraintNameAfter()) { 192 writer.write(foreignKey.getName() + " "); 193 } 194 foreignKey.appendDBString(writer, session); 195 if (session.getPlatform().shouldPrintConstraintNameAfter()) { 196 writer.write(" CONSTRAINT " + foreignKey.getName()); 197 } 198 } catch (IOException ioException) { 199 throw ValidationException.fileError(ioException); 200 } 201 return writer; 202 } 203 204 209 public Writer buildConstraintDeletionWriter(AbstractSession session, ForeignKeyConstraint foreignKey, Writer writer) throws ValidationException { 210 try { 211 writer.write("ALTER TABLE " + getFullName()); 212 writer.write(session.getPlatform().getConstraintDeletionString() + foreignKey.getName()); 213 } catch (IOException ioException) { 214 throw ValidationException.fileError(ioException); 215 } 216 return writer; 217 } 218 219 224 public Writer buildUniqueConstraintCreationWriter(AbstractSession session, UniqueKeyConstraint uniqueKey, Writer writer) throws ValidationException { 225 try { 226 writer.write("ALTER TABLE " + getFullName()); 227 writer.write(" ADD CONSTRAINT "); 228 if (!session.getPlatform().shouldPrintConstraintNameAfter()) { 229 writer.write(uniqueKey.getName() + " "); 230 } 231 uniqueKey.appendDBString(writer, session); 232 if (session.getPlatform().shouldPrintConstraintNameAfter()) { 233 writer.write(" CONSTRAINT " + uniqueKey.getName()); 234 } 235 } catch (IOException ioException) { 236 throw ValidationException.fileError(ioException); 237 } 238 return writer; 239 } 240 241 246 public Writer buildUniqueConstraintDeletionWriter(AbstractSession session, UniqueKeyConstraint uniqueKey, Writer writer) throws ValidationException { 247 try { 248 writer.write("ALTER TABLE " + getFullName()); 249 writer.write(session.getPlatform().getConstraintDeletionString() + uniqueKey.getName()); 250 } catch (IOException ioException) { 251 throw ValidationException.fileError(ioException); 252 } 253 return writer; 254 } 255 256 261 public String getCreationPrefix() { 262 return creationPrefix; 263 } 264 265 270 public void setCreationPrefix(String creationPrefix) { 271 this.creationPrefix = creationPrefix; 272 } 273 274 279 public String getCreationSuffix() { 280 return creationSuffix; 281 } 282 283 288 public void setCreationSuffix(String creationSuffix) { 289 this.creationSuffix = creationSuffix; 290 } 291 292 296 public Writer buildCreationWriter(AbstractSession session, Writer writer) throws ValidationException { 297 try { 298 writer.write(getCreationPrefix() + getFullName() + " ("); 299 for (Enumeration fieldsEnum = getFields().elements(); fieldsEnum.hasMoreElements();) { 300 FieldDefinition field = (FieldDefinition)fieldsEnum.nextElement(); 301 field.appendDBString(writer, session, this); 302 if (fieldsEnum.hasMoreElements()) { 303 writer.write(", "); 304 } 305 } 306 Vector keyFields = getPrimaryKeyFieldNames(); 307 if ((!keyFields.isEmpty()) && session.getPlatform().supportsPrimaryKeyConstraint()) { 308 writer.write(", "); 309 if (session.getPlatform().requiresNamedPrimaryKeyConstraints()) { 310 writer.write("CONSTRAINT " + getFullName() + "_PK "); 311 } 312 writer.write("PRIMARY KEY ("); 313 for (Enumeration keyEnum = keyFields.elements(); keyEnum.hasMoreElements();) { 314 writer.write((String )keyEnum.nextElement()); 315 if (keyEnum.hasMoreElements()) { 316 writer.write(", "); 317 } 318 } 319 writer.write(")"); 320 } 321 writer.write(")"); 322 if(getCreationSuffix().length() > 0) { 323 writer.write(getCreationSuffix()); 324 } 325 } catch (IOException ioException) { 326 throw ValidationException.fileError(ioException); 327 } 328 return writer; 329 } 330 331 335 public Writer buildDeletionWriter(AbstractSession session, Writer writer) throws ValidationException { 336 try { 337 writer.write("DROP TABLE " + getFullName()); 338 } catch (IOException ioException) { 339 throw ValidationException.fileError(ioException); 340 } 341 return writer; 342 } 343 344 349 protected void buildFieldTypes(AbstractSession session) { 350 buildForeignFieldTypes(session); 351 buildUniqueFieldTypes(session); 352 } 353 354 private void buildUniqueFieldTypes(final AbstractSession session) { 355 Vector uniqueKeysClone = (Vector)getUniqueKeys().clone(); 356 357 Object [] uniqueKeysArray = uniqueKeysClone.toArray(); 358 setUniqueKeys(new Vector()); 359 UniqueKeyConstraint uniqueKeyConstraint; 360 int serialNumber = 0; 361 for (Object uniqueKeys : uniqueKeysArray) { 362 uniqueKeyConstraint = buildUniqueKeyConstraint(this, serialNumber++, session.getPlatform()); 363 String [] columnNames = (String [])uniqueKeys; 364 for (String columnName : columnNames) { 365 uniqueKeyConstraint.addSourceField(columnName); 366 } 367 addUniqueKeyConstraint(uniqueKeyConstraint); 368 } 369 } 370 371 private void buildForeignFieldTypes(final AbstractSession session) { 372 Hashtable fieldTypes = session.getPlatform().getClassTypes(); 373 FieldDefinition field = null; 374 DatabaseField dbField = null; 375 376 Vector foreignKeysClone = (Vector)getForeignKeys().clone(); 377 setForeignKeys(new Vector()); 378 for (Enumeration enumtr = getFields().elements(); enumtr.hasMoreElements();) { 379 field = (FieldDefinition)enumtr.nextElement(); 380 if (field.getForeignKeyFieldName() != null) { 381 addForeignKeyConstraint(buildForeignKeyConstraint(field, this, session.getPlatform())); 382 383 } 384 if (field.getType() == null) { 385 field.setType((Class )fieldTypes.get(field.getTypeName())); 386 if (field.getType() != null) { 387 field.setTypeName(null); 388 } 389 } 390 } 391 392 394 if (getForeignKeys().isEmpty()) { 395 setForeignKeys(foreignKeysClone); 397 } 398 } 399 400 protected ForeignKeyConstraint buildForeignKeyConstraint(FieldDefinition field, TableDefinition table, DatabasePlatform platform) { 401 Vector sourceFields = new Vector(); 402 Vector targetFields = new Vector(); 403 ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(); 404 DatabaseField tempTargetField = new DatabaseField(field.getForeignKeyFieldName()); 405 DatabaseField tempSourceField = new DatabaseField(field.getName()); 406 407 sourceFields.addElement(tempSourceField.getName()); 408 targetFields.addElement(tempTargetField.getName()); 409 410 fkConstraint.setSourceFields(sourceFields); 411 fkConstraint.setTargetFields(targetFields); 412 fkConstraint.setTargetTable(tempTargetField.getTable().getQualifiedName()); 413 String tempName = buildForeignKeyConstraintName(table.getName(), tempSourceField.getName(), platform.getMaxForeignKeyNameSize()); 414 415 fkConstraint.setName(tempName); 416 return fkConstraint; 417 } 418 419 427 protected String buildForeignKeyConstraintName(String tableName, String fieldName, int maximumNameLength) { 428 String foreignKeyName = "FK_" + tableName + "_" + fieldName; 429 if (foreignKeyName.length() > maximumNameLength) { 430 foreignKeyName = tableName + "_" + fieldName; 432 if (foreignKeyName.length() > maximumNameLength) { 433 foreignKeyName = Helper.removeAllButAlphaNumericToFit(tableName + fieldName, maximumNameLength); 435 if (foreignKeyName.length() > maximumNameLength) { 436 String onlyAlphaNumericTableName = Helper.removeAllButAlphaNumericToFit(tableName, 0); 438 String onlyAlphaNumericFieldName = Helper.removeAllButAlphaNumericToFit(fieldName, 0); 439 foreignKeyName = Helper.shortenStringsByRemovingVowelsToFit(onlyAlphaNumericTableName, onlyAlphaNumericFieldName, maximumNameLength); 440 if (foreignKeyName.length() > maximumNameLength) { 441 String shortenedFieldName = Helper.removeVowels(onlyAlphaNumericFieldName); 443 String shortenedTableName = Helper.removeVowels(onlyAlphaNumericTableName); 444 foreignKeyName = Helper.truncate(shortenedTableName, maximumNameLength - shortenedFieldName.length()) + shortenedFieldName; 445 } 446 } 447 } 448 } 449 return foreignKeyName; 450 } 451 452 protected UniqueKeyConstraint buildUniqueKeyConstraint(TableDefinition table, int serialNumber, DatabasePlatform platform) { 453 UniqueKeyConstraint unqConstraint = new UniqueKeyConstraint(); 454 String tempName = buildUniqueKeyConstraintName(table.getName(), serialNumber, platform.getMaxUniqueKeyNameSize()); 455 unqConstraint.setName(tempName); 456 return unqConstraint; 457 } 458 459 467 protected String buildUniqueKeyConstraintName(String tableName, int serialNumber, int maximumNameLength) { 468 String uniqueKeyName = "UNQ_" + tableName + "_" + serialNumber; 469 if (uniqueKeyName.length() > maximumNameLength) { 470 uniqueKeyName = tableName; 472 if (uniqueKeyName.length() > maximumNameLength) { 473 uniqueKeyName = Helper.removeAllButAlphaNumericToFit(tableName + serialNumber, maximumNameLength); 475 if (uniqueKeyName.length() > maximumNameLength) { 476 String onlyAlphaNumericTableName = Helper.removeAllButAlphaNumericToFit(tableName, 0); 478 uniqueKeyName = Helper.shortenStringsByRemovingVowelsToFit(onlyAlphaNumericTableName, "", maximumNameLength); 479 if (uniqueKeyName.length() > maximumNameLength) { 480 String shortenedTableName = Helper.removeVowels(onlyAlphaNumericTableName); 482 uniqueKeyName = Helper.truncate(shortenedTableName, maximumNameLength - shortenedTableName.length()); 483 } 484 } 485 } 486 } 487 return uniqueKeyName; 488 } 489 490 494 public Object clone() { 495 TableDefinition clone = (TableDefinition)super.clone(); 496 if (fields != null) { 497 clone.setFields(new Vector(fields.size())); 498 for (Enumeration enumtr = getFields().elements(); enumtr.hasMoreElements();) { 499 FieldDefinition fieldDef = (FieldDefinition)enumtr.nextElement(); 500 clone.addField((FieldDefinition)fieldDef.clone()); 501 } 502 } 503 if (foreignKeys != null) { 504 clone.setForeignKeys((Vector)foreignKeys.clone()); 505 } 506 if (uniqueKeys != null) { 507 clone.setUniqueKeys((Vector)uniqueKeys.clone()); 508 } 509 return clone; 510 } 511 512 516 public void createConstraints(AbstractSession session, Writer schemaWriter) throws TopLinkException { 517 if (schemaWriter == null) { 518 this.createConstraintsOnDatabase(session); 519 } else { 520 createForeignConstraints(session, schemaWriter); 521 createUniqueContraints(session, schemaWriter); 522 523 } 524 } 525 526 private void createUniqueContraints(final AbstractSession session, final Writer schemaWriter) throws ValidationException { 527 for (Enumeration uniqueKeysEnum = getUniqueKeys().elements(); 528 uniqueKeysEnum.hasMoreElements();) { 529 UniqueKeyConstraint uniqueKey = (UniqueKeyConstraint)uniqueKeysEnum.nextElement(); 530 buildUniqueConstraintCreationWriter(session, uniqueKey, schemaWriter).toString(); 531 try { 532 if (createSQLFiles) { 533 schemaWriter.write(session.getPlatform().getStoredProcedureTerminationToken()); 534 } 535 schemaWriter.write("\n"); 536 } catch (IOException exception) { 537 throw ValidationException.fileError(exception); 538 } 539 } 540 } 541 542 private void createForeignConstraints(final AbstractSession session, final Writer schemaWriter) throws ValidationException { 543 for (Enumeration foreignKeysEnum = getForeignKeys().elements(); 544 foreignKeysEnum.hasMoreElements();) { 545 ForeignKeyConstraint foreignKey = (ForeignKeyConstraint)foreignKeysEnum.nextElement(); 546 buildConstraintCreationWriter(session, foreignKey, schemaWriter).toString(); 547 try { 548 if (createSQLFiles){ 549 schemaWriter.write(session.getPlatform().getStoredProcedureTerminationToken()); 550 } 551 schemaWriter.write("\n"); 552 } catch (IOException exception) { 553 throw ValidationException.fileError(exception); 554 } 555 } 556 } 557 558 562 public void createConstraintsOnDatabase(AbstractSession session) throws TopLinkException { 563 createForeignConstraintsOnDatabase(session); 564 createUniqueConstraintsOnDatabase(session); 565 } 566 567 private void createUniqueConstraintsOnDatabase(final AbstractSession session) throws ValidationException, DatabaseException { 568 if ((!session.getPlatform().supportsUniqueKeyConstraints()) || getUniqueKeys().isEmpty()) { 569 return; 570 } 571 572 for (Enumeration uniqueKeysEnum = getUniqueKeys().elements(); 573 uniqueKeysEnum.hasMoreElements();) { 574 UniqueKeyConstraint uniqueKey = (UniqueKeyConstraint)uniqueKeysEnum.nextElement(); 575 session.executeNonSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(buildUniqueConstraintCreationWriter(session, uniqueKey, new StringWriter()).toString())); 576 } 577 } 578 579 private void createForeignConstraintsOnDatabase(final AbstractSession session) throws ValidationException, DatabaseException { 580 if ((!session.getPlatform().supportsForeignKeyConstraints()) || getForeignKeys().isEmpty()) { 581 return; 582 } 583 584 for (Enumeration foreignKeysEnum = getForeignKeys().elements(); 585 foreignKeysEnum.hasMoreElements();) { 586 ForeignKeyConstraint foreignKey = (ForeignKeyConstraint)foreignKeysEnum.nextElement(); 587 session.executeNonSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(buildConstraintCreationWriter(session, foreignKey, new StringWriter()).toString())); 588 } 589 } 590 591 595 public String deletionStringFor(DatabaseAccessor accessor) { 596 return "DROP TABLE " + this.getName(); 597 } 598 599 603 public void dropConstraints(AbstractSession session, Writer schemaWriter) throws TopLinkException { 604 if (schemaWriter == null) { 605 this.dropConstraintsOnDatabase(session); 606 } else { 607 for (Enumeration foreignKeysEnum = getForeignKeys().elements(); 608 foreignKeysEnum.hasMoreElements();) { 609 ForeignKeyConstraint foreignKey = (ForeignKeyConstraint)foreignKeysEnum.nextElement(); 610 buildConstraintDeletionWriter(session, foreignKey, schemaWriter).toString(); 611 try { 612 if (createSQLFiles) { 613 schemaWriter.write(session.getPlatform().getStoredProcedureTerminationToken()); 614 } 615 schemaWriter.write("\n"); 616 } catch (IOException exception) { 617 throw ValidationException.fileError(exception); 618 } 619 } 620 621 for (Enumeration uniqueKeysEnum = getUniqueKeys().elements(); 622 uniqueKeysEnum.hasMoreElements();) { 623 UniqueKeyConstraint uniqueKey = (UniqueKeyConstraint)uniqueKeysEnum.nextElement(); 624 buildUniqueConstraintDeletionWriter(session, uniqueKey, schemaWriter).toString(); 625 try { 626 if (createSQLFiles) { 627 schemaWriter.write(session.getPlatform().getStoredProcedureTerminationToken()); 628 } 629 schemaWriter.write("\n"); 630 } catch (IOException exception) { 631 throw ValidationException.fileError(exception); 632 } 633 } 634 } 635 } 636 637 642 public void dropConstraintsOnDatabase(AbstractSession session) throws TopLinkException { 643 dropForeignConstraintsOnDatabase(session); 644 dropUniqueConstraintsOnDatabase(session); 645 } 646 647 private void dropUniqueConstraintsOnDatabase(final AbstractSession session) throws ValidationException { 648 if ((!session.getPlatform().supportsUniqueKeyConstraints()) || getUniqueKeys().isEmpty()) { 649 return; 650 } 651 652 for (Enumeration uniqueKeysEnum = getUniqueKeys().elements(); 653 uniqueKeysEnum.hasMoreElements();) { 654 UniqueKeyConstraint uniqueKey = (UniqueKeyConstraint)uniqueKeysEnum.nextElement(); 655 try { 656 session.executeNonSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(buildUniqueConstraintDeletionWriter(session, uniqueKey, new StringWriter()).toString())); 657 } catch (DatabaseException ex) { 658 } 659 } 660 } 661 662 private void dropForeignConstraintsOnDatabase(final AbstractSession session) throws ValidationException { 663 if ((!session.getPlatform().supportsForeignKeyConstraints()) || getForeignKeys().isEmpty()) { 664 return; 665 } 666 667 for (Enumeration foreignKeysEnum = getForeignKeys().elements(); 668 foreignKeysEnum.hasMoreElements();) { 669 ForeignKeyConstraint foreignKey = (ForeignKeyConstraint)foreignKeysEnum.nextElement(); 670 try { 671 session.executeNonSelectingCall(new oracle.toplink.essentials.queryframework.SQLCall(buildConstraintDeletionWriter(session, foreignKey, new StringWriter()).toString())); 672 } catch (DatabaseException ex) { 673 } 674 } 675 } 676 677 public Vector getFields() { 678 return fields; 679 } 680 681 public Vector getForeignKeys() { 682 return foreignKeys; 683 } 684 685 public Vector getUniqueKeys() { 686 return uniqueKeys; 687 } 688 689 public Vector getPrimaryKeyFieldNames() { 690 Vector keyNames = new Vector(); 691 692 for (Enumeration fieldEnum = getFields().elements(); fieldEnum.hasMoreElements();) { 693 FieldDefinition field = (FieldDefinition)fieldEnum.nextElement(); 694 if (field.isPrimaryKey()) { 695 keyNames.addElement(field.getName()); 696 } 697 } 698 return keyNames; 699 } 700 701 public void setFields(Vector fields) { 702 this.fields = fields; 703 } 704 705 public void setForeignKeys(Vector foreignKeys) { 706 this.foreignKeys = foreignKeys; 707 } 708 709 public void setUniqueKeys(Vector uniqueKeys) { 710 this.uniqueKeys = uniqueKeys; 711 } 712 713 public void setCreateSQLFiles(boolean genFlag) { 714 this.createSQLFiles = genFlag; 715 } 716 } 717 | Popular Tags |