View Javadoc

1   package liquibase.ext.spatial.sqlgenerator;
2   
3   import java.util.Arrays;
4   import java.util.Iterator;
5   
6   import liquibase.database.Database;
7   import liquibase.database.core.PostgresDatabase;
8   import liquibase.ext.spatial.statement.CreateSpatialIndexStatement;
9   import liquibase.sql.Sql;
10  import liquibase.sql.UnparsedSql;
11  import liquibase.sqlgenerator.SqlGeneratorChain;
12  import liquibase.structure.core.Index;
13  
14  /**
15   * <code>CreateSpatialIndexGeneratorMySQL</code> generates the SQL for creating a spatial index in
16   * MySQL.
17   */
18  public class CreateSpatialIndexGeneratorPostgreSQL extends AbstractCreateSpatialIndexGenerator {
19     @Override
20     public boolean supports(final CreateSpatialIndexStatement statement, final Database database) {
21        return database instanceof PostgresDatabase;
22     }
23  
24     @Override
25     public Sql[] generateSql(final CreateSpatialIndexStatement statement, final Database database,
26           final SqlGeneratorChain sqlGeneratorChain) {
27        final StringBuilder sql = new StringBuilder();
28        sql.append("CREATE INDEX ");
29        sql.append(database.escapeObjectName(statement.getIndexName(), Index.class));
30        sql.append(" ON ");
31        sql.append(database.escapeTableName(statement.getTableCatalogName(),
32              statement.getTableSchemaName(), statement.getTableName()));
33        sql.append(" USING GIST ").append("(");
34        final Iterator<String> iterator = Arrays.asList(statement.getColumns()).iterator();
35        while (iterator.hasNext()) {
36           final String column = iterator.next();
37           sql.append(database.escapeColumnName(statement.getTableCatalogName(),
38                 statement.getTableSchemaName(), statement.getTableName(), column));
39           if (iterator.hasNext()) {
40              sql.append(", ");
41           }
42        }
43        sql.append(")");
44        final Sql createIndex = new UnparsedSql(sql.toString(), getAffectedIndex(statement));
45        return new Sql[] { createIndex };
46     }
47  }