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.DropColumnGenerator;
17 import liquibase.statement.core.DropColumnStatement;
18 import liquibase.structure.core.Column;
19
20
21
22
23
24
25
26
27 public class DropGeometryColumnGeneratorGeoDB extends DropColumnGenerator {
28
29
30
31
32 @Override
33 public boolean supports(final DropColumnStatement statement, final Database database) {
34 return database instanceof DerbyDatabase || database instanceof H2Database;
35 }
36
37
38
39
40 @Override
41 public int getPriority() {
42 return super.getPriority() + 1;
43 }
44
45 @Override
46 public ValidationErrors validate(final DropColumnStatement statement, final Database database,
47 final SqlGeneratorChain sqlGeneratorChain) {
48 return sqlGeneratorChain.validate(statement, database);
49 }
50
51 @Override
52 public Sql[] generateSql(final DropColumnStatement statement, final Database database,
53 final SqlGeneratorChain sqlGeneratorChain) {
54
55 String schemaName = statement.getSchemaName();
56 if (schemaName == null) {
57 schemaName = database.getDefaultSchemaName();
58 }
59 final String tableName = statement.getTableName();
60 final String columnName = statement.getColumnName();
61 final boolean isGeometryColumn = GeometryColumnsUtils.isGeometryColumn(database, schemaName,
62 tableName, columnName);
63 final List<Sql> list = new ArrayList<Sql>();
64 if (isGeometryColumn) {
65 dropSpatialIndexIfExists(statement.getCatalogName(), schemaName, tableName, database, list);
66 final String sql = "CALL DropGeometryColumn('" + schemaName + "', '" + tableName + "', '"
67 + columnName + "')";
68 final Column column = getAffectedColumn(statement);
69 final Sql dropGeometryColumn = new UnparsedSql(sql, column);
70 list.add(dropGeometryColumn);
71 } else {
72 list.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
73 }
74 return list.toArray(new Sql[list.size()]);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 protected void dropSpatialIndexIfExists(final String catalogName, final String schemaName,
92 final String tableName, final Database database, final List<Sql> list) {
93 final DropSpatialIndexGeneratorGeoDB generator = new DropSpatialIndexGeneratorGeoDB();
94 final DropSpatialIndexStatement statement = new DropSpatialIndexStatement(null, catalogName,
95 schemaName, tableName);
96 list.addAll(Arrays.asList(generator.generateSqlIfExists(statement, database)));
97 }
98 }