Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2_0_x' of github.com:liquibase/liquibase into 2_0_x
- Loading branch information
Showing
6 changed files
with
121 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
liquibase-core/src/main/java/liquibase/sqlgenerator/core/CreateIndexGeneratorPostgres.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package liquibase.sqlgenerator.core; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import liquibase.database.Database; | ||
import liquibase.database.core.DB2Database; | ||
import liquibase.database.core.InformixDatabase; | ||
import liquibase.database.core.MSSQLDatabase; | ||
import liquibase.database.core.PostgresDatabase; | ||
import liquibase.database.core.SybaseASADatabase; | ||
import liquibase.database.structure.Index; | ||
import liquibase.sql.Sql; | ||
import liquibase.sql.UnparsedSql; | ||
import liquibase.sqlgenerator.SqlGeneratorChain; | ||
import liquibase.statement.core.CreateIndexStatement; | ||
import liquibase.util.StringUtils; | ||
|
||
public class CreateIndexGeneratorPostgres extends CreateIndexGenerator { | ||
|
||
@Override | ||
public int getPriority() { | ||
return PRIORITY_DATABASE; | ||
} | ||
|
||
@Override | ||
public boolean supports(CreateIndexStatement statement, Database database) { | ||
return database instanceof PostgresDatabase; | ||
} | ||
|
||
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { | ||
|
||
// Default filter of index creation: | ||
// creation of all indexes with associations are switched off. | ||
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ","); | ||
if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || | ||
associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT) || | ||
associatedWith.contains(Index.MARK_FOREIGN_KEY))) { | ||
return new Sql[0]; | ||
} | ||
|
||
StringBuilder buffer = new StringBuilder(); | ||
|
||
buffer.append("CREATE "); | ||
if (statement.isUnique() != null && statement.isUnique()) { | ||
buffer.append("UNIQUE "); | ||
} | ||
buffer.append("INDEX "); | ||
|
||
if (statement.getIndexName() != null) { | ||
String indexSchema = statement.getTableSchemaName(); | ||
buffer.append(database.escapeIndexName(null, statement.getIndexName())).append(" "); | ||
} | ||
buffer.append("ON "); | ||
buffer.append(database.escapeTableName(statement.getTableSchemaName(), statement.getTableName())).append("("); | ||
Iterator<String> iterator = Arrays.asList(statement.getColumns()).iterator(); | ||
while (iterator.hasNext()) { | ||
String column = iterator.next(); | ||
buffer.append(database.escapeColumnName(statement.getTableSchemaName(), statement.getTableName(), column)); | ||
if (iterator.hasNext()) { | ||
buffer.append(", "); | ||
} | ||
} | ||
buffer.append(")"); | ||
|
||
if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) { | ||
if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) { | ||
buffer.append(" ON ").append(statement.getTablespace()); | ||
} else if (database instanceof DB2Database || database instanceof InformixDatabase) { | ||
buffer.append(" IN ").append(statement.getTablespace()); | ||
} else { | ||
buffer.append(" TABLESPACE ").append(statement.getTablespace()); | ||
} | ||
} | ||
|
||
return new Sql[]{new UnparsedSql(buffer.toString())}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters