/**
   * Add a column that's defined in xml metadata. Assumes that a column named colMeta.getName()
   * doesn't already exist in <code>columns</code>.
   *
   * @param colMeta
   * @return
   */
  protected TableColumn addColumn(TableColumnMeta colMeta) {
    TableColumn column = new TableColumn(this, colMeta);

    columns.put(column.getName(), column);

    return column;
  }
Esempio n. 2
0
  @Override
  public void putAll(Map<? extends String, ? extends V> map) {
    Iterator<? extends Map.Entry<? extends String, ? extends V>> iter = map.entrySet().iterator();

    while (iter.hasNext()) {
      Map.Entry<? extends String, ? extends V> e = iter.next();
      put(e.getKey(), e.getValue());
    }
  }
  /**
   * @param rs - from {@link DatabaseMetaData#getColumns(String, String, String, String)}
   * @param excludeIndirectColumns
   * @param excludeColumns
   * @throws SQLException
   */
  protected void addColumn(ResultSet rs, Pattern excludeIndirectColumns, Pattern excludeColumns)
      throws SQLException {
    String columnName = rs.getString("COLUMN_NAME");

    if (columnName == null) return;

    if (getColumn(columnName) == null) {
      TableColumn column = new TableColumn(this, rs, excludeIndirectColumns, excludeColumns);

      columns.put(column.getName(), column);
    }
  }
  /**
   * @param rs
   * @throws SQLException
   */
  private void addIndex(ResultSet rs) throws SQLException {
    String indexName = rs.getString("INDEX_NAME");

    if (indexName == null) return;

    TableIndex index = getIndex(indexName);

    if (index == null) {
      index = new TableIndex(rs);

      indexes.put(index.getName(), index);
    }

    index.addColumn(getColumn(rs.getString("COLUMN_NAME")), rs.getString("ASC_OR_DESC"));
  }
  /**
   * @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");
    }
  }
Esempio n. 6
0
  /**
   * @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,
      Map<String, Table> tables,
      Database db,
      Properties properties,
      Pattern excludeIndirectColumns,
      Pattern excludeColumns)
      throws SQLException {
    if (fkName == null) return;

    ForeignKeyConstraint foreignKey = getForeignKey(fkName);

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

      foreignKeys.put(foreignKey.getName(), foreignKey);
    }

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

      Table parentTable = tables.get(pkTableName);
      if (parentTable == null) {
        String otherSchema = pkTableSchema;
        if (otherSchema != null
            && !otherSchema.equals(getSchema())
            && Config.getInstance().isOneOfMultipleSchemas()) {
          parentTable =
              db.addRemoteTable(
                  otherSchema,
                  pkTableName,
                  getSchema(),
                  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 {
          System.err.println(
              "Couldn't add FK '"
                  + foreignKey.getName()
                  + "' to table '"
                  + this
                  + "' - Column '"
                  + pkColName
                  + "' doesn't exist in table '"
                  + parentTable
                  + "'");
        }
      } else {
        System.err.println(
            "Couldn't add FK '"
                + foreignKey.getName()
                + "' to table '"
                + this
                + "' - Unknown Referenced Table '"
                + pkTableName
                + "'");
      }
    } else {
      System.err.println(
          "Couldn't add FK '"
              + foreignKey.getName()
              + "' to table '"
              + this
              + "' - Column '"
              + fkColName
              + "' doesn't exist");
    }
  }