View Javadoc

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