コード例 #1
0
  /**
   * Compare this table to another table. Results are based on 1: identity, 2: table name, 3: schema
   * name
   *
   * <p>This implementation was put in place to deal with analyzing multiple schemas that contain
   * identically named tables.
   *
   * @see {@link Comparable#compareTo(Object)}
   */
  public int compareTo(Table other) {
    if (other == this) // fast way out
    return 0;

    int rc = getName().compareToIgnoreCase(other.getName());
    if (rc == 0) {
      // should only get here if we're dealing with cross-schema references (rare)
      String ours = getSchema();
      String theirs = other.getSchema();
      if (ours != null && theirs != null) rc = ours.compareToIgnoreCase(theirs);
      else if (ours == null) rc = -1;
      else rc = 1;
    }

    return rc;
  }
コード例 #2
0
  private void writeLineItem(Table table, boolean showIds, LineWriter html) throws IOException {
    html.write(" <tr class='" + (table.isView() ? "view" : "tbl") + "' valign='top'>");
    html.write("  <td class='detail'><a href='tables/");
    html.write(encodeHref(table.getName()));
    html.write(".html'>");
    html.write(table.getName());
    html.writeln("</a></td>");

    if (showIds) {
      html.write("  <td class='detail' align='right'>");
      Object id = table.getId();
      if (id != null) html.write(String.valueOf(id));
      else html.writeln("&nbsp;");
      html.writeln("</td>");
    }

    html.write("  <td class='detail' align='right'>");
    int numRelatives = table.getNumNonImpliedChildren();
    if (numRelatives != 0) html.write(String.valueOf(integerFormatter.format(numRelatives)));
    html.writeln("</td>");
    html.write("  <td class='detail' align='right'>");
    numRelatives = table.getNumNonImpliedParents();
    if (numRelatives != 0) html.write(String.valueOf(integerFormatter.format(numRelatives)));
    html.writeln("</td>");

    html.write("  <td class='detail' align='right'>");
    html.write(String.valueOf(integerFormatter.format(table.getColumns().size())));
    html.writeln("</td>");

    if (displayNumRows) {
      html.write("  <td class='detail' align='right'>");
      if (!table.isView()) html.write(String.valueOf(integerFormatter.format(table.getNumRows())));
      else html.write("<span title='Views contain no real rows'>view</span>");
      html.writeln("</td>");
    }
    html.write("  <td class='comment detail'>");
    String comments = table.getComments();
    if (comments != null) {
      if (encodeComments)
        for (int i = 0; i < comments.length(); ++i)
          html.write(HtmlEncoder.encodeToken(comments.charAt(i)));
      else html.write(comments);
    }
    html.writeln("</td>");
    html.writeln("  </tr>");
  }
コード例 #3
0
  /**
   * Same as {@link #connectForeignKeys(Map, Database, Properties, Pattern, Pattern)}, but uses
   * XML-based metadata
   *
   * @param tableMeta
   * @param tables
   * @param remoteTables
   */
  public void connect(
      TableMeta tableMeta, Map<String, Table> tables, Map<String, Table> remoteTables) {
    for (TableColumnMeta colMeta : tableMeta.getColumns()) {
      TableColumn col = getColumn(colMeta.getName());

      // go thru the new foreign key defs and associate them with our columns
      for (ForeignKeyMeta fk : colMeta.getForeignKeys()) {
        Table parent =
            fk.getRemoteSchema() == null
                ? tables.get(fk.getTableName())
                : remoteTables.get(fk.getRemoteSchema() + '.' + fk.getTableName());
        if (parent != null) {
          TableColumn parentColumn = parent.getColumn(fk.getColumnName());

          if (parentColumn == null) {
            logger.warning(parent.getName() + '.' + fk.getColumnName() + " doesn't exist");
          } else {
            /**
             * Merely instantiating a foreign key constraint ties it into its parent and child
             * columns (& therefore their tables)
             */
            new ForeignKeyConstraint(parentColumn, col) {
              @Override
              public String getName() {
                return "Defined in XML";
              }
            };
          }
        } else {
          logger.warning(
              "Undefined table '"
                  + fk.getTableName()
                  + "' referenced by '"
                  + getName()
                  + '.'
                  + col.getName()
                  + '\'');
        }
      }
    }
  }
コード例 #4
0
  /**
   * Write check constraints associated with the specified table (if any)
   *
   * @param table Table
   * @param html LineWriter
   * @throws IOException
   * @return int
   */
  private int writeCheckConstraints(Table table, LineWriter html) throws IOException {
    Map<String, String> constraints = table.getCheckConstraints(); // constraint name -> text pairs
    int constraintsWritten = 0;
    for (String name : constraints.keySet()) {
      html.writeln(" <tr>");
      html.write("  <td class='detail' valign='top'><a href='tables/");
      html.write(table.getName());
      html.write(".html'>");
      html.write(table.getName());
      html.write("</a></td>");
      html.write("  <td class='detail' valign='top'>");
      html.write(name);
      html.writeln("</td>");
      html.write("  <td class='detail'>");
      html.write(HtmlEncoder.encodeString(constraints.get(name).toString()));
      html.writeln("</td>");
      html.writeln(" </tr>");
      ++constraintsWritten;
    }

    return constraintsWritten;
  }
コード例 #5
0
ファイル: Table.java プロジェクト: jaguililla/schemaspy
  /** @param tableMeta */
  public void connect(
      TableMeta tableMeta, Map<String, Table> tables, Map<String, Table> remoteTables) {
    for (TableColumnMeta colMeta : tableMeta.getColumns()) {
      TableColumn col = getColumn(colMeta.getName());

      // go thru the new foreign key defs and associate them with our columns
      for (ForeignKeyMeta fk : colMeta.getForeignKeys()) {
        Table parent =
            fk.getRemoteSchema() == null
                ? tables.get(fk.getTableName())
                : remoteTables.get(fk.getRemoteSchema() + '.' + fk.getTableName());
        if (parent != null) {
          TableColumn parentColumn = parent.getColumn(fk.getColumnName());

          if (parentColumn == null) {
            System.err.println();
            System.err.println(parent.getName() + '.' + fk.getColumnName() + " doesn't exist");
          } else {
            new ForeignKeyConstraint(parentColumn, col) {
              @Override
              public String getName() {
                return "Defined in XML";
              }
            };
          }
        } else {
          System.err.println();
          System.err.print(
              "Undefined table '"
                  + fk.getTableName()
                  + "' referenced by '"
                  + getName()
                  + '.'
                  + col.getName()
                  + '\'');
        }
      }
    }
  }
コード例 #6
0
  public void write(
      Database database, Collection<Table> tables, boolean showOrphansDiagram, LineWriter html)
      throws IOException {
    Set<Table> byName =
        new TreeSet<Table>(
            new Comparator<Table>() {
              public int compare(Table table1, Table table2) {
                return table1.compareTo(table2);
              }
            });
    byName.addAll(tables);

    boolean showIds = false;
    int numViews = 0;
    boolean comments = false;

    for (Table table : byName) {
      if (table.isView()) ++numViews;
      showIds |= table.getId() != null;
      if (table.getComments() != null) comments = true;
    }

    writeHeader(
        database, byName.size() - numViews, numViews, showIds, showOrphansDiagram, comments, html);

    int numTableCols = 0;
    int numViewCols = 0;
    long numRows = 0;
    for (Table table : byName) {
      writeLineItem(table, showIds, html);

      if (!table.isView()) numTableCols += table.getColumns().size();
      else numViewCols += table.getColumns().size();
      numRows += table.getNumRows();
    }

    writeFooter(byName.size() - numViews, numTableCols, numViews, numViewCols, numRows, html);
  }
コード例 #7
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,
      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");
    }
  }
コード例 #8
0
  public boolean write(Table table, File diagramDir, LineWriter html) {
    File oneDegreeDotFile = new File(diagramDir, table.getName() + ".1degree.dot");
    File oneDegreeDiagramFile = new File(diagramDir, table.getName() + ".1degree.png");
    File twoDegreesDotFile = new File(diagramDir, table.getName() + ".2degrees.dot");
    File twoDegreesDiagramFile = new File(diagramDir, table.getName() + ".2degrees.png");
    File impliedDotFile = new File(diagramDir, table.getName() + ".implied2degrees.dot");
    File impliedDiagramFile = new File(diagramDir, table.getName() + ".implied2degrees.png");

    try {
      Dot dot = getDot();
      if (dot == null) return false;

      String map = dot.generateDiagram(oneDegreeDotFile, oneDegreeDiagramFile);

      html.write("<br><form action='get'><b>Close relationships");
      if (twoDegreesDotFile.exists()) {
        html.writeln(
            "</b><span class='degrees' id='degrees' title='Detail diminishes with increased separation from "
                + table.getName()
                + "'>");
        html.write(
            "&nbsp;within <label for='oneDegree'><input type='radio' name='degrees' id='oneDegree' checked>one</label>");
        html.write(
            "  <label for='twoDegrees'><input type='radio' name='degrees' id='twoDegrees'>two degrees</label> of separation");
        html.write("</span><b>:</b>");
        html.writeln("</form>");
      } else {
        html.write(":</b></form>");
      }
      html.write(map);
      map = null;
      html.writeln(
          "  <a name='diagram'><img id='oneDegreeImg' src='../diagrams/"
              + oneDegreeDiagramFile.getName()
              + "' usemap='#oneDegreeRelationshipsDiagram' class='diagram' border='0' alt='' align='left'></a>");

      if (impliedDotFile.exists()) {
        html.writeln(dot.generateDiagram(impliedDotFile, impliedDiagramFile));
        html.writeln(
            "  <a name='diagram'><img id='impliedTwoDegreesImg' src='../diagrams/"
                + impliedDiagramFile.getName()
                + "' usemap='#impliedTwoDegreesRelationshipsDiagram' class='diagram' border='0' alt='' align='left'></a>");
      } else {
        impliedDotFile.delete();
        impliedDiagramFile.delete();
      }
      if (twoDegreesDotFile.exists()) {
        html.writeln(dot.generateDiagram(twoDegreesDotFile, twoDegreesDiagramFile));
        html.writeln(
            "  <a name='diagram'><img id='twoDegreesImg' src='../diagrams/"
                + twoDegreesDiagramFile.getName()
                + "' usemap='#twoDegreesRelationshipsDiagram' class='diagram' border='0' alt='' align='left'></a>");
      } else {
        twoDegreesDotFile.delete();
        twoDegreesDiagramFile.delete();
      }
    } catch (Dot.DotFailure dotFailure) {
      System.err.println(dotFailure);
      return false;
    } catch (IOException ioExc) {
      ioExc.printStackTrace();
      return false;
    }

    return true;
  }
コード例 #9
0
ファイル: Table.java プロジェクト: jaguililla/schemaspy
 public int compareTo(Table table) {
   return getName().compareToIgnoreCase(table.getName());
 }
コード例 #10
0
ファイル: Table.java プロジェクト: jaguililla/schemaspy
  /**
   * @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");
    }
  }