From 8348c1c4fd096e853624f7b6758fa10bc7b6cb74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20W=C3=BChrer?= Date: Sun, 17 Mar 2024 11:55:21 +0100 Subject: map-db --- map-db | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 map-db (limited to 'map-db') 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 '>]' + 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 "$Column$Type" + else + echo "$Column$Type" + 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=<" + echo '
' + 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 '
\\N
>]' +done +echo '}' -- cgit v1.2.3