Search in sources :

Example 56 with Sql

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());
            }
        }
    }
}
Also used : SetSequenceStatement(liquibase.ext.opennms.setsequence.SetSequenceStatement) SetSequenceGenerator(liquibase.ext.opennms.setsequence.SetSequenceGenerator) PostgresDatabase(liquibase.database.core.PostgresDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) Database(liquibase.database.Database) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 57 with Sql

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()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 58 with Sql

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;
}
Also used : SqlVisitor(liquibase.sql.visitor.SqlVisitor) Sql(liquibase.sql.Sql)

Example 59 with Sql

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()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 60 with Sql

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()) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) CatalogAndSchema(liquibase.CatalogAndSchema) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Aggregations

Sql (liquibase.sql.Sql)95 Test (org.junit.Test)55 Database (liquibase.database.Database)42 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)40 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)37 OracleDatabase (liquibase.database.core.OracleDatabase)36 PostgresDatabase (liquibase.database.core.PostgresDatabase)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DB2Database (liquibase.database.core.DB2Database)33 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)32 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)32 CreateTableStatement (liquibase.statement.core.CreateTableStatement)32 DerbyDatabase (liquibase.database.core.DerbyDatabase)31 H2Database (liquibase.database.core.H2Database)31 UnparsedSql (liquibase.sql.UnparsedSql)31 ArrayList (java.util.ArrayList)11 CatalogAndSchema (liquibase.CatalogAndSchema)8