Search in sources :

Example 11 with UnparsedSql

use of liquibase.sql.UnparsedSql 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 12 with UnparsedSql

use of liquibase.sql.UnparsedSql 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 13 with UnparsedSql

use of liquibase.sql.UnparsedSql 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 14 with UnparsedSql

use of liquibase.sql.UnparsedSql 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 15 with UnparsedSql

use of liquibase.sql.UnparsedSql 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

UnparsedSql (liquibase.sql.UnparsedSql)29 Sql (liquibase.sql.Sql)28 ArrayList (java.util.ArrayList)10 CatalogAndSchema (liquibase.CatalogAndSchema)8 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)3 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)3 ForeignKeyConstraint (liquibase.statement.ForeignKeyConstraint)3 Schema (liquibase.structure.core.Schema)3 LinkedList (java.util.LinkedList)2 AddColumnConfig (liquibase.change.AddColumnConfig)2 ColumnConfig (liquibase.change.ColumnConfig)2 DB2Database (liquibase.database.core.DB2Database)2 MySQLDatabase (liquibase.database.core.MySQLDatabase)2 DatabaseDataType (liquibase.datatype.DatabaseDataType)2 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)2 UniqueConstraint (liquibase.statement.UniqueConstraint)2 Table (liquibase.structure.core.Table)2 StringClauses (liquibase.util.StringClauses)2 BigInteger (java.math.BigInteger)1 InformixDatabase (liquibase.database.core.InformixDatabase)1