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
22
23
24 public class DropSpatialTableGeneratorGeoDB extends AbstractSqlGenerator<DropTableStatement> {
25
26
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
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
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
70
71
72
73
74
75
76
77
78
79
80
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 }