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.MySQLDatabase;
8   import liquibase.ext.spatial.statement.CreateSpatialIndexStatement;
9   import liquibase.sql.Sql;
10  import liquibase.sql.UnparsedSql;
11  import liquibase.sqlgenerator.SqlGeneratorChain;
12  
13  /**
14   * <code>CreateSpatialIndexGeneratorMySQL</code> generates the SQL for creating a spatial index in
15   * MySQL.
16   */
17  public class CreateSpatialIndexGeneratorMySQL extends AbstractCreateSpatialIndexGenerator {
18     @Override
19     public boolean supports(final CreateSpatialIndexStatement statement, final Database database) {
20        return database instanceof MySQLDatabase;
21     }
22  
23     @Override
24     public Sql[] generateSql(final CreateSpatialIndexStatement statement, final Database database,
25           final SqlGeneratorChain sqlGeneratorChain) {
26        final StringBuilder sql = new StringBuilder();
27        sql.append("CREATE SPATIAL INDEX ");
28        final String indexSchema = statement.getTableSchemaName();
29        sql.append(database.escapeIndexName(statement.getTableCatalogName(), indexSchema,
30              statement.getIndexName()));
31        sql.append(" ON ");
32        sql.append(
33              database.escapeTableName(statement.getTableCatalogName(),
34                    statement.getTableSchemaName(), statement.getTableName())).append("(");
35        final Iterator<String> iterator = Arrays.asList(statement.getColumns()).iterator();
36        final String column = iterator.next();
37        sql.append(database.escapeColumnName(statement.getTableCatalogName(),
38              statement.getTableSchemaName(), statement.getTableName(), column));
39        sql.append(")");
40        final Sql createIndex = new UnparsedSql(sql.toString(), getAffectedIndex(statement));
41        return new Sql[] { createIndex };
42     }
43  }