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.OracleDatabase; |
15 | |
import liquibase.database.core.PostgresDatabase; |
16 | |
import liquibase.database.core.SQLiteDatabase; |
17 | |
import liquibase.database.core.SybaseASADatabase; |
18 | |
import liquibase.exception.ValidationErrors; |
19 | |
import liquibase.sql.Sql; |
20 | |
import liquibase.sql.UnparsedSql; |
21 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
22 | |
import liquibase.statement.core.RenameTableStatement; |
23 | |
|
24 | 80 | public class RenameTableGenerator extends AbstractSqlGenerator<RenameTableStatement> { |
25 | |
|
26 | |
@Override |
27 | |
public boolean supports(RenameTableStatement statement, Database database) { |
28 | 57 | return !(database instanceof CacheDatabase || database instanceof FirebirdDatabase); |
29 | |
} |
30 | |
|
31 | |
@Override |
32 | |
public ValidationErrors validate(RenameTableStatement renameTableStatement, Database database, |
33 | |
SqlGeneratorChain sqlGeneratorChain) { |
34 | 13 | ValidationErrors validationErrors = new ValidationErrors(); |
35 | 13 | validationErrors.checkRequiredField("newTableName", renameTableStatement.getNewTableName()); |
36 | 13 | validationErrors.checkRequiredField("oldTableName", renameTableStatement.getOldTableName()); |
37 | 13 | return validationErrors; |
38 | |
} |
39 | |
|
40 | |
@Override |
41 | |
public Sql[] generateSql(RenameTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
42 | |
String sql; |
43 | 0 | if (database instanceof MSSQLDatabase) { |
44 | 0 | sql = "exec sp_rename '" + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
45 | |
+ "', '" + statement.getNewTableName() + '\''; |
46 | 0 | } else if (database instanceof MySQLDatabase) { |
47 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
48 | |
+ " RENAME " + database.escapeTableName(statement.getSchemaName(), statement.getNewTableName()); |
49 | 0 | } else if (database instanceof PostgresDatabase) { |
50 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
51 | |
+ " RENAME TO " + database.escapeDatabaseObject(statement.getNewTableName()); |
52 | 0 | } else if (database instanceof SybaseASADatabase) { |
53 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
54 | |
+ " RENAME " + database.escapeDatabaseObject(statement.getNewTableName()); |
55 | 0 | } else if ((database instanceof DerbyDatabase) || (database instanceof MaxDBDatabase) |
56 | |
|| (database instanceof InformixDatabase)) { |
57 | 0 | sql = "RENAME TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
58 | |
+ " TO " + database.escapeDatabaseObject(statement.getNewTableName()); |
59 | 0 | } else if (database instanceof HsqlDatabase || database instanceof H2Database) { |
60 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
61 | |
+ " RENAME TO " + database.escapeDatabaseObject(statement.getNewTableName()); |
62 | 0 | } else if (database instanceof OracleDatabase) { |
63 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
64 | |
+ " RENAME TO " + database.escapeDatabaseObject(statement.getNewTableName()); |
65 | 0 | } else if (database instanceof DB2Database) { |
66 | 0 | sql = "RENAME " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) + " TO " |
67 | |
+ database.escapeDatabaseObject(statement.getNewTableName()); |
68 | |
|
69 | 0 | } else if (database instanceof SQLiteDatabase) { |
70 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) |
71 | |
+ " RENAME TO " + database.escapeDatabaseObject(statement.getNewTableName()); |
72 | |
} else { |
73 | 0 | sql = "RENAME " + database.escapeTableName(statement.getSchemaName(), statement.getOldTableName()) + " TO " |
74 | |
+ database.escapeTableName(statement.getSchemaName(), statement.getNewTableName()); |
75 | |
} |
76 | |
|
77 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
78 | |
|
79 | |
} |
80 | |
} |