/**
  * Create foreign keys from table.
  *
  * @param table
  * @uses schema
  * @throws SQLException
  */
 @Override
 protected void buildForeignKeys(final DatabaseTable table) throws SQLException {
   final ResultSet resultSet =
       dbmd.getImportedKeys(null, getSchemaSpacePattern(), table.getTitle());
   while (resultSet.next()) {
     final ForeignKey foreign = new ForeignKey(resultSet.getString(12));
     foreign.setColumn(resultSet.getString(8));
     foreign.setReferToTable(resultSet.getString(3));
     foreign.setReferToColumn(resultSet.getString(4));
     schema.addForeignKey(foreign);
   }
   resultSet.close();
 }
  /**
   * Create primary keys from table.
   *
   * @param table
   * @uses schema
   * @throws SQLException
   */
  @Override
  protected void buildPrimaryKeys(final DatabaseTable table) throws SQLException {
    PrimaryKey primary = null;

    final ResultSet resultSet =
        dbmd.getPrimaryKeys(null, getSchemaSpacePattern(), table.getTitle());
    while (resultSet.next()) {
      if (primary == null) {
        primary = new PrimaryKey(resultSet.getString(6));
      }
      primary.addColumn(resultSet.getString(4));
    }
    resultSet.close();

    if (primary != null) {
      schema.addPrimaryKey(primary);
    }
  }
  /**
   * Create unique keys keys from table.
   *
   * @param table
   * @uses schema
   * @throws SQLException
   */
  @Override
  protected void buildUniqueKeys(final DatabaseTable table) throws SQLException {

    final List<UniqueKey> uniqueKeys = new ArrayList<UniqueKey>();
    final List<Index> indices = new ArrayList<Index>();

    // For each result, create a key if 9th value (name) is present and not already exists.
    final ResultSet resultSet =
        dbmd.getIndexInfo(null, getSchemaSpacePattern(), table.getTitle(), false, false);
    while (resultSet.next()) {
      if (resultSet.getString(9) != null) {
        if (resultSet.getBoolean(4)) {
          Index index = new Index(resultSet.getString(6));
          if (indices.contains(index)) {
            index = indices.get(indices.indexOf(index));
          } else {
            indices.add(index);
          }
          index.addColumn(resultSet.getString(9));
        } else {
          UniqueKey uniqueKey = new UniqueKey(resultSet.getString(6));
          if (uniqueKeys.contains(uniqueKey)) {
            // If already in the list, get that key instead of the
            // one
            // above.
            uniqueKey = uniqueKeys.get(uniqueKeys.indexOf(uniqueKey));
          } else {
            uniqueKeys.add(uniqueKey);
          }
          uniqueKey.addColumn(resultSet.getString(9));
        }
      }
    }
    resultSet.close();

    // Add them all to the schema.
    for (final UniqueKey uniqueKey : uniqueKeys) {
      schema.addUniqueKey(uniqueKey);
    }
    for (final Index index : indices) {
      schema.addIndex(index);
    }
  }