oracle.toplink.essentials.tools.schemaframework.TableCreator Maven / Gradle / Ivy
The newest version!
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2005, Oracle. All rights reserved.
package oracle.toplink.essentials.tools.schemaframework;
import java.util.*;
/**
* 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
*/
public class TableCreator {
protected Vector tableDefinitions;
protected String name;
public TableCreator() {
this(new Vector());
}
public TableCreator(Vector tableDefinitions) {
super();
this.tableDefinitions = tableDefinitions;
}
/**
* Add the table.
*/
public void addTableDefinition(TableDefinition tableDefinition) {
tableDefinitions.addElement(tableDefinition);
}
/**
* Add a set of tables.
*/
public void addTableDefinitions(Collection tableDefs) {
tableDefinitions.addAll(tableDefs);
}
/**
* Create constraints.
*/
public void createConstraints(oracle.toplink.essentials.sessions.DatabaseSession session) {
//CR2612669
createConstraints(session, new SchemaManager(session));
}
/**
* Create constraints.
*/
public void createConstraints(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
schemaManager.buildFieldTypes((TableDefinition)enumtr.nextElement());
}
for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
schemaManager.createConstraints((TableDefinition)enumtr.nextElement());
}
}
/**
* This creates the tables on the database.
* If the table already exists this will fail.
*/
public void createTables(oracle.toplink.essentials.sessions.DatabaseSession session) {
//CR2612669
createTables(session, new SchemaManager(session));
}
/**
* This creates the tables on the database.
* If the table already exists this will fail.
*/
public void createTables(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
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)) {
schemaManager.createObject(table);
}
}
for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
schemaManager.createConstraints((TableDefinition)enumtr.nextElement());
}
schemaManager.createSequences();
}
/**
* Drop the table constraints from the database.
*/
public void dropConstraints(oracle.toplink.essentials.sessions.DatabaseSession session) {
//CR2612669
dropConstraints(session, new SchemaManager(session));
}
/**
* Drop the table constraints from the database.
*/
public void dropConstraints(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
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
}
}
}
/**
* Drop the tables from the database.
*/
public void dropTables(oracle.toplink.essentials.sessions.DatabaseSession session) {
//CR2612669
dropTables(session, new SchemaManager(session));
}
/**
* Drop the tables from the database.
*/
public void dropTables(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
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)) {
schemaManager.dropObject(table);
}
}
}
/**
* Return the name.
*/
public String getName() {
return name;
}
/**
* Return the tables.
*/
public Vector getTableDefinitions() {
return tableDefinitions;
}
/**
* Recreate the tables on the database.
* This will drop the tables if they exist and recreate them.
*/
public void replaceTables(oracle.toplink.essentials.sessions.DatabaseSession session) {
replaceTables(session, new SchemaManager(session));
}
/**
* Recreate the tables on the database.
* This will drop the tables if they exist and recreate them.
*/
public void replaceTables(oracle.toplink.essentials.sessions.DatabaseSession session, SchemaManager schemaManager) {
replaceTablesAndConstraints(schemaManager, session);
schemaManager.createSequences();
}
/**
* Recreate the tables on the database.
* This will drop the tables if they exist and recreate them.
*/
public void replaceTables(oracle.toplink.essentials.sessions.DatabaseSession session,
SchemaManager schemaManager, boolean keepSequenceTable) {
replaceTablesAndConstraints(schemaManager, session);
schemaManager.createOrReplaceSequences(keepSequenceTable, false);
}
private void replaceTablesAndConstraints(final SchemaManager schemaManager,
final 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)) {
schemaManager.replaceObject(table);
}
}
for (Enumeration enumtr = getTableDefinitions().elements(); enumtr.hasMoreElements();) {
schemaManager.createConstraints((TableDefinition)enumtr.nextElement());
}
}
/**
* Set the name.
*/
public void setName(String name) {
this.name = name;
}
/**
* Set the tables.
*/
public void setTableDefinitions(Vector tableDefinitions) {
this.tableDefinitions = tableDefinitions;
}
protected String getSequenceTableName(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;
}
}