/**
   * 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;
  }
  /**
   * Returns the number of tables that are referenced by this table
   *
   * @return
   */
  public int getNumParents() {
    int numParents = 0;

    for (TableColumn column : columns.values()) {
      numParents += column.getParents().size();
    }

    return numParents;
  }
  /**
   * Returns <code>true</code> if this table references no other tables..
   *
   * <p>Used in dependency analysis.
   *
   * @return
   */
  public boolean isRoot() {
    for (TableColumn column : columns.values()) {
      if (column.isForeignKey()) {
        return false;
      }
    }

    return true;
  }
  /**
   * Returns <code>true</code> if this table is referenced by no other tables.
   *
   * <p>Used in dependency analysis.
   *
   * @return
   */
  public boolean isLeaf() {
    for (TableColumn column : columns.values()) {
      if (!column.getChildren().isEmpty()) {
        return false;
      }
    }

    return true;
  }
  /**
   * Returns the number of tables that reference this table
   *
   * @return
   */
  public int getNumChildren() {
    int numChildren = 0;

    for (TableColumn column : columns.values()) {
      numChildren += column.getChildren().size();
    }

    return numChildren;
  }
 /**
  * Return a self referencing constraint if one exists
  *
  * @return
  */
 private ForeignKeyConstraint getSelfReferencingConstraint() {
   for (TableColumn column : columns.values()) {
     for (TableColumn parentColumn : column.getParents()) {
       if (compareTo(parentColumn.getTable()) == 0) {
         return column.getParentConstraint(parentColumn);
       }
     }
   }
   return null;
 }
Example #7
0
 /**
  * Return a self referencing constraint if one exists
  *
  * @return
  */
 private ForeignKeyConstraint getSelfReferencingConstraint() {
   for (TableColumn column : columns.values()) {
     for (TableColumn parentColumn : column.getParents()) {
       if (parentColumn.getTable().getName().equals(getName())) {
         return column.getParentConstraint(parentColumn);
       }
     }
   }
   return null;
 }
  /**
   * Returns the number of non-implied children
   *
   * @return
   */
  public int getNumNonImpliedChildren() {
    int numChildren = 0;

    for (TableColumn column : columns.values()) {
      for (TableColumn childColumn : column.getChildren()) {
        if (!column.getChildConstraint(childColumn).isImplied()) ++numChildren;
      }
    }

    return numChildren;
  }
  /**
   * Returns the number of non-implied parents
   *
   * @return
   */
  public int getNumNonImpliedParents() {
    int numParents = 0;

    for (TableColumn column : columns.values()) {
      for (TableColumn parentColumn : column.getParents()) {
        if (!column.getParentConstraint(parentColumn).isImplied()) ++numParents;
      }
    }

    return numParents;
  }
  /**
   * @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);
    }
  }
  /**
   * Returns <code>true</code> if this table has no relationships
   *
   * @param withImpliedRelationships boolean
   * @return boolean
   */
  public boolean isOrphan(boolean withImpliedRelationships) {
    if (withImpliedRelationships) return getMaxParents() == 0 && getMaxChildren() == 0;

    for (TableColumn column : columns.values()) {
      for (TableColumn parentColumn : column.getParents()) {
        if (!column.getParentConstraint(parentColumn).isImplied()) return false;
      }
      for (TableColumn childColumn : column.getChildren()) {
        if (!column.getChildConstraint(childColumn).isImplied()) return false;
      }
    }
    return true;
  }
Example #12
0
  /**
   * Remove any non-real foreign keys
   *
   * @return
   */
  public List<ForeignKeyConstraint> removeNonRealForeignKeys() {
    List<ForeignKeyConstraint> nonReals = new ArrayList<ForeignKeyConstraint>();

    for (TableColumn column : columns.values()) {
      for (TableColumn parentColumn : column.getParents()) {
        ForeignKeyConstraint constraint = column.getParentConstraint(parentColumn);
        if (constraint != null && !constraint.isReal()) {
          nonReals.add(remove(constraint));
        }
      }
    }

    return nonReals;
  }
  /**
   * @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"));
  }
  /**
   * Remove any non-real foreign keys
   *
   * @return
   */
  public List<ForeignKeyConstraint> removeNonRealForeignKeys() {
    List<ForeignKeyConstraint> nonReals = new ArrayList<ForeignKeyConstraint>();

    for (TableColumn column : columns.values()) {
      for (TableColumn parentColumn : column.getParents()) {
        ForeignKeyConstraint constraint = column.getParentConstraint(parentColumn);
        if (constraint != null && !constraint.isReal()) {
          nonReals.add(constraint);
        }
      }
    }

    // remove constraints outside of above loop to prevent
    // concurrent modification exceptions while iterating
    for (ForeignKeyConstraint constraint : nonReals) {
      remove(constraint);
    }

    return nonReals;
  }
 /**
  * Returns the indexes that are applied to this table
  *
  * @return
  */
 public Set<TableIndex> getIndexes() {
   return new HashSet<TableIndex>(indexes.values());
 }
 /** "Unlink" all of the child tables from this table */
 public void unlinkChildren() {
   for (TableColumn column : columns.values()) {
     column.unlinkChildren();
   }
 }
 /** "Unlink" all of the parent tables from this table */
 public void unlinkParents() {
   for (TableColumn column : columns.values()) {
     column.unlinkParents();
   }
 }
 /**
  * Get the foreign keys associated with this table
  *
  * @return
  */
 public Collection<ForeignKeyConstraint> getForeignKeys() {
   return Collections.unmodifiableCollection(foreignKeys.values());
 }
  /**
   * @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");
    }
  }
 /**
  * Returns <code>List</code> of <code>TableColumn</code>s in ascending column number order.
  *
  * @return
  */
 public List<TableColumn> getColumns() {
   Set<TableColumn> sorted = new TreeSet<TableColumn>(new ByColumnIdComparator());
   sorted.addAll(columns.values());
   return new ArrayList<TableColumn>(sorted);
 }
Example #21
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");
    }
  }
 /**
  * @param indexName
  * @return
  */
 public TableIndex getIndex(String indexName) {
   return indexes.get(indexName);
 }
 /**
  * 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);
 }
Example #24
0
 public ForeignKeyConstraint getForeignKey(String keyName) {
   return foreignKeys.get(keyName);
 }