1999-03-26 15:21:58 +08:00
|
|
|
#! /bin/sh
|
|
|
|
|
1998-09-14 09:14:49 +08:00
|
|
|
# You first run findoidjoins on the template1 database, and send that
|
2001-08-11 02:57:42 +08:00
|
|
|
# output into this script to generate a list of SQL statements.
|
1999-03-26 15:21:58 +08:00
|
|
|
|
|
|
|
# NOTE: any field that findoidjoins thinks joins to more than one table
|
|
|
|
# will NOT be checked by the output of this script. You should be
|
|
|
|
# suspicious of multiple entries in findoidjoins' output.
|
|
|
|
|
|
|
|
# Caution: you may need to use GNU awk.
|
|
|
|
AWK=${AWK:-awk}
|
|
|
|
|
2004-11-04 10:04:10 +08:00
|
|
|
TMP="${TMPDIR:-/tmp}/make_oidjoins_check.$$"
|
|
|
|
trap "rm -rf $TMP" 0 1 2 3 15
|
2004-10-21 00:42:46 +08:00
|
|
|
|
2004-11-04 06:46:15 +08:00
|
|
|
# Create a temporary directory with the proper permissions so no one can
|
|
|
|
# intercept our temporary files and cause a security breach.
|
|
|
|
OMASK="`umask`"
|
|
|
|
umask 077
|
|
|
|
if ! mkdir $TMP
|
|
|
|
then echo "Can't create temporary directory $TMP." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
umask "$OMASK"
|
|
|
|
unset OMASK
|
|
|
|
|
|
|
|
INPUTFILE="$TMP/a"
|
|
|
|
DUPSFILE="$TMP/b"
|
|
|
|
NONDUPSFILE="$TMP/c"
|
1998-09-14 09:14:49 +08:00
|
|
|
|
1999-03-26 15:21:58 +08:00
|
|
|
# Read input
|
2004-10-21 00:42:46 +08:00
|
|
|
cat "$@" >$INPUTFILE
|
1999-03-26 15:21:58 +08:00
|
|
|
|
|
|
|
# Look for fields with multiple references.
|
2004-10-21 00:42:46 +08:00
|
|
|
cat $INPUTFILE | cut -d' ' -f2 | sort | uniq -d >$DUPSFILE
|
|
|
|
if [ -s $DUPSFILE ] ; then
|
1999-03-26 15:21:58 +08:00
|
|
|
echo "Ignoring these fields that link to multiple tables:" 1>&2
|
2004-10-21 00:42:46 +08:00
|
|
|
cat $DUPSFILE 1>&2
|
1999-03-26 15:21:58 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Get the non-multiply-referenced fields.
|
2004-10-21 00:42:46 +08:00
|
|
|
cat $INPUTFILE | while read LINE
|
1998-09-14 09:14:49 +08:00
|
|
|
do
|
|
|
|
set -- $LINE
|
2004-10-21 00:42:46 +08:00
|
|
|
grep "^$2\$" $DUPSFILE >/dev/null 2>&1 || echo $LINE
|
|
|
|
done >$NONDUPSFILE
|
1999-03-26 15:21:58 +08:00
|
|
|
|
|
|
|
# Generate the output.
|
2004-10-21 00:42:46 +08:00
|
|
|
cat $NONDUPSFILE |
|
1999-03-26 15:21:58 +08:00
|
|
|
$AWK -F'[ \.]' '\
|
1998-09-14 09:14:49 +08:00
|
|
|
BEGIN \
|
|
|
|
{
|
|
|
|
printf "\
|
|
|
|
--\n\
|
|
|
|
-- This is created by pgsql/contrib/findoidjoins/make_oidjoin_check\n\
|
|
|
|
--\n";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
printf "\
|
2002-09-06 03:57:32 +08:00
|
|
|
SELECT ctid, %s \n\
|
|
|
|
FROM %s.%s fk \n\
|
|
|
|
WHERE %s != 0 AND \n\
|
|
|
|
NOT EXISTS(SELECT 1 FROM %s.%s pk WHERE pk.oid = fk.%s);\n",
|
|
|
|
$4, $2, $3, $4,
|
|
|
|
$6, $7, $4;
|
1998-09-14 09:14:49 +08:00
|
|
|
}'
|
1999-03-26 15:21:58 +08:00
|
|
|
|
|
|
|
exit 0
|