liquibase.sqlgenerator.ext.AddPrimaryKeyGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kualigan-lb-extensions Show documentation
Show all versions of kualigan-lb-extensions Show documentation
Extensions to liquibase that make it more database agnostic
package liquibase.sqlgenerator.ext;
import liquibase.database.Database;
import liquibase.database.core.*;
import liquibase.exception.ValidationErrors;
import liquibase.sql.Sql;
import liquibase.sql.UnparsedSql;
import liquibase.sqlgenerator.SqlGeneratorChain;
import liquibase.statement.core.AddPrimaryKeyStatement;
import liquibase.util.StringUtils;
import static liquibase.ext.Constants.EXTENSION_PRIORITY;
public class AddPrimaryKeyGenerator extends liquibase.sqlgenerator.core.AddPrimaryKeyGenerator {
@Override
public int getPriority() {
return EXTENSION_PRIORITY;
}
public Sql[] generateSql(AddPrimaryKeyStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String sql;
if (statement.getConstraintName() == null || database instanceof MySQLDatabase || database instanceof SybaseASADatabase) {
sql = "ALTER TABLE " + database.escapeTableName(null, statement.getTableName()) + " ADD PRIMARY KEY (" + database.escapeColumnNameList(statement.getColumnNames()) + ")";
} else {
sql = "ALTER TABLE " + database.escapeTableName(null, statement.getTableName()) + " ADD CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName()) + " PRIMARY KEY (" + database.escapeColumnNameList(statement.getColumnNames()) + ")";
}
if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
if (database instanceof MSSQLDatabase) {
sql += " ON "+statement.getTablespace();
} else if (database instanceof DB2Database || database instanceof SybaseASADatabase) {
; //not supported
} else {
sql += " USING INDEX TABLESPACE "+statement.getTablespace();
}
}
return new Sql[] {
new UnparsedSql(sql)
};
}
}