use of liquibase.sql.Sql in project opennms by OpenNMS.
the class SetSequenceGeneratorTest method testBasicOperation.
@Test
public void testBasicOperation() {
for (final Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof PostgresDatabase) {
final SetSequenceStatement statement = new SetSequenceStatement("SEQUENCE_NAME");
statement.addTable("TABLE_NAME", "COLUMN1_NAME");
if (shouldBeImplementation(database)) {
final SqlGenerator<SetSequenceStatement> generator = this.generatorUnderTest;
final String tempTableName = ((SetSequenceGenerator) generator).getTempTableName();
final Sql[] sql = generator.generateSql(statement, database, null);
assertEquals("SELECT pg_catalog.setval('SEQUENCE_NAME',(SELECT max(" + tempTableName + ".id)+1 AS id FROM ((SELECT max(COLUMN1_NAME) AS id FROM TABLE_NAME LIMIT 1)) AS " + tempTableName + " LIMIT 1),true);", sql[0].toSql());
}
}
}
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class MockSqlGenerator method generateSql.
@Override
public Sql[] generateSql(SqlStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
List<Sql> sql = new ArrayList<Sql>();
for (String returnSql : this.returnSql) {
sql.add(new UnparsedSql(returnSql));
}
sql.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
return sql.toArray(new Sql[sql.size()]);
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class AbstractExecutor method applyVisitors.
protected String[] applyVisitors(SqlStatement statement, List<SqlVisitor> sqlVisitors) throws DatabaseException {
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database);
if (sql == null) {
return new String[0];
}
String[] returnSql = new String[sql.length];
for (int i = 0; i < sql.length; i++) {
if (sql[i] == null) {
continue;
}
returnSql[i] = sql[i].toSql();
if (sqlVisitors != null) {
for (SqlVisitor visitor : sqlVisitors) {
returnSql[i] = visitor.modifySql(returnSql[i], database);
}
}
}
return returnSql;
}
use of liquibase.sql.Sql 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.Sql 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()) };
}
Aggregations