Skip to content

Commit

Permalink
Bug 3148639 - views weren't being fully evaluated for relationships.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncurrier committed Jan 13, 2011
1 parent bf1eef8 commit cf2a8b8
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 22 deletions.
121 changes: 99 additions & 22 deletions src/net/sourceforge/schemaspy/model/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class Database {
private String description;
private final Map<String, Table> tables = new CaseInsensitiveMap<Table>();
private final Map<String, View> views = new CaseInsensitiveMap<View>();
private final Map<String, Table> remoteTables = new CaseInsensitiveMap<Table>(); // key: schema.tableName value: RemoteTable
private final Map<String, Table> remoteTables = new CaseInsensitiveMap<Table>(); // key: schema.tableName
private final Map<String, Table> combined = new CombinedMap(tables, views);
private final DatabaseMetaData meta;
private final Connection connection;
private final String connectTime = new SimpleDateFormat("EEE MMM dd HH:mm z yyyy").format(new Date());
Expand Down Expand Up @@ -458,7 +459,7 @@ private void initCheckConstraints(Properties properties) throws SQLException {

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = tables.get(tableName);
Table table = combined.get(tableName);
if (table != null)
table.addCheckConstraint(rs.getString("constraint_name"), rs.getString("text"));
}
Expand Down Expand Up @@ -488,7 +489,7 @@ private void initTableIds(Properties properties) throws SQLException {

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = tables.get(tableName);
Table table = combined.get(tableName);
if (table != null)
table.setId(rs.getObject("table_id"));
}
Expand Down Expand Up @@ -517,7 +518,7 @@ private void initIndexIds(Properties properties) throws SQLException {

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = tables.get(tableName);
Table table = combined.get(tableName);
if (table != null) {
TableIndex index = table.getIndex(rs.getString("index_name"));
if (index != null)
Expand Down Expand Up @@ -557,10 +558,7 @@ private void initTableComments(Properties properties) throws SQLException {

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = tables.get(tableName);
if (table == null)
table = views.get(tableName);

Table table = combined.get(tableName);
if (table != null)
table.setComments(rs.getString("comments"));
}
Expand Down Expand Up @@ -637,10 +635,7 @@ private void initTableColumnComments(Properties properties) throws SQLException

while (rs.next()) {
String tableName = rs.getString("table_name");
Table table = tables.get(tableName);
if (table == null)
table = views.get(tableName);

Table table = combined.get(tableName);
if (table != null) {
TableColumn column = table.getColumn(rs.getString("column_name"));
if (column != null)
Expand Down Expand Up @@ -744,7 +739,7 @@ public Table addRemoteTable(String remoteSchema, String remoteTableName, String

logger.fine("Adding remote table " + fullName);
remoteTables.put(fullName, remoteTable);
remoteTable.connectForeignKeys(tables, excludeIndirectColumns, excludeColumns);
remoteTable.connectForeignKeys(combined, excludeIndirectColumns, excludeColumns);
}

return remoteTable;
Expand Down Expand Up @@ -945,10 +940,7 @@ private void updateFromXmlMetadata(SchemaMeta schemaMeta) throws SQLException {
table = addRemoteTable(tableMeta.getRemoteSchema(), tableMeta.getName(), getSchema(), null, excludeNone, excludeNone);
}
} else {
table = tables.get(tableMeta.getName());

if (table == null)
table = views.get(tableMeta.getName());
table = combined.get(tableMeta.getName());

if (table == null) {
table = new Table(Database.this, getSchema(), tableMeta.getName(), null, noProps, excludeNone, excludeNone);
Expand All @@ -966,12 +958,10 @@ private void updateFromXmlMetadata(SchemaMeta schemaMeta) throws SQLException {
if (tableMeta.getRemoteSchema() != null) {
table = remoteTables.get(tableMeta.getRemoteSchema() + '.' + tableMeta.getName());
} else {
table = tables.get(tableMeta.getName());
if (table == null)
table = views.get(tableMeta.getName());
table = combined.get(tableMeta.getName());
}

table.connect(tableMeta, tables, remoteTables);
table.connect(tableMeta, combined, remoteTables);
}
}
}
Expand All @@ -981,7 +971,10 @@ private void connectTables() throws SQLException {
Pattern excludeIndirectColumns = Config.getInstance().getIndirectColumnExclusions();

for (Table table : tables.values()) {
table.connectForeignKeys(tables, excludeIndirectColumns, excludeColumns);
table.connectForeignKeys(combined, excludeIndirectColumns, excludeColumns);
}
for (Table view : views.values()) {
view.connectForeignKeys(combined, excludeIndirectColumns, excludeColumns);
}
}

Expand Down Expand Up @@ -1091,4 +1084,88 @@ public void join() {
}
}
}

/**
* A read-only map that treats both collections of Tables and Views as one
* combined collection.
* This is a bit strange, but it simplifies logic that otherwise treats
* the two as if they were one collection.
*/
private class CombinedMap implements Map<String, Table> {
private final Map<String, ? extends Table> map1;
private final Map<String, ? extends Table> map2;

public CombinedMap(Map<String, ? extends Table> map1, Map<String, ? extends Table> map2)
{
this.map1 = map1;
this.map2 = map2;
}

public Table get(Object name) {
Table table = map1.get(name);
if (table == null)
table = map2.get(name);
return table;
}

public int size() {
return map1.size() + map2.size();
}

public boolean isEmpty() {
return map1.isEmpty() && map2.isEmpty();
}

public boolean containsKey(Object key) {
return map1.containsKey(key) || map2.containsKey(key);
}

public boolean containsValue(Object value) {
return map1.containsValue(value) || map2.containsValue(value);
}

public Table put(String name, Table table) {
throw new UnsupportedOperationException();
}

/**
* Warning: potentially expensive operation
*/
public Set<String> keySet() {
return getCombined().keySet();
}

/**
* Warning: potentially expensive operation
*/
public Set<Map.Entry<String, Table>> entrySet() {
return getCombined().entrySet();
}

/**
* Warning: potentially expensive operation
*/
public Collection<Table> values() {
return getCombined().values();
}

private Map<String, Table> getCombined() {
Map<String, Table> all = new CaseInsensitiveMap<Table>(size());
all.putAll(map1);
all.putAll(map2);
return all;
}

public Table remove(Object key) {
throw new UnsupportedOperationException();
}

public void putAll(Map<? extends String, ? extends Table> table) {
throw new UnsupportedOperationException();
}

public void clear() {
throw new UnsupportedOperationException();
}
}
}
9 changes: 9 additions & 0 deletions src/net/sourceforge/schemaspy/util/CaseInsensitiveMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class CaseInsensitiveMap<V> extends HashMap<String, V>
{
private static final long serialVersionUID = 1L;

public CaseInsensitiveMap()
{
}

public CaseInsensitiveMap(int initialCapacity)
{
super(initialCapacity);
}

@Override
public V get(Object key) {
return super.get(((String)key).toUpperCase());
Expand Down

0 comments on commit cf2a8b8

Please sign in to comment.