001: package liquibase.change;
002:
003: import liquibase.database.DB2Database;
004: import liquibase.database.Database;
005: import liquibase.database.sql.RenameTableStatement;
006: import liquibase.database.sql.ReorganizeTableStatement;
007: import liquibase.database.sql.SqlStatement;
008: import liquibase.database.structure.DatabaseObject;
009: import liquibase.database.structure.Table;
010: import liquibase.exception.UnsupportedChangeException;
011: import org.w3c.dom.Document;
012: import org.w3c.dom.Element;
013:
014: import java.util.*;
015:
016: /**
017: * Renames an existing table.
018: */
019: public class RenameTableChange extends AbstractChange {
020:
021: private String schemaName;
022: private String oldTableName;
023:
024: private String newTableName;
025:
026: public RenameTableChange() {
027: super ("renameTable", "Rename Table");
028: }
029:
030: public String getSchemaName() {
031: return schemaName;
032: }
033:
034: public void setSchemaName(String schemaName) {
035: this .schemaName = schemaName;
036: }
037:
038: public String getOldTableName() {
039: return oldTableName;
040: }
041:
042: public void setOldTableName(String oldTableName) {
043: this .oldTableName = oldTableName;
044: }
045:
046: public String getNewTableName() {
047: return newTableName;
048: }
049:
050: public void setNewTableName(String newTableName) {
051: this .newTableName = newTableName;
052: }
053:
054: public SqlStatement[] generateStatements(Database database)
055: throws UnsupportedChangeException {
056: List<SqlStatement> statements = new ArrayList<SqlStatement>();
057: String schemaName = getSchemaName() == null ? database
058: .getDefaultSchemaName() : getSchemaName();
059: statements.add(new RenameTableStatement(schemaName,
060: getOldTableName(), getNewTableName()));
061: if (database instanceof DB2Database) {
062: statements.add(new ReorganizeTableStatement(schemaName,
063: getNewTableName()));
064: }
065:
066: return statements.toArray(new SqlStatement[statements.size()]);
067: }
068:
069: protected Change[] createInverses() {
070: RenameTableChange inverse = new RenameTableChange();
071: inverse.setSchemaName(getSchemaName());
072: inverse.setOldTableName(getNewTableName());
073: inverse.setNewTableName(getOldTableName());
074:
075: return new Change[] { inverse };
076: }
077:
078: public String getConfirmationMessage() {
079: return "Table " + oldTableName + " renamed to " + newTableName;
080: }
081:
082: public Element createNode(Document currentChangeLogFileDOM) {
083: Element element = currentChangeLogFileDOM
084: .createElement("renameTable");
085: if (getSchemaName() != null) {
086: element.setAttribute("schemaName", getSchemaName());
087: }
088:
089: element.setAttribute("oldTableName", getOldTableName());
090:
091: element.setAttribute("newTableName", getNewTableName());
092:
093: return element;
094: }
095:
096: public Set<DatabaseObject> getAffectedDatabaseObjects() {
097: Table oldTable = new Table(getOldTableName());
098:
099: Table newTable = new Table(getNewTableName());
100:
101: return new HashSet<DatabaseObject>(Arrays.asList(oldTable,
102: newTable));
103: }
104: }
|