Skip to content

Commit

Permalink
Comments are now displayed, even for 'disagreeable' databases that do…
Browse files Browse the repository at this point in the history
…n't return comment info through their driver's metadata.
  • Loading branch information
johncurrier committed May 1, 2006
1 parent f2cb91f commit 416d004
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 57 deletions.
70 changes: 34 additions & 36 deletions src/net/sourceforge/schemaspy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ public static void main(String[] argv) {

int maxDbThreads = getMaxDbThreads(args, properties);

if (!args.remove("-nologo")) {
// nasty hack, but passing this info everywhere churns my stomach
System.setProperty("sourceforgelogo", "true");
}
// nasty hack, but passing this info everywhere churns my stomach
System.setProperty("sourceforgelogo", String.valueOf(args.remove("-nologo")));

if (args.remove("-rankdirbug")) {
// and another nasty hack with the same justification as the one above
System.setProperty("rankdirbug", "true");
}
// and another nasty hack with the same justification as the one above
System.setProperty("rankdirbug", String.valueOf(args.remove("-rankdirbug")));

// and yet another one (Allow Html In Comments - encode them unless otherwise specified)
System.setProperty("encodeComments", String.valueOf(!args.remove("-ahic")));


Pattern exclusions;
String exclude = getParam(args, "-x");
Expand Down Expand Up @@ -208,11 +208,11 @@ public static void main(String[] argv) {
out = new LineWriter(new FileOutputStream(new File(graphsDir, dotBaseFilespec + ".real.compact.dot")));
WriteStats stats = new WriteStats(exclusions, includeImpliedConstraints);
DotFormatter.getInstance().writeRealRelationships(tables, true, showDetailedTables, stats, out);
boolean hasRelationships = stats.getNumTablesWritten() > 0 || stats.getNumViewsWritten() > 0;
boolean hasRealRelationships = stats.getNumTablesWritten() > 0 || stats.getNumViewsWritten() > 0;
stats = new WriteStats(stats);
out.close();

if (hasRelationships) {
if (hasRealRelationships) {
System.out.print(".");
out = new LineWriter(new FileOutputStream(new File(graphsDir, dotBaseFilespec + ".real.large.dot")));
DotFormatter.getInstance().writeRealRelationships(tables, false, showDetailedTables, stats, out);
Expand All @@ -231,62 +231,60 @@ public static void main(String[] argv) {
List orphans = DBAnalyzer.getOrphans(tables);
boolean hasOrphans = !orphans.isEmpty() && Dot.getInstance().isValid();

if (hasRelationships) {
System.out.print(".");
System.out.print(".");

File impliedDotFile = new File(graphsDir, dotBaseFilespec + ".implied.compact.dot");
File impliedDotFile = new File(graphsDir, dotBaseFilespec + ".implied.compact.dot");
out = new LineWriter(new FileOutputStream(impliedDotFile));
stats = new WriteStats(exclusions, includeImpliedConstraints);
DotFormatter.getInstance().writeAllRelationships(tables, true, showDetailedTables, stats, out);
boolean hasImplied = stats.wroteImplied();
Set excludedColumns = stats.getExcludedColumns();
stats = new WriteStats(stats);
out.close();
if (hasImplied) {
impliedDotFile = new File(graphsDir, dotBaseFilespec + ".implied.large.dot");
out = new LineWriter(new FileOutputStream(impliedDotFile));
stats = new WriteStats(exclusions, includeImpliedConstraints);
DotFormatter.getInstance().writeAllRelationships(tables, true, showDetailedTables, stats, out);
boolean hasImplied = stats.wroteImplied();
Set excludedColumns = stats.getExcludedColumns();
DotFormatter.getInstance().writeAllRelationships(tables, false, showDetailedTables, stats, out);
stats = new WriteStats(stats);
out.close();
if (hasImplied) {
impliedDotFile = new File(graphsDir, dotBaseFilespec + ".implied.large.dot");
out = new LineWriter(new FileOutputStream(impliedDotFile));
DotFormatter.getInstance().writeAllRelationships(tables, false, showDetailedTables, stats, out);
stats = new WriteStats(stats);
out.close();
} else {
impliedDotFile.delete();
}

out = new LineWriter(new FileWriter(new File(outputDir, dotBaseFilespec + ".html")));
hasRelationships = HtmlRelationshipsPage.getInstance().write(db, graphsDir, dotBaseFilespec, hasOrphans, hasImplied, excludedColumns, out);
out.close();
} else {
impliedDotFile.delete();
}

out = new LineWriter(new FileWriter(new File(outputDir, dotBaseFilespec + ".html")));
HtmlRelationshipsPage.getInstance().write(db, graphsDir, dotBaseFilespec, hasOrphans, hasRealRelationships, hasImplied, excludedColumns, out);
out.close();

System.out.print(".");
dotBaseFilespec = "utilities";
out = new LineWriter(new FileWriter(new File(outputDir, dotBaseFilespec + ".html")));
HtmlOrphansPage.getInstance().write(db, orphans, hasRelationships, graphsDir, out);
HtmlOrphansPage.getInstance().write(db, orphans, graphsDir, out);
stats = new WriteStats(stats);
out.close();

System.out.print(".");
out = new LineWriter(new FileWriter(new File(outputDir, "index.html")), 64 * 1024);
HtmlMainIndexPage.getInstance().write(db, tables, hasRelationships, hasOrphans, out);
HtmlMainIndexPage.getInstance().write(db, tables, hasOrphans, out);
stats = new WriteStats(stats);
out.close();

System.out.print(".");
List constraints = DBAnalyzer.getForeignKeyConstraints(tables);
out = new LineWriter(new FileWriter(new File(outputDir, "constraints.html")), 256 * 1024);
HtmlConstraintsPage constraintIndexFormatter = HtmlConstraintsPage.getInstance();
constraintIndexFormatter.write(db, constraints, tables, hasRelationships, hasOrphans, out);
constraintIndexFormatter.write(db, constraints, tables, hasOrphans, out);
stats = new WriteStats(stats);
out.close();

System.out.print(".");
out = new LineWriter(new FileWriter(new File(outputDir, "anomalies.html")), 16 * 1024);
HtmlAnomaliesPage.getInstance().write(db, tables, impliedConstraints, hasRelationships, hasOrphans, out);
HtmlAnomaliesPage.getInstance().write(db, tables, impliedConstraints, hasOrphans, out);
stats = new WriteStats(stats);
out.close();

System.out.print(".");
out = new LineWriter(new FileWriter(new File(outputDir, "columns.html")), 16 * 1024);
HtmlColumnsPage.getInstance().write(db, tables, hasRelationships, hasOrphans, out);
HtmlColumnsPage.getInstance().write(db, tables, hasOrphans, out);
stats = new WriteStats(stats);
out.close();

Expand All @@ -300,7 +298,7 @@ public static void main(String[] argv) {
System.out.print('.');
Table table = (Table)iter.next();
out = new LineWriter(new FileWriter(new File(outputDir, "tables/" + table.getName() + ".html")), 24 * 1024);
tableFormatter.write(db, table, hasRelationships, hasOrphans, outputDir, stats, out);
tableFormatter.write(db, table, hasOrphans, outputDir, stats, out);
stats = new WriteStats(stats);
out.close();
}
Expand Down
70 changes: 68 additions & 2 deletions src/net/sourceforge/schemaspy/model/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ boolean isValid(ResultSet rs) throws SQLException {
initCheckConstraints(properties);
initTableIds(properties);
initIndexIds(properties);
initTableComments(properties);
initColumnComments(properties);
}

private void initCheckConstraints(Properties properties) throws SQLException {
Expand Down Expand Up @@ -214,7 +216,68 @@ private void initIndexIds(Properties properties) throws SQLException {
}
}
}

private void initTableComments(Properties properties) throws SQLException {
String sql = properties.getProperty("selectTableCommentsSql");
if (sql != null) {
PreparedStatement stmt = null;
ResultSet rs = null;

try {
stmt = prepareStatement(sql, null);
rs = stmt.executeQuery();

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = (Table)tables.get(tableName.toUpperCase());
if (table != null)
table.setComments(rs.getString("comments"));
}
} catch (SQLException sqlException) {
System.err.println();
System.err.println(sql);
throw sqlException;
} finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
}
}
}

private void initColumnComments(Properties properties) throws SQLException {
String sql = properties.getProperty("selectColumnCommentsSql");
if (sql != null) {
PreparedStatement stmt = null;
ResultSet rs = null;

try {
stmt = prepareStatement(sql, null);
rs = stmt.executeQuery();

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = (Table)tables.get(tableName.toUpperCase());
if (table != null) {
TableColumn column = table.getColumn(rs.getString("column_name"));
if (column != null)
column.setComments(rs.getString("comments"));
}
}
} catch (SQLException sqlException) {
System.err.println();
System.err.println(sql);
throw sqlException;
} finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
}
}
}

/**
* Create a <code>PreparedStatement</code> from the specified SQL.
* The SQL can contain these named parameters (but <b>not</b> question marks).
Expand Down Expand Up @@ -260,8 +323,11 @@ public PreparedStatement prepareStatement(String sql, String tableName) throws S
*/
private List getSqlParams(StringBuffer sql, String tableName) {
Map namedParams = new HashMap();
namedParams.put(":schema", getSchema());
namedParams.put(":owner", getSchema()); // alias for :schema
String schema = getSchema();
if (schema == null)
schema = getName(); // some 'schema-less' db's treat the db name like a schema (unusual case)
namedParams.put(":schema", schema);
namedParams.put(":owner", schema); // alias for :schema
if (tableName != null) {
namedParams.put(":table", tableName);
namedParams.put(":view", tableName); // alias for :table
Expand Down
8 changes: 6 additions & 2 deletions src/net/sourceforge/schemaspy/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class Table implements Comparable {
private Object id;
private final Map checkConstraints = new TreeMap(new ByCheckConstraintStringsComparator());
private final int numRows;
private final String comments;
private String comments;
private int maxChildren;
private int maxParents;

public Table(Database db, String schema, String name, String comments, DatabaseMetaData meta, Properties properties) throws SQLException {
this.schema = schema;
this.name = name;
this.comments = (comments == null || comments.length() == 0) ? null : comments;
setComments(comments);
initColumns(meta);
initIndexes(db, meta, properties);
initPrimaryKeys(meta);
Expand Down Expand Up @@ -332,6 +332,10 @@ public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = (comments == null || comments.trim().length() == 0) ? null : comments.trim();
}

public TableColumn getColumn(String columnName) {
return (TableColumn)columns.get(columnName.toUpperCase());
}
Expand Down
9 changes: 6 additions & 3 deletions src/net/sourceforge/schemaspy/model/TableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TableColumn {
private final boolean isNullable;
private boolean isAutoUpdated;
private final Object defaultValue;
private final String comments;
private String comments;
private final Map parents = new HashMap();
private final Map children = new TreeMap(new ColumnComparator());

Expand Down Expand Up @@ -43,8 +43,7 @@ public class TableColumn {

isNullable = rs.getInt("NULLABLE") == DatabaseMetaData.columnNullable;
defaultValue = rs.getString("COLUMN_DEF");
String tmp = rs.getString("REMARKS");
comments = (tmp == null || tmp.length() == 0) ? null : tmp;
setComments(rs.getString("REMARKS"));
id = new Integer(rs.getInt("ORDINAL_POSITION") - 1);
}

Expand Down Expand Up @@ -114,6 +113,10 @@ public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = (comments == null || comments.trim().length() == 0) ? null : comments.trim();
}

public void addParent(TableColumn parent, ForeignKeyConstraint constraint) {
parents.put(parent, constraint);
table.addedParent();
Expand Down
8 changes: 4 additions & 4 deletions src/net/sourceforge/schemaspy/view/HtmlColumnsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static HtmlColumnsPage getInstance() {
return instance;
}

public void write(Database database, Collection tables, boolean showRelationshipsGraph, boolean showOrphansGraph, LineWriter html) throws IOException {
public void write(Database database, Collection tables, boolean showOrphansGraph, LineWriter html) throws IOException {
Set columns = new TreeSet(new Comparator() {
public int compare(Object object1, Object object2) {
TableColumn column1 = (TableColumn)object1;
Expand Down Expand Up @@ -46,7 +46,7 @@ public int compare(Object object1, Object object2) {
}
}

writeHeader(database, columns.size(), showRelationshipsGraph, showOrphansGraph, html);
writeHeader(database, columns.size(), showOrphansGraph, html);

HtmlTablePage formatter = HtmlTablePage.getInstance();

Expand All @@ -59,8 +59,8 @@ public int compare(Object object1, Object object2) {
writeFooter(html);
}

private void writeHeader(Database db, int numberOfColumns, boolean hasRelationships, boolean hasOrphans, LineWriter html) throws IOException {
writeHeader(db, null, "Columns", hasRelationships, hasOrphans, html);
private void writeHeader(Database db, int numberOfColumns, boolean hasOrphans, LineWriter html) throws IOException {
writeHeader(db, null, "Columns", hasOrphans, html);

html.writeln("<table width='100%' border='0'>");
html.writeln("<tr><td class='container'>");
Expand Down
18 changes: 14 additions & 4 deletions src/net/sourceforge/schemaspy/view/HtmlMainIndexPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static HtmlMainIndexPage getInstance() {
return instance;
}

public void write(Database database, Collection tables, boolean showRelationshipsGraph, boolean showOrphansGraph, LineWriter html) throws IOException {
public void write(Database database, Collection tables, boolean showOrphansGraph, LineWriter html) throws IOException {
Set byName = new TreeSet(new Comparator() {
public int compare(Object object1, Object object2) {
return ((Table)object1).getName().compareTo(((Table)object2).getName());
Expand All @@ -38,7 +38,7 @@ public int compare(Object object1, Object object2) {
showIds |= table.getId() != null;
}

writeHeader(database, byName.size() - numViews, numViews, showIds, showRelationshipsGraph, showOrphansGraph, html);
writeHeader(database, byName.size() - numViews, numViews, showIds, showOrphansGraph, html);

int numRows = 0;
for (Iterator iter = byName.iterator(); iter.hasNext(); ) {
Expand All @@ -49,8 +49,8 @@ public int compare(Object object1, Object object2) {
writeFooter(numRows, html);
}

private void writeHeader(Database db, int numberOfTables, int numberOfViews, boolean showIds, boolean hasRelationships, boolean hasOrphans, LineWriter html) throws IOException {
writeHeader(db, null, null, hasRelationships, hasOrphans, html);
private void writeHeader(Database db, int numberOfTables, int numberOfViews, boolean showIds, boolean hasOrphans, LineWriter html) throws IOException {
writeHeader(db, null, null, hasOrphans, html);
html.writeln("<table width='100%'>");
html.writeln(" <tr><td class='container'>");
writeGeneratedBy(db.getConnectTime(), html);
Expand Down Expand Up @@ -144,6 +144,16 @@ private int writeLineItem(Table table, boolean showIds, LineWriter html) throws
else
html.write("<span title='Views contain no real rows'>view</span>");
html.writeln("</td>");
html.write(" <td class='detail'>");
String comments = table.getComments();
if (comments != null) {
if (Boolean.getBoolean("encodeComments"))
for (int i = 0; i < comments.length(); ++i)
html.write(HtmlEncoder.encode(comments.charAt(i)));
else
html.write(comments);
}
html.writeln("</td>");
html.writeln(" </tr>");

return table.getNumRows();
Expand Down
Loading

0 comments on commit 416d004

Please sign in to comment.