KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > tools > schemaframework > TableDefinition


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
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 /**
32  * <p>
33  * <b>Purpose</b>: Allow a generic way of creating tables on the different platforms.
34  * <p>
35  */

36 public class TableDefinition extends DatabaseObjectDefinition {
37     protected Vector fields;
38     protected Vector foreignKeys;
39     protected Vector uniqueKeys;
40     protected String JavaDoc creationPrefix;
41     protected String JavaDoc 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     /**
53      * PUBLIC:
54      * Add the field to the table, default sizes are used.
55      * @param type is the Java class type coresponding to the database type.
56      */

57     public void addField(String JavaDoc fieldName, Class JavaDoc type) {
58         this.addField(new FieldDefinition(fieldName, type));
59     }
60
61     /**
62      * PUBLIC:
63      * Add the field to the table.
64      * @param type is the Java class type coresponding to the database type.
65      */

66     public void addField(String JavaDoc fieldName, Class JavaDoc type, int fieldSize) {
67         this.addField(new FieldDefinition(fieldName, type, fieldSize));
68     }
69
70     /**
71      * PUBLIC:
72      * Add the field to the table.
73      * @param type is the Java class type coresponding to the database type.
74      */

75     public void addField(String JavaDoc fieldName, Class JavaDoc type, int fieldSize, int fieldSubSize) {
76         this.addField(new FieldDefinition(fieldName, type, fieldSize, fieldSubSize));
77     }
78
79     /**
80      * PUBLIC:
81      * Add the field to the type to a nested type.
82      * @param typeName is the name of the nested type.
83      */

84     public void addField(String JavaDoc fieldName, String JavaDoc typeName) {
85         this.addField(new FieldDefinition(fieldName, typeName));
86     }
87
88     /**
89      * PUBLIC:
90      * Add the field to the table.
91      */

92     public void addField(FieldDefinition field) {
93         this.getFields().addElement(field);
94     }
95
96     /**
97      * PUBLIC:
98      * Add a foreign key constraint to the table.
99      */

100     public void addForeignKeyConstraint(String JavaDoc name, String JavaDoc sourceField, String JavaDoc targetField, String JavaDoc targetTable) {
101         ForeignKeyConstraint foreignKey = new ForeignKeyConstraint(name, sourceField, targetField, targetTable);
102         addForeignKeyConstraint(foreignKey);
103     }
104
105     /**
106      * PUBLIC:
107      * Add a foreign key constraint to the table.
108      */

109     public void addUniqueKeyConstraint(String JavaDoc name, String JavaDoc sourceField) {
110         UniqueKeyConstraint uniqueKey = new UniqueKeyConstraint(name, sourceField);
111         addUniqueKeyConstraint(uniqueKey);
112     }
113     
114     /**
115      * PUBLIC:
116      * Add a foreign key constraint to the table.
117      */

118     public void addForeignKeyConstraint(ForeignKeyConstraint foreignKey) {
119         getForeignKeys().addElement(foreignKey);
120     }
121     
122     /**
123      * PUBLIC:
124      * Add a unique key constraint to the table.
125      */

126     public void addUniqueKeyConstraint(UniqueKeyConstraint uniqueKey) {
127         getUniqueKeys().addElement(uniqueKey);
128     }
129     
130     /**
131      * PUBLIC:
132      * Add the field to the table, default sizes are used.
133      * Identity fields are used on Sybase for native sequencing,
134      * The field must be of numberish type and cannot have a subsize.
135      * @param type is the Java class type coresponding to the database type.
136      */

137     public void addIdentityField(String JavaDoc fieldName, Class JavaDoc type) {
138         FieldDefinition fieldDef = new FieldDefinition(fieldName, type);
139         fieldDef.setIsIdentity(true);
140         fieldDef.setIsPrimaryKey(true);
141         addField(fieldDef);
142     }
143
144     /**
145      * PUBLIC:
146      * Add the field to the table, default sizes are used.
147      * Identity fields are used on Sybase for native sequencing,
148      * The field must be of numberish type and cannot have a subsize.
149      * @param type is the Java class type coresponding to the database type.
150      */

151     public void addIdentityField(String JavaDoc fieldName, Class JavaDoc 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     /**
159      * PUBLIC:
160      * Add the field to the table, default sizes are used.
161      * This field is set as part of the primary key.
162      * @param type is the Java class type coresponding to the database type.
163      */

164     public void addPrimaryKeyField(String JavaDoc fieldName, Class JavaDoc type) {
165         FieldDefinition fieldDef = new FieldDefinition(fieldName, type);
166         fieldDef.setIsPrimaryKey(true);
167         addField(fieldDef);
168     }
169
170     /**
171      * PUBLIC:
172      * Add the field to the table, default sizes are used.
173      * This field is set as part of the primary key.
174      * @param type is the Java class type coresponding to the database type.
175      */

176     public void addPrimaryKeyField(String JavaDoc fieldName, Class JavaDoc type, int fieldSize) {
177         FieldDefinition fieldDef = new FieldDefinition(fieldName, type, fieldSize);
178         fieldDef.setIsPrimaryKey(true);
179         addField(fieldDef);
180     }
181
182     /**
183      * INTERNAL:
184      * Return the alter table statement to add the constraints.
185      * This is done seperatly from the create because of dependecies.
186      */

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     /**
205      * INTERNAL:
206      * Return the alter table statement to drop the constraints.
207      * This is done seperatly to allow constraints to be dropped before the tables.
208      */

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     /**
220      * INTERNAL:
221      * Return the alter table statement to add the constraints.
222      * This is done seperatly from the create because of dependecies.
223      */

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     /**
242      * INTERNAL:
243      * Return the alter table statement to drop the constraints.
244      * This is done seperatly to allow constraints to be dropped before the tables.
245      */

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     /**
257      * INTERNAL:
258      * Return the beginning of the sql create statement - the part before the name.
259      * Unless temp table is created should be "CREATE TABLE "
260      */

261     public String JavaDoc getCreationPrefix() {
262         return creationPrefix;
263     }
264     
265     /**
266      * INTERNAL:
267      * Set the beginning of the sql create statement - the part before the name.
268      * Use to create temp. table.
269      */

270     public void setCreationPrefix(String JavaDoc creationPrefix) {
271         this.creationPrefix = creationPrefix;
272     }
273
274     /**
275      * INTERNAL:
276      * Return the end of the sql create statement - the part after the field list.
277      * Unless temp table is created should be empty.
278      */

279     public String JavaDoc getCreationSuffix() {
280         return creationSuffix;
281     }
282
283     /**
284      * INTERNAL:
285      * Set the end of the sql create statement - the part after the field list.
286      * Use to create temp table.
287      */

288     public void setCreationSuffix(String JavaDoc creationSuffix) {
289         this.creationSuffix = creationSuffix;
290     }
291     
292     /**
293      * INTERNAL:
294      * Return the create table statement.
295      */

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 JavaDoc)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     /**
332      * INTERNAL:
333      * Return the drop table statement.
334      */

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     /**
345      * INTERNAL:
346      * Build the field types based on the given name read in from the file.
347      * Build the foriegn key constraintsas well.
348      */

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 JavaDoc[] uniqueKeysArray = uniqueKeysClone.toArray();
358         setUniqueKeys(new Vector());
359         UniqueKeyConstraint uniqueKeyConstraint;
360         int serialNumber = 0;
361         for (Object JavaDoc uniqueKeys : uniqueKeysArray) {
362             uniqueKeyConstraint = buildUniqueKeyConstraint(this, serialNumber++, session.getPlatform());
363             String JavaDoc[] columnNames = (String JavaDoc[])uniqueKeys;
364             for (String JavaDoc 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 JavaDoc)fieldTypes.get(field.getTypeName()));
386                 if (field.getType() != null) {
387                     field.setTypeName(null);
388                 }
389             }
390         }
391
392         /* bug #2997188 constraints not getting generated when creating/replacing tables via TableCreator
393            had to add the next few lines instead of removing the above code for backwards compatibility */

394         if (getForeignKeys().isEmpty()) {
395             //if foreignKeys is empty then we know we are not in 2.5
396
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 JavaDoc tempName = buildForeignKeyConstraintName(table.getName(), tempSourceField.getName(), platform.getMaxForeignKeyNameSize());
414
415         fkConstraint.setName(tempName);
416         return fkConstraint;
417     }
418
419     /**
420      * Return foreign key constraint name built from the table and field name with the specified maximum length. To
421      * make the name short enough we
422      * 1. Drop the "FK_" prefix.
423      * 2. Drop the underscore characters if any.
424      * 3. Drop the vowels from the table and field name.
425      * 4. Truncate the table name to zero length if necessary.
426      */

427     protected String JavaDoc buildForeignKeyConstraintName(String JavaDoc tableName, String JavaDoc fieldName, int maximumNameLength) {
428         String JavaDoc foreignKeyName = "FK_" + tableName + "_" + fieldName;
429         if (foreignKeyName.length() > maximumNameLength) {
430             // First Remove the "FK_" prefix.
431
foreignKeyName = tableName + "_" + fieldName;
432             if (foreignKeyName.length() > maximumNameLength) {
433                 // Still too long: remove the underscore characters
434
foreignKeyName = Helper.removeAllButAlphaNumericToFit(tableName + fieldName, maximumNameLength);
435                 if (foreignKeyName.length() > maximumNameLength) {
436                     // Still too long: remove vowels from the table name and field name.
437
String JavaDoc onlyAlphaNumericTableName = Helper.removeAllButAlphaNumericToFit(tableName, 0);
438                     String JavaDoc onlyAlphaNumericFieldName = Helper.removeAllButAlphaNumericToFit(fieldName, 0);
439                     foreignKeyName = Helper.shortenStringsByRemovingVowelsToFit(onlyAlphaNumericTableName, onlyAlphaNumericFieldName, maximumNameLength);
440                     if (foreignKeyName.length() > maximumNameLength) {
441                         // Still too long: remove vowels from the table name and field name and truncate the table name.
442
String JavaDoc shortenedFieldName = Helper.removeVowels(onlyAlphaNumericFieldName);
443                         String JavaDoc 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 JavaDoc tempName = buildUniqueKeyConstraintName(table.getName(), serialNumber, platform.getMaxUniqueKeyNameSize());
455         unqConstraint.setName(tempName);
456         return unqConstraint;
457     }
458
459     /**
460      * Return foreign key constraint name built from the table and field name with the specified maximum length. To
461      * make the name short enough we
462      * 1. Drop the "FK_" prefix.
463      * 2. Drop the underscore characters if any.
464      * 3. Drop the vowels from the table and field name.
465      * 4. Truncate the table name to zero length if necessary.
466      */

467     protected String JavaDoc buildUniqueKeyConstraintName(String JavaDoc tableName, int serialNumber, int maximumNameLength) {
468         String JavaDoc uniqueKeyName = "UNQ_" + tableName + "_" + serialNumber;
469         if (uniqueKeyName.length() > maximumNameLength) {
470             // First Remove the "UNQ_" prefix.
471
uniqueKeyName = tableName;
472             if (uniqueKeyName.length() > maximumNameLength) {
473                 // Still too long: remove the underscore characters
474
uniqueKeyName = Helper.removeAllButAlphaNumericToFit(tableName + serialNumber, maximumNameLength);
475                 if (uniqueKeyName.length() > maximumNameLength) {
476                     // Still too long: remove vowels from the table name and field name.
477
String JavaDoc onlyAlphaNumericTableName = Helper.removeAllButAlphaNumericToFit(tableName, 0);
478                     uniqueKeyName = Helper.shortenStringsByRemovingVowelsToFit(onlyAlphaNumericTableName, "", maximumNameLength);
479                     if (uniqueKeyName.length() > maximumNameLength) {
480                         // Still too long: remove vowels from the table name and field name and truncate the table name.
481
String JavaDoc shortenedTableName = Helper.removeVowels(onlyAlphaNumericTableName);
482                         uniqueKeyName = Helper.truncate(shortenedTableName, maximumNameLength - shortenedTableName.length());
483                     }
484                 }
485             }
486         }
487         return uniqueKeyName;
488     }
489
490     /**
491      * PUBLIC:
492      * Performs a deep copy of this table definition.
493      */

494     public Object JavaDoc 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     /**
513      * INTERNAL:
514      * Execute the SQL alter table constraint creation string.
515      */

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     /**
559      * INTERNAL:
560      * Execute the SQL alter table constraint creation string.
561      */

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     /**
592      * INTERNAL:
593      * Return the delete SQL string.
594      */

595     public String JavaDoc deletionStringFor(DatabaseAccessor accessor) {
596         return "DROP TABLE " + this.getName();
597     }
598
599     /**
600      * INTERNAL:
601      * Execute the SQL alter table constraint creation string.
602      */

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     /**
638      * INTERNAL:
639      * Execute the SQL alter table constraint creation string. Exceptions are caught and masked so that all
640      * the foreign keys are dropped (even if they don't exist).
641      */

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) {/* ignore */
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) {/* ignore */
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