/**
  * Returns the {@link TableColumn} with the given name, or <code>null</code> if it doesn't exist
  *
  * @param columnName
  * @return
  */
 public TableColumn getColumn(String columnName) {
   return columns.get(columnName);
 }
  /**
   * @param rs ResultSet from {@link DatabaseMetaData#getImportedKeys(String, String, String)}
   *     rs.getString("FK_NAME"); rs.getString("FKCOLUMN_NAME"); rs.getString("PKTABLE_SCHEM");
   *     rs.getString("PKTABLE_NAME"); rs.getString("PKCOLUMN_NAME");
   * @param tables Map
   * @param db
   * @throws SQLException
   */
  protected void addForeignKey(
      String fkName,
      String fkColName,
      String pkTableSchema,
      String pkTableName,
      String pkColName,
      int updateRule,
      int deleteRule,
      Map<String, Table> tables,
      Pattern excludeIndirectColumns,
      Pattern excludeColumns)
      throws SQLException {
    if (fkName == null) return;

    ForeignKeyConstraint foreignKey = foreignKeys.get(fkName);

    if (foreignKey == null) {
      foreignKey = new ForeignKeyConstraint(this, fkName, updateRule, deleteRule);

      foreignKeys.put(fkName, foreignKey);
    }

    TableColumn childColumn = getColumn(fkColName);
    if (childColumn != null) {
      foreignKey.addChildColumn(childColumn);

      Table parentTable = tables.get(pkTableName);
      String parentSchema = pkTableSchema;
      String baseSchema = Config.getInstance().getSchema();

      // if named table doesn't exist in this schema
      // or exists here but really referencing same named table in another schema
      if (parentTable == null
          || (baseSchema != null && parentSchema != null && !baseSchema.equals(parentSchema))) {
        parentTable =
            db.addRemoteTable(
                parentSchema,
                pkTableName,
                baseSchema,
                properties,
                excludeIndirectColumns,
                excludeColumns);
      }

      if (parentTable != null) {
        TableColumn parentColumn = parentTable.getColumn(pkColName);
        if (parentColumn != null) {
          foreignKey.addParentColumn(parentColumn);

          childColumn.addParent(parentColumn, foreignKey);
          parentColumn.addChild(childColumn, foreignKey);
        } else {
          logger.warning(
              "Couldn't add FK '"
                  + foreignKey.getName()
                  + "' to table '"
                  + this
                  + "' - Column '"
                  + pkColName
                  + "' doesn't exist in table '"
                  + parentTable
                  + "'");
        }
      } else {
        logger.warning(
            "Couldn't add FK '"
                + foreignKey.getName()
                + "' to table '"
                + this
                + "' - Unknown Referenced Table '"
                + pkTableName
                + "'");
      }
    } else {
      logger.warning(
          "Couldn't add FK '"
              + foreignKey.getName()
              + "' to table '"
              + this
              + "' - Column '"
              + fkColName
              + "' doesn't exist");
    }
  }
 /**
  * @param indexName
  * @return
  */
 public TableIndex getIndex(String indexName) {
   return indexes.get(indexName);
 }
Esempio n. 4
0
 public ForeignKeyConstraint getForeignKey(String keyName) {
   return foreignKeys.get(keyName);
 }