Search in sources :

Example 6 with AbstractJdbcDatabase

use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.

the class TableSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
    Database database = snapshot.getDatabase();
    String objectName = example.getName();
    Schema schema = example.getSchema();
    List<CachedRow> rs = null;
    try {
        JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
        rs = metaData.getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), objectName);
        Table table;
        if (!rs.isEmpty()) {
            table = readTable(rs.get(0), database);
        } else {
            return null;
        }
        return table;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) Table(liquibase.structure.core.Table) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 7 with AbstractJdbcDatabase

use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.

the class TableSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
        return;
    }
    if (foundObject instanceof Schema) {
        Database database = snapshot.getDatabase();
        Schema schema = (Schema) foundObject;
        List<CachedRow> tableMetaDataRs = null;
        try {
            tableMetaDataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache().getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
            for (CachedRow row : tableMetaDataRs) {
                String tableName = row.getString("TABLE_NAME");
                Table tableExample = (Table) new Table().setName(cleanNameFromDatabase(tableName, database)).setSchema(schema);
                schema.addDatabaseObject(tableExample);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) Table(liquibase.structure.core.Table) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 8 with AbstractJdbcDatabase

use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.

the class TableSnapshotGenerator method readTable.

protected Table readTable(CachedRow tableMetadataResultSet, Database database) throws SQLException, DatabaseException {
    String rawTableName = tableMetadataResultSet.getString("TABLE_NAME");
    String rawSchemaName = StringUtil.trimToNull(tableMetadataResultSet.getString("TABLE_SCHEM"));
    String rawCatalogName = StringUtil.trimToNull(tableMetadataResultSet.getString("TABLE_CAT"));
    String remarks = StringUtil.trimToNull(tableMetadataResultSet.getString("REMARKS"));
    String tablespace = StringUtil.trimToNull(tableMetadataResultSet.getString("TABLESPACE_NAME"));
    String defaultTablespaceString = StringUtil.trimToNull(tableMetadataResultSet.getString("DEFAULT_TABLESPACE"));
    if (remarks != null) {
        // come back escaped sometimes
        remarks = remarks.replace("''", "'");
    }
    Table table = new Table().setName(cleanNameFromDatabase(rawTableName, database));
    table.setRemarks(remarks);
    table.setTablespace(tablespace);
    table.setDefaultTablespace(BooleanUtil.isTrue(Boolean.parseBoolean(defaultTablespaceString)));
    CatalogAndSchema schemaFromJdbcInfo = ((AbstractJdbcDatabase) database).getSchemaFromJdbcInfo(rawCatalogName, rawSchemaName);
    table.setSchema(new Schema(schemaFromJdbcInfo.getCatalogName(), schemaFromJdbcInfo.getSchemaName()));
    if ("Y".equals(tableMetadataResultSet.getString("TEMPORARY"))) {
        table.setAttribute("temporary", "GLOBAL");
        String duration = tableMetadataResultSet.getString("DURATION");
        if ((duration != null) && "SYS$TRANSACTION".equals(duration)) {
            table.setAttribute("duration", "ON COMMIT DELETE ROWS");
        } else if ((duration != null) && "SYS$SESSION".equals(duration)) {
            table.setAttribute("duration", "ON COMMIT PRESERVE ROWS");
        }
    }
    return table;
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) CatalogAndSchema(liquibase.CatalogAndSchema) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase)

Example 9 with AbstractJdbcDatabase

use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.

the class ColumnSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException {
    if (!snapshot.getSnapshotControl().shouldInclude(Column.class)) {
        return;
    }
    if (foundObject instanceof Relation) {
        Database database = snapshot.getDatabase();
        Relation relation = (Relation) foundObject;
        List<CachedRow> allColumnsMetadataRs;
        try {
            JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData = ((JdbcDatabaseSnapshot) snapshot).getMetaDataFromCache();
            Schema schema;
            schema = relation.getSchema();
            allColumnsMetadataRs = databaseMetaData.getColumns(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), relation.getName(), null);
            List<CachedRow> metaDataNotNullConst = databaseMetaData.getNotNullConst(schema.getCatalogName(), schema.getName(), relation.getName());
            /*
                 * Microsoft SQL Server, SAP SQL Anywhere and probably other RDBMS guarantee non-duplicate
                 * ORDINAL_POSITIONs for the columns of a single table. But they do not guarantee there are no gaps
                 * in that integers (e.g. if columns have been deleted). So we need to check for that and renumber
                 * if needed.
                 */
            TreeMap<Integer, CachedRow> treeSet = new TreeMap<>();
            for (CachedRow row : allColumnsMetadataRs) {
                treeSet.put(row.getInt("ORDINAL_POSITION"), row);
            }
            Logger log = Scope.getCurrentScope().getLog(getClass());
            // Now we can iterate through the sorted list and repair if needed.
            int currentOrdinal = 0;
            for (CachedRow row : treeSet.values()) {
                currentOrdinal++;
                int rsOrdinal = row.getInt("ORDINAL_POSITION");
                if (rsOrdinal != currentOrdinal) {
                    log.fine(String.format("Repairing ORDINAL_POSITION with gaps for table=%s, column name=%s, " + "bad ordinal=%d, new ordinal=%d", relation.getName(), row.getString("COLUMN_NAME"), rsOrdinal, currentOrdinal));
                    row.set("ORDINAL_POSITION", currentOrdinal);
                }
            }
            // Iterate through all (repaired) rows and add the columns to our result.
            for (CachedRow row : allColumnsMetadataRs) {
                Column column = readColumn(row, relation, database);
                setAutoIncrementDetails(column, database, snapshot);
                populateValidateNullableIfNeeded(column, metaDataNotNullConst, database);
                column.setAttribute(LIQUIBASE_COMPLETE, !column.isNullable());
                relation.getColumns().add(column);
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) TreeMap(java.util.TreeMap) Logger(liquibase.logging.Logger) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) Database(liquibase.database.Database) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Example 10 with AbstractJdbcDatabase

use of liquibase.database.AbstractJdbcDatabase in project liquibase by liquibase.

the class CatalogSnapshotGenerator method getDatabaseCatalogNames.

protected String[] getDatabaseCatalogNames(Database database) throws SQLException, DatabaseException {
    List<String> returnList = new ArrayList<String>();
    ResultSet catalogs = null;
    try {
        if (((AbstractJdbcDatabase) database).jdbcCallsCatalogsSchemas()) {
            catalogs = ((JdbcConnection) database.getConnection()).getMetaData().getSchemas();
        } else {
            catalogs = ((JdbcConnection) database.getConnection()).getMetaData().getCatalogs();
        }
        while (catalogs.next()) {
            if (((AbstractJdbcDatabase) database).jdbcCallsCatalogsSchemas()) {
                returnList.add(catalogs.getString("TABLE_SCHEM"));
            } else {
                returnList.add(catalogs.getString("TABLE_CAT"));
            }
        }
    } finally {
        if (catalogs != null) {
            try {
                catalogs.close();
            } catch (SQLException ignore) {
            }
        }
    }
    return returnList.toArray(new String[returnList.size()]);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase)

Aggregations

AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)17 Database (liquibase.database.Database)12 DatabaseException (liquibase.exception.DatabaseException)12 CachedRow (liquibase.snapshot.CachedRow)12 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)12 SQLException (java.sql.SQLException)10 CatalogAndSchema (liquibase.CatalogAndSchema)9 OracleDatabase (liquibase.database.core.OracleDatabase)6 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 MySQLDatabase (liquibase.database.core.MySQLDatabase)5 InvalidExampleException (liquibase.snapshot.InvalidExampleException)5 Schema (liquibase.structure.core.Schema)5 ArrayList (java.util.ArrayList)4 MariaDBDatabase (liquibase.database.core.MariaDBDatabase)4 Table (liquibase.structure.core.Table)3 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 DB2Database (liquibase.database.core.DB2Database)2 Db2zDatabase (liquibase.database.core.Db2zDatabase)2 InformixDatabase (liquibase.database.core.InformixDatabase)2