summaryrefslogtreecommitdiff
path: root/bin/map-db
blob: feb528cabbb2d493d4aac2d8a99df43592e12600 (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
58
59
60
#!/bin/bash

query(){
#	psql-gridware -A -F$'\t' -t -P pager=off -c "$@"
#	psql-gridware -A -F$'\t' -P pager=off -P title=off -c "$@"
	psql-gridware -A -F$'\t' -P pager=off -c "$@"
}

exec {in}<&0

pforkey(){
	echo '</TABLE>>]'
	open_table=0
	data=forkey
}
forkey(){
	# ${constraint name} FOREIGN KEY (${Column}) REFERENCES ${table}(${Column})
	read -ra line <<<"$Column"
	this_column=${line[3]%)}
	this_column=${this_column#(}
	other_table=${line[5]%%(*}
	other_column=${line[5]##*\(}
	other_column=${other_column%)}
	printf '%s:%s_out -> %s:%s_in\n' "$table" "$this_column" "$other_table" "$other_column"
}

record(){
	if [[ $Nullable = 'not null' ]]
	then
		echo "<TR><TD PORT=\"${Column}_in\"><U>$Column</U></TD><TD PORT=\"${Column}_out\">$Type</TD></TR>"
	else
		echo "<TR><TD PORT=\"${Column}_in\">$Column</TD><TD PORT=\"${Column}_out\">$Type</TD></TR>"
	fi
}

echo 'strict digraph db {'
echo 'node[shape=plain]'
query '\d' -t | cut -f 2,3 | grep table\$ | cut -f1 | while read -r table
do
	printf '\e[1m%s\e[0m\n' "$table" >&2
	data=record
	open_table=1
	echo
	echo "$table [label=<<TABLE BORDER=\"0\"><TR><TD COLSPAN=\"2\">\\N</TD></TR>"
	echo '<HR/>'
	while IFS=$'\t' read -r Column Type Collation Nullable Default  
	do
		case $Column in
			Foreign-key\ constraints:)
				echo >&2 'foreign keys'
				data=pforkey
		;;	*:)
				echo >&2 '… skipping '"$Column"
				data=:
		esac
		$data
	done < <(query '\d '"$table" <&${in} | tail -n+3 | tr -d $'\r')
	[[ $open_table = 1 ]] && echo '</TABLE>>]'
done
echo '}'