Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions plantuml2mysql.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3
#!/usr/bin/env python3
#-*-coding:utf-8-*-
# Usage: ./plantuml2mysql <dbsource.plu> <dbname>
# Author: Alexander I.Grafov <grafov@gmail.com>
# See https://github.com/grafov/plantuml2mysql
# The code is public domain.
#Open Power Shell and Switch Directory: cd U:\VIVIJ\TaxDataHub\Information_Modelling
# python plantuml2mysql.py information_model_v1.puml sqlDwhTest

CHARSET="utf8_unicode_ci"

Expand All @@ -19,9 +18,8 @@ def strip_html_tags(t):

# A minimal help
def print_usage():
print("Convert PlantUML classes schema into mySQL database creation script")
print("Convert PlantUML classes schema into SQL database creation script")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think the script promise compatibility for mysql only, so I reflected it in this message. I not planned it as a common script for many RDBMS though it could be applied with minor changes to other SQL-based systems.

print("Usage:\n", sys.argv[0], "<dbsource.plu> <dbname>")
print("\nSee https://github.com/grafov/plantuml2mysql for details\n")

def main():
# Check arguments (exactly 1 + 2):
Expand All @@ -35,9 +33,14 @@ def main():
print("Cannot open file: '" + sys.argv[1] + "'")
sys.exit()
# Add information for future self ;-)
print("# Database created on", time.strftime('%d/%m/%y %H:%M',time.localtime()), "from", sys.argv[1])
print("CREATE DATABASE %s CHARACTER SET = utf8 COLLATE = %s;" % (sys.argv[2], CHARSET))
print("USE %s;\n" % sys.argv[2])
print('')
print('')
print('####################################################################################################')
print("****************************\t TDH Info Model DB Script")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, could you explain TDH acronym?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TDH can be totally ignored. It is dome term sued in my project, forgot to remove.

print("****************************\t Script Generated on", time.strftime('%d/%m/%y %H:%M',time.localtime()), "from", sys.argv[1])
print('####################################################################################################')
print('')
print('')
uml = False; table = False; field = False
pk = False; idx = False
primary = []; index = ""
Expand Down Expand Up @@ -71,23 +74,25 @@ def main():
uml = False
continue
if not uml:
continue
continue
if l.startswith("class"):
table = True; field = False
primary = []; index = ""
# Table names are quoted and lower cased to avoid conflict with a mySQL reserved word
print("CREATE TABLE IF NOT EXISTS `" + i[1].lower() + "` (")
print("CREATE TABLE stg." + i[1].lower() + " (")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't understand the syntax here. What is this stg prefix?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stg is just the schema or the user name in which the table is being created. This was done as we have two layers of tables with same name, same like stg and pub schema in data warehouse systems.

continue
if table and not field and l == "==": # Seperator after table description
field = True
continue
if field and l == "}":
table = False; field = False
print(" PRIMARY KEY (%s)" % ", ".join(primary), end="")
if primary: #Vips
print(" PRIMARY KEY (%s)" % ", ".join(primary), end="")
if index:
print(",\n%s" % index[:-2],)
index = ""
print(");\n")
print("GO\n") #Vips
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought "go" doesn't work with mySQL as a delimiter by default. Is it really works with mysql without redefining it with delimiter command?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well frankly I changed this script to suite Azure SQL database. So may be if you find suitable you may create a separate branch for this. :)
And Of course #Vips needs to be ignored. This was basically a TAG kind of thing which I added to mark the changes done by me.

continue
if field and l == "#id":
print(" %-16s SERIAL," % "id")
Expand All @@ -101,7 +106,7 @@ def main():
primary.append(fname)
if field and idx:
index += " INDEX (%s),\n" % fname


if __name__ == "__main__":
main()