C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sql-to-dot.sed 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #! /bin/sed -nf
  2. 1i\
  3. /** @page database Database Schema\
  4. \
  5. @dot\
  6. digraph schema {
  7. # get everithing on one single line
  8. H;$!d;$x
  9. # remove all single-line comment lines
  10. s/\n--[^\n]*//g
  11. # encode html entities
  12. s/&/\&amp##SEMICOLON##/g
  13. s/</\&lt##SEMICOLON##/g
  14. s/>/\&gt##SEMICOLON##/g
  15. # reduce spaces
  16. s,\t\| \+, ,g
  17. # remove multiline comments
  18. :f;s,\(.*\)/\*.*\*/[ \n]*;*,\1,g;tf
  19. # remove empty lines
  20. s,\n\+,\n,g
  21. # remove unknown commands
  22. s,\(;\|\n\) *\(INSERT\|DELIMITER\|USE\|DROP\|CREATE[ \n]\+DATABASE\)[ \n]\+[^;]*;\+,,ig
  23. # convert special characters within quotes
  24. :a;s/^\(\([^"]*"[^",]*"\)*[^"]*"[^"]*\),\([^"]*".*\)/\1\##COMMA##\3/g;ta
  25. :c;s/^\(\([^']*'[^',]*'\)*[^']*'[^']*\),\([^']*'.*\)/\1\##COMMA##\3/g;tc
  26. # backup everything to the buffer
  27. # then analyze only on one create table
  28. :i
  29. h
  30. s,.*\(create[ \n]\+table[^;]*;\).*,\1,ig
  31. # start html table node
  32. 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
  33. # remove key definitions
  34. s/[),][\n ]*\(\(UNIQUE\|PRIMARY\)[ \n]\+\)\?KEY[ \n]\+[^(]*([^)]*)//gi
  35. # move foreign keys as relation to the end
  36. :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
  37. # create table rows
  38. 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><td></td></tr>|g
  39. # extract comment
  40. s|\(<td\( *\w*="\w*"\)* *>[^<]*\)COMMENT[ \n]\+["']\([^"']*\)["']\([^<]*</td>\)<td></td>|\1\4<td align="left">\3</td>|g
  41. # add line breaks for long lines
  42. s|\(<td[^>]*>[^<]\{30,40\}\)[ \n]\+\([^<]\{20,\}</td>\)|\1<br/>\2|g
  43. #:d;s|\(<br/>[^<]\{30,40\}\)[ \n]\+\([^<]\{20,\}</td>\)|\1<br/>\2|g;td
  44. # add table comment below
  45. :k;tk
  46. s|[ \n]*)[^)]*COMMENT[ \n]*=[ \n]*["']\?\([^"']*\)["']\?[^;]*|\n <tr><td bgcolor="#ddddff" colspan="4">\1</td></tr>|ig;th
  47. s|)[^);]*;|\n;|ig
  48. :h
  49. # cleanup comment below, add line breaksfor long lines
  50. s|\(<td[^>]*>[^<]\{60,80\}\)[ \n]\+\([^<]\{30,\}</td>\)|\1<br/>\2|g
  51. #:e;s|\(<br/>[^<]\{60,80\}\)[ \n]\+\([^<]\{30,\}</td>\)|\1<br/>\2|g;te
  52. # close table
  53. s|;|\n </table>\n >];|ig
  54. # convert ##COMMA## to ,
  55. s|##COMMA##|,|g
  56. # convert ##SEMICOLON## to ;
  57. s,##SEMICOLON##,;,g
  58. # print one table
  59. p
  60. # get buffer back and remove the table that has just been analyzed
  61. x
  62. s,\(.*\)create[ \n]\+table[^;]*;\(.*\),\1\2,ig
  63. ti
  64. $a\
  65. }\
  66. @enddot\
  67. */