FileDocCategorySizeDatePackage
TableCreator.javaAPI DocGlassfish v2 API15137Tue May 22 16:54:54 BST 2007oracle.toplink.essentials.tools.schemaframework

TableCreator

public class TableCreator extends Object
Purpose: This class is reponsible for creating the tables defined in the project. A specific subclass of this class is created for each project. The specific table information is defined in the subclass.
since
TopLink 2.0
author
Peter Krogh

Fields Summary
protected Vector
tableDefinitions
protected String
name
protected boolean
ignoreDatabaseException
Constructors Summary
public TableCreator()

        this(new Vector());
    
public TableCreator(Vector tableDefinitions)

        super();
        this.tableDefinitions = tableDefinitions;
    
Methods Summary
public voidaddTableDefinition(oracle.toplink.essentials.tools.schemaframework.TableDefinition tableDefinition)
Add the table.

        tableDefinitions.addElement(tableDefinition);
    
public voidaddTableDefinitions(java.util.Collection tableDefs)
Add a set of tables.

        tableDefinitions.addAll(tableDefs);
    
public voidcreateConstraints(oracle.toplink.essentials.sessions.DatabaseSession session)
Create constraints.

        //CR2612669
        createConstraints(session, new SchemaManager(session));
    
public voidcreateConstraints(oracle.toplink.essentials.sessions.DatabaseSession session, oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager)
Create constraints.

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
        }

        // Unique constraints should be generated before foreign key constraints,
        // because foreign key constraints can reference unique constraints
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.createUniqueConstraints((TableDefinition)enumtr.nextElement());
            } catch (DatabaseException ex) {
                if (!shouldIgnoreDatabaseException()) {
                    throw ex;
                }
            }
        }
        
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.createForeignConstraints((TableDefinition)enumtr.nextElement());
            } catch (DatabaseException ex) {
                if (!shouldIgnoreDatabaseException()) {
                    throw ex;
                }
            }
        }
    
public voidcreateTables(oracle.toplink.essentials.sessions.DatabaseSession session)
This creates the tables on the database. If the table already exists this will fail.

        //CR2612669
        createTables(session, new SchemaManager(session));
    
public voidcreateTables(oracle.toplink.essentials.sessions.DatabaseSession session, oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager)
This creates the tables on the database. If the table already exists this will fail.

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
        }

        String sequenceTableName = getSequenceTableName(session);
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            // Must not create sequence table as done in createSequences.
            TableDefinition table = (TableDefinition)enumtr.nextElement();
            if (!table.getName().equals(sequenceTableName)) {
                try {
                    schemaManager.createObject(table);
                    session.getSessionLog().log(SessionLog.FINEST, "default_tables_created", table.getFullName());
                } catch (DatabaseException ex) {
                    session.getSessionLog().log(SessionLog.FINEST, "default_tables_already_existed", table.getFullName());
                    if (!shouldIgnoreDatabaseException()) {
                        throw ex;
                    }
                }
            }
        }

        // Unique constraints should be generated before foreign key constraints,
        // because foreign key constraints can reference unique constraints
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.createUniqueConstraints((TableDefinition)enumtr.nextElement());
            } catch (DatabaseException ex) {
                if (!shouldIgnoreDatabaseException()) {
                    throw ex;
                }
            }
        }
        
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.createForeignConstraints((TableDefinition)enumtr.nextElement());
            } catch (DatabaseException ex) {
                if (!shouldIgnoreDatabaseException()) {
                    throw ex;
                }
            }
        }

        schemaManager.createSequences();
    
public voiddropConstraints(oracle.toplink.essentials.sessions.DatabaseSession session, oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager)
Drop the table constraints from the database.

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
        }

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.dropConstraints((TableDefinition)enumtr.nextElement());
            } catch (oracle.toplink.essentials.exceptions.DatabaseException dbE) {
                //ignore
            }
        }
    
public voiddropConstraints(oracle.toplink.essentials.sessions.DatabaseSession session)
Drop the table constraints from the database.

        //CR2612669
        dropConstraints(session, new SchemaManager(session));
    
public voiddropTables(oracle.toplink.essentials.sessions.DatabaseSession session)
Drop the tables from the database.

        //CR2612669
        dropTables(session, new SchemaManager(session));
    
public voiddropTables(oracle.toplink.essentials.sessions.DatabaseSession session, oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager)
Drop the tables from the database.

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
        }

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.dropConstraints((TableDefinition)enumtr.nextElement());
            } catch (oracle.toplink.essentials.exceptions.DatabaseException dbE) {
                //ignore
            }
        }

        String sequenceTableName = getSequenceTableName(session);
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            // Must not create sequence table as done in createSequences.
            TableDefinition table = (TableDefinition)enumtr.nextElement();
            if (!table.getName().equals(sequenceTableName)) {
                try {
                    schemaManager.dropObject(table);
                } catch (DatabaseException ex) {
                    if (!shouldIgnoreDatabaseException()) {
                        throw ex;
                    }
                }
            }
        }
    
public java.lang.StringgetName()
Return the name.

        return name;
    
protected java.lang.StringgetSequenceTableName(oracle.toplink.essentials.sessions.Session session)

        String sequenceTableName = null;
        if (session.getProject().usesSequencing()) {
            oracle.toplink.essentials.sequencing.Sequence sequence = session.getLogin().getDefaultSequence();
            if (sequence instanceof oracle.toplink.essentials.sequencing.TableSequence) {
                sequenceTableName = ((oracle.toplink.essentials.sequencing.TableSequence)sequence).getTableName();
            }
        }
        return sequenceTableName;
    
public java.util.VectorgetTableDefinitions()
Return the tables.

        return tableDefinitions;
    
public voidreplaceTables(oracle.toplink.essentials.sessions.DatabaseSession session)
Recreate the tables on the database. This will drop the tables if they exist and recreate them.

        replaceTables(session, new SchemaManager(session));
    
public voidreplaceTables(oracle.toplink.essentials.sessions.DatabaseSession session, oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager)
Recreate the tables on the database. This will drop the tables if they exist and recreate them.

        replaceTablesAndConstraints(schemaManager, session);

        schemaManager.createSequences();

    
public voidreplaceTables(oracle.toplink.essentials.sessions.DatabaseSession session, oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager, boolean keepSequenceTable)
Recreate the tables on the database. This will drop the tables if they exist and recreate them.

        replaceTablesAndConstraints(schemaManager, session);

        schemaManager.createOrReplaceSequences(keepSequenceTable, false);
    
private voidreplaceTablesAndConstraints(oracle.toplink.essentials.tools.schemaframework.SchemaManager schemaManager, oracle.toplink.essentials.sessions.DatabaseSession session)

        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
        }
        
        // CR 3870467, do not log stack
        boolean shouldLogExceptionStackTrace = session.getSessionLog().shouldLogExceptionStackTrace();
        if (shouldLogExceptionStackTrace) {
            session.getSessionLog().setShouldLogExceptionStackTrace(false);
        }
        try {
            for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
                try {
                    schemaManager.dropConstraints((TableDefinition)enumtr.nextElement());
                } catch (oracle.toplink.essentials.exceptions.DatabaseException dbE) {
                    //ignore
                }
            }
        } finally {
            if (shouldLogExceptionStackTrace) {
                session.getSessionLog().setShouldLogExceptionStackTrace(true);
            }
        }

        String sequenceTableName = getSequenceTableName(session);
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            // Must not create sequence table as done in createSequences.
            TableDefinition table = (TableDefinition)enumtr.nextElement();
            if (!table.getName().equals(sequenceTableName)) {
                try {
                    schemaManager.replaceObject(table);
                } catch (DatabaseException ex) {
                    if (!shouldIgnoreDatabaseException()) {
                        throw ex;
                    }
                }
            }
        }

        // Unique constraints should be generated before foreign key constraints,
        // because foreign key constraints can reference unique constraints
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.createUniqueConstraints((TableDefinition)enumtr.nextElement());
            } catch (DatabaseException ex) {
                if (!shouldIgnoreDatabaseException()) {
                    throw ex;
                }
            }
        }
        
        for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
            try {
                schemaManager.createForeignConstraints((TableDefinition)enumtr.nextElement());
            } catch (DatabaseException ex) {
                if (!shouldIgnoreDatabaseException()) {
                    throw ex;
                }
            }
        }
    
voidsetIgnoreDatabaseException(boolean ignoreDatabaseException)
Set flag whether DatabaseException should be ignored.

        this.ignoreDatabaseException = ignoreDatabaseException;
    
public voidsetName(java.lang.String name)
Set the name.

        this.name = name;
    
public voidsetTableDefinitions(java.util.Vector tableDefinitions)
Set the tables.

        this.tableDefinitions = tableDefinitions;
    
booleanshouldIgnoreDatabaseException()
Return true if DatabaseException is to be ignored.

        return ignoreDatabaseException;