use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class SetNullableGenerator method generateSql.
@Override
public Sql[] generateSql(SetNullableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String sql;
String nullableString;
if (statement.isNullable()) {
nullableString = " NULL";
} else {
nullableString = " NOT NULL";
}
if (database instanceof OracleDatabase && statement.getConstraintName() != null) {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " CONSTRAINT " + statement.getConstraintName() + nullableString;
} else if (database instanceof OracleDatabase || database instanceof SybaseDatabase || database instanceof SybaseASADatabase) {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + nullableString;
} else if (database instanceof MSSQLDatabase) {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database).toDatabaseDataType(database) + nullableString;
} else if (database instanceof MySQLDatabase) {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database).toDatabaseDataType(database) + nullableString;
} else if (database instanceof DerbyDatabase) {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + nullableString;
} else if (database instanceof HsqlDatabase || database instanceof H2Database) {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " SET" + nullableString;
} else if (database instanceof InformixDatabase) {
// Informix simply omits the null for nullables
if (statement.isNullable()) {
nullableString = "";
}
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " MODIFY (" + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " " + DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database).toDatabaseDataType(database) + nullableString + ")";
} else {
sql = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()) + " ALTER COLUMN " + database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + (statement.isNullable() ? " DROP NOT NULL" : " SET NOT NULL");
}
List<Sql> returnList = new ArrayList<Sql>();
returnList.add(new UnparsedSql(sql, getAffectedColumn(statement)));
if (database instanceof DB2Database) {
Sql[] a = SqlGeneratorFactory.getInstance().generateSql(new ReorganizeTableStatement(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName()), database);
if (a != null) {
returnList.addAll(Arrays.asList(a));
}
}
return returnList.toArray(new Sql[returnList.size()]);
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class FindForeignKeyConstraintsGeneratorMySQL method generateSql.
@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
CatalogAndSchema schema = new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()).customize(database);
StringBuilder sb = new StringBuilder();
sb.append("SELECT ");
sb.append("RC.TABLE_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
sb.append("KCU.COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
sb.append("RC.REFERENCED_TABLE_NAME ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
sb.append("KCU.REFERENCED_COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(", ");
sb.append("RC.CONSTRAINT_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
sb.append("FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,");
sb.append(" INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ");
sb.append("WHERE RC.TABLE_NAME = KCU.TABLE_NAME ");
sb.append("AND RC.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA ");
sb.append("AND RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME ");
sb.append("AND RC.TABLE_NAME = '").append(statement.getBaseTableName()).append("' ");
sb.append("AND RC.CONSTRAINT_SCHEMA = '").append(schema.getCatalogName()).append("'");
sb.append("AND KCU.TABLE_SCHEMA = '").append(schema.getCatalogName()).append("'");
return new Sql[] { new UnparsedSql(sb.toString()) };
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class DropIndexGenerator method generateSql.
@Override
public Sql[] generateSql(DropIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
if (associatedWith != null) {
if (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT)) {
return new Sql[0];
} else if (associatedWith.contains(Index.MARK_FOREIGN_KEY)) {
if (!(database instanceof OracleDatabase || database instanceof MSSQLDatabase)) {
return new Sql[0];
}
}
}
String schemaName = statement.getTableSchemaName();
if (database instanceof MySQLDatabase) {
return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(null, null, statement.getIndexName()) + " ON " + database.escapeTableName(statement.getTableCatalogName(), schemaName, statement.getTableName()), getAffectedIndex(statement)) };
} else if (database instanceof MSSQLDatabase) {
return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(null, null, statement.getIndexName()) + " ON " + database.escapeTableName(null, schemaName, statement.getTableName()), getAffectedIndex(statement)) };
} else if (database instanceof SybaseDatabase) {
return new Sql[] { new UnparsedSql("DROP INDEX " + statement.getTableName() + "." + statement.getIndexName(), getAffectedIndex(statement)) };
} else if (database instanceof PostgresDatabase) {
return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(statement.getTableCatalogName(), schemaName, statement.getIndexName()), getAffectedIndex(statement)) };
}
return new Sql[] { new UnparsedSql("DROP INDEX " + database.escapeIndexName(statement.getTableCatalogName(), schemaName, statement.getIndexName()), getAffectedIndex(statement)) };
}
use of liquibase.sql.UnparsedSql in project liquibase by liquibase.
the class ModifyDataTypeGenerator method generateSql.
@Override
public Sql[] generateSql(ModifyDataTypeStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
String alterTable = "ALTER TABLE " + database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
// add "MODIFY"
alterTable += " " + getModifyString(database) + " ";
// add column name
String columnName = database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), statement.getColumnName());
alterTable += columnName;
// adds a space if nothing else
alterTable += getPreDataTypeString(database);
// add column type
DatabaseDataType newDataType = DataTypeFactory.getInstance().fromDescription(statement.getNewDataType(), database).toDatabaseDataType(database);
alterTable += newDataType;
if (database instanceof PostgresDatabase) {
alterTable += " USING (" + columnName + "::" + newDataType + ")";
}
return new Sql[] { new UnparsedSql(alterTable, getAffectedTable(statement)) };
}
Aggregations