use of liquibase.sql.Sql in project liquibase by liquibase.
the class AddColumnGeneratorSQLite method generateSql.
@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
// SQLite does not support this ALTER TABLE operation until now.
// For more information see: http://www.sqlite.org/omitted.html.
// This is a small work around...
List<Sql> sql = new ArrayList<Sql>();
// define alter table logic
SQLiteDatabase.AlterTableVisitor rename_alter_visitor = new SQLiteDatabase.AlterTableVisitor() {
public ColumnConfig[] getColumnsToAdd() {
return new ColumnConfig[] { new ColumnConfig().setName(statement.getColumnName()).setType(statement.getColumnType()).setAutoIncrement(statement.isAutoIncrement()) };
}
public boolean copyThisColumn(ColumnConfig column) {
return !column.getName().equals(statement.getColumnName());
}
public boolean createThisColumn(ColumnConfig column) {
return true;
}
public boolean createThisIndex(Index index) {
return true;
}
};
try {
// alter table
List<SqlStatement> alterTableStatements = SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
sql.addAll(Arrays.asList(SqlGeneratorFactory.getInstance().generateSql(alterTableStatements.toArray(new SqlStatement[alterTableStatements.size()]), database)));
} catch (DatabaseException e) {
System.err.println(e);
e.printStackTrace();
}
return sql.toArray(new Sql[sql.size()]);
}
use of liquibase.sql.Sql 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.Sql in project liquibase by liquibase.
the class SqlGeneratorFactory method getAffectedDatabaseObjects.
public Set<DatabaseObject> getAffectedDatabaseObjects(SqlStatement statement, Database database) {
Set<DatabaseObject> affectedObjects = new HashSet<DatabaseObject>();
SqlGeneratorChain sqlGeneratorChain = createGeneratorChain(statement, database);
if (sqlGeneratorChain != null) {
//noinspection unchecked
Sql[] sqls = sqlGeneratorChain.generateSql(statement, database);
if (sqls != null) {
for (Sql sql : sqls) {
affectedObjects.addAll(sql.getAffectedDatabaseObjects());
}
}
}
return affectedObjects;
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class SqlGeneratorFactory method generateSql.
public Sql[] generateSql(SqlStatement[] statements, Database database) {
List<Sql> returnList = new ArrayList<Sql>();
SqlGeneratorFactory factory = SqlGeneratorFactory.getInstance();
for (SqlStatement statement : statements) {
Sql[] sqlArray = factory.generateSql(statement, database);
if (sqlArray != null && sqlArray.length > 0) {
List<Sql> sqlList = Arrays.asList(sqlArray);
returnList.addAll(sqlList);
}
}
return returnList.toArray(new Sql[returnList.size()]);
}
use of liquibase.sql.Sql 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