forked from gbmav/sql2puml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql2puml.awk
216 lines (186 loc) · 5.58 KB
/
sql2puml.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# =========================================================================
#
# sql2puml - SQL DDL to markdown converter
#
# Invocation/Execution:
# awk -f sql2puml.awk inputfile > outputfile
#
# Supports
# Markdown format
# PlantUML format
# mermaid format
# dot format
# =========================================================================
# Globals
# output_format="plantuml"
# running=0
# =========================================================================
function check_arguments() {
if (ARGC==1) {
print_usage()
return
}
for (i=1; i<= ARGC; i++) {
if (ARGV[i] == "omd") output_format="markdown"
if (ARGV[i] == "opl") output_format="plantuml"
if (ARGV[i] == "ome") output_format="mermaid"
if (ARGV[i] == "help" ) print_help()
if (ARGV[i] == "version" ) print_version()
}
running=1
}
function print_usage() {
print("Usage: sql2puml [help] [version] [omd | opl | ome ] inputsqlfile")
exit
}
function print_version() {
print("sql2puml Version: 0.1")
exit
}
function print_help() {
print("sql2puml Help")
print("options:")
print(" version")
print(" help")
print("")
print("Examples")
print("awk -f ddl2md -opl dump.dql")
exit
}
function uml_start()
{
plantuml_start()
}
function plantuml_start()
{
print "@startuml"
print "left to right direction"
print "skinparam roundcorner 5"
print "skinparam linetype ortho"
print"skinparam shadowing false"
print "skinparam handwritten false"
print "skinparam class {"
print " BackgroundColor white"
print " ArrowColor #2688d4"
print " BorderColor #2688d4"
print "}"
print"!define primary_key(x) <b><color:#b8861b><&key></color> x</b>"
print "!define foreign_key(x) <color:#aaaaaa><&key></color> x"
print "!define column(x) <color:#efefef><&media-record></color> x"
print "!define table(x) entity x << (T, white) >>"
running=1
}
function uml_end()
{
plantuml_end()
}
function plantuml_end()
{
print "@enduml"
}
function erase_braces(mystr)
{
#sub("\(","",mystr)
split(mystr,arr,"\(")
return arr[1]
}
function uml_table(line)
# DDL to plantuml
# CREATE TABLE portfolio_details(ID INT, portfolioID INT, currencyID INT, quantity FLOAT);#table( user )
# CREATE TABLE table10_pk_and_fk (id INT PRIMARY KEY, firstname VARCHAR(100), lastname VARCHAR(100) , town_id INT, FOREIGN KEY (town_id) REFERENCES table11(id));
#table( user ) {
# primary_key( id ): UUID
# column( isActive ): BOOLEAN
# foreign_key( cityId ): INTEGER <<FK>>
#}
#
{
tablerelationships=""
sub("CREATE TABLE","",line)
# extract name of table and handle if there is no space between table_name and braces (
split(line,arr,"\(")
tablename =arr[1]
printf("table(%s) { \n",tablename )
# columns
braceposition = match(line,"\\(")
revisedlength = length(line)-braceposition
params = substr(line,braceposition,revisedlength)
sub("\\(","",params)
sub("\)","",params)
numparams = split(params,param,",")
for (i=1;i<= numparams;i++) {
# printf("%s \n",param[i])
split(param[i], fields," ")
cname = fields[1]
ctype = fields[2]
key = fields[3]
if (match(fields[3],"PRIMARY") == 1) {
printf("primary_key ( %s ): %s\n",cname,ctype)
}
else if (match(fields[3],"FOREIGN") == 1) {
printf("foreign_key ( %s ): %s\n",cname,ctype)
split(fields[6],fkf,"(")
foreigntablename = fkf[1]
foreigntablecolumn = fkf[2]
tablerelationships=tablerelationships uml_table_relationship(tablename,foreigntablename)
}
else
{
if (cname == "FOREIGN" && ctype =="KEY")
{
#print("possible FOREIGN")
#print("key",key)
#printf("foreign_key ( %s ): %s\n",cname,ctype)
split(fields[5],fkf,"(")
foreigntablename = fkf[1]
foreigntablecolumn = fkf[2]
#print("f5 ",fields[5])
#print("foreigntablename",foreigntablename)
#print("foreigntablecolumn",foreigntablecolumn)
tablerelationships=tablerelationships uml_table_relationship(tablename,foreigntablename)
}
else
printf("column ( %s ): %s\n",cname,ctype)
}
}
printf("}\n")
printf("%s\n",tablerelationships)
}
function uml_table_relationship(t1,t2) {
mystr = sprintf("%s -- %s",t1,t2)
#print("my str = ",mystr)
return mystr
}
# =========================================================================
function uml_parse_sql_line(line)
{
#print("uml_parse_sql_line",line)
if (match(line,"CREATE TABLE") > 0) {
uml_table(line)
}
}
# =========================================================================
function uml_parse_line(line)
{
if (length(line) < 2) {
return
}
if (match(line,";")>0) {
sql_line = sql_line line
uml_parse_sql_line(sql_line)
sql_line=""
}
else {
sql_line = sql_line line
}
}
# =========================================================================
BEGIN {
check_arguments()
uml_start()
}
{
uml_parse_line($0)
}
END { uml_end() }
# =========================================================================