コード例 #1
0
  /**
   * 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);
    }
  }
コード例 #2
0
  /**
   * Create unique keys fromt table.
   *
   * @param view
   * @uses schema, uniques
   * @throws SQLException
   */
  protected void buildAllKeysOnlyForView(final DatabaseView view) throws SQLException {

    statementConstraints.setString(1, view.getTitle());
    statementConstraints.setString(2, getSchemaSpacePattern());
    final ResultSet resultSet = statementConstraints.executeQuery();

    final List<UniqueKey> uniqueKeys = new ArrayList<UniqueKey>();
    PrimaryKey primaryKey = null;

    // For each result, create a key if it is one.
    while (resultSet.next()) {
      if (resultSet.getString("COLUMN_NAME") != null) {
        if (resultSet.getString("COLUMN_KEY").equalsIgnoreCase("PRI")) {
          // If the type is PRI, it is a primary key.
          // It is possible, that a primary key is build by more than
          // one columns.
          if (primaryKey == null) {
            primaryKey = new PrimaryKey(resultSet.getString("CONSTRAINT_NAME"));
          }
          primaryKey.addColumn(resultSet.getString("COLUMN_NAME"));
          UniqueKey uniqueKey = new UniqueKey(resultSet.getString("CONSTRAINT_NAME"));
          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("COLUMN_NAME"));
        } else if (resultSet.getString("COLUMN_KEY").equalsIgnoreCase("UNI")) {
          // If the type is a UNI, it is a unique key.
          UniqueKey uniqueKey = new UniqueKey(resultSet.getString("CONSTRAINT_NAME"));
          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("COLUMN_NAME"));
          // } else if (resultSet.getString(3).toUpperCase().equals("R")) {
          // // If the type is a r (referred), it is a foreign key.
          // final ForeignKey foreignKey = new ForeignKey(resultSet.getString(2));
          // foreignKey.setColumn(resultSet.getString(1));
          // foreignKey.setReferToTable(resultSet.getString(5));
          // foreignKey.setReferToColumn(resultSet.getString(4));
          // schema.addForeignKey(foreignKey);
        }
      }
    }

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

    // If the primary key to the schema if available.
    if (primaryKey != null) {
      schema.addPrimaryKey(primaryKey);
    }

    resultSet.close();
  }