1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import java.util.List; |
4 | |
|
5 | |
import liquibase.database.Database; |
6 | |
import liquibase.database.core.MSSQLDatabase; |
7 | |
import liquibase.database.core.MySQLDatabase; |
8 | |
import liquibase.database.core.OracleDatabase; |
9 | |
import liquibase.database.structure.Index; |
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.DropIndexStatement; |
15 | |
import liquibase.util.StringUtils; |
16 | |
|
17 | 25 | public class DropIndexGenerator extends AbstractSqlGenerator<DropIndexStatement> { |
18 | |
|
19 | |
@Override |
20 | |
public ValidationErrors validate(DropIndexStatement statement, Database database, |
21 | |
SqlGeneratorChain sqlGeneratorChain) { |
22 | 15 | ValidationErrors validationErrors = new ValidationErrors(); |
23 | 15 | validationErrors.checkRequiredField("indexName", statement.getIndexName()); |
24 | |
|
25 | 15 | if (database instanceof MySQLDatabase || database instanceof MSSQLDatabase) { |
26 | 2 | validationErrors.checkRequiredField("tableName", statement.getTableName()); |
27 | |
} |
28 | |
|
29 | 15 | return validationErrors; |
30 | |
} |
31 | |
|
32 | |
@Override |
33 | |
public Sql[] generateSql(DropIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
34 | 0 | List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ","); |
35 | 0 | if (associatedWith != null) { |
36 | 0 | if (associatedWith.contains(Index.MARK_PRIMARY_KEY) |
37 | |
|| associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT)) { |
38 | 0 | return new Sql[0]; |
39 | 0 | } else if (associatedWith.contains(Index.MARK_FOREIGN_KEY)) { |
40 | 0 | if (!(database instanceof OracleDatabase || database instanceof MSSQLDatabase)) { |
41 | 0 | return new Sql[0]; |
42 | |
} |
43 | |
} |
44 | |
} |
45 | |
|
46 | 0 | String schemaName = statement.getTableSchemaName(); |
47 | |
|
48 | 0 | if (database instanceof MySQLDatabase) { |
49 | 0 | return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(null, statement.getIndexName()) |
50 | |
+ " ON " + database.escapeTableName(schemaName, statement.getTableName())) }; |
51 | 0 | } else if (database instanceof MSSQLDatabase) { |
52 | 0 | return new Sql[] { new UnparsedSql("DROP INDEX " |
53 | |
+ database.escapeTableName(schemaName, statement.getTableName()) + "." |
54 | |
+ database.escapeIndexName(null, statement.getIndexName())) }; |
55 | |
} |
56 | |
|
57 | 0 | return new Sql[] { new UnparsedSql("DROP INDEX " |
58 | |
+ database.escapeIndexName(schemaName, statement.getIndexName())) }; |
59 | |
} |
60 | |
} |