예제 #1
0
  private void createForeignKeys(
      final MetadataResultSet results, final NamedObjectList<MutableForeignKey> foreignKeys)
      throws SQLException {
    try {
      while (results.next()) {
        String foreignKeyName = quotedName(results.getString("FK_NAME"));
        if (Utility.isBlank(foreignKeyName)) {
          foreignKeyName = UNKNOWN;
        }
        LOGGER.log(Level.FINER, "Retrieving foreign key: " + foreignKeyName);

        final String pkTableCatalogName = quotedName(results.getString("PKTABLE_CAT"));
        final String pkTableSchemaName = quotedName(results.getString("PKTABLE_SCHEM"));
        final String pkTableName = quotedName(results.getString("PKTABLE_NAME"));
        final String pkColumnName = quotedName(results.getString("PKCOLUMN_NAME"));

        final String fkTableCatalogName = quotedName(results.getString("FKTABLE_CAT"));
        final String fkTableSchemaName = quotedName(results.getString("FKTABLE_SCHEM"));
        final String fkTableName = quotedName(results.getString("FKTABLE_NAME"));
        final String fkColumnName = quotedName(results.getString("FKCOLUMN_NAME"));

        MutableForeignKey foreignKey = foreignKeys.lookup(foreignKeyName);
        if (foreignKey == null) {
          foreignKey = new MutableForeignKey(foreignKeyName);
          foreignKeys.add(foreignKey);
        }

        final int keySequence = results.getInt("KEY_SEQ", 0);
        final int updateRule = results.getInt("UPDATE_RULE", ForeignKeyUpdateRule.unknown.getId());
        final int deleteRule = results.getInt("DELETE_RULE", ForeignKeyUpdateRule.unknown.getId());
        final int deferrability =
            results.getInt("DEFERRABILITY", ForeignKeyDeferrability.unknown.getId());

        final MutableColumn pkColumn =
            lookupOrCreateColumn(pkTableCatalogName, pkTableSchemaName, pkTableName, pkColumnName);
        final MutableColumn fkColumn =
            lookupOrCreateColumn(fkTableCatalogName, fkTableSchemaName, fkTableName, fkColumnName);
        // Make a direct connection between the two columns
        if (pkColumn != null && fkColumn != null) {
          foreignKey.addColumnPair(keySequence, pkColumn, fkColumn);
          foreignKey.setUpdateRule(ForeignKeyUpdateRule.valueOf(updateRule));
          foreignKey.setDeleteRule(ForeignKeyUpdateRule.valueOf(deleteRule));
          foreignKey.setDeferrability(ForeignKeyDeferrability.valueOf(deferrability));
          foreignKey.addAttributes(results.getAttributes());

          fkColumn.setReferencedColumn(pkColumn);
          ((MutableTable) pkColumn.getParent()).addForeignKey(foreignKey);
          ((MutableTable) fkColumn.getParent()).addForeignKey(foreignKey);
        }
      }
    } finally {
      results.close();
    }
  }
  private void createForeignKeys(
      final MetadataResultSet results, final NamedObjectList<MutableForeignKey> foreignKeys)
      throws SQLException {
    try {
      while (results.next()) {
        String foreignKeyName = quotedName(results.getString("FK_NAME"));
        LOGGER.log(Level.FINER, "Retrieving foreign key: " + foreignKeyName);

        final String pkTableCatalogName = quotedName(results.getString("PKTABLE_CAT"));
        final String pkTableSchemaName = quotedName(results.getString("PKTABLE_SCHEM"));
        final String pkTableName = quotedName(results.getString("PKTABLE_NAME"));
        final String pkColumnName = quotedName(results.getString("PKCOLUMN_NAME"));

        final String fkTableCatalogName = quotedName(results.getString("FKTABLE_CAT"));
        final String fkTableSchemaName = quotedName(results.getString("FKTABLE_SCHEM"));
        final String fkTableName = quotedName(results.getString("FKTABLE_NAME"));
        final String fkColumnName = quotedName(results.getString("FKCOLUMN_NAME"));

        final int keySequence = results.getInt("KEY_SEQ", 0);
        final int updateRule = results.getInt("UPDATE_RULE", ForeignKeyUpdateRule.unknown.getId());
        final int deleteRule = results.getInt("DELETE_RULE", ForeignKeyUpdateRule.unknown.getId());
        final int deferrability =
            results.getInt("DEFERRABILITY", ForeignKeyDeferrability.unknown.getId());

        final Column pkColumn =
            lookupOrCreateColumn(pkTableCatalogName, pkTableSchemaName, pkTableName, pkColumnName);
        final Column fkColumn =
            lookupOrCreateColumn(fkTableCatalogName, fkTableSchemaName, fkTableName, fkColumnName);
        final boolean isPkColumnPartial = pkColumn instanceof ColumnPartial;
        final boolean isFkColumnPartial = fkColumn instanceof ColumnPartial;

        if (pkColumn == null || fkColumn == null || (isFkColumnPartial && isPkColumnPartial)) {
          continue;
        }

        if (Utility.isBlank(foreignKeyName)) {
          foreignKeyName = MetaDataUtility.constructForeignKeyName(pkColumn, fkColumn);
        }

        final Optional<MutableForeignKey> foreignKeyOptional = foreignKeys.lookup(foreignKeyName);
        final MutableForeignKey foreignKey;
        if (foreignKeyOptional.isPresent()) {
          foreignKey = foreignKeyOptional.get();
        } else {
          foreignKey = new MutableForeignKey(foreignKeyName);
          foreignKeys.add(foreignKey);
        }

        foreignKey.addColumnReference(keySequence, pkColumn, fkColumn);
        foreignKey.setUpdateRule(ForeignKeyUpdateRule.valueOf(updateRule));
        foreignKey.setDeleteRule(ForeignKeyUpdateRule.valueOf(deleteRule));
        foreignKey.setDeferrability(ForeignKeyDeferrability.valueOf(deferrability));
        foreignKey.addAttributes(results.getAttributes());

        if (fkColumn instanceof MutableColumn) {
          ((MutableColumn) fkColumn).setReferencedColumn(pkColumn);
          ((MutableTable) fkColumn.getParent()).addForeignKey(foreignKey);
        } else if (isFkColumnPartial) {
          ((ColumnPartial) fkColumn).setReferencedColumn(pkColumn);
          ((TablePartial) fkColumn.getParent()).addForeignKey(foreignKey);
        }

        if (pkColumn instanceof MutableColumn) {
          ((MutableTable) pkColumn.getParent()).addForeignKey(foreignKey);
        } else if (isPkColumnPartial) {
          ((TablePartial) pkColumn.getParent()).addForeignKey(foreignKey);
        }
      }
    } finally {
      results.close();
    }
  }