protected void getColumnsForUniqueConstraint( Connection jdbcConnection, UniqueConstraint constraint, String schema) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null; try { stmt = jdbcConnection.prepareStatement( "select ucc.column_name from all_cons_columns ucc where ucc.constraint_name=? and ucc.owner=? order by ucc.position"); stmt.setString(1, constraint.getName()); stmt.setString(2, schema); rs = stmt.executeQuery(); while (rs.next()) { String columnName = rs.getString("column_name"); constraint.getColumns().add(columnName); } } finally { if (rs != null) { try { rs.close(); } catch (SQLException ignored) { } } if (stmt != null) stmt.close(); } }
@Override protected void readUniqueConstraints( DatabaseSnapshot snapshot, String schema, DatabaseMetaData databaseMetaData) throws DatabaseException, SQLException { Database database = snapshot.getDatabase(); updateListeners("Reading unique constraints for " + database.toString() + " ..."); List<UniqueConstraint> foundUC = new ArrayList<UniqueConstraint>(); Connection jdbcConnection = ((JdbcConnection) database.getConnection()).getUnderlyingConnection(); PreparedStatement statement = null; ResultSet rs = null; // Setting default schema name. Needed for correct statement generation if (schema == null) schema = database.convertRequestedSchemaToSchema(schema); try { String query = "select uc.constraint_name,uc.table_name,uc.status,uc.deferrable,uc.deferred,ui.tablespace_name from all_constraints uc, all_cons_columns ucc, all_indexes ui where uc.constraint_type='U' and uc.index_name = ui.index_name and uc.constraint_name = ucc.constraint_name and uc.owner = '" + schema + "' and ui.table_owner = '" + schema + "' and ucc.owner = '" + schema + "'"; statement = jdbcConnection.prepareStatement(query); rs = statement.executeQuery(); while (rs.next()) { String constraintName = rs.getString("constraint_name"); String tableName = rs.getString("table_name"); String status = rs.getString("status"); String deferrable = rs.getString("deferrable"); String deferred = rs.getString("deferred"); String tablespace = rs.getString("tablespace_name"); UniqueConstraint constraintInformation = new UniqueConstraint(); constraintInformation.setName(constraintName); constraintInformation.setTablespace(tablespace); if (!database.isSystemTable(null, schema, tableName) && !database.isLiquibaseTable(tableName)) { Table table = snapshot.getTable(tableName); if (table == null) { continue; // probably different schema } constraintInformation.setTable(table); constraintInformation.setDisabled("DISABLED".equals(status)); if ("DEFERRABLE".equals(deferrable)) { constraintInformation.setDeferrable(true); constraintInformation.setInitiallyDeferred("DEFERRED".equals(deferred)); } getColumnsForUniqueConstraint(jdbcConnection, constraintInformation, schema); foundUC.add(constraintInformation); } } snapshot.getUniqueConstraints().addAll(foundUC); } finally { try { rs.close(); } catch (SQLException ignored) { } if (statement != null) { statement.close(); } } }
@Override protected void readIndexes( DatabaseSnapshot snapshot, String schema, DatabaseMetaData databaseMetaData) throws DatabaseException, SQLException { Database database = snapshot.getDatabase(); updateListeners("Reading indexes for " + database.toString() + " ..."); String query = "select aic.index_name, 3 AS TYPE, aic.table_name, aic.column_name, aic.column_position AS ORDINAL_POSITION, null AS FILTER_CONDITION, ai.tablespace_name AS TABLESPACE, ai.uniqueness FROM all_ind_columns aic, all_indexes ai WHERE aic.table_owner='" + database.convertRequestedSchemaToSchema(schema) + "' and aic.index_name = ai.index_name ORDER BY INDEX_NAME, ORDINAL_POSITION"; Statement statement = null; ResultSet rs = null; Map<String, Index> indexMap = null; try { statement = ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement(); rs = statement.executeQuery(query); indexMap = new HashMap<String, Index>(); while (rs.next()) { String indexName = convertFromDatabaseName(rs.getString("INDEX_NAME")); String tableName = rs.getString("TABLE_NAME"); String tableSpace = rs.getString("TABLESPACE"); String columnName = convertFromDatabaseName(rs.getString("COLUMN_NAME")); if (columnName == null) { // nothing to index, not sure why these come through sometimes continue; } short type = rs.getShort("TYPE"); boolean nonUnique; String uniqueness = rs.getString("UNIQUENESS"); if ("UNIQUE".equals(uniqueness)) { nonUnique = false; } else { nonUnique = true; } short position = rs.getShort("ORDINAL_POSITION"); String filterCondition = rs.getString("FILTER_CONDITION"); if (type == DatabaseMetaData.tableIndexStatistic) { continue; } Index index; if (indexMap.containsKey(indexName)) { index = indexMap.get(indexName); } else { index = new Index(); Table table = snapshot.getTable(tableName); if (table == null) { continue; // probably different schema } index.setTable(table); index.setTablespace(tableSpace); index.setName(indexName); index.setUnique(!nonUnique); index.setFilterCondition(filterCondition); indexMap.put(indexName, index); } for (int i = index.getColumns().size(); i < position; i++) { index.getColumns().add(null); } index.getColumns().set(position - 1, columnName); } } finally { JdbcUtils.closeResultSet(rs); JdbcUtils.closeStatement(statement); } for (Map.Entry<String, Index> entry : indexMap.entrySet()) { snapshot.getIndexes().add(entry.getValue()); } /* * marks indexes as "associated with" instead of "remove it" * Index should have associations with: * foreignKey, primaryKey or uniqueConstraint * */ for (Index index : snapshot.getIndexes()) { for (PrimaryKey pk : snapshot.getPrimaryKeys()) { if (index.getTable().getName().equalsIgnoreCase(pk.getTable().getName()) && index.getColumnNames().equals(pk.getColumnNames())) { index.addAssociatedWith(Index.MARK_PRIMARY_KEY); } } for (ForeignKey fk : snapshot.getForeignKeys()) { if (index.getTable().getName().equalsIgnoreCase(fk.getForeignKeyTable().getName()) && index.getColumnNames().equals(fk.getForeignKeyColumns())) { index.addAssociatedWith(Index.MARK_FOREIGN_KEY); } } for (UniqueConstraint uc : snapshot.getUniqueConstraints()) { if (index.getTable().getName().equalsIgnoreCase(uc.getTable().getName()) && index.getColumnNames().equals(uc.getColumnNames())) { index.addAssociatedWith(Index.MARK_UNIQUE_CONSTRAINT); } } } }