01: package liquibase.database.sql;
02:
03: import liquibase.database.*;
04: import liquibase.exception.StatementNotSupportedOnDatabaseException;
05:
06: public class RenameTableStatement implements SqlStatement {
07: private String schemaName;
08: private String oldTableName;
09: private String newTableName;
10:
11: public RenameTableStatement(String schemaName, String oldTableName,
12: String newTableName) {
13: this .schemaName = schemaName;
14: this .oldTableName = oldTableName;
15: this .newTableName = newTableName;
16: }
17:
18: public String getSchemaName() {
19: return schemaName;
20: }
21:
22: public String getOldTableName() {
23: return oldTableName;
24: }
25:
26: public String getNewTableName() {
27: return newTableName;
28: }
29:
30: public String getSqlStatement(Database database)
31: throws StatementNotSupportedOnDatabaseException {
32: if (!supportsDatabase(database)) {
33: throw new StatementNotSupportedOnDatabaseException(this ,
34: database);
35: }
36:
37: if (database instanceof MSSQLDatabase) {
38: return "exec sp_rename '"
39: + database.escapeTableName(getSchemaName(),
40: oldTableName) + "', "
41: + database.escapeTableName(null, newTableName);
42: } else if (database instanceof MySQLDatabase) {
43: return "ALTER TABLE "
44: + database.escapeTableName(getSchemaName(),
45: oldTableName)
46: + " RENAME "
47: + database.escapeTableName(getSchemaName(),
48: getNewTableName());
49: } else if (database instanceof PostgresDatabase) {
50: return "ALTER TABLE "
51: + database.escapeTableName(getSchemaName(),
52: oldTableName) + " RENAME TO "
53: + database.escapeTableName(null, newTableName);
54: } else if ((database instanceof DerbyDatabase)
55: || ((database instanceof MaxDBDatabase))) {
56: return "RENAME TABLE "
57: + database.escapeTableName(getSchemaName(),
58: oldTableName) + " TO "
59: + database.escapeTableName(null, newTableName);
60: } else if (database instanceof HsqlDatabase) {
61: return "ALTER TABLE "
62: + database.escapeTableName(getSchemaName(),
63: oldTableName) + " RENAME TO "
64: + database.escapeTableName(null, newTableName);
65: } else if (database instanceof OracleDatabase) {
66: return "ALTER TABLE "
67: + database.escapeTableName(getSchemaName(),
68: oldTableName) + " RENAME TO "
69: + database.escapeTableName(null, newTableName);
70: } else if (database instanceof DB2Database) {
71: return "RENAME "
72: + database.escapeTableName(getSchemaName(),
73: oldTableName) + " TO "
74: + database.escapeTableName(null, newTableName);//db2 doesn't allow specifying new schema name
75: }
76:
77: return "RENAME "
78: + database.escapeTableName(getSchemaName(),
79: getOldTableName())
80: + " TO "
81: + database.escapeTableName(getSchemaName(),
82: getNewTableName());
83: }
84:
85: public String getEndDelimiter(Database database) {
86: return ";";
87: }
88:
89: public boolean supportsDatabase(Database database) {
90: return !(database instanceof CacheDatabase || database instanceof FirebirdDatabase);
91: }
92: }
|