Search in sources :

Example 11 with CatalogAndSchema

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

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

use of liquibase.CatalogAndSchema 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)

Example 14 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class FindForeignKeyConstraintsGeneratorDB2 method generateSql.

@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    StringBuilder sb = new StringBuilder();
    if (((DB2Database) database).getDataServerType() == DataServerType.DB2Z) {
        CatalogAndSchema baseTableSchema = new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()).customize(database);
        sb.append("SELECT PK.TBNAME  as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(",");
        sb.append("       PK.NAME    as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(",");
        sb.append("       FK.TBNAME  as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(",");
        sb.append("       FK.COLNAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(",");
        sb.append("       R.RELNAME  as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
        sb.append("  FROM SYSIBM.SYSRELS R,");
        sb.append("       SYSIBM.SYSFOREIGNKEYS FK,");
        sb.append("       SYSIBM.SYSCOLUMNS PK");
        sb.append(" WHERE R.CREATOR = '").append(baseTableSchema.getSchemaName()).append("'");
        sb.append("   AND R.TBNAME  = '").append(statement.getBaseTableName()).append("'");
        sb.append("   AND R.RELNAME = FK.RELNAME ");
        sb.append("   AND R.CREATOR=FK.CREATOR ");
        sb.append("   AND R.TBNAME=FK.TBNAME ");
        sb.append("   AND R.REFTBCREATOR=PK.TBCREATOR ");
        sb.append("   AND R.REFTBNAME=PK.TBNAME ");
        sb.append("   AND FK.COLSEQ=PK.KEYSEQ ");
        sb.append(" ORDER BY R.RELNAME, FK.COLSEQ asc ");
    } else {
        sb.append("SELECT ");
        sb.append("TABNAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
        sb.append("PK_COLNAMES as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
        sb.append("REFTABNAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
        sb.append("FK_COLNAMES as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(",");
        sb.append("CONSTNAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
        sb.append("FROM SYSCAT.REFERENCES ");
        sb.append("WHERE TABNAME='").append(statement.getBaseTableName()).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 15 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class FindForeignKeyConstraintsGeneratorDerby method generateSql.

@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    final CatalogAndSchema schema = database.correctSchema(new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()));
    final StringBuilder sb = new StringBuilder();
    sb.append("SELECT ");
    sb.append("co.constraintname AS ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(", ");
    sb.append("t.tablename AS ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
    sb.append("t2.tablename AS ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(" ");
    sb.append("FROM sys.sysconstraints co ");
    sb.append("JOIN sys.sysschemas sc ON co.schemaid = sc.schemaid ");
    sb.append("JOIN sys.systables t ON co.tableid = t.tableid ");
    sb.append("JOIN sys.sysforeignkeys f ON co.constraintid = f.constraintid ");
    sb.append("JOIN sys.sysconglomerates cg ON f.conglomerateid = cg.conglomerateid ");
    sb.append("JOIN sys.sysconstraints co2 ON f.keyconstraintid = co2.constraintid ");
    sb.append("JOIN sys.systables t2 ON co2.tableid = t2.tableid ");
    sb.append("JOIN sys.syskeys k ON co2.constraintid = k.constraintid ");
    sb.append("JOIN sys.sysconglomerates cg2 ON k.conglomerateid = cg2.conglomerateid ");
    sb.append("WHERE co.type = 'F' ");
    sb.append("AND sc.schemaname = '").append(schema.getCatalogName()).append("' ");
    sb.append("AND t.tablename = '").append(statement.getBaseTableName()).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

CatalogAndSchema (liquibase.CatalogAndSchema)36 Database (liquibase.database.Database)9 DatabaseException (liquibase.exception.DatabaseException)9 CompareControl (liquibase.diff.compare.CompareControl)8 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 Sql (liquibase.sql.Sql)7 UnparsedSql (liquibase.sql.UnparsedSql)7 Liquibase (liquibase.Liquibase)5 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)5 InvalidExampleException (liquibase.snapshot.InvalidExampleException)5 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 LiquibaseException (liquibase.exception.LiquibaseException)4 LockService (liquibase.lockservice.LockService)3 SnapshotControl (liquibase.snapshot.SnapshotControl)3 Schema (liquibase.structure.core.Schema)3 IOException (java.io.IOException)2