View Javadoc

1   package liquibase.ext.spatial.change;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import liquibase.change.AbstractChange;
7   import liquibase.change.ChangeMetaData;
8   import liquibase.change.DatabaseChange;
9   import liquibase.change.DatabaseChangeProperty;
10  import liquibase.database.Database;
11  import liquibase.database.core.DerbyDatabase;
12  import liquibase.database.core.H2Database;
13  import liquibase.database.core.MySQLDatabase;
14  import liquibase.database.core.PostgresDatabase;
15  import liquibase.ext.spatial.statement.DropSpatialIndexStatement;
16  import liquibase.ext.spatial.xml.XmlConstants;
17  import liquibase.statement.SqlStatement;
18  import liquibase.statement.core.DropIndexStatement;
19  import liquibase.util.StringUtils;
20  
21  /**
22   * @author Lonny Jacobson
23   */
24  @DatabaseChange(name = "dropSpatialIndex", description = "Drops the spatial index on an existing column or set of columns.", priority = ChangeMetaData.PRIORITY_DEFAULT, appliesTo = "index")
25  public class DropSpatialIndexChange extends AbstractChange {
26     /** The name of the catalog. */
27     private String catalogName;
28  
29     /** The name of the schema. */
30     private String schemaName;
31  
32     /** The name of the indexed table. */
33     private String tableName;
34  
35     /** The name of the index to drop. */
36     private String indexName;
37  
38     @DatabaseChangeProperty(mustEqualExisting = "index.schema")
39     public String getSchemaName() {
40        return this.schemaName;
41     }
42  
43     public void setSchemaName(final String schemaName) {
44        this.schemaName = schemaName;
45     }
46  
47     @DatabaseChangeProperty(mustEqualExisting = "index", description = "Name of the index to drop", requiredForDatabase = "mysql, oracle, postgresql")
48     public String getIndexName() {
49        return this.indexName;
50     }
51  
52     public void setIndexName(final String indexName) {
53        this.indexName = indexName;
54     }
55  
56     @DatabaseChangeProperty(mustEqualExisting = "index.table", description = "Name fo the indexed table.", requiredForDatabase = "h2, derby")
57     public String getTableName() {
58        return this.tableName;
59     }
60  
61     public void setTableName(final String tableName) {
62        this.tableName = tableName;
63     }
64  
65     @DatabaseChangeProperty(mustEqualExisting = "index.catalog")
66     public String getCatalogName() {
67        return this.catalogName;
68     }
69  
70     public void setCatalogName(final String catalogName) {
71        this.catalogName = catalogName;
72     }
73  
74     @Override
75     public String getSerializedObjectNamespace() {
76        return XmlConstants.SPATIAL_CHANGELOG_NAMESPACE;
77     }
78  
79     @Override
80     public String getConfirmationMessage() {
81        final StringBuilder message = new StringBuilder("Spatial index");
82        if (StringUtils.trimToNull(getIndexName()) != null) {
83           message.append(' ').append(getIndexName().trim());
84        }
85        message.append(" dropped");
86        if (StringUtils.trimToNull(getTableName()) != null) {
87           message.append(" from ").append(getTableName().trim());
88        }
89        return message.toString();
90     }
91  
92     /**
93      * Generates a {@link DropSpatialIndexStatement} followed by a {@link DropIndexStatement}, if
94      * applicable. The first statement allows extra clean-up when dropping an index. The second
95      * statement leverages the normal <code>DROP INDEX</code> logic.
96      */
97     @Override
98     public SqlStatement[] generateStatements(final Database database) {
99        final Collection<SqlStatement> statements = new ArrayList<SqlStatement>();
100       // MySQL and PostgreSQL only need the normal DROP INDEX statement.
101       if (!(database instanceof MySQLDatabase) && !(database instanceof PostgresDatabase)) {
102          final DropSpatialIndexStatement dropSpatialIndex = new DropSpatialIndexStatement(
103                this.indexName, this.catalogName, this.schemaName, this.tableName);
104          statements.add(dropSpatialIndex);
105       }
106 
107       // GeoDB doesn't use a tradition index structure so don't issue the normal DROP INDEX
108       // statement.
109       if (!(database instanceof DerbyDatabase) && !(database instanceof H2Database)) {
110          final DropIndexStatement dropIndex = new DropIndexStatement(this.indexName,
111                this.catalogName, this.schemaName, this.tableName, null);
112          statements.add(dropIndex);
113       }
114       return statements.toArray(new SqlStatement[statements.size()]);
115    }
116 }