Skip to content

Commit

Permalink
Not perfect yet, but *much* cleaner way of dealing with excluded colu…
Browse files Browse the repository at this point in the history
…mns.
  • Loading branch information
johncurrier committed Aug 22, 2008
1 parent 052e13f commit 59ee34c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 45 deletions.
26 changes: 16 additions & 10 deletions src/net/sourceforge/schemaspy/model/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.schemaspy.Config;
import net.sourceforge.schemaspy.model.xml.SchemaMeta;
import net.sourceforge.schemaspy.model.xml.TableMeta;
import net.sourceforge.schemaspy.util.CaseInsensitiveMap;
Expand Down Expand Up @@ -377,16 +378,16 @@ public PreparedStatement prepareStatement(String sql, String tableName) throws S
return stmt;
}

public Table addRemoteTable(String remoteSchema, String remoteTableName, String baseSchema, Properties properties) throws SQLException {
public Table addRemoteTable(String remoteSchema, String remoteTableName, String baseSchema, Properties properties, Pattern excludeColumns) throws SQLException {
String fullName = remoteSchema + "." + remoteTableName;
Table remoteTable = remoteTables.get(fullName);
if (remoteTable == null) {
if (properties != null)
remoteTable = new RemoteTable(this, remoteSchema, remoteTableName, baseSchema, properties);
remoteTable = new RemoteTable(this, remoteSchema, remoteTableName, baseSchema, properties, excludeColumns);
else
remoteTable = new ExplicitRemoteTable(this, remoteSchema, remoteTableName, baseSchema);

remoteTable.connectForeignKeys(tables, this, properties);
remoteTable.connectForeignKeys(tables, this, properties, excludeColumns);
remoteTables.put(fullName, remoteTable);
}

Expand Down Expand Up @@ -556,6 +557,7 @@ private List<String> getSqlParams(StringBuffer sql, String tableName) {
private void initViews(@SuppressWarnings("hiding")String schema, DatabaseMetaData metadata, Properties properties, Pattern include) throws SQLException {
String[] types = {"VIEW"};
ResultSet rs = null;
Pattern excludeColumns = Config.getInstance().getColumnExclusions();

try {
rs = metadata.getTables(null, schema, "%", types);
Expand All @@ -565,7 +567,7 @@ private void initViews(@SuppressWarnings("hiding")String schema, DatabaseMetaDat
System.out.print('.');

try {
View view = new View(this, rs, properties.getProperty("selectViewSql"));
View view = new View(this, rs, properties.getProperty("selectViewSql"), excludeColumns);

if (include.matcher(view.getName()).matches())
views.put(view.getName(), view);
Expand All @@ -586,14 +588,16 @@ private void initViews(@SuppressWarnings("hiding")String schema, DatabaseMetaDat

private void updateFromXmlMetadata(SchemaMeta schemaMeta) throws SQLException {
if (schemaMeta != null) {
final Pattern excludeNone = Pattern.compile("[^.]");

comments = schemaMeta.getComments();

// add the newly defined remote tables first
for (TableMeta tableMeta : schemaMeta.getTables()) {
if (tableMeta.getRemoteSchema() != null) {
Table table = remoteTables.get(tableMeta.getName());
if (table == null) {
table = addRemoteTable(tableMeta.getRemoteSchema(), tableMeta.getName(), getSchema(), null);
table = addRemoteTable(tableMeta.getRemoteSchema(), tableMeta.getName(), getSchema(), null, excludeNone);
}

table.update(tableMeta, tables, remoteTables);
Expand All @@ -613,17 +617,19 @@ private void updateFromXmlMetadata(SchemaMeta schemaMeta) throws SQLException {
}

private void connectTables(Properties properties) throws SQLException {
Iterator<Table> iter = tables.values().iterator();
while (iter.hasNext()) {
Table table = iter.next();
table.connectForeignKeys(tables, this, properties);
Pattern excludeColumns = Config.getInstance().getColumnExclusions();

for (Table table : tables.values()) {
table.connectForeignKeys(tables, this, properties, excludeColumns);
}
}

/**
* Single-threaded implementation of a class that creates tables
*/
private class TableCreator {
private final Pattern excludeColumns = Config.getInstance().getColumnExclusions();

/**
* Create a table and put it into <code>tables</code>
*/
Expand All @@ -632,7 +638,7 @@ void create(String schemaName, String tableName, String remarks, Properties prop
}

protected void createImpl(String schemaName, String tableName, String remarks, Properties properties) throws SQLException {
Table table = new Table(Database.this, schemaName, tableName, remarks, properties);
Table table = new Table(Database.this, schemaName, tableName, remarks, properties, excludeColumns);
tables.put(table.getName(), table);
System.out.print('.');
}
Expand Down
9 changes: 6 additions & 3 deletions src/net/sourceforge/schemaspy/model/ExplicitRemoteTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

/**
* A remote table (exists in another schema) that was explicitly created via XML metadata.
*
* @author John Currier
*/
public class ExplicitRemoteTable extends RemoteTable {
private static final Pattern excludeNone = Pattern.compile("[^.]");

public ExplicitRemoteTable(Database db, String schema, String name, String baseSchema) throws SQLException {
super(db, schema, name, baseSchema, null);
super(db, schema, name, baseSchema, null, excludeNone);
}

@Override
public void connectForeignKeys(Map<String, Table> tables, Database db, Properties properties) throws SQLException {
public void connectForeignKeys(Map<String, Table> tables, Database db, Properties properties, Pattern excludeColumns) throws SQLException {
// this probably won't work, so ignore any failures...but try anyways just in case
try {
super.connectForeignKeys(tables, db, properties);
super.connectForeignKeys(tables, db, properties, excludeColumns);
} catch (SQLException ignore) {}
}
}
9 changes: 5 additions & 4 deletions src/net/sourceforge/schemaspy/model/RemoteTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

/**
* A table that's outside of the default schema but is referenced
Expand All @@ -14,8 +15,8 @@
public class RemoteTable extends Table {
private final String baseSchema;

public RemoteTable(Database db, String schema, String name, String baseSchema, Properties properties) throws SQLException {
super(db, schema, name, null, properties);
public RemoteTable(Database db, String schema, String name, String baseSchema, Properties properties, Pattern excludeColumns) throws SQLException {
super(db, schema, name, null, properties, excludeColumns);
this.baseSchema = baseSchema;
}

Expand All @@ -25,7 +26,7 @@ public RemoteTable(Database db, String schema, String name, String baseSchema, P
* @param tables
*/
@Override
public void connectForeignKeys(Map<String, Table> tables, Database db, Properties properties) throws SQLException {
public void connectForeignKeys(Map<String, Table> tables, Database db, Properties properties, Pattern excludeColumns) throws SQLException {
ResultSet rs = null;

try {
Expand All @@ -36,7 +37,7 @@ public void connectForeignKeys(Map<String, Table> tables, Database db, Propertie
if (otherSchema != null && otherSchema.equals(baseSchema))
addForeignKey(rs.getString("FK_NAME"), rs.getString("FKCOLUMN_NAME"),
rs.getString("PKTABLE_SCHEM"), rs.getString("PKTABLE_NAME"),
rs.getString("PKCOLUMN_NAME"), tables, db, properties);
rs.getString("PKCOLUMN_NAME"), tables, db, properties, excludeColumns);
}
} finally {
if (rs != null)
Expand Down
28 changes: 13 additions & 15 deletions src/net/sourceforge/schemaspy/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Pattern;
import net.sourceforge.schemaspy.Config;
import net.sourceforge.schemaspy.model.xml.ForeignKeyMeta;
import net.sourceforge.schemaspy.model.xml.TableColumnMeta;
Expand All @@ -36,17 +37,17 @@ public class Table implements Comparable<Table> {
private int maxChildren;
private int maxParents;

public Table(Database db, String schema, String name, String comments, Properties properties) throws SQLException {
public Table(Database db, String schema, String name, String comments, Properties properties, Pattern excludeColumns) throws SQLException {
this.schema = schema;
this.name = name;
setComments(comments);
initColumns(db);
initColumns(db, excludeColumns);
initIndexes(db, properties);
initPrimaryKeys(db.getMetaData(), properties);
numRows = Config.getInstance().isNumRowsEnabled() ? fetchNumRows(db, properties) : -1;
}

public void connectForeignKeys(Map<String, Table> tables, Database db, Properties properties) throws SQLException {
public void connectForeignKeys(Map<String, Table> tables, Database db, Properties properties, Pattern excludeColumns) throws SQLException {
ResultSet rs = null;

try {
Expand All @@ -55,7 +56,7 @@ public void connectForeignKeys(Map<String, Table> tables, Database db, Propertie
while (rs.next()) {
addForeignKey(rs.getString("FK_NAME"), rs.getString("FKCOLUMN_NAME"),
rs.getString("PKTABLE_SCHEM"), rs.getString("PKTABLE_NAME"),
rs.getString("PKCOLUMN_NAME"), tables, db, properties);
rs.getString("PKCOLUMN_NAME"), tables, db, properties, excludeColumns);
}
} finally {
if (rs != null)
Expand All @@ -72,7 +73,7 @@ public void connectForeignKeys(Map<String, Table> tables, Database db, Propertie
while (rs.next()) {
String otherSchema = rs.getString("FKTABLE_SCHEM");
if (!getSchema().equals(otherSchema))
db.addRemoteTable(otherSchema, rs.getString("FKTABLE_NAME"), getSchema(), properties);
db.addRemoteTable(otherSchema, rs.getString("FKTABLE_NAME"), getSchema(), properties, excludeColumns);
}
} finally {
if (rs != null)
Expand Down Expand Up @@ -104,7 +105,7 @@ public void addCheckConstraint(String constraintName, String text) {
* @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) throws SQLException {
protected void addForeignKey(String fkName, String fkColName, String pkTableSchema, String pkTableName, String pkColName, Map<String, Table> tables, Database db, Properties properties, Pattern excludeColumns) throws SQLException {
if (fkName == null)
return;

Expand All @@ -123,7 +124,7 @@ protected void addForeignKey(String fkName, String fkColName, String pkTableSche
if (parentTable == null) {
String otherSchema = pkTableSchema;
if (otherSchema != null && !otherSchema.equals(getSchema()) && Config.getInstance().isOneOfMultipleSchemas()) {
parentTable = db.addRemoteTable(otherSchema, pkTableName, getSchema(), properties);
parentTable = db.addRemoteTable(otherSchema, pkTableName, getSchema(), properties, excludeColumns);
}
}

Expand Down Expand Up @@ -178,15 +179,15 @@ void setPrimaryColumn(TableColumn primaryColumn) {
primaryKeys.add(primaryColumn);
}

private void initColumns(Database db) throws SQLException {
private void initColumns(Database db, Pattern excludeColumns) throws SQLException {
ResultSet rs = null;

synchronized (Table.class) {
try {
rs = db.getMetaData().getColumns(null, getSchema(), getName(), "%");

while (rs.next())
addColumn(rs);
addColumn(rs, excludeColumns);
} catch (SQLException exc) {
class ColumnInitializationFailure extends SQLException {
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -236,10 +237,7 @@ private void initColumnAutoUpdate(Database db, boolean forceQuotes) throws SQLEx
ResultSetMetaData rsMeta = rs.getMetaData();
for (int i = rsMeta.getColumnCount(); i > 0; --i) {
TableColumn column = getColumn(rsMeta.getColumnName(i));
if (column == null) // sometimes happens with AS400 databases!
System.err.println("Inconsistent metadata for " + getName() + '.' + rsMeta.getColumnName(i));
else
column.setIsAutoUpdated(rsMeta.isAutoIncrement(i));
column.setIsAutoUpdated(rsMeta.isAutoIncrement(i));
}
} catch (SQLException exc) {
if (forceQuotes) {
Expand All @@ -261,14 +259,14 @@ private void initColumnAutoUpdate(Database db, boolean forceQuotes) throws SQLEx
* @param rs - from {@link DatabaseMetaData#getColumns(String, String, String, String)}
* @throws SQLException
*/
protected void addColumn(ResultSet rs) throws SQLException {
protected void addColumn(ResultSet rs, Pattern excludeColumns) throws SQLException {
String columnName = rs.getString("COLUMN_NAME");

if (columnName == null)
return;

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

columns.put(column.getName(), column);
}
Expand Down
19 changes: 17 additions & 2 deletions src/net/sourceforge/schemaspy/model/TableColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class TableColumn {
private final Map<TableColumn, ForeignKeyConstraint> children = new TreeMap<TableColumn, ForeignKeyConstraint>(new ColumnComparator());
private boolean allowImpliedParents = true;
private boolean allowImpliedChildren = true;
private final boolean isExcluded;

/**
* Create a column associated with a table.
Expand All @@ -36,7 +37,7 @@ public class TableColumn {
* @param rs ResultSet returned from <code>java.sql.DatabaseMetaData.getColumns()</code>.
* @throws SQLException
*/
TableColumn(Table table, ResultSet rs) throws SQLException {
TableColumn(Table table, ResultSet rs, Pattern excludeColumns) throws SQLException {
this.table = table;

// names and types are typically reused *many* times in a database,
Expand Down Expand Up @@ -66,6 +67,8 @@ public class TableColumn {
defaultValue = rs.getString("COLUMN_DEF");
setComments(rs.getString("REMARKS"));
id = new Integer(rs.getInt("ORDINAL_POSITION") - 1);

isExcluded = matches(excludeColumns);
}

/**
Expand All @@ -84,6 +87,7 @@ public TableColumn(Table table, TableColumnMeta colMeta) {
isAutoUpdated = false;
defaultValue = null;
comments = colMeta.getComments();
isExcluded = false;
}

public Table getTable() {
Expand Down Expand Up @@ -161,7 +165,18 @@ public String getComments() {
public void setComments(String comments) {
this.comments = (comments == null || comments.trim().length() == 0) ? null : comments.trim();
}


/**
* Returns <code>true</code> if this column is to be excluded from relationship diagrams.
* This is typically an attempt to reduce clutter that can be introduced when many tables
* reference a given column.
*
* @return
*/
public boolean isExcluded() {
return isExcluded;
}

public void addParent(TableColumn parent, ForeignKeyConstraint constraint) {
parents.put(parent, constraint);
table.addedParent();
Expand Down
5 changes: 3 additions & 2 deletions src/net/sourceforge/schemaspy/model/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.regex.Pattern;

public class View extends Table {
private final String viewSql;
Expand All @@ -14,8 +15,8 @@ public class View extends Table {
* @param selectViewSql
* @throws java.sql.SQLException
*/
public View(Database db, ResultSet rs, String selectViewSql) throws SQLException {
super(db, rs.getString("TABLE_SCHEM"), rs.getString("TABLE_NAME"), db.getOptionalString(rs, "REMARKS"), null);
public View(Database db, ResultSet rs, String selectViewSql, Pattern excludeColumns) throws SQLException {
super(db, rs.getString("TABLE_SCHEM"), rs.getString("TABLE_NAME"), db.getOptionalString(rs, "REMARKS"), null, excludeColumns);
viewSql = getViewSql(db, selectViewSql);
}

Expand Down
9 changes: 5 additions & 4 deletions src/net/sourceforge/schemaspy/model/xml/SchemaMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ private Document parse(File file) {
Document doc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setIgnoringComments(true);

try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
System.out.println("Wrong parser configuration: " + e.getMessage());
System.err.println("Wrong parser configuration: " + e.getMessage());
return null;
}

try {
doc = docBuilder.parse(file);
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
System.err.println("Wrong XML file structure: " + e.getMessage());
} catch (IOException e) {
System.out.println("Could not read source file: " + e.getMessage());
System.err.println("Could not read source file: " + e.getMessage());
}

return doc;
Expand Down
Loading

0 comments on commit 59ee34c

Please sign in to comment.