Search in sources :

Example 16 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class AddColumnGenerator method generateMultipleColumns.

private Sql[] generateMultipleColumns(List<AddColumnStatement> columns, Database database) {
    List<Sql> result = new ArrayList<Sql>();
    if (database instanceof MySQLDatabase) {
        String alterTable = generateSingleColumBaseSQL(columns.get(0), database);
        for (int i = 0; i < columns.size(); i++) {
            alterTable += generateSingleColumnSQL(columns.get(i), database);
            if (i < columns.size() - 1) {
                alterTable += ",";
            }
        }
        result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
        for (AddColumnStatement statement : columns) {
            addUniqueConstrantStatements(statement, database, result);
            addForeignKeyStatements(statement, database, result);
        }
    } else {
        for (AddColumnStatement column : columns) {
            result.addAll(Arrays.asList(generateSingleColumn(column, database)));
        }
    }
    return result.toArray(new Sql[result.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) ColumnConstraint(liquibase.statement.ColumnConstraint) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 17 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class FindForeignKeyConstraintsGeneratorOracle method generateSql.

@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema baseTableSchema = new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()).customize(database);
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT ");
    sb.append("BASE.TABLE_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
    sb.append("BCOLS.COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
    sb.append("FRGN.TABLE_NAME ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
    sb.append("FCOLS.COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(", ");
    sb.append("BASE.CONSTRAINT_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
    sb.append("FROM ALL_CONSTRAINTS BASE,");
    sb.append("     ALL_CONSTRAINTS FRGN,");
    sb.append("     ALL_CONS_COLUMNS BCOLS,");
    sb.append("     ALL_CONS_COLUMNS FCOLS ");
    sb.append("WHERE BASE.R_OWNER = FRGN.OWNER ");
    sb.append("AND BASE.R_CONSTRAINT_NAME = FRGN.CONSTRAINT_NAME ");
    sb.append("AND BASE.OWNER = BCOLS.OWNER ");
    sb.append("AND BASE.CONSTRAINT_NAME = BCOLS.CONSTRAINT_NAME ");
    sb.append("AND FRGN.OWNER = FCOLS.OWNER ");
    sb.append("AND FRGN.CONSTRAINT_NAME = FCOLS.CONSTRAINT_NAME ");
    sb.append("AND BASE.TABLE_NAME =  '").append(statement.getBaseTableName().toUpperCase()).append("' ");
    sb.append("AND BASE.CONSTRAINT_TYPE = 'R' ");
    sb.append("AND BASE.OWNER = '").append(baseTableSchema.getSchemaName()).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)

Example 18 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class GetViewDefinitionGenerator method generateSql.

@Override
public Sql[] generateSql(GetViewDefinitionStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema schema = new CatalogAndSchema(statement.getCatalogName(), statement.getSchemaName()).customize(database);
    String sql;
    if (database instanceof MSSQLDatabase)
        sql = "select VIEW_DEFINITION from INFORMATION_SCHEMA.VIEWS where TABLE_NAME='" + database.correctObjectName(statement.getViewName(), View.class) + "'";
    else
        sql = "select view_definition from information_schema.views where table_name='" + database.correctObjectName(statement.getViewName(), View.class) + "'";
    if (database instanceof MySQLDatabase) {
        sql += " and table_schema='" + schema.getCatalogName() + "'";
    } else {
        if (database.supportsSchemas()) {
            String schemaName = schema.getSchemaName();
            if (schemaName != null) {
                if (database instanceof MSSQLDatabase)
                    sql += " and TABLE_SCHEMA='" + schemaName + "'";
                else
                    sql += " and table_schema='" + schemaName + "'";
            }
        }
        if (database.supportsCatalogs()) {
            String catalogName = schema.getCatalogName();
            if (catalogName != null) {
                if (database instanceof MSSQLDatabase)
                    sql += " and TABLE_CATALOG='" + catalogName + "'";
                else
                    sql += " and table_catalog='" + catalogName + "'";
            }
        }
    }
    return new Sql[] { new UnparsedSql(sql) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) MySQLDatabase(liquibase.database.core.MySQLDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) CatalogAndSchema(liquibase.CatalogAndSchema) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 19 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class GetViewDefinitionGeneratorMSSQL method generateSql.

@Override
public Sql[] generateSql(GetViewDefinitionStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema schema = new CatalogAndSchema(statement.getCatalogName(), statement.getSchemaName()).customize(database);
    boolean sql2005OrLater = true;
    try {
        sql2005OrLater = database.getDatabaseMajorVersion() >= 9;
    } catch (Exception ignored) {
    // Assume 2005 or later
    }
    String viewNameEscaped = database.escapeObjectName(schema.getCatalogName(), schema.getSchemaName(), statement.getViewName(), View.class);
    String sql;
    if (sql2005OrLater) {
        sql = "SELECT OBJECT_DEFINITION(OBJECT_ID(N'" + database.escapeStringForDatabase(viewNameEscaped) + "')) AS [ObjectDefinition]";
    } else {
        sql = "SELECT [c].[text] " + "FROM [dbo].[syscomments] AS [c] " + "WHERE [c].[id] = OBJECT_ID(N'" + database.escapeStringForDatabase(viewNameEscaped) + "') " + "ORDER BY [c].[colid]";
    }
    return new Sql[] { new UnparsedSql(sql) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) CatalogAndSchema(liquibase.CatalogAndSchema) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 20 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class GetViewDefinitionGeneratorSybase method generateSql.

@Override
public Sql[] generateSql(GetViewDefinitionStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema schema = new CatalogAndSchema(statement.getCatalogName(), statement.getSchemaName()).customize(database);
    String schemaName = schema.getSchemaName();
    if (schemaName == null) {
        schemaName = database.getDefaultSchemaName();
    }
    if (schemaName == null) {
        schemaName = "dbo";
    }
    String sql = "select text from syscomments where id = object_id('" + schemaName + "." + statement.getViewName() + "') order by colid";
    return new Sql[] { new UnparsedSql(sql) };
}
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