]> granicus.if.org Git - postgresql/commitdiff
Fixes to getImportedKeys/getExportedKeys from Jason Davies
authorDave Cramer <davec@fastcrypt.com>
Fri, 18 Jan 2002 17:21:51 +0000 (17:21 +0000)
committerDave Cramer <davec@fastcrypt.com>
Fri, 18 Jan 2002 17:21:51 +0000 (17:21 +0000)
Previous versions did not correctly identify primary/foreign keys

src/interfaces/jdbc/org/postgresql/jdbc1/DatabaseMetaData.java
src/interfaces/jdbc/org/postgresql/jdbc2/DatabaseMetaData.java

index cf9a46b4f12fb42658d29373a3319b63f5aba52d..f9907e92df822310384433e9eb17618a82ceab3f 100644 (file)
@@ -13,7 +13,7 @@ import org.postgresql.util.PSQLException;
 /*
  * This class provides information about the database as a whole.
  *
- * $Id: DatabaseMetaData.java,v 1.40 2001/11/19 23:16:45 momjian Exp $
+ * $Id: DatabaseMetaData.java,v 1.41 2002/01/18 17:21:51 davec Exp $
  *
  * <p>Many of the methods here return lists of information in ResultSets.  You
  * can use the normal ResultSet methods such as getString and getInt to
@@ -2299,8 +2299,8 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
                                                                + "pg_class ic,pg_proc p, pg_index i "
                                                                + "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
                                                                + "AND t.tgfoid=p.oid AND tgisconstraint "
-                                                               + ((primaryTable != null) ? "AND c2.relname='" + primaryTable + "' " : "")
-                                                               + ((foreignTable != null) ? "AND c.relname='" + foreignTable + "' " : "")
+                                                               + ((primaryTable != null) ? "AND c.relname='" + primaryTable + "' " : "")
+                                                               + ((foreignTable != null) ? "AND c2.relname='" + foreignTable + "' " : "")
                                                                + "AND i.indrelid=c.oid "
                                                                + "AND i.indexrelid=ic.oid AND i.indisprimary "
                                                                + "ORDER BY c.relname,c2.relname"
@@ -2339,65 +2339,67 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
                                                        else if ("setdefault".equals(rule))
                                                                action = importedKeySetDefault;
                                                        tuple[col] = Integer.toString(action).getBytes();
-                                                       foundRule = true;
-                                               }
-                                       }
-                               }
-                               while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
 
-                               if (foundRule)
-                               {
-                                       tuple[2] = rs.getBytes(2); //PKTABLE_NAME
-                                       tuple[6] = rs.getBytes(1); //FKTABLE_NAME
-
-                                       // Parse the tgargs data
-                                       StringBuffer fkeyColumns = new StringBuffer();
-                                       StringBuffer pkeyColumns = new StringBuffer();
-                                       int numColumns = (rs.getInt(7) >> 1) - 2;
-                                       String s = rs.getString(8);
-                                       int pos = s.lastIndexOf("\\000");
-                                       for (int c = 0;c < numColumns;c++)
-                                       {
-                                               if (pos > -1)
-                                               {
-                                                       int pos2 = s.lastIndexOf("\\000", pos - 1);
-                                                       if (pos2 > -1)
+                                                       if (!foundRule)
                                                        {
-                                                               if (fkeyColumns.length() > 0)
-                                                                       fkeyColumns.insert(0, ',');
-                                                               fkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //FKCOLUMN_NAME
-                                                               pos = s.lastIndexOf("\\000", pos2 - 1);
-                                                               if (pos > -1)
+                                                               tuple[2] = rs.getBytes(1); //PKTABLE_NAME
+                                                               tuple[6] = rs.getBytes(2); //FKTABLE_NAME
+
+                                                               // Parse the tgargs data
+                                                               StringBuffer fkeyColumns = new StringBuffer();
+                                                               StringBuffer pkeyColumns = new StringBuffer();
+                                                               int numColumns = (rs.getInt(7) >> 1) - 2;
+                                                               String s = rs.getString(8);
+                                                               int pos = s.lastIndexOf("\\000");
+                                                               for (int c = 0;c < numColumns;c++)
+                                                               {
+                                                                       if (pos > -1)
+                                                                       {
+                                                                               int pos2 = s.lastIndexOf("\\000", pos - 1);
+                                                                               if (pos2 > -1)
+                                                                               {
+                                                                                       if (pkeyColumns.length() > 0)
+                                                                                               pkeyColumns.insert(0, ',');
+                                                                                       pkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //PKCOLUMN_NAME
+                                                                                       pos = s.lastIndexOf("\\000", pos2 - 1);
+                                                                                       if (pos > -1)
+                                                                                       {
+                                                                                               if (fkeyColumns.length() > 0)
+                                                                                                   fkeyColumns.insert(0, ',');
+                                                                                               fkeyColumns.insert(0, s.substring(pos + 4, pos2)); //FKCOLUMN_NAME
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               }
+                                                               tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
+                                                               tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
+
+                                                               tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
+                                                               tuple[11] = fKeyName.getBytes(); //FK_NAME
+                                                               tuple[12] = rs.getBytes(4); //PK_NAME
+
+                                                               // DEFERRABILITY
+                                                               int deferrability = importedKeyNotDeferrable;
+                                                               boolean deferrable = rs.getBoolean(5);
+                                                               boolean initiallyDeferred = rs.getBoolean(6);
+                                                               if (deferrable)
                                                                {
-                                                                       if (pkeyColumns.length() > 0)
-                                                                               pkeyColumns.insert(0, ',');
-                                                                       pkeyColumns.insert(0, s.substring(pos + 4, pos2)); //PKCOLUMN_NAME
+                                                                       if (initiallyDeferred)
+                                                                               deferrability = importedKeyInitiallyDeferred;
+                                                                       else
+                                                                               deferrability = importedKeyInitiallyImmediate;
                                                                }
+                                                               tuple[13] = Integer.toString(deferrability).getBytes();
+
+                                                               foundRule = true;
                                                        }
                                                }
                                        }
-                                       tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
-                                       tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
-
-                                       tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
-                                       tuple[11] = fKeyName.getBytes(); //FK_NAME
-                                       tuple[12] = rs.getBytes(4); //PK_NAME
-
-                                       // DEFERRABILITY
-                                       int deferrability = importedKeyNotDeferrable;
-                                       boolean deferrable = rs.getBoolean(5);
-                                       boolean initiallyDeferred = rs.getBoolean(6);
-                                       if (deferrable)
-                                       {
-                                               if (initiallyDeferred)
-                                                       deferrability = importedKeyInitiallyDeferred;
-                                               else
-                                                       deferrability = importedKeyInitiallyImmediate;
-                                       }
-                                       tuple[13] = Integer.toString(deferrability).getBytes();
-
-                                       tuples.addElement(tuple);
                                }
+                               while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
+
+                               if(foundRule) tuples.addElement(tuple);
+
                        }
                        while (hasMore);
                }
index 50dee09b87c74101b7ac20446e4b794b24422cea..4b7707b2056e6b7bad8b0a3923a2320fb3214134 100644 (file)
@@ -15,7 +15,7 @@ import org.postgresql.util.PSQLException;
 /*
  * This class provides information about the database as a whole.
  *
- * $Id: DatabaseMetaData.java,v 1.47 2001/11/19 23:16:46 momjian Exp $
+ * $Id: DatabaseMetaData.java,v 1.48 2002/01/18 17:21:31 davec Exp $
  *
  * <p>Many of the methods here return lists of information in ResultSets.  You
  * can use the normal ResultSet methods such as getString and getInt to
@@ -2427,8 +2427,8 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
                                                                + "pg_class ic,pg_proc p, pg_index i "
                                                                + "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
                                                                + "AND t.tgfoid=p.oid AND tgisconstraint "
-                                                               + ((primaryTable != null) ? "AND c2.relname='" + primaryTable + "' " : "")
-                                                               + ((foreignTable != null) ? "AND c.relname='" + foreignTable + "' " : "")
+                                                               + ((primaryTable != null) ? "AND c.relname='" + primaryTable + "' " : "")
+                                                               + ((foreignTable != null) ? "AND c2.relname='" + foreignTable + "' " : "")
                                                                + "AND i.indrelid=c.oid "
                                                                + "AND i.indexrelid=ic.oid AND i.indisprimary "
                                                                + "ORDER BY c.relname,c2.relname"
@@ -2467,65 +2467,67 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
                                                        else if ("setdefault".equals(rule))
                                                                action = importedKeySetDefault;
                                                        tuple[col] = Integer.toString(action).getBytes();
-                                                       foundRule = true;
-                                               }
-                                       }
-                               }
-                               while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
 
-                               if (foundRule)
-                               {
-                                       tuple[2] = rs.getBytes(2); //PKTABLE_NAME
-                                       tuple[6] = rs.getBytes(1); //FKTABLE_NAME
-
-                                       // Parse the tgargs data
-                                       StringBuffer fkeyColumns = new StringBuffer();
-                                       StringBuffer pkeyColumns = new StringBuffer();
-                                       int numColumns = (rs.getInt(7) >> 1) - 2;
-                                       String s = rs.getString(8);
-                                       int pos = s.lastIndexOf("\\000");
-                                       for (int c = 0;c < numColumns;c++)
-                                       {
-                                               if (pos > -1)
-                                               {
-                                                       int pos2 = s.lastIndexOf("\\000", pos - 1);
-                                                       if (pos2 > -1)
+                                                       if (!foundRule)
                                                        {
-                                                               if (fkeyColumns.length() > 0)
-                                                                       fkeyColumns.insert(0, ',');
-                                                               fkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //FKCOLUMN_NAME
-                                                               pos = s.lastIndexOf("\\000", pos2 - 1);
-                                                               if (pos > -1)
+                                                               tuple[2] = rs.getBytes(1); //PKTABLE_NAME
+                                                               tuple[6] = rs.getBytes(2); //FKTABLE_NAME
+
+                                                               // Parse the tgargs data
+                                                               StringBuffer fkeyColumns = new StringBuffer();
+                                                               StringBuffer pkeyColumns = new StringBuffer();
+                                                               int numColumns = (rs.getInt(7) >> 1) - 2;
+                                                               String s = rs.getString(8);
+                                                               int pos = s.lastIndexOf("\\000");
+                                                               for (int c = 0;c < numColumns;c++)
+                                                               {
+                                                                       if (pos > -1)
+                                                                       {
+                                                                               int pos2 = s.lastIndexOf("\\000", pos - 1);
+                                                                               if (pos2 > -1)
+                                                                               {
+                                                                                       if (pkeyColumns.length() > 0)
+                                                                                               pkeyColumns.insert(0, ',');
+                                                                                       pkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //PKCOLUMN_NAME
+                                                                                       pos = s.lastIndexOf("\\000", pos2 - 1);
+                                                                                       if (pos > -1)
+                                                                                       {
+                                                                                               if (fkeyColumns.length() > 0)
+                                                                                                   fkeyColumns.insert(0, ',');
+                                                                                               fkeyColumns.insert(0, s.substring(pos + 4, pos2)); //FKCOLUMN_NAME
+                                                                                       }
+                                                                               }
+                                                                       }
+                                                               }
+                                                               tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
+                                                               tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
+
+                                                               tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
+                                                               tuple[11] = fKeyName.getBytes(); //FK_NAME
+                                                               tuple[12] = rs.getBytes(4); //PK_NAME
+
+                                                               // DEFERRABILITY
+                                                               int deferrability = importedKeyNotDeferrable;
+                                                               boolean deferrable = rs.getBoolean(5);
+                                                               boolean initiallyDeferred = rs.getBoolean(6);
+                                                               if (deferrable)
                                                                {
-                                                                       if (pkeyColumns.length() > 0)
-                                                                               pkeyColumns.insert(0, ',');
-                                                                       pkeyColumns.insert(0, s.substring(pos + 4, pos2)); //PKCOLUMN_NAME
+                                                                       if (initiallyDeferred)
+                                                                               deferrability = importedKeyInitiallyDeferred;
+                                                                       else
+                                                                               deferrability = importedKeyInitiallyImmediate;
                                                                }
+                                                               tuple[13] = Integer.toString(deferrability).getBytes();
+
+                                                               foundRule = true;
                                                        }
                                                }
                                        }
-                                       tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
-                                       tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
-
-                                       tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
-                                       tuple[11] = fKeyName.getBytes(); //FK_NAME
-                                       tuple[12] = rs.getBytes(4); //PK_NAME
-
-                                       // DEFERRABILITY
-                                       int deferrability = importedKeyNotDeferrable;
-                                       boolean deferrable = rs.getBoolean(5);
-                                       boolean initiallyDeferred = rs.getBoolean(6);
-                                       if (deferrable)
-                                       {
-                                               if (initiallyDeferred)
-                                                       deferrability = importedKeyInitiallyDeferred;
-                                               else
-                                                       deferrability = importedKeyInitiallyImmediate;
-                                       }
-                                       tuple[13] = Integer.toString(deferrability).getBytes();
-
-                                       tuples.addElement(tuple);
                                }
+                               while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
+
+                               if(foundRule) tuples.addElement(tuple);
+
                        }
                        while (hasMore);
                }