making it more robust

master
Marc Wäckerlin 9 years ago
parent 11aedd59de
commit 404158ee7d
  1. 80
      scripts/sql-to-dot.sed

@ -1,39 +1,79 @@
#! /bin/sed -f #! /bin/sed -nf
1i\ 1i\
/** @page database Database Schema\ /** @page database Database Schema\
\ \
@dot\ @dot\
digraph schema { digraph schema {
# remove newlines # get everithing on one single line
:a;N;$!ba;s/\n/ /g H;$!d;$x
# remove all single-line comment lines
s/\n--[^\n]*//g
# reduce spaces # reduce spaces
s, \+, ,g s,\t\| \+, ,g
# remove multiline comments
:f;s,\(.*\)/\*.*\*/[ \n]*;*,\1,g;tf
# remove empty lines
s,\n\+,\n,g
# remove unknown commands
s,\(;\|\n\) *\(DELIMITER\|USE\|DROP\|CREATE[ \n]\+DATABASE\)[ \n]\+[^;]*;\+,,ig
# convert special characters within quotes
:a;s/^\(\([^"]*"[^",]*"\)*[^"]*"[^"]*\),\([^"]*".*\)/\1\##COMMA##\3/g;ta
:c;s/^\(\([^']*'[^',]*'\)*[^']*'[^']*\),\([^']*'.*\)/\1\##COMMA##\3/g;tc
# backup everything to the buffer
# then analyze only on one create table
:i
h
s,.*\(create[ \n]\+table[^;]*;\).*,\1,ig
# start html table node
s|CREATE[ \n]\+TABLE[ \n]\+\(if[ \n]\+not[ \n]\+exists[ \n]\+\)\?`\?\(\w\+\)`\?| \2\n [shape=none, margin=0, label=<\n <table bgcolor="#dddddd">\n <tr><td bgcolor="#ddddff" colspan="4"><b>\2</b></td></tr>|ig
s| *CREATE *TABLE *\(if *not *exists\)\? *`\?\([a-z]\+\)`\? *|\n \2\n [shape=none, margin=0, label=<\n <table bgcolor="#dddddd">\n <tr><td bgcolor="#ddddff" colspan="4"><b>\2</b></td></tr>\n|ig # remove key definitions
s/[),][\n ]*\(PRIMARY[ \n]\+\)\?KEY[ \n]\+[^(]*([^)]*)//gi
s/varchar *( *\([0-9]\+\) *)/varchar[\1]/ig # move foreign keys as relation to the end
:b;s/\(\w\+\)\([^;]*\)FOREIGN[\n ]\+KEY[ \n]*([ \n]*`\?\([a-z]\+\)`\?[ \n]*)[ \n]*REFERENCES[ \n]*`\?\([a-z]\+\)`\?[ \n]*([ \n]*`\?\([a-z]\+\)`\?[ \n]*)[ \n]*\([^,)]*\)\([,)].*\)/\1\2\7\n \1:\3 -> \4:\5 [label="\6"]##SEMICOLON##/ig;tb
s/ *PRIMARY KEY *( *`\?\([a-z]\+\)`\? *) *[,)]//ig # create table rows
s|[(,][ \n]*`\?\(\w\+\)`\?[ \n]\+\(\w\+\(([^)]\+)\)\?\)[ \n]*\([^,)]*\)[ \n]\+COMMENT[ \n]*["']\([^"']*\)["'][ \n]*|\n <tr><td align="left" port="\1"><b>\1</b></td><td align="left">\2</td><td align="left">\4</td><td align="left">\5</td></tr>|gi
s|[(,][ \n]*`\?\(\w\+\)`\?[ \n]\+\(\w\+\(([^)]\+)\)\?\)[ \n]*\([^,)]*\)|\n <tr><td align="left" port="\1"><b>\1</b></td><td align="left">\2</td><td align="left">\4</td></tr>|g
:b;s/\([a-z]\+\)\([^;]*\)FOREIGN KEY *( *`\?\([a-z]\+\)`\? *) *REFERENCES *`\?\([a-z]\+\)`\? *( *`\?\([a-z]\+\)`\? *) *\([^,)]*\)[,)]\(.*\)/\1\2\7\n \1:\3 -> \4:\5/ig;tb # add line breaks for long lines
s|\(<td[^>]*>[^<]\{30,40\}\)[ \n]\+\([^<]\{20,\}</td>\)|\1<br/>\2|g
#:d;s|\(<br/>[^<]\{30,40\}\)[ \n]\+\([^<]\{20,\}</td>\)|\1<br/>\2|g;td
:c;s|\n[(,] *`\?\([a-z]\+\)`\? *\([^ ]\+\)*\([^,)]*\) *COMMENT *[`"']\([^`"']*\)[`"'] *[,)]|\n <tr><td align="left" port="\1"><b>\1</b></td><td align="left">\2</td><td align="left">\3</td><td align="left">\4</td></tr>\n,|ig;tc # add table comment below
:k;tk
s|[ \n]*)[^)]*COMMENT[ \n]*=[ \n]*["']\?\([^"']*\)["']\?[^;]*|\n <tr><td bgcolor="#ddddff" colspan="4">\1</td></tr>|ig;th
s|)[^);]*;|\n;|ig
:h
s| \+</td>|</td>|g # cleanup comment below, add line breaksfor long lines
s|<td> \+|<td>|g s|\(<td[^>]*>[^<]\{60,80\}\)[ \n]\+\([^<]\{30,\}</td>\)|\1<br/>\2|g
s|\(<td[^>]*>[^<]\{30,40\}\) \+\([^<]\{20,\}</td>\)|\1<br/>\2|g #:e;s|\(<br/>[^<]\{60,80\}\)[ \n]\+\([^<]\{30,\}</td>\)|\1<br/>\2|g;te
:d;s|\(<br/>[^<]\{30,40\}\) \+\([^<]\{20,\}</td>\)|\1<br/>\2|g;td
s| *COMMENT *= *["']\?\([^"']*\)["']\?|\n <tr><td bgcolor="#ddddff" colspan="4">\1</td></tr>|ig # close table
s|;|\n </table>\n >];|ig
s| \+</td>|</td>|g # convert ##COMMA## to ,
s|<td> \+|<td>|g s|##COMMA##|,|g
s|\(<td[^>]*>[^<]\{60,80\}\) \+\([^<]\{30,\}</td>\)|\1<br/>\2|g # convert ##SEMICOLON## to ;
:e;s|\(<br/>[^<]\{60,80\}\) \+\([^<]\{30,\}</td>\)|\1<br/>\2|g;te s,##SEMICOLON##,;,g
s/\n,[^\n]*/\n/g # print one table
s|;|\n </table>\n >];\n|ig p
# get buffer back and remove the table that has just been analyzed
x
s,\(.*\)create[ \n]\+table[^;]*;\(.*\),\1\2,ig
ti
$a\ $a\
}\ }\

Loading…
Cancel
Save