001: package liquibase.database.sql;
002:
003: import liquibase.database.*;
004: import liquibase.exception.StatementNotSupportedOnDatabaseException;
005:
006: public class RenameColumnStatement implements SqlStatement {
007:
008: private String schemaName;
009: private String tableName;
010: private String oldColumnName;
011: private String newColumnName;
012: private String columnDataType;
013:
014: public RenameColumnStatement(String schemaName, String tableName,
015: String oldColumnName, String newColumnName,
016: String columnDataType) {
017: this .schemaName = schemaName;
018: this .tableName = tableName;
019: this .oldColumnName = oldColumnName;
020: this .newColumnName = newColumnName;
021: this .columnDataType = columnDataType;
022: }
023:
024: public String getSchemaName() {
025: return schemaName;
026: }
027:
028: public void setSchemaName(String schemaName) {
029: this .schemaName = schemaName;
030: }
031:
032: public String getTableName() {
033: return tableName;
034: }
035:
036: public void setTableName(String tableName) {
037: this .tableName = tableName;
038: }
039:
040: public String getOldColumnName() {
041: return oldColumnName;
042: }
043:
044: public void setOldColumnName(String oldColumnName) {
045: this .oldColumnName = oldColumnName;
046: }
047:
048: public String getNewColumnName() {
049: return newColumnName;
050: }
051:
052: public void setNewColumnName(String newColumnName) {
053: this .newColumnName = newColumnName;
054: }
055:
056: public String getColumnDataType() {
057: return columnDataType;
058: }
059:
060: public void setColumnDataType(String columnDataType) {
061: this .columnDataType = columnDataType;
062: }
063:
064: public String getSqlStatement(Database database)
065: throws StatementNotSupportedOnDatabaseException {
066: if (!supportsDatabase(database)) {
067: throw new StatementNotSupportedOnDatabaseException(this ,
068: database);
069: }
070:
071: if (database instanceof MSSQLDatabase) {
072: return "exec sp_rename '"
073: + database.escapeTableName(getSchemaName(),
074: getTableName()) + "."
075: + database.escapeColumnName(getOldColumnName())
076: + "', '"
077: + database.escapeColumnName(getNewColumnName())
078: + "'";
079: } else if (database instanceof MySQLDatabase) {
080: if (getColumnDataType() == null) {
081: throw new StatementNotSupportedOnDatabaseException(
082: "columnDataType is required to rename columns",
083: this , database);
084: }
085:
086: return "ALTER TABLE "
087: + database.escapeTableName(getSchemaName(),
088: getTableName()) + " CHANGE "
089: + database.escapeColumnName(getOldColumnName())
090: + " "
091: + database.escapeColumnName(getNewColumnName())
092: + " " + getColumnDataType();
093: } else if (database instanceof HsqlDatabase) {
094: return "ALTER TABLE "
095: + database.escapeTableName(getSchemaName(),
096: getTableName()) + " ALTER COLUMN "
097: + database.escapeColumnName(getOldColumnName())
098: + " RENAME TO "
099: + database.escapeColumnName(getNewColumnName());
100: } else if (database instanceof FirebirdDatabase) {
101: return "ALTER TABLE "
102: + database.escapeTableName(getSchemaName(),
103: getTableName()) + " ALTER COLUMN "
104: + database.escapeColumnName(getOldColumnName())
105: + " TO "
106: + database.escapeColumnName(getNewColumnName());
107: } else if (database instanceof MaxDBDatabase) {
108: return "RENAME COLUMN "
109: + database.escapeTableName(getSchemaName(),
110: getTableName()) + "."
111: + database.escapeColumnName(getOldColumnName())
112: + " TO "
113: + database.escapeColumnName(getNewColumnName());
114: }
115:
116: return "ALTER TABLE "
117: + database.escapeTableName(getSchemaName(),
118: getTableName()) + " RENAME COLUMN "
119: + database.escapeColumnName(getOldColumnName())
120: + " TO "
121: + database.escapeColumnName(getNewColumnName());
122: }
123:
124: public String getEndDelimiter(Database database) {
125: return ";";
126: }
127:
128: public boolean supportsDatabase(Database database) {
129: return !(database instanceof DB2Database
130: || database instanceof CacheDatabase || database instanceof DerbyDatabase);
131: }
132: }
|