Skip to content

Commit

Permalink
Now should show links between remote table FK's to local PK's
Browse files Browse the repository at this point in the history
  • Loading branch information
johncurrier committed Dec 7, 2006
1 parent 1d156ea commit 1271a8d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/net/sourceforge/schemaspy/model/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,12 @@ public PreparedStatement prepareStatement(String sql, String tableName) throws S
return stmt;
}

public Table addRemoteTable(String remoteSchema, String remoteTableName) throws SQLException {
public Table addRemoteTable(String remoteSchema, String remoteTableName, String baseSchema) throws SQLException {
String fullName = remoteSchema + "." + remoteTableName;
Table remoteTable = (Table)remoteTables.get(fullName);
if (remoteTable == null) {
remoteTable = new RemoteTable(this, remoteSchema, remoteTableName);
remoteTable = new RemoteTable(this, remoteSchema, remoteTableName, baseSchema);
remoteTable.connectForeignKeys(tables, this);
remoteTables.put(fullName, remoteTable);
}

Expand Down
26 changes: 25 additions & 1 deletion src/net/sourceforge/schemaspy/model/RemoteTable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.sourceforge.schemaspy.model;

import java.sql.*;
import java.util.*;

/**
* A table that's outside of the default schema but is referenced
Expand All @@ -9,10 +10,33 @@
* @author John Currier
*/
public class RemoteTable extends Table {
public RemoteTable(Database db, String schema, String name) throws SQLException {
private final String baseSchema;

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

/**
* Connect to the PK's referenced by this table that live in the original schema
*/
public void connectForeignKeys(Map tables, Database db) throws SQLException {
ResultSet rs = null;

try {
rs = db.getMetaData().getImportedKeys(null, getSchema(), getName());

while (rs.next()) {
String otherSchema = rs.getString("PKTABLE_SCHEM");
if (otherSchema != null && otherSchema.equals(baseSchema))
addForeignKey(rs, tables, db);
}
} finally {
if (rs != null)
rs.close();
}
}

public boolean isRemote() {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/net/sourceforge/schemaspy/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void connectForeignKeys(Map tables, Database db) throws SQLException {
while (rs.next()) {
String otherSchema = rs.getString("FKTABLE_SCHEM");
if (!getSchema().equals(otherSchema))
db.addRemoteTable(otherSchema, rs.getString("FKTABLE_NAME"));
db.addRemoteTable(otherSchema, rs.getString("FKTABLE_NAME"), getSchema());
}
} finally {
if (rs != null)
Expand Down Expand Up @@ -79,7 +79,7 @@ public void addCheckConstraint(String name, String text) {
* @param db
* @throws SQLException
*/
private void addForeignKey(ResultSet rs, Map tables, Database db) throws SQLException {
protected void addForeignKey(ResultSet rs, Map tables, Database db) throws SQLException {
String name = rs.getString("FK_NAME");

if (name == null)
Expand All @@ -100,7 +100,7 @@ private void addForeignKey(ResultSet rs, Map tables, Database db) throws SQLExce
if (parentTable == null) {
String otherSchema = rs.getString("PKTABLE_SCHEM");
if (otherSchema != null && !otherSchema.equals(getSchema()) && Config.getInstance().isOneOfMultipleSchemas()) {
parentTable = db.addRemoteTable(otherSchema, rs.getString("PKTABLE_NAME"));
parentTable = db.addRemoteTable(otherSchema, rs.getString("PKTABLE_NAME"), getSchema());
}
}

Expand Down

0 comments on commit 1271a8d

Please sign in to comment.