1 package liquibase.ext.spatial.sqlgenerator;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Map.Entry;
7 import java.util.TreeSet;
8
9 import liquibase.database.Database;
10 import liquibase.database.core.DerbyDatabase;
11 import liquibase.database.core.H2Database;
12 import liquibase.datatype.LiquibaseDataType;
13 import liquibase.exception.ValidationErrors;
14 import liquibase.ext.spatial.datatype.GeometryType;
15 import liquibase.sql.Sql;
16 import liquibase.sqlgenerator.SqlGenerator;
17 import liquibase.sqlgenerator.SqlGeneratorChain;
18 import liquibase.sqlgenerator.core.AbstractSqlGenerator;
19 import liquibase.sqlgenerator.core.CreateTableGenerator;
20 import liquibase.statement.core.AddColumnStatement;
21 import liquibase.statement.core.CreateTableStatement;
22
23
24
25
26
27
28 public class CreateSpatialTableGeneratorGeoDB extends AbstractSqlGenerator<CreateTableStatement> {
29
30
31
32
33 @Override
34 public boolean supports(final CreateTableStatement statement, final Database database) {
35 return database instanceof DerbyDatabase || database instanceof H2Database;
36 }
37
38
39
40
41 @Override
42 public int getPriority() {
43 return super.getPriority() + 1;
44 }
45
46 @Override
47 public ValidationErrors validate(final CreateTableStatement statement, final Database database,
48 final SqlGeneratorChain sqlGeneratorChain) {
49 final ValidationErrors validationErrors = new ValidationErrors();
50 for (final Entry<String, LiquibaseDataType> entry : statement.getColumnTypes().entrySet()) {
51 if (entry.getValue() instanceof GeometryType) {
52 final GeometryType geometryType = (GeometryType) entry.getValue();
53 if (geometryType.getSRID() == null) {
54 validationErrors.addError("The SRID parameter is required on the geometry type");
55 }
56 }
57 }
58 validationErrors.addAll(sqlGeneratorChain.validate(statement, database));
59 return validationErrors;
60 }
61
62 @Override
63 public Sql[] generateSql(final CreateTableStatement statement, final Database database,
64 final SqlGeneratorChain sqlGeneratorChain) {
65 final List<Sql> list = new ArrayList<Sql>(Arrays.asList(sqlGeneratorChain.generateSql(
66 statement, database)));
67 for (final Entry<String, LiquibaseDataType> entry : statement.getColumnTypes().entrySet()) {
68 if (entry.getValue() instanceof GeometryType) {
69 final String columnName = entry.getKey();
70 final GeometryType geometryType = (GeometryType) entry.getValue();
71 final AddGeometryColumnGeneratorGeoDB generator = new AddGeometryColumnGeneratorGeoDB();
72 final AddColumnStatement addColumnStatement = new AddColumnStatement(
73 statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(),
74 columnName, geometryType.toString(), null);
75 @SuppressWarnings("rawtypes")
76 final SqlGeneratorChain emptyChain = new SqlGeneratorChain(new TreeSet<SqlGenerator>());
77 final Sql[] addGeometryColumnSql = generator.generateSql(addColumnStatement, database,
78 emptyChain);
79 list.addAll(Arrays.asList(addGeometryColumnSql));
80 }
81 }
82 return list.toArray(new Sql[list.size()]);
83 }
84 }