Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 83b5ae6

Browse files
author
Dave Cramer
committed
Fixes to getImportedKeys/getExportedKeys from Jason Davies
Previous versions did not correctly identify primary/foreign keys
1 parent bb698c2 commit 83b5ae6

File tree

2 files changed

+112
-108
lines changed

2 files changed

+112
-108
lines changed

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

+56-54
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/*
1414
* This class provides information about the database as a whole.
1515
*
16-
* $Id: DatabaseMetaData.java,v 1.40 2001/11/19 23:16:45 momjian Exp $
16+
* $Id: DatabaseMetaData.java,v 1.41 2002/01/18 17:21:51 davec Exp $
1717
*
1818
* <p>Many of the methods here return lists of information in ResultSets. You
1919
* can use the normal ResultSet methods such as getString and getInt to
@@ -2299,8 +2299,8 @@ private java.sql.ResultSet getImportedExportedKeys(String catalog, String schema
22992299
+ "pg_class ic,pg_proc p, pg_index i "
23002300
+ "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
23012301
+ "AND t.tgfoid=p.oid AND tgisconstraint "
2302-
+ ((primaryTable != null) ? "AND c2.relname='" + primaryTable + "' " : "")
2303-
+ ((foreignTable != null) ? "AND c.relname='" + foreignTable + "' " : "")
2302+
+ ((primaryTable != null) ? "AND c.relname='" + primaryTable + "' " : "")
2303+
+ ((foreignTable != null) ? "AND c2.relname='" + foreignTable + "' " : "")
23042304
+ "AND i.indrelid=c.oid "
23052305
+ "AND i.indexrelid=ic.oid AND i.indisprimary "
23062306
+ "ORDER BY c.relname,c2.relname"
@@ -2339,65 +2339,67 @@ else if ("setnull".equals(rule))
23392339
else if ("setdefault".equals(rule))
23402340
action = importedKeySetDefault;
23412341
tuple[col] = Integer.toString(action).getBytes();
2342-
foundRule = true;
2343-
}
2344-
}
2345-
}
2346-
while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
23472342

2348-
if (foundRule)
2349-
{
2350-
tuple[2] = rs.getBytes(2); //PKTABLE_NAME
2351-
tuple[6] = rs.getBytes(1); //FKTABLE_NAME
2352-
2353-
// Parse the tgargs data
2354-
StringBuffer fkeyColumns = new StringBuffer();
2355-
StringBuffer pkeyColumns = new StringBuffer();
2356-
int numColumns = (rs.getInt(7) >> 1) - 2;
2357-
String s = rs.getString(8);
2358-
int pos = s.lastIndexOf("\\000");
2359-
for (int c = 0;c < numColumns;c++)
2360-
{
2361-
if (pos > -1)
2362-
{
2363-
int pos2 = s.lastIndexOf("\\000", pos - 1);
2364-
if (pos2 > -1)
2343+
if (!foundRule)
23652344
{
2366-
if (fkeyColumns.length() > 0)
2367-
fkeyColumns.insert(0, ',');
2368-
fkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //FKCOLUMN_NAME
2369-
pos = s.lastIndexOf("\\000", pos2 - 1);
2370-
if (pos > -1)
2345+
tuple[2] = rs.getBytes(1); //PKTABLE_NAME
2346+
tuple[6] = rs.getBytes(2); //FKTABLE_NAME
2347+
2348+
// Parse the tgargs data
2349+
StringBuffer fkeyColumns = new StringBuffer();
2350+
StringBuffer pkeyColumns = new StringBuffer();
2351+
int numColumns = (rs.getInt(7) >> 1) - 2;
2352+
String s = rs.getString(8);
2353+
int pos = s.lastIndexOf("\\000");
2354+
for (int c = 0;c < numColumns;c++)
2355+
{
2356+
if (pos > -1)
2357+
{
2358+
int pos2 = s.lastIndexOf("\\000", pos - 1);
2359+
if (pos2 > -1)
2360+
{
2361+
if (pkeyColumns.length() > 0)
2362+
pkeyColumns.insert(0, ',');
2363+
pkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //PKCOLUMN_NAME
2364+
pos = s.lastIndexOf("\\000", pos2 - 1);
2365+
if (pos > -1)
2366+
{
2367+
if (fkeyColumns.length() > 0)
2368+
fkeyColumns.insert(0, ',');
2369+
fkeyColumns.insert(0, s.substring(pos + 4, pos2)); //FKCOLUMN_NAME
2370+
}
2371+
}
2372+
}
2373+
}
2374+
tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
2375+
tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
2376+
2377+
tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
2378+
tuple[11] = fKeyName.getBytes(); //FK_NAME
2379+
tuple[12] = rs.getBytes(4); //PK_NAME
2380+
2381+
// DEFERRABILITY
2382+
int deferrability = importedKeyNotDeferrable;
2383+
boolean deferrable = rs.getBoolean(5);
2384+
boolean initiallyDeferred = rs.getBoolean(6);
2385+
if (deferrable)
23712386
{
2372-
if (pkeyColumns.length() > 0)
2373-
pkeyColumns.insert(0, ',');
2374-
pkeyColumns.insert(0, s.substring(pos + 4, pos2)); //PKCOLUMN_NAME
2387+
if (initiallyDeferred)
2388+
deferrability = importedKeyInitiallyDeferred;
2389+
else
2390+
deferrability = importedKeyInitiallyImmediate;
23752391
}
2392+
tuple[13] = Integer.toString(deferrability).getBytes();
2393+
2394+
foundRule = true;
23762395
}
23772396
}
23782397
}
2379-
tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
2380-
tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
2381-
2382-
tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
2383-
tuple[11] = fKeyName.getBytes(); //FK_NAME
2384-
tuple[12] = rs.getBytes(4); //PK_NAME
2385-
2386-
// DEFERRABILITY
2387-
int deferrability = importedKeyNotDeferrable;
2388-
boolean deferrable = rs.getBoolean(5);
2389-
boolean initiallyDeferred = rs.getBoolean(6);
2390-
if (deferrable)
2391-
{
2392-
if (initiallyDeferred)
2393-
deferrability = importedKeyInitiallyDeferred;
2394-
else
2395-
deferrability = importedKeyInitiallyImmediate;
2396-
}
2397-
tuple[13] = Integer.toString(deferrability).getBytes();
2398-
2399-
tuples.addElement(tuple);
24002398
}
2399+
while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
2400+
2401+
if(foundRule) tuples.addElement(tuple);
2402+
24012403
}
24022404
while (hasMore);
24032405
}

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

+56-54
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/*
1616
* This class provides information about the database as a whole.
1717
*
18-
* $Id: DatabaseMetaData.java,v 1.47 2001/11/19 23:16:46 momjian Exp $
18+
* $Id: DatabaseMetaData.java,v 1.48 2002/01/18 17:21:31 davec Exp $
1919
*
2020
* <p>Many of the methods here return lists of information in ResultSets. You
2121
* can use the normal ResultSet methods such as getString and getInt to
@@ -2427,8 +2427,8 @@ private java.sql.ResultSet getImportedExportedKeys(String catalog, String schema
24272427
+ "pg_class ic,pg_proc p, pg_index i "
24282428
+ "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
24292429
+ "AND t.tgfoid=p.oid AND tgisconstraint "
2430-
+ ((primaryTable != null) ? "AND c2.relname='" + primaryTable + "' " : "")
2431-
+ ((foreignTable != null) ? "AND c.relname='" + foreignTable + "' " : "")
2430+
+ ((primaryTable != null) ? "AND c.relname='" + primaryTable + "' " : "")
2431+
+ ((foreignTable != null) ? "AND c2.relname='" + foreignTable + "' " : "")
24322432
+ "AND i.indrelid=c.oid "
24332433
+ "AND i.indexrelid=ic.oid AND i.indisprimary "
24342434
+ "ORDER BY c.relname,c2.relname"
@@ -2467,65 +2467,67 @@ else if ("setnull".equals(rule))
24672467
else if ("setdefault".equals(rule))
24682468
action = importedKeySetDefault;
24692469
tuple[col] = Integer.toString(action).getBytes();
2470-
foundRule = true;
2471-
}
2472-
}
2473-
}
2474-
while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
24752470

2476-
if (foundRule)
2477-
{
2478-
tuple[2] = rs.getBytes(2); //PKTABLE_NAME
2479-
tuple[6] = rs.getBytes(1); //FKTABLE_NAME
2480-
2481-
// Parse the tgargs data
2482-
StringBuffer fkeyColumns = new StringBuffer();
2483-
StringBuffer pkeyColumns = new StringBuffer();
2484-
int numColumns = (rs.getInt(7) >> 1) - 2;
2485-
String s = rs.getString(8);
2486-
int pos = s.lastIndexOf("\\000");
2487-
for (int c = 0;c < numColumns;c++)
2488-
{
2489-
if (pos > -1)
2490-
{
2491-
int pos2 = s.lastIndexOf("\\000", pos - 1);
2492-
if (pos2 > -1)
2471+
if (!foundRule)
24932472
{
2494-
if (fkeyColumns.length() > 0)
2495-
fkeyColumns.insert(0, ',');
2496-
fkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //FKCOLUMN_NAME
2497-
pos = s.lastIndexOf("\\000", pos2 - 1);
2498-
if (pos > -1)
2473+
tuple[2] = rs.getBytes(1); //PKTABLE_NAME
2474+
tuple[6] = rs.getBytes(2); //FKTABLE_NAME
2475+
2476+
// Parse the tgargs data
2477+
StringBuffer fkeyColumns = new StringBuffer();
2478+
StringBuffer pkeyColumns = new StringBuffer();
2479+
int numColumns = (rs.getInt(7) >> 1) - 2;
2480+
String s = rs.getString(8);
2481+
int pos = s.lastIndexOf("\\000");
2482+
for (int c = 0;c < numColumns;c++)
2483+
{
2484+
if (pos > -1)
2485+
{
2486+
int pos2 = s.lastIndexOf("\\000", pos - 1);
2487+
if (pos2 > -1)
2488+
{
2489+
if (pkeyColumns.length() > 0)
2490+
pkeyColumns.insert(0, ',');
2491+
pkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //PKCOLUMN_NAME
2492+
pos = s.lastIndexOf("\\000", pos2 - 1);
2493+
if (pos > -1)
2494+
{
2495+
if (fkeyColumns.length() > 0)
2496+
fkeyColumns.insert(0, ',');
2497+
fkeyColumns.insert(0, s.substring(pos + 4, pos2)); //FKCOLUMN_NAME
2498+
}
2499+
}
2500+
}
2501+
}
2502+
tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
2503+
tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
2504+
2505+
tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
2506+
tuple[11] = fKeyName.getBytes(); //FK_NAME
2507+
tuple[12] = rs.getBytes(4); //PK_NAME
2508+
2509+
// DEFERRABILITY
2510+
int deferrability = importedKeyNotDeferrable;
2511+
boolean deferrable = rs.getBoolean(5);
2512+
boolean initiallyDeferred = rs.getBoolean(6);
2513+
if (deferrable)
24992514
{
2500-
if (pkeyColumns.length() > 0)
2501-
pkeyColumns.insert(0, ',');
2502-
pkeyColumns.insert(0, s.substring(pos + 4, pos2)); //PKCOLUMN_NAME
2515+
if (initiallyDeferred)
2516+
deferrability = importedKeyInitiallyDeferred;
2517+
else
2518+
deferrability = importedKeyInitiallyImmediate;
25032519
}
2520+
tuple[13] = Integer.toString(deferrability).getBytes();
2521+
2522+
foundRule = true;
25042523
}
25052524
}
25062525
}
2507-
tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
2508-
tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
2509-
2510-
tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
2511-
tuple[11] = fKeyName.getBytes(); //FK_NAME
2512-
tuple[12] = rs.getBytes(4); //PK_NAME
2513-
2514-
// DEFERRABILITY
2515-
int deferrability = importedKeyNotDeferrable;
2516-
boolean deferrable = rs.getBoolean(5);
2517-
boolean initiallyDeferred = rs.getBoolean(6);
2518-
if (deferrable)
2519-
{
2520-
if (initiallyDeferred)
2521-
deferrability = importedKeyInitiallyDeferred;
2522-
else
2523-
deferrability = importedKeyInitiallyImmediate;
2524-
}
2525-
tuple[13] = Integer.toString(deferrability).getBytes();
2526-
2527-
tuples.addElement(tuple);
25282526
}
2527+
while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
2528+
2529+
if(foundRule) tuples.addElement(tuple);
2530+
25292531
}
25302532
while (hasMore);
25312533
}

0 commit comments

Comments
 (0)