Search in sources :

Example 21 with Database

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

the class PrimaryKeySnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    Database database = snapshot.getDatabase();
    Schema schema = example.getSchema();
    String searchTableName = null;
    if (((PrimaryKey) example).getTable() != null) {
        searchTableName = ((PrimaryKey) example).getTable().getName();
        searchTableName = database.correctObjectName(searchTableName, Table.class);
    }
    List<CachedRow> rs = null;
    try {
        JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaData();
        rs = metaData.getPrimaryKeys(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), searchTableName);
        PrimaryKey returnKey = null;
        for (CachedRow row : rs) {
            if (example.getName() != null && !example.getName().equalsIgnoreCase(row.getString("PK_NAME"))) {
                continue;
            }
            String columnName = cleanNameFromDatabase(row.getString("COLUMN_NAME"), database);
            short position = row.getShort("KEY_SEQ");
            if (returnKey == null) {
                returnKey = new PrimaryKey();
                CatalogAndSchema tableSchema = ((AbstractJdbcDatabase) database).getSchemaFromJdbcInfo(row.getString("TABLE_CAT"), row.getString("TABLE_SCHEM"));
                returnKey.setTable((Table) new Table().setName(row.getString("TABLE_NAME")).setSchema(new Schema(tableSchema.getCatalogName(), tableSchema.getSchemaName())));
                returnKey.setName(row.getString("PK_NAME"));
            }
            if (database instanceof SQLiteDatabase) {
                //SQLite is zero based position?
                position = (short) (position + 1);
            }
            String ascOrDesc = row.getString("ASC_OR_DESC");
            Boolean descending = "D".equals(ascOrDesc) ? Boolean.TRUE : "A".equals(ascOrDesc) ? Boolean.FALSE : null;
            returnKey.addColumn(position - 1, new Column(columnName).setDescending(descending).setRelation(((PrimaryKey) example).getTable()));
        }
        if (returnKey != null) {
            Index exampleIndex = new Index().setTable(returnKey.getTable());
            exampleIndex.setColumns(returnKey.getColumns());
            returnKey.setBackingIndex(exampleIndex);
        }
        return returnKey;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) CatalogAndSchema(liquibase.CatalogAndSchema) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Example 22 with Database

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

the class PrimaryKeySnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(PrimaryKey.class)) {
        return;
    }
    if (foundObject instanceof Table) {
        Table table = (Table) foundObject;
        Database database = snapshot.getDatabase();
        Schema schema = table.getSchema();
        List<CachedRow> rs = null;
        try {
            JdbcDatabaseSnapshot.CachingDatabaseMetaData metaData = ((JdbcDatabaseSnapshot) snapshot).getMetaData();
            rs = metaData.getPrimaryKeys(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), table.getName());
            if (rs.size() > 0) {
                PrimaryKey primaryKey = new PrimaryKey().setName(rs.get(0).getString("PK_NAME"));
                primaryKey.setTable((Table) foundObject);
                if (!database.isSystemObject(primaryKey)) {
                    table.setPrimaryKey(primaryKey.setTable(table));
                }
            }
        } catch (SQLException e) {
            throw new DatabaseException(e);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 23 with Database

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

the class SchemaSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    Database database = snapshot.getDatabase();
    Schema match = null;
    String catalogName = ((Schema) example).getCatalogName();
    String schemaName = example.getName();
    if (database.supportsSchemas()) {
        if (catalogName == null) {
            catalogName = database.getDefaultCatalogName();
        }
        if (schemaName == null) {
            schemaName = database.getDefaultSchemaName();
        }
    } else {
        if (database.supportsCatalogs()) {
            if (catalogName == null && schemaName != null) {
                catalogName = schemaName;
                schemaName = null;
            }
        } else {
            catalogName = null;
            schemaName = null;
        }
    }
    example = new Schema(catalogName, schemaName);
    // use LEGACY quoting since we're dealing with system objects
    ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
    try {
        if (database.supportsSchemas()) {
            for (String tableSchema : getDatabaseSchemaNames(database)) {
                CatalogAndSchema schemaFromJdbcInfo = toCatalogAndSchema(tableSchema, database);
                Catalog catalog = new Catalog(schemaFromJdbcInfo.getCatalogName());
                Schema schema = new Schema(catalog, tableSchema);
                if (DatabaseObjectComparatorFactory.getInstance().isSameObject(schema, example, snapshot.getSchemaComparisons(), database)) {
                    if (match == null) {
                        match = schema;
                    } else {
                        throw new InvalidExampleException("Found multiple catalog/schemas matching " + ((Schema) example).getCatalogName() + "." + example.getName());
                    }
                }
            }
        } else {
            Catalog catalog = new Catalog(catalogName);
            match = new Schema(catalog, catalogName);
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        database.setObjectQuotingStrategy(currentStrategy);
    }
    if (match != null && (match.getName() == null || match.getName().equalsIgnoreCase(database.getDefaultSchemaName()))) {
        match.setDefault(true);
    }
    return match;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) SQLException(java.sql.SQLException) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseException(liquibase.exception.DatabaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) Catalog(liquibase.structure.core.Catalog)

Example 24 with Database

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

the class SequenceSnapshotGenerator method addTo.

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
    if (!snapshot.getDatabase().supportsSequences()) {
        return;
    }
    if (foundObject instanceof Schema) {
        Schema schema = (Schema) foundObject;
        Database database = snapshot.getDatabase();
        if (!database.supportsSequences()) {
            updateListeners("Sequences not supported for " + database.toString() + " ...");
        }
        //noinspection unchecked
        List<Map<String, ?>> sequences = ExecutorService.getInstance().getExecutor(database).queryForList(new RawSqlStatement(getSelectSequenceSql(schema, database)));
        if (sequences != null) {
            for (Map<String, ?> sequence : sequences) {
                schema.addDatabaseObject(mapToSequence(sequence, (Schema) foundObject, database));
            }
        }
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Schema(liquibase.structure.core.Schema) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) Map(java.util.Map)

Example 25 with Database

use of liquibase.database.Database 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).getMetaData();
        rs = metaData.getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), objectName);
        Table table;
        if (rs.size() > 0) {
            table = readTable(rs.get(0), database);
        } else {
            return null;
        }
        if (table != null && database instanceof MSSQLDatabase) {
            String schemaName;
            Schema tableSchema = table.getSchema();
            if (tableSchema == null) {
                schemaName = database.getDefaultSchemaName();
            } else {
                schemaName = tableSchema.getName();
            }
            String sql;
            if (database.getDatabaseMajorVersion() >= 9) {
                sql = "SELECT" + " CAST(value as varchar(max)) as REMARKS" + " FROM" + " sys.extended_properties" + " WHERE" + " name='MS_Description'" + " AND major_id = OBJECT_ID('" + database.escapeStringForDatabase(database.escapeTableName(null, schemaName, table.getName())) + "')" + " AND" + " minor_id = 0";
            } else {
                sql = "SELECT CAST(value as varchar) as REMARKS FROM dbo.sysproperties WHERE name='MS_Description' AND id = OBJECT_ID('" + database.escapeStringForDatabase(database.escapeTableName(null, schemaName, table.getName())) + "') AND smallid = 0";
            }
            List<String> remarks = ExecutorService.getInstance().getExecutor(snapshot.getDatabase()).queryForList(new RawSqlStatement(sql), String.class);
            if (remarks != null && remarks.size() > 0) {
                table.setRemarks(StringUtils.trimToNull(remarks.iterator().next()));
            }
        }
        return table;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

Database (liquibase.database.Database)146 Test (org.junit.Test)74 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)50 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)43 Sql (liquibase.sql.Sql)42 DB2Database (liquibase.database.core.DB2Database)41 OracleDatabase (liquibase.database.core.OracleDatabase)39 PostgresDatabase (liquibase.database.core.PostgresDatabase)37 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DerbyDatabase (liquibase.database.core.DerbyDatabase)33 H2Database (liquibase.database.core.H2Database)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)33 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 CreateTableStatement (liquibase.statement.core.CreateTableStatement)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)30 DatabaseException (liquibase.exception.DatabaseException)25 AbstractJdbcDatabaseTest (liquibase.database.AbstractJdbcDatabaseTest)21 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)17