001: package liquibase.change;
002:
003: import liquibase.database.*;
004: import liquibase.database.sql.RawSqlStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.sql.ReorganizeTableStatement;
007: import liquibase.database.structure.Column;
008: import liquibase.database.structure.DatabaseObject;
009: import liquibase.database.structure.Table;
010: import liquibase.exception.UnsupportedChangeException;
011: import liquibase.util.StringUtils;
012: import org.w3c.dom.Document;
013: import org.w3c.dom.Element;
014:
015: import java.util.ArrayList;
016: import java.util.HashSet;
017: import java.util.List;
018: import java.util.Set;
019:
020: /**
021: * Modifies the data type of an existing column.
022: */
023: public class ModifyColumnChange extends AbstractChange {
024:
025: private String schemaName;
026: private String tableName;
027: private List<ColumnConfig> columns;
028:
029: public ModifyColumnChange() {
030: super ("modifyColumn", "Modify Column");
031: columns = new ArrayList<ColumnConfig>();
032: }
033:
034: public String getSchemaName() {
035: return schemaName;
036: }
037:
038: public void setSchemaName(String schemaName) {
039: this .schemaName = schemaName;
040: }
041:
042: public String getTableName() {
043: return tableName;
044: }
045:
046: public void setTableName(String tableName) {
047: this .tableName = tableName;
048: }
049:
050: public List<ColumnConfig> getColumns() {
051: return columns;
052: }
053:
054: public void addColumn(ColumnConfig column) {
055: columns.add(column);
056: }
057:
058: public void removeColumn(ColumnConfig column) {
059: columns.remove(column);
060: }
061:
062: public SqlStatement[] generateStatements(Database database)
063: throws UnsupportedChangeException {
064: List<SqlStatement> sql = new ArrayList<SqlStatement>();
065:
066: for (ColumnConfig aColumn : columns) {
067:
068: String schemaName = getSchemaName() == null ? database
069: .getDefaultSchemaName() : getSchemaName();
070: if (database instanceof SybaseDatabase) {
071: sql.add(new RawSqlStatement("ALTER TABLE "
072: + database.escapeTableName(schemaName,
073: getTableName()) + " MODIFY "
074: + aColumn.getName() + " " + aColumn.getType()));
075: } else if (database instanceof MSSQLDatabase) {
076: sql.add(new RawSqlStatement("ALTER TABLE "
077: + database.escapeTableName(schemaName,
078: getTableName()) + " ALTER COLUMN "
079: + aColumn.getName() + " " + aColumn.getType()));
080: } else if (database instanceof MySQLDatabase) {
081: sql.add(new RawSqlStatement("ALTER TABLE "
082: + database.escapeTableName(schemaName,
083: getTableName()) + " MODIFY COLUMN "
084: + aColumn.getName() + " " + aColumn.getType()));
085: } else if (database instanceof OracleDatabase
086: || database instanceof MaxDBDatabase) {
087: sql.add(new RawSqlStatement("ALTER TABLE "
088: + database.escapeTableName(schemaName,
089: getTableName()) + " MODIFY ("
090: + aColumn.getName() + " " + aColumn.getType()
091: + ")"));
092: } else if (database instanceof DerbyDatabase) {
093: sql.add(new RawSqlStatement("ALTER TABLE "
094: + database.escapeTableName(schemaName,
095: getTableName()) + " ALTER COLUMN "
096: + aColumn.getName() + " SET DATA TYPE "
097: + aColumn.getType()));
098: } else if (database instanceof HsqlDatabase) {
099: sql.add(new RawSqlStatement("ALTER TABLE "
100: + database.escapeTableName(schemaName,
101: getTableName()) + " ALTER COLUMN "
102: + aColumn.getName() + " " + aColumn.getType()));
103: } else if (database instanceof CacheDatabase) {
104: sql.add(new RawSqlStatement("ALTER TABLE "
105: + database.escapeTableName(schemaName,
106: getTableName()) + " ALTER COLUMN "
107: + aColumn.getName() + " " + aColumn.getType()));
108: } else if (database instanceof DB2Database) {
109: sql.add(new RawSqlStatement("ALTER TABLE "
110: + database.escapeTableName(schemaName,
111: getTableName()) + " ALTER COLUMN "
112: + aColumn.getName() + " SET DATA TYPE "
113: + aColumn.getType()));
114: sql.add(new ReorganizeTableStatement(schemaName,
115: getTableName()));
116: } else {
117: sql.add(new RawSqlStatement("ALTER TABLE "
118: + database.escapeTableName(schemaName,
119: getTableName()) + " ALTER COLUMN "
120: + aColumn.getName() + " TYPE "
121: + aColumn.getType()));
122: }
123: }
124:
125: return sql.toArray(new SqlStatement[sql.size()]);
126: }
127:
128: public String getConfirmationMessage() {
129: List<String> names = new ArrayList<String>(columns.size());
130: for (ColumnConfig col : columns) {
131: names.add(col.getName() + "(" + col.getType() + ")");
132: }
133:
134: return "Columns " + StringUtils.join(names, ",") + " of "
135: + getTableName() + " modified";
136: }
137:
138: public Element createNode(Document currentChangeLogFileDOM) {
139: Element node = currentChangeLogFileDOM
140: .createElement("modifyColumn");
141: if (getSchemaName() != null) {
142: node.setAttribute("schemaName", getSchemaName());
143: }
144: node.setAttribute("tableName", getTableName());
145:
146: for (ColumnConfig col : getColumns()) {
147: Element subNode = col.createNode(currentChangeLogFileDOM);
148: node.appendChild(subNode);
149: }
150:
151: return node;
152: }
153:
154: public Set<DatabaseObject> getAffectedDatabaseObjects() {
155: List<DatabaseObject> result = new ArrayList<DatabaseObject>(
156: columns.size());
157:
158: Table table = new Table(getTableName());
159: for (ColumnConfig aColumn : columns) {
160: Column each = new Column();
161: each.setTable(table);
162: each.setName(aColumn.getName());
163: result.add(each);
164: }
165:
166: return new HashSet<DatabaseObject>(result);
167: }
168:
169: }
|