1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.FirebirdDatabase; |
5 | |
import liquibase.database.core.InformixDatabase; |
6 | |
import liquibase.database.core.MSSQLDatabase; |
7 | |
import liquibase.database.core.OracleDatabase; |
8 | |
import liquibase.database.core.PostgresDatabase; |
9 | |
import liquibase.database.core.SQLiteDatabase; |
10 | |
import liquibase.exception.ValidationErrors; |
11 | |
import liquibase.sql.Sql; |
12 | |
import liquibase.sql.UnparsedSql; |
13 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
14 | |
import liquibase.statement.core.DropPrimaryKeyStatement; |
15 | |
|
16 | 86 | public class DropPrimaryKeyGenerator extends AbstractSqlGenerator<DropPrimaryKeyStatement> { |
17 | |
|
18 | |
@Override |
19 | |
public boolean supports(DropPrimaryKeyStatement statement, Database database) { |
20 | 61 | return (!(database instanceof SQLiteDatabase)); |
21 | |
} |
22 | |
|
23 | |
@Override |
24 | |
public ValidationErrors validate(DropPrimaryKeyStatement dropPrimaryKeyStatement, Database database, |
25 | |
SqlGeneratorChain sqlGeneratorChain) { |
26 | 15 | ValidationErrors validationErrors = new ValidationErrors(); |
27 | 15 | validationErrors.checkRequiredField("tableName", dropPrimaryKeyStatement.getTableName()); |
28 | |
|
29 | 15 | if (database instanceof FirebirdDatabase || database instanceof InformixDatabase) { |
30 | 2 | validationErrors.checkRequiredField("constraintName", dropPrimaryKeyStatement.getConstraintName()); |
31 | |
} |
32 | |
|
33 | 15 | return validationErrors; |
34 | |
} |
35 | |
|
36 | |
@Override |
37 | |
public Sql[] generateSql(DropPrimaryKeyStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
38 | |
String sql; |
39 | |
|
40 | 0 | if (database instanceof MSSQLDatabase) { |
41 | 0 | if (statement.getConstraintName() == null) { |
42 | 0 | StringBuilder query = new StringBuilder(); |
43 | 0 | query.append("DECLARE @pkname nvarchar(255)"); |
44 | 0 | query.append("\n"); |
45 | 0 | query.append("DECLARE @sql nvarchar(2048)"); |
46 | 0 | query.append("\n"); |
47 | 0 | query.append("select @pkname=i.name from sysindexes i"); |
48 | 0 | query.append(" join sysobjects o ON i.id = o.id"); |
49 | 0 | query.append(" join sysobjects pk ON i.name = pk.name AND pk.parent_obj = i.id AND pk.xtype = 'PK'"); |
50 | 0 | query.append(" join sysindexkeys ik on i.id = ik.id AND i.indid = ik.indid"); |
51 | 0 | query.append(" join syscolumns c ON ik.id = c.id AND ik.colid = c.colid"); |
52 | 0 | query.append(" where o.name = '").append(statement.getTableName()).append("'"); |
53 | 0 | query.append("\n"); |
54 | 0 | query.append("set @sql='alter table ") |
55 | |
.append(database.escapeTableName(statement.getSchemaName(), statement.getTableName())) |
56 | |
.append(" drop constraint ' + @pkname"); |
57 | 0 | query.append("\n"); |
58 | 0 | query.append("exec(@sql)"); |
59 | 0 | query.append("\n"); |
60 | 0 | sql = query.toString(); |
61 | 0 | } else { |
62 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
63 | |
+ " DROP CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName()); |
64 | |
} |
65 | 0 | } else if (database instanceof PostgresDatabase) { |
66 | 0 | if (statement.getConstraintName() == null) { |
67 | 0 | StringBuilder query = new StringBuilder(); |
68 | 0 | query.append("create or replace function __liquibase_drop_pk(tableName text) returns void as $$"); |
69 | 0 | query.append(" declare"); |
70 | 0 | query.append(" pkname text;"); |
71 | 0 | query.append(" sql text;"); |
72 | 0 | query.append(" begin"); |
73 | 0 | query.append(" pkname = c.conname"); |
74 | 0 | query.append(" from pg_class r, pg_constraint c"); |
75 | 0 | query.append(" where r.oid = c.conrelid"); |
76 | 0 | query.append(" and contype = 'p'"); |
77 | 0 | query.append(" and relname ilike tableName;"); |
78 | 0 | query.append(" sql = 'alter table ' || tableName || ' drop constraint ' || pkname;"); |
79 | 0 | query.append(" execute sql;"); |
80 | 0 | query.append(" end;"); |
81 | 0 | query.append(" $$ language plpgsql;"); |
82 | 0 | query.append(" select __liquibase_drop_pk('").append(statement.getTableName()).append("');"); |
83 | 0 | query.append(" drop function __liquibase_drop_pk(tableName text);"); |
84 | 0 | sql = query.toString(); |
85 | 0 | } else { |
86 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
87 | |
+ " DROP CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName()); |
88 | |
} |
89 | 0 | } else if (database instanceof FirebirdDatabase) { |
90 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
91 | |
+ " DROP CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName()); |
92 | 0 | } else if (database instanceof OracleDatabase) { |
93 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
94 | |
+ " DROP PRIMARY KEY DROP INDEX"; |
95 | 0 | } else if (database instanceof InformixDatabase) { |
96 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
97 | |
+ " DROP CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName()); |
98 | |
} else { |
99 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
100 | |
+ " DROP PRIMARY KEY"; |
101 | |
} |
102 | |
|
103 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
104 | |
} |
105 | |
} |