use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class CreateIndexGenerator method generateSql.
@Override
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
if (database instanceof OracleDatabase) {
// Oracle don't create index when creates foreignKey
// It means that all indexes associated with foreignKey should be created manualy
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT))) {
return new Sql[0];
}
} else {
// 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];
}
}
StringBuffer buffer = new StringBuffer();
buffer.append("CREATE ");
if (statement.isUnique() != null && statement.isUnique()) {
buffer.append("UNIQUE ");
}
if (database instanceof MSSQLDatabase) {
if (statement.isClustered() != null) {
if (statement.isClustered()) {
buffer.append("CLUSTERED ");
} else {
buffer.append("NONCLUSTERED ");
}
}
}
buffer.append("INDEX ");
if (statement.getIndexName() != null) {
String indexSchema = statement.getTableSchemaName();
buffer.append(database.escapeIndexName(statement.getTableCatalogName(), indexSchema, statement.getIndexName())).append(" ");
}
buffer.append("ON ");
if (database instanceof OracleDatabase && statement.isClustered() != null && statement.isClustered()) {
buffer.append("CLUSTER ");
}
buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
while (iterator.hasNext()) {
AddColumnConfig column = iterator.next();
if (column.getComputed() == null) {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
} else {
if (column.getComputed()) {
buffer.append(column.getName());
} else {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
}
}
if (column.getDescending() != null && column.getDescending()) {
buffer.append(" DESC");
}
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());
}
}
if (database instanceof DB2Database && statement.isClustered() != null && statement.isClustered()) {
buffer.append(" CLUSTER");
}
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class CreateIndexGeneratorPostgres method generateSql.
@Override
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) {
// for postgres setting the schema name for the index name is invalid
buffer.append(database.escapeObjectName(statement.getIndexName(), Index.class)).append(" ");
}
buffer.append("ON ");
buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
while (iterator.hasNext()) {
AddColumnConfig column = iterator.next();
if (column.getComputed() == null) {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
} else {
if (column.getComputed()) {
buffer.append(column.getName());
} else {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
}
}
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());
}
}
if (statement.isClustered() != null && statement.isClustered()) {
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)), new UnparsedSql("CLUSTER " + database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName()) + " USING " + database.escapeObjectName(statement.getIndexName(), Index.class)) };
} else {
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
}
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class CreateProcedureGenerator method surroundWithSchemaSets.
/**
* Convenience method for when the schemaName is set but we don't want to parse the body
*/
public static void surroundWithSchemaSets(List<Sql> sql, String schemaName, Database database) {
if ((StringUtils.trimToNull(schemaName) != null) && !LiquibaseConfiguration.getInstance().getProperty(ChangeLogParserCofiguration.class, ChangeLogParserCofiguration.USE_PROCEDURE_SCHEMA).getValue(Boolean.class)) {
String defaultSchema = database.getDefaultSchemaName();
if (database instanceof OracleDatabase) {
sql.add(0, new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(schemaName, Schema.class)));
sql.add(new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(defaultSchema, Schema.class)));
} else if (database instanceof DB2Database) {
sql.add(0, new UnparsedSql("SET CURRENT SCHEMA " + schemaName));
sql.add(new UnparsedSql("SET CURRENT SCHEMA " + defaultSchema));
}
}
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class AddDefaultValueGeneratorPostgres method generateSql.
@Override
public Sql[] generateSql(final AddDefaultValueStatement statement, final Database database, final SqlGeneratorChain sqlGeneratorChain) {
if (!(statement.getDefaultValue() instanceof SequenceNextValueFunction)) {
return super.generateSql(statement, database, sqlGeneratorChain);
}
List<Sql> commands = new ArrayList<Sql>(Arrays.asList(super.generateSql(statement, database, sqlGeneratorChain)));
// for postgres, we need to also set the sequence to be owned by this table for true serial like functionality.
// this will allow a drop table cascade to remove the sequence as well.
SequenceNextValueFunction sequenceFunction = (SequenceNextValueFunction) statement.getDefaultValue();
String sequenceName = sequenceFunction.getValue();
String sequenceSchemaName = sequenceFunction.getSequenceSchemaName();
String sequence = database.escapeObjectName(null, sequenceSchemaName, sequenceName, Sequence.class);
Sql alterSequenceOwner = new UnparsedSql("ALTER SEQUENCE " + sequence + " OWNED BY " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + "." + database.escapeObjectName(statement.getColumnName(), Column.class), getAffectedColumn(statement), getAffectedSequence(sequenceFunction));
commands.add(alterSequenceOwner);
return commands.toArray(new Sql[commands.size()]);
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class AddAutoIncrementGeneratorMySQL method generateSql.
@Override
public Sql[] generateSql(final AddAutoIncrementStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
Sql[] sql = super.generateSql(statement, database, sqlGeneratorChain);
if (statement.getStartWith() != null) {
MySQLDatabase mysqlDatabase = (MySQLDatabase) database;
String alterTableSql = "ALTER TABLE " + mysqlDatabase.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " " + mysqlDatabase.getTableOptionAutoIncrementStartWithClause(statement.getStartWith());
sql = concact(sql, new UnparsedSql(alterTableSql, getAffectedTable(statement)));
}
return sql;
}
Aggregations