From 1271a8d5420a17454364fb87a1710332ebc96800 Mon Sep 17 00:00:00 2001 From: johncurrier Date: Thu, 7 Dec 2006 22:25:12 +0000 Subject: [PATCH] Now should show links between remote table FK's to local PK's --- .../sourceforge/schemaspy/model/Database.java | 5 ++-- .../schemaspy/model/RemoteTable.java | 26 ++++++++++++++++++- .../sourceforge/schemaspy/model/Table.java | 6 ++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/net/sourceforge/schemaspy/model/Database.java b/src/net/sourceforge/schemaspy/model/Database.java index a027401..caa9f36 100755 --- a/src/net/sourceforge/schemaspy/model/Database.java +++ b/src/net/sourceforge/schemaspy/model/Database.java @@ -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); } diff --git a/src/net/sourceforge/schemaspy/model/RemoteTable.java b/src/net/sourceforge/schemaspy/model/RemoteTable.java index 776e6ea..5ffcfd2 100644 --- a/src/net/sourceforge/schemaspy/model/RemoteTable.java +++ b/src/net/sourceforge/schemaspy/model/RemoteTable.java @@ -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 @@ -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; } diff --git a/src/net/sourceforge/schemaspy/model/Table.java b/src/net/sourceforge/schemaspy/model/Table.java index 64852c4..873f67e 100755 --- a/src/net/sourceforge/schemaspy/model/Table.java +++ b/src/net/sourceforge/schemaspy/model/Table.java @@ -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) @@ -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) @@ -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()); } }