aboutsummaryrefslogtreecommitdiff
path: root/contrib/findoidjoins/make_oidjoins_check
blob: 5106eb1bfd940c1aefdb59852745616158ba89ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /bin/sh

# You first run findoidjoins on the template1 database, and send that
# output into this script to generate a list of SQL statements.

# 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}

INPUTFILE="tmp$$a"
DUPSFILE="tmp$$b"
NONDUPSFILE="tmp$$c"
rm -f $INPUTFILE $DUPSFILE $NONDUPSFILE

trap "rm -f $INPUTFILE $DUPSFILE $NONDUPSFILE" 0 1 2 3 15

# Read input
cat "$@" >$INPUTFILE

# Look for fields with multiple references.
cat $INPUTFILE | cut -d' ' -f2 | sort | uniq -d >$DUPSFILE
if [ -s $DUPSFILE ] ; then
	echo "Ignoring these fields that link to multiple tables:" 1>&2
	cat $DUPSFILE 1>&2
fi

# Get the non-multiply-referenced fields.
cat $INPUTFILE | while read LINE
do
	set -- $LINE
	grep "^$2\$" $DUPSFILE >/dev/null 2>&1 || echo $LINE
done >$NONDUPSFILE

# Generate the output.
cat $NONDUPSFILE |
$AWK -F'[ \.]' '\
	BEGIN \
	{
		printf "\
--\n\
-- This is created by pgsql/contrib/findoidjoins/make_oidjoin_check\n\
--\n";
	}
	{
		printf "\
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;
	}'

exit 0