1 package liquibase.ext.spatial.sqlgenerator;
2
3 import liquibase.database.Database;
4 import liquibase.database.core.OracleDatabase;
5 import liquibase.exception.UnexpectedLiquibaseException;
6 import liquibase.exception.ValidationErrors;
7 import liquibase.ext.spatial.statement.DropSpatialIndexStatement;
8 import liquibase.snapshot.SnapshotGeneratorFactory;
9 import liquibase.sql.Sql;
10 import liquibase.sql.UnparsedSql;
11 import liquibase.sqlgenerator.SqlGeneratorChain;
12 import liquibase.sqlgenerator.core.AbstractSqlGenerator;
13 import liquibase.structure.core.Column;
14 import liquibase.structure.core.Index;
15 import liquibase.structure.core.Table;
16 import liquibase.structure.core.View;
17
18
19
20
21
22 public class DropSpatialIndexGeneratorOracle extends
23 AbstractSqlGenerator<DropSpatialIndexStatement> {
24 @Override
25 public boolean supports(final DropSpatialIndexStatement statement, final Database database) {
26 return database instanceof OracleDatabase;
27 }
28
29
30
31
32 @Override
33 public ValidationErrors validate(final DropSpatialIndexStatement statement,
34 final Database database, final SqlGeneratorChain sqlGeneratorChain) {
35 final ValidationErrors validationErrors = new ValidationErrors();
36 validationErrors.checkRequiredField("indexName", statement.getIndexName());
37 return validationErrors;
38 }
39
40 @Override
41 public Sql[] generateSql(final DropSpatialIndexStatement statement, final Database database,
42 final SqlGeneratorChain sqlGeneratorChain) {
43 final String indexName = statement.getIndexName();
44 final Index example = new Index().setName(indexName);
45 if (statement.getTableName() != null) {
46 example.setTable((Table) new Table().setName(statement.getTableName()).setSchema(
47 statement.getTableCatalogName(), statement.getTableSchemaName()));
48 }
49 Index index;
50 try {
51 index = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
52 } catch (final Exception e) {
53 throw new UnexpectedLiquibaseException("Failed to create a snapshot of '" + indexName
54 + "'", e);
55 }
56
57 final String tableName = index.getTable().getName();
58 final Column column = index.getColumns().get(0);
59
60 final StringBuilder sql = new StringBuilder();
61 sql.append("DELETE FROM user_sdo_geom_metadata ");
62 sql.append("WHERE table_name = '").append(database.correctObjectName(tableName, Table.class));
63 sql.append("' AND column_name = '").append(
64 database.correctObjectName(column.getName(), Column.class));
65 sql.append("'");
66 final UnparsedSql deleteMetadata = new UnparsedSql(sql.toString(),
67 new View().setName("user_sdo_geom_metadata"));
68 return new Sql[] { deleteMetadata };
69 }
70 }