Arrow head sizes adjusted
[sql2dot:sql2dot.git] / sql2dot.awk
1 #!/usr/bin/awk
2
3 BEGIN {
4         print "digraph \"\" {"
5         print " graph [rankdir=LR];"
6         print " node [shape=record,fontsize=8];"
7         print " edge [arrowsize=0.5];"
8         tablename=""
9 }
10
11 # table definitions rules
12
13 /CREATE TABLE/ {
14         tablename=$3
15         attribs=""
16 }
17
18 tablename && NF > 1 {
19         if (!attribs)
20                 attribs=" <" tablename "> " tablename
21         else {
22                 attr = $1
23                 gsub("\"", "\\\"", attr)  # escape double-quotes
24                 attribs = attribs "| <" attr "> " attr " "
25         }
26 }
27
28 /^\)/ && tablename {
29         print " " tablename " ["
30         print "         label=\"" attribs "\""
31         print " ];"
32 }
33
34 /;$/ && tablename {
35         if ($1 ~ "INHERITS") {
36                 parent=$2
37                 gsub("\(|\)|;", "", parent)
38                 print " " tablename ":" tablename " -> " parent ":" parent " [arrowhead=onormal,arrowsize=2.0]"
39         }
40
41         tablename=""
42 }
43
44 # relationship rules
45
46 /ALTER TABLE ONLY/ {
47         reltable=$4
48 }
49
50 /FOREIGN KEY/ && reltable {
51         relfield = $6
52         rellink = $8
53         gsub("\(|\)", "", relfield)
54         gsub("\(", ":", rellink)
55         gsub("\)", "", rellink)
56         print " " reltable ":" relfield " -> " rellink
57         reltable=""
58 }
59
60
61 END {
62         print "}"
63 }