2
2
#-*-coding:utf-8-*-
3
3
# Usage: ./plantuml2mysql <dbsource.plu> <dbname>
4
4
# Author: Alexander I.Grafov <[email protected] >
5
+ # See https://github.com/grafov/plantuml2mysql
5
6
# The code is public domain.
6
7
7
8
CHARSET = "utf8_unicode_ci"
8
9
9
10
import sys
11
+ import re
12
+ import time
13
+
14
+ # PlantUML allows some HTML tags in comments.
15
+ # We don't want them anymore here...
16
+ TAG_RE = re .compile (r'<[^>]+>' )
17
+ def strip_html_tags (t ):
18
+ return TAG_RE .sub ('' , t )
19
+
20
+ # A minimal help
21
+ def print_usage ():
22
+ print ("Convert PlantUML classes schema into mySQL database creation script" )
23
+ print ("Usage:\n " , sys .argv [0 ], "<dbsource.plu> <dbname>" )
24
+ print ("\n See https://github.com/grafov/plantuml2mysql for details\n " )
10
25
11
26
def main ():
27
+ # Check arguments (exactly 1 + 2):
28
+ if len (sys .argv ) != 3 :
29
+ print_usage ()
30
+ sys .exit ()
31
+ try : # Avoid exception on STDOUT
32
+ with open (sys .argv [1 ]) as src :
33
+ data = src .readlines ()
34
+ except :
35
+ print ("Cannot open file: '" + sys .argv [1 ] + "'" )
36
+ sys .exit ()
37
+ # Add information for future self ;-)
38
+ print ("# Database created on" , time .strftime ('%d/%m/%y %H:%M' ,time .localtime ()), "from" , sys .argv [1 ])
12
39
print ("CREATE DATABASE %s CHARACTER SET = utf8 COLLATE = %s;" % (sys .argv [2 ], CHARSET ))
13
40
print ("USE %s;\n " % sys .argv [2 ])
14
41
uml = False ; table = False ; field = False
15
42
pk = False ; idx = False
16
43
primary = []; index = ""
17
- with open (sys .argv [1 ]) as src :
18
- data = src .readlines ()
19
44
for l in data :
20
45
l = l .strip ()
21
46
if not l :
@@ -25,15 +50,17 @@ def main():
25
50
continue
26
51
if not uml :
27
52
continue
28
- if l == "--" :
29
- continue
53
+ if l == "--" : # Separator
54
+ continue
30
55
comment = ""
31
56
i = l .split ()
32
- fname = i [0 ]
57
+ fname = i [0 ]
58
+ if fname == ".." or fname == "__" : # Separators in table definition
59
+ continue
33
60
if field and ("--" in l ):
34
61
i , comment = l .split ("--" , 2 )
35
62
i = i .split ()
36
- pk = False ; idx = False
63
+ pk = False ; idx = False
37
64
if fname [0 ] in ("+" , "#" ):
38
65
if fname [0 ] == "#" :
39
66
pk = True
@@ -48,9 +75,10 @@ def main():
48
75
if l .startswith ("class" ):
49
76
table = True ; field = False
50
77
primary = []; index = ""
51
- print ("CREATE TABLE IF NOT EXISTS" , i [1 ], "(" )
78
+ # Table names are quoted and lower cased to avoid conflict with a mySQL reserved word
79
+ print ("CREATE TABLE IF NOT EXISTS `" + i [1 ].lower () + "` (" )
52
80
continue
53
- if table and not field and l == "==" :
81
+ if table and not field and l == "==" : # Seperator after table description
54
82
field = True
55
83
continue
56
84
if field and l == "}" :
@@ -66,7 +94,8 @@ def main():
66
94
if field and l != "#id" :
67
95
print (" %-16s %s" % (fname , " " .join (i [2 :]).upper ()), end = "" )
68
96
if comment :
69
- print (" COMMENT '%s'" % comment .strip (), end = "" )
97
+ # Avoid conflict with apostrophes (use double quotation marks)
98
+ print (" COMMENT \" %s\" " % strip_html_tags (comment .strip ()), end = "" )
70
99
print ("," )
71
100
if field and pk :
72
101
primary .append (fname )
0 commit comments