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.ValidationErrors;
7 import liquibase.ext.spatial.statement.CreateSpatialIndexStatement;
8 import liquibase.sql.Sql;
9 import liquibase.sql.UnparsedSql;
10 import liquibase.sqlgenerator.SqlGeneratorChain;
11 import liquibase.structure.core.Table;
12
13
14
15
16
17 public class CreateSpatialIndexGeneratorGeoDB extends AbstractCreateSpatialIndexGenerator {
18 @Override
19 public boolean supports(final CreateSpatialIndexStatement statement, final Database database) {
20 return database instanceof DerbyDatabase || database instanceof H2Database;
21 }
22
23
24
25
26 @Override
27 public ValidationErrors validate(final CreateSpatialIndexStatement statement,
28 final Database database, final SqlGeneratorChain sqlGeneratorChain) {
29 final ValidationErrors validationErrors = super.validate(statement, database,
30 sqlGeneratorChain);
31 validationErrors.checkRequiredField("srid", statement.getSrid());
32 return validationErrors;
33 }
34
35
36
37
38
39 @Override
40 public Sql[] generateSql(final CreateSpatialIndexStatement statement, final Database database,
41 final SqlGeneratorChain sqlGeneratorChain) {
42 final String catalogName = statement.getTableCatalogName();
43 String schemaName = statement.getTableSchemaName();
44 if (schemaName == null) {
45 schemaName = database.getDefaultSchemaName();
46 }
47 final StringBuilder sql = new StringBuilder("CALL ");
48 sql.append(schemaName).append(".CreateSpatialIndex(");
49
50
51 sql.append("'").append(schemaName).append("'");
52 sql.append(", ");
53
54
55 final String tableName = statement.getTableName();
56 sql.append("'").append(tableName).append("'");
57 sql.append(", ");
58
59
60 final String columnName = statement.getColumns()[0];
61 sql.append("'").append(columnName).append("'");
62 sql.append(", ");
63
64
65 final int srid = statement.getSrid();
66 sql.append("'").append(srid).append("'");
67 sql.append(')');
68 final Table hatboxTable = new Table().setName(database.correctObjectName(tableName
69 + "_HATBOX", Table.class));
70 hatboxTable.setSchema(catalogName, schemaName);
71 final UnparsedSql spatialize = new UnparsedSql(sql.toString(), hatboxTable);
72
73 return new Sql[] { spatialize };
74 }
75 }