summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Wührer <def@gmx.at>2024-03-17 11:55:21 +0100
committerDavid Wührer <def@gmx.at>2024-03-17 11:55:21 +0100
commit8348c1c4fd096e853624f7b6758fa10bc7b6cb74 (patch)
tree7c60e95b583435d0f1c02e0b54039d2fe9ecf367
parent4e0ac4acb97308b782c39d1137f03a56f635c88f (diff)
map-db
-rwxr-xr-xmap-db60
1 files changed, 60 insertions, 0 deletions
diff --git a/map-db b/map-db
new file mode 100755
index 0000000..feb528c
--- /dev/null
+++ b/map-db
@@ -0,0 +1,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 '}'