View Javadoc

1   package liquibase.ext.spatial.sqlgenerator;
2   
3   import liquibase.database.Database;
4   import liquibase.database.core.DerbyDatabase;
5   import liquibase.database.core.H2Database;
6   import liquibase.exception.UnexpectedLiquibaseException;
7   import liquibase.exception.ValidationErrors;
8   import liquibase.ext.spatial.preconditions.SpatialIndexExistsPrecondition;
9   import liquibase.ext.spatial.statement.DropSpatialIndexStatement;
10  import liquibase.snapshot.SnapshotGeneratorFactory;
11  import liquibase.sql.Sql;
12  import liquibase.sql.UnparsedSql;
13  import liquibase.sqlgenerator.SqlGeneratorChain;
14  import liquibase.sqlgenerator.core.AbstractSqlGenerator;
15  import liquibase.structure.DatabaseObject;
16  import liquibase.structure.core.Table;
17  
18  /**
19   * <code>DropSpatialIndexGeneratorGeoDB</code> ...
20   */
21  public class DropSpatialIndexGeneratorGeoDB extends AbstractSqlGenerator<DropSpatialIndexStatement> {
22  
23     /**
24      * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#supports(liquibase.statement.SqlStatement,
25      *      liquibase.database.Database)
26      */
27     @Override
28     public boolean supports(final DropSpatialIndexStatement statement, final Database database) {
29        return database instanceof DerbyDatabase || database instanceof H2Database;
30     }
31  
32     /**
33      * Ensures that the table name is populated.
34      */
35     @Override
36     public ValidationErrors validate(final DropSpatialIndexStatement statement,
37           final Database database, final SqlGeneratorChain sqlGeneratorChain) {
38        final ValidationErrors validationErrors = new ValidationErrors();
39        validationErrors.checkRequiredField("tableName", statement.getTableName());
40        return validationErrors;
41     }
42  
43     @Override
44     public Sql[] generateSql(final DropSpatialIndexStatement statement, final Database database,
45           final SqlGeneratorChain sqlGeneratorChain) {
46        final String catalogName = statement.getTableCatalogName();
47        String schemaName = statement.getTableSchemaName();
48        if (schemaName == null) {
49           schemaName = database.getDefaultSchemaName();
50        }
51  
52        final StringBuilder sql = new StringBuilder("CALL ");
53        sql.append(schemaName).append(".DropSpatialIndex(");
54  
55        // Add the schema name parameter.
56        sql.append("'").append(database.escapeStringForDatabase(schemaName)).append("'");
57        sql.append(", ");
58  
59        // Add the table name parameter.
60        final String tableName = statement.getTableName();
61        sql.append("'").append(database.escapeStringForDatabase(tableName)).append("'");
62        sql.append(')');
63  
64        final Table hatboxTable = new Table().setName(tableName + "_HATBOX");
65        hatboxTable.setSchema(catalogName, schemaName);
66        final UnparsedSql spatialize = new UnparsedSql(sql.toString(), hatboxTable);
67        return new Sql[] { spatialize };
68     }
69  
70     /**
71      * Generates the SQL statement to drop the spatial index if it exists.
72      * 
73      * @param statement
74      *           the drop spatial index statement.
75      * @param database
76      *           the database.
77      * @param list
78      *           the list of SQL statements to execute.
79      */
80     public Sql[] generateSqlIfExists(final DropSpatialIndexStatement statement,
81           final Database database) {
82        final String catalogName = statement.getTableCatalogName();
83        final String schemaName = statement.getTableSchemaName();
84        final String tableName = statement.getTableName();
85        final SpatialIndexExistsPrecondition precondition = new SpatialIndexExistsPrecondition();
86        precondition.setCatalogName(catalogName);
87        precondition.setSchemaName(schemaName);
88        precondition.setTableName(tableName);
89        final DatabaseObject example = precondition.getExample(database, tableName);
90        try {
91           // If a spatial index exists on the table, drop it.
92           if (SnapshotGeneratorFactory.getInstance().has(example, database)) {
93              return generateSql(statement, database, null);
94           }
95        } catch (final Exception e) {
96           throw new UnexpectedLiquibaseException(e);
97        }
98        return new Sql[0];
99     }
100 }