@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);
        }
      }
    }
  }
  @Override
  protected void readPrimaryKeys(
      DatabaseSnapshot snapshot, String schema, DatabaseMetaData databaseMetaData)
      throws DatabaseException, SQLException {
    Database database = snapshot.getDatabase();
    updateListeners("Reading primary keys for " + database.toString() + " ...");

    // we can't add directly to the this.primaryKeys hashSet because adding columns to an exising PK
    // changes the hashCode and .contains() fails
    List<PrimaryKey> foundPKs = new ArrayList<PrimaryKey>();
    // Setting default schema name. Needed for correct statement generation
    if (schema == null) schema = database.convertRequestedSchemaToSchema(schema);

    String query =
        "select uc.table_name TABLE_NAME,ucc.column_name COLUMN_NAME,ucc.position KEY_SEQ,uc.constraint_name PK_NAME,ui.tablespace_name TABLESPACE from all_constraints uc,all_indexes ui,all_cons_columns ucc where uc.constraint_type = 'P' 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
            + "' and uc.table_name = ui.table_name and ui.table_name = ucc.table_name";
    Statement statement = null;
    ResultSet rs = null;
    try {
      statement =
          ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement();
      rs = statement.executeQuery(query);

      while (rs.next()) {
        String tableName = convertFromDatabaseName(rs.getString("TABLE_NAME"));
        String tablespace = convertFromDatabaseName(rs.getString("TABLESPACE"));
        String columnName = convertFromDatabaseName(rs.getString("COLUMN_NAME"));
        short position = rs.getShort("KEY_SEQ");

        boolean foundExistingPK = false;
        for (PrimaryKey pk : foundPKs) {
          if (pk.getTable().getName().equals(tableName)) {
            pk.addColumnName(position - 1, columnName);

            foundExistingPK = true;
          }
        }

        if (!foundExistingPK && !database.isLiquibaseTable(tableName)) {
          PrimaryKey primaryKey = new PrimaryKey();
          primaryKey.setTablespace(tablespace);
          Table table = snapshot.getTable(tableName);
          if (table == null) {
            continue; // probably a different schema
          }
          primaryKey.setTable(table);
          primaryKey.addColumnName(position - 1, columnName);
          primaryKey.setName(convertPrimaryKeyName(rs.getString("PK_NAME")));

          foundPKs.add(primaryKey);
        }
      }
    } finally {
      JdbcUtils.closeResultSet(rs);
      JdbcUtils.closeStatement(statement);
    }

    snapshot.getPrimaryKeys().addAll(foundPKs);
  }
  @Override
  public Change[] fixMissing(
      DatabaseObject missingObject,
      DiffOutputControl control,
      Database referenceDatabase,
      Database comparisonDatabase,
      ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();

    PrimaryKey pk = (PrimaryKey) missingObject;

    AddPrimaryKeyChange change = new AddPrimaryKeyChange();
    change.setTableName(pk.getTable().getName());
    if (control.getIncludeCatalog()) {
      change.setCatalogName(pk.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
      change.setSchemaName(pk.getTable().getSchema().getName());
    }
    change.setConstraintName(pk.getName());
    change.setColumnNames(pk.getColumnNames());
    if (control.getIncludeTablespace()) {
      change.setTablespace(pk.getTablespace());
    }

    if (referenceDatabase instanceof MSSQLDatabase
        && pk.getBackingIndex() != null
        && pk.getBackingIndex().getClustered() != null
        && !pk.getBackingIndex().getClustered()) {
      change.setClustered(false);
    }

    if (comparisonDatabase instanceof OracleDatabase
        || (comparisonDatabase instanceof DB2Database
            && pk.getBackingIndex() != null
            && !comparisonDatabase.isSystemObject(pk.getBackingIndex()))) {
      Index backingIndex = pk.getBackingIndex();
      if (backingIndex != null && backingIndex.getName() != null) {
        try {
          if (!control.getIncludeCatalog() && !control.getIncludeSchema()) {
            CatalogAndSchema schema =
                comparisonDatabase.getDefaultSchema().customize(comparisonDatabase);
            backingIndex
                .getTable()
                .setSchema(
                    schema.getCatalogName(),
                    schema
                        .getSchemaName()); // set table schema so it is found in the correct schema
          }
          if (referenceDatabase.equals(comparisonDatabase)
              || !SnapshotGeneratorFactory.getInstance().has(backingIndex, comparisonDatabase)) {
            Change[] fixes =
                ChangeGeneratorFactory.getInstance()
                    .fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);

            if (fixes != null) {
              for (Change fix : fixes) {
                if (fix != null) {
                  returnList.add(fix);
                }
              }
            }
          }
        } catch (Exception e) {
          throw new UnexpectedLiquibaseException(e);
        }

        change.setForIndexName(backingIndex.getName());
        Schema schema = backingIndex.getSchema();
        if (schema != null) {
          if (control.getIncludeCatalog()) {
            change.setForIndexCatalogName(schema.getCatalogName());
          }
          if (control.getIncludeSchema()) {
            change.setForIndexSchemaName(schema.getName());
          }
        }
      }
    }

    control.setAlreadyHandledMissing(pk.getBackingIndex());
    returnList.add(change);

    return returnList.toArray(new Change[returnList.size()]);
  }