View Javadoc

1   package liquibase.ext.spatial.sqlgenerator;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   
7   import liquibase.database.Database;
8   import liquibase.database.core.DerbyDatabase;
9   import liquibase.database.core.H2Database;
10  import liquibase.exception.ValidationErrors;
11  import liquibase.ext.spatial.statement.DropSpatialIndexStatement;
12  import liquibase.ext.spatial.utils.GeometryColumnsUtils;
13  import liquibase.sql.Sql;
14  import liquibase.sql.UnparsedSql;
15  import liquibase.sqlgenerator.SqlGeneratorChain;
16  import liquibase.sqlgenerator.core.AbstractSqlGenerator;
17  import liquibase.statement.SqlStatement;
18  import liquibase.statement.core.DropTableStatement;
19  
20  /**
21   * <code>DropSpatialTableGeneratorGeoDB</code> generates the SQL statements for cleaning up any
22   * indexes and geometry columns prior to dropping the table.
23   */
24  public class DropSpatialTableGeneratorGeoDB extends AbstractSqlGenerator<DropTableStatement> {
25     /**
26      * @see AbstractSqlGenerator#supports(SqlStatement, Database)
27      */
28     @Override
29     public boolean supports(final DropTableStatement statement, final Database database) {
30        return database instanceof DerbyDatabase || database instanceof H2Database;
31     }
32  
33     /**
34      * @see AbstractSqlGenerator#getPriority()
35      */
36     @Override
37     public int getPriority() {
38        return super.getPriority() + 1;
39     }
40  
41     @Override
42     public ValidationErrors validate(final DropTableStatement statement, final Database database,
43           final SqlGeneratorChain sqlGeneratorChain) {
44        return sqlGeneratorChain.validate(statement, database);
45     }
46  
47     @Override
48     public Sql[] generateSql(final DropTableStatement statement, final Database database,
49           final SqlGeneratorChain sqlGeneratorChain) {
50        final List<Sql> list = new ArrayList<Sql>();
51        String schemaName = statement.getSchemaName();
52        if (schemaName == null) {
53           schemaName = database.getDefaultSchemaName();
54        }
55        final String tableName = statement.getTableName();
56  
57        // If the table has a geometry column, drop it first.
58        if (GeometryColumnsUtils.hasGeometryColumn(database, schemaName, tableName)) {
59           dropSpatialIndexIfExists(statement.getCatalogName(), schemaName, tableName, database, list);
60           final String sql = "CALL DropGeometryColumns('" + schemaName + "', '" + tableName + "')";
61           final Sql addGeometryColumn = new UnparsedSql(sql);
62           list.add(addGeometryColumn);
63        }
64        list.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
65        return list.toArray(new Sql[list.size()]);
66     }
67  
68     /**
69      * Adds the SQL statement to drop the spatial index if it is present.
70      * 
71      * @param catalogName
72      *           the catalog name.
73      * @param schemaName
74      *           the schema name.
75      * @param tableName
76      *           the table name.
77      * @param database
78      *           the database.
79      * @param list
80      *           the list of SQL statements to execute.
81      */
82     protected void dropSpatialIndexIfExists(final String catalogName, final String schemaName,
83           final String tableName, final Database database, final List<Sql> list) {
84        final DropSpatialIndexGeneratorGeoDB generator = new DropSpatialIndexGeneratorGeoDB();
85        final DropSpatialIndexStatement statement = new DropSpatialIndexStatement(null, catalogName,
86              schemaName, tableName);
87        list.addAll(Arrays.asList(generator.generateSqlIfExists(statement, database)));
88     }
89  }