mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-04 00:02:52 -05:00 
			
		
		
		
	Fixes to getImportedKeys/getExportedKeys from Jason Davies
Previous versions did not correctly identify primary/foreign keys
This commit is contained in:
		
							parent
							
								
									bb698c25c7
								
							
						
					
					
						commit
						83b5ae65a0
					
				@ -13,7 +13,7 @@ import org.postgresql.util.PSQLException;
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * This class provides information about the database as a whole.
 | 
					 * 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
 | 
					 * <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
 | 
					 * 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 "
 | 
													+ "pg_class ic,pg_proc p, pg_index i "
 | 
				
			||||||
								+ "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
 | 
													+ "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
 | 
				
			||||||
								+ "AND t.tgfoid=p.oid AND tgisconstraint "
 | 
													+ "AND t.tgfoid=p.oid AND tgisconstraint "
 | 
				
			||||||
								+ ((primaryTable != null) ? "AND c2.relname='" + primaryTable + "' " : "")
 | 
													+ ((primaryTable != null) ? "AND c.relname='" + primaryTable + "' " : "")
 | 
				
			||||||
								+ ((foreignTable != null) ? "AND c.relname='" + foreignTable + "' " : "")
 | 
													+ ((foreignTable != null) ? "AND c2.relname='" + foreignTable + "' " : "")
 | 
				
			||||||
								+ "AND i.indrelid=c.oid "
 | 
													+ "AND i.indrelid=c.oid "
 | 
				
			||||||
								+ "AND i.indexrelid=ic.oid AND i.indisprimary "
 | 
													+ "AND i.indexrelid=ic.oid AND i.indisprimary "
 | 
				
			||||||
								+ "ORDER BY c.relname,c2.relname"
 | 
													+ "ORDER BY c.relname,c2.relname"
 | 
				
			||||||
@ -2339,16 +2339,11 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
 | 
				
			|||||||
							else if ("setdefault".equals(rule))
 | 
												else if ("setdefault".equals(rule))
 | 
				
			||||||
								action = importedKeySetDefault;
 | 
													action = importedKeySetDefault;
 | 
				
			||||||
							tuple[col] = Integer.toString(action).getBytes();
 | 
												tuple[col] = Integer.toString(action).getBytes();
 | 
				
			||||||
							foundRule = true;
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if (foundRule)
 | 
												if (!foundRule)
 | 
				
			||||||
							{
 | 
												{
 | 
				
			||||||
					tuple[2] = rs.getBytes(2); //PKTABLE_NAME
 | 
													tuple[2] = rs.getBytes(1); //PKTABLE_NAME
 | 
				
			||||||
					tuple[6] = rs.getBytes(1); //FKTABLE_NAME
 | 
													tuple[6] = rs.getBytes(2); //FKTABLE_NAME
 | 
				
			||||||
 | 
					
 | 
				
			||||||
								// Parse the tgargs data
 | 
													// Parse the tgargs data
 | 
				
			||||||
								StringBuffer fkeyColumns = new StringBuffer();
 | 
													StringBuffer fkeyColumns = new StringBuffer();
 | 
				
			||||||
@ -2363,21 +2358,21 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
 | 
				
			|||||||
										int pos2 = s.lastIndexOf("\\000", pos - 1);
 | 
															int pos2 = s.lastIndexOf("\\000", pos - 1);
 | 
				
			||||||
										if (pos2 > -1)
 | 
															if (pos2 > -1)
 | 
				
			||||||
										{
 | 
															{
 | 
				
			||||||
								if (fkeyColumns.length() > 0)
 | 
																if (pkeyColumns.length() > 0)
 | 
				
			||||||
									fkeyColumns.insert(0, ',');
 | 
																        pkeyColumns.insert(0, ',');
 | 
				
			||||||
								fkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //FKCOLUMN_NAME
 | 
																pkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //PKCOLUMN_NAME
 | 
				
			||||||
											pos = s.lastIndexOf("\\000", pos2 - 1);
 | 
																pos = s.lastIndexOf("\\000", pos2 - 1);
 | 
				
			||||||
											if (pos > -1)
 | 
																if (pos > -1)
 | 
				
			||||||
											{
 | 
																{
 | 
				
			||||||
									if (pkeyColumns.length() > 0)
 | 
																	if (fkeyColumns.length() > 0)
 | 
				
			||||||
										pkeyColumns.insert(0, ',');
 | 
																	    fkeyColumns.insert(0, ',');
 | 
				
			||||||
									pkeyColumns.insert(0, s.substring(pos + 4, pos2)); //PKCOLUMN_NAME
 | 
																	fkeyColumns.insert(0, s.substring(pos + 4, pos2)); //FKCOLUMN_NAME
 | 
				
			||||||
											}
 | 
																}
 | 
				
			||||||
										}
 | 
															}
 | 
				
			||||||
									}
 | 
														}
 | 
				
			||||||
								}
 | 
													}
 | 
				
			||||||
					tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
 | 
					 | 
				
			||||||
								tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
 | 
													tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
 | 
				
			||||||
 | 
													tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
 | 
				
			||||||
 | 
					
 | 
				
			||||||
								tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
 | 
													tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
 | 
				
			||||||
								tuple[11] = fKeyName.getBytes(); //FK_NAME
 | 
													tuple[11] = fKeyName.getBytes(); //FK_NAME
 | 
				
			||||||
@ -2396,9 +2391,16 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
 | 
				
			|||||||
								}
 | 
													}
 | 
				
			||||||
								tuple[13] = Integer.toString(deferrability).getBytes();
 | 
													tuple[13] = Integer.toString(deferrability).getBytes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					tuples.addElement(tuple);
 | 
													foundRule = true;
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if(foundRule) tuples.addElement(tuple);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			while (hasMore);
 | 
								while (hasMore);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -15,7 +15,7 @@ import org.postgresql.util.PSQLException;
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * This class provides information about the database as a whole.
 | 
					 * 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
 | 
					 * <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
 | 
					 * 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 "
 | 
													+ "pg_class ic,pg_proc p, pg_index i "
 | 
				
			||||||
								+ "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
 | 
													+ "WHERE t.tgrelid=c.oid AND t.tgconstrrelid=c2.oid "
 | 
				
			||||||
								+ "AND t.tgfoid=p.oid AND tgisconstraint "
 | 
													+ "AND t.tgfoid=p.oid AND tgisconstraint "
 | 
				
			||||||
								+ ((primaryTable != null) ? "AND c2.relname='" + primaryTable + "' " : "")
 | 
													+ ((primaryTable != null) ? "AND c.relname='" + primaryTable + "' " : "")
 | 
				
			||||||
								+ ((foreignTable != null) ? "AND c.relname='" + foreignTable + "' " : "")
 | 
													+ ((foreignTable != null) ? "AND c2.relname='" + foreignTable + "' " : "")
 | 
				
			||||||
								+ "AND i.indrelid=c.oid "
 | 
													+ "AND i.indrelid=c.oid "
 | 
				
			||||||
								+ "AND i.indexrelid=ic.oid AND i.indisprimary "
 | 
													+ "AND i.indexrelid=ic.oid AND i.indisprimary "
 | 
				
			||||||
								+ "ORDER BY c.relname,c2.relname"
 | 
													+ "ORDER BY c.relname,c2.relname"
 | 
				
			||||||
@ -2467,16 +2467,11 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
 | 
				
			|||||||
							else if ("setdefault".equals(rule))
 | 
												else if ("setdefault".equals(rule))
 | 
				
			||||||
								action = importedKeySetDefault;
 | 
													action = importedKeySetDefault;
 | 
				
			||||||
							tuple[col] = Integer.toString(action).getBytes();
 | 
												tuple[col] = Integer.toString(action).getBytes();
 | 
				
			||||||
							foundRule = true;
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if (foundRule)
 | 
												if (!foundRule)
 | 
				
			||||||
							{
 | 
												{
 | 
				
			||||||
					tuple[2] = rs.getBytes(2); //PKTABLE_NAME
 | 
													tuple[2] = rs.getBytes(1); //PKTABLE_NAME
 | 
				
			||||||
					tuple[6] = rs.getBytes(1); //FKTABLE_NAME
 | 
													tuple[6] = rs.getBytes(2); //FKTABLE_NAME
 | 
				
			||||||
 | 
					
 | 
				
			||||||
								// Parse the tgargs data
 | 
													// Parse the tgargs data
 | 
				
			||||||
								StringBuffer fkeyColumns = new StringBuffer();
 | 
													StringBuffer fkeyColumns = new StringBuffer();
 | 
				
			||||||
@ -2491,21 +2486,21 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
 | 
				
			|||||||
										int pos2 = s.lastIndexOf("\\000", pos - 1);
 | 
															int pos2 = s.lastIndexOf("\\000", pos - 1);
 | 
				
			||||||
										if (pos2 > -1)
 | 
															if (pos2 > -1)
 | 
				
			||||||
										{
 | 
															{
 | 
				
			||||||
								if (fkeyColumns.length() > 0)
 | 
																if (pkeyColumns.length() > 0)
 | 
				
			||||||
									fkeyColumns.insert(0, ',');
 | 
																        pkeyColumns.insert(0, ',');
 | 
				
			||||||
								fkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //FKCOLUMN_NAME
 | 
																pkeyColumns.insert(0, s.substring(pos2 + 4, pos)); //PKCOLUMN_NAME
 | 
				
			||||||
											pos = s.lastIndexOf("\\000", pos2 - 1);
 | 
																pos = s.lastIndexOf("\\000", pos2 - 1);
 | 
				
			||||||
											if (pos > -1)
 | 
																if (pos > -1)
 | 
				
			||||||
											{
 | 
																{
 | 
				
			||||||
									if (pkeyColumns.length() > 0)
 | 
																	if (fkeyColumns.length() > 0)
 | 
				
			||||||
										pkeyColumns.insert(0, ',');
 | 
																	    fkeyColumns.insert(0, ',');
 | 
				
			||||||
									pkeyColumns.insert(0, s.substring(pos + 4, pos2)); //PKCOLUMN_NAME
 | 
																	fkeyColumns.insert(0, s.substring(pos + 4, pos2)); //FKCOLUMN_NAME
 | 
				
			||||||
											}
 | 
																}
 | 
				
			||||||
										}
 | 
															}
 | 
				
			||||||
									}
 | 
														}
 | 
				
			||||||
								}
 | 
													}
 | 
				
			||||||
					tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
 | 
					 | 
				
			||||||
								tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
 | 
													tuple[3] = pkeyColumns.toString().getBytes(); //PKCOLUMN_NAME
 | 
				
			||||||
 | 
													tuple[7] = fkeyColumns.toString().getBytes(); //FKCOLUMN_NAME
 | 
				
			||||||
 | 
					
 | 
				
			||||||
								tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
 | 
													tuple[8] = Integer.toString(seq++).getBytes(); //KEY_SEQ
 | 
				
			||||||
								tuple[11] = fKeyName.getBytes(); //FK_NAME
 | 
													tuple[11] = fKeyName.getBytes(); //FK_NAME
 | 
				
			||||||
@ -2524,9 +2519,16 @@ public class DatabaseMetaData implements java.sql.DatabaseMetaData
 | 
				
			|||||||
								}
 | 
													}
 | 
				
			||||||
								tuple[13] = Integer.toString(deferrability).getBytes();
 | 
													tuple[13] = Integer.toString(deferrability).getBytes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					tuples.addElement(tuple);
 | 
													foundRule = true;
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									while ((hasMore = rs.next()) && fKeyName.equals(rs.getString(3)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if(foundRule) tuples.addElement(tuple);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			while (hasMore);
 | 
								while (hasMore);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user