1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.CacheDatabase; |
5 | |
import liquibase.database.core.DB2Database; |
6 | |
import liquibase.database.core.DerbyDatabase; |
7 | |
import liquibase.database.core.FirebirdDatabase; |
8 | |
import liquibase.database.core.H2Database; |
9 | |
import liquibase.database.core.HsqlDatabase; |
10 | |
import liquibase.database.core.InformixDatabase; |
11 | |
import liquibase.database.core.MSSQLDatabase; |
12 | |
import liquibase.database.core.MaxDBDatabase; |
13 | |
import liquibase.database.core.MySQLDatabase; |
14 | |
import liquibase.database.core.SQLiteDatabase; |
15 | |
import liquibase.database.core.SybaseASADatabase; |
16 | |
import liquibase.exception.ValidationErrors; |
17 | |
import liquibase.sql.Sql; |
18 | |
import liquibase.sql.UnparsedSql; |
19 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
20 | |
import liquibase.statement.core.RenameColumnStatement; |
21 | |
|
22 | 81 | public class RenameColumnGenerator extends AbstractSqlGenerator<RenameColumnStatement> { |
23 | |
|
24 | |
@Override |
25 | |
public boolean supports(RenameColumnStatement statement, Database database) { |
26 | 58 | return !(database instanceof DB2Database || database instanceof CacheDatabase || database instanceof SQLiteDatabase); |
27 | |
} |
28 | |
|
29 | |
@Override |
30 | |
public ValidationErrors validate(RenameColumnStatement renameColumnStatement, Database database, |
31 | |
SqlGeneratorChain sqlGeneratorChain) { |
32 | 13 | ValidationErrors validationErrors = new ValidationErrors(); |
33 | 13 | validationErrors.checkRequiredField("tableName", renameColumnStatement.getTableName()); |
34 | 13 | validationErrors.checkRequiredField("oldColumnName", renameColumnStatement.getOldColumnName()); |
35 | 13 | validationErrors.checkRequiredField("newColumnName", renameColumnStatement.getNewColumnName()); |
36 | |
|
37 | 13 | if (database instanceof MySQLDatabase) { |
38 | 1 | validationErrors.checkRequiredField("columnDataType", renameColumnStatement.getColumnDataType()); |
39 | |
} |
40 | |
|
41 | 13 | return validationErrors; |
42 | |
} |
43 | |
|
44 | |
@Override |
45 | |
public Sql[] generateSql(RenameColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
46 | |
String sql; |
47 | 0 | if (database instanceof MSSQLDatabase) { |
48 | |
|
49 | |
|
50 | 0 | sql = "exec sp_rename '" |
51 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
52 | |
+ "." |
53 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
54 | |
statement.getOldColumnName()) + "', '" + statement.getNewColumnName() + "'"; |
55 | 0 | } else if (database instanceof MySQLDatabase) { |
56 | 0 | sql = "ALTER TABLE " |
57 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
58 | |
+ " CHANGE " |
59 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
60 | |
statement.getOldColumnName()) |
61 | |
+ " " |
62 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
63 | |
statement.getNewColumnName()) + " " + statement.getColumnDataType(); |
64 | 0 | } else if (database instanceof HsqlDatabase || database instanceof H2Database) { |
65 | 0 | sql = "ALTER TABLE " |
66 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
67 | |
+ " ALTER COLUMN " |
68 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
69 | |
statement.getOldColumnName()) |
70 | |
+ " RENAME TO " |
71 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
72 | |
statement.getNewColumnName()); |
73 | 0 | } else if (database instanceof FirebirdDatabase) { |
74 | 0 | sql = "ALTER TABLE " |
75 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
76 | |
+ " ALTER COLUMN " |
77 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
78 | |
statement.getOldColumnName()) |
79 | |
+ " TO " |
80 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
81 | |
statement.getNewColumnName()); |
82 | 0 | } else if ((database instanceof MaxDBDatabase) |
83 | |
|
84 | |
|| (database instanceof DerbyDatabase) || (database instanceof InformixDatabase)) { |
85 | 0 | sql = "RENAME COLUMN " |
86 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
87 | |
+ "." |
88 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
89 | |
statement.getOldColumnName()) |
90 | |
+ " TO " |
91 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
92 | |
statement.getNewColumnName()); |
93 | 0 | } else if (database instanceof SybaseASADatabase) { |
94 | 0 | sql = "ALTER TABLE " |
95 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
96 | |
+ " RENAME " |
97 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
98 | |
statement.getOldColumnName()) |
99 | |
+ " TO " |
100 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
101 | |
statement.getNewColumnName()); |
102 | |
} else { |
103 | 0 | sql = "ALTER TABLE " |
104 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
105 | |
+ " RENAME COLUMN " |
106 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
107 | |
statement.getOldColumnName()) |
108 | |
+ " TO " |
109 | |
+ database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), |
110 | |
statement.getNewColumnName()); |
111 | |
} |
112 | |
|
113 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
114 | |
} |
115 | |
} |