1998-08-11 10:32:00 +08:00
|
|
|
/*
|
2000-01-23 07:05:14 +08:00
|
|
|
* findoidjoins.c, requires src/interfaces/libpgeasy
|
1998-08-11 10:32:00 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-08-11 13:32:46 +08:00
|
|
|
#include <string.h>
|
2000-01-23 07:05:14 +08:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
|
1998-08-11 10:32:00 +08:00
|
|
|
#include "halt.h"
|
2000-04-29 06:49:25 +08:00
|
|
|
#include <libpgeasy.h>
|
1998-08-11 10:32:00 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
PGresult *attres,
|
|
|
|
*relres;
|
1998-08-11 10:32:00 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char query[4000];
|
|
|
|
char relname[256];
|
|
|
|
char relname2[256];
|
|
|
|
char attname[256];
|
1998-08-11 13:32:46 +08:00
|
|
|
char typname[256];
|
1998-08-11 10:32:00 +08:00
|
|
|
int count;
|
2000-05-18 22:24:38 +08:00
|
|
|
char optstr[256];
|
1998-08-11 10:32:00 +08:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
halt("Usage: %s database\n", argv[0]);
|
|
|
|
|
2000-05-18 22:24:38 +08:00
|
|
|
snprintf(optstr, 256, "dbname=%s", argv[1]);
|
|
|
|
connectdb(optstr);
|
|
|
|
|
1998-08-11 10:32:00 +08:00
|
|
|
on_error_continue();
|
|
|
|
on_error_stop();
|
|
|
|
|
|
|
|
doquery("BEGIN WORK");
|
|
|
|
doquery("\
|
|
|
|
DECLARE c_attributes BINARY CURSOR FOR \
|
1998-08-11 13:32:46 +08:00
|
|
|
SELECT typname, relname, a.attname \
|
1998-08-11 10:32:00 +08:00
|
|
|
FROM pg_class c, pg_attribute a, pg_type t \
|
|
|
|
WHERE a.attnum > 0 AND \
|
|
|
|
relkind = 'r' AND \
|
1998-09-14 09:14:49 +08:00
|
|
|
relhasrules = 'f' AND \
|
1998-08-11 13:32:46 +08:00
|
|
|
(typname = 'oid' OR \
|
|
|
|
typname = 'regproc') AND \
|
1998-08-11 10:32:00 +08:00
|
|
|
a.attrelid = c.oid AND \
|
|
|
|
a.atttypid = t.oid \
|
1998-08-12 02:28:49 +08:00
|
|
|
ORDER BY 2, a.attnum ; \
|
1998-08-11 10:32:00 +08:00
|
|
|
");
|
|
|
|
doquery("FETCH ALL IN c_attributes");
|
|
|
|
attres = get_result();
|
|
|
|
|
|
|
|
doquery("\
|
|
|
|
DECLARE c_relations BINARY CURSOR FOR \
|
|
|
|
SELECT relname \
|
|
|
|
FROM pg_class c \
|
|
|
|
WHERE relkind = 'r' AND \
|
1999-03-26 15:21:58 +08:00
|
|
|
relhasrules = 'f' \
|
1998-08-11 10:32:00 +08:00
|
|
|
ORDER BY 1; \
|
|
|
|
");
|
|
|
|
doquery("FETCH ALL IN c_relations");
|
|
|
|
relres = get_result();
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1998-08-11 10:32:00 +08:00
|
|
|
set_result(attres);
|
1998-08-11 13:32:46 +08:00
|
|
|
while (fetch(typname, relname, attname) != END_OF_TUPLES)
|
1998-08-11 10:32:00 +08:00
|
|
|
{
|
|
|
|
set_result(relres);
|
|
|
|
reset_fetch();
|
|
|
|
while (fetch(relname2) != END_OF_TUPLES)
|
|
|
|
{
|
|
|
|
unset_result(relres);
|
1998-08-11 13:32:46 +08:00
|
|
|
if (strcmp(typname, "oid") == 0)
|
1999-05-26 00:15:34 +08:00
|
|
|
sprintf(query, "\
|
1998-08-11 13:32:46 +08:00
|
|
|
DECLARE c_matches BINARY CURSOR FOR \
|
1999-05-26 00:15:34 +08:00
|
|
|
SELECT count(*) \
|
2000-01-23 07:05:14 +08:00
|
|
|
FROM %s t1, %s t2 \
|
|
|
|
WHERE t1.%s = t2.oid ", relname, relname2, attname);
|
1998-08-11 13:32:46 +08:00
|
|
|
else
|
1999-05-26 00:15:34 +08:00
|
|
|
sprintf(query, "\
|
1998-08-11 13:32:46 +08:00
|
|
|
DECLARE c_matches BINARY CURSOR FOR \
|
1999-05-26 00:15:34 +08:00
|
|
|
SELECT count(*) \
|
2000-01-23 07:05:14 +08:00
|
|
|
FROM %s t1, %s t2 \
|
|
|
|
WHERE RegprocToOid(t1.%s) = t2.oid ", relname, relname2, attname);
|
1998-08-11 10:32:00 +08:00
|
|
|
|
|
|
|
doquery(query);
|
|
|
|
doquery("FETCH ALL IN c_matches");
|
|
|
|
fetch(&count);
|
|
|
|
if (count != 0)
|
|
|
|
printf("Join %s.%s => %s.oid\n", relname, attname, relname2);
|
|
|
|
doquery("CLOSE c_matches");
|
|
|
|
set_result(relres);
|
|
|
|
}
|
|
|
|
set_result(attres);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_result(relres);
|
|
|
|
doquery("CLOSE c_relations");
|
|
|
|
PQclear(relres);
|
|
|
|
|
|
|
|
set_result(attres);
|
|
|
|
doquery("CLOSE c_attributes");
|
|
|
|
PQclear(attres);
|
|
|
|
unset_result(attres);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
1998-08-11 10:32:00 +08:00
|
|
|
doquery("COMMIT WORK");
|
|
|
|
|
|
|
|
disconnectdb();
|
|
|
|
return 0;
|
|
|
|
}
|