-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-wb-confluence-markup-exporter.lua
300 lines (234 loc) · 8.3 KB
/
mysql-wb-confluence-markup-exporter.lua
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
-- standard plugin functions
--
-- this function is called first by MySQL Workbench core to determine number of
-- plugins in this module and basic plugin info
-- see the comments in the function body and adjust the parameters as appropriate
--
function getModuleInfo()
-- module properties
local props =
{
-- module name (ID)
name = "MySQLConfluenceMarkupExporter",
-- module author(s)
author = "Ralf Schaeftlein",
--module version
version = "1.0.2",
-- interface implemented by this module
implements = "PluginInterface",
-- plugin functions exposed by this module
-- l looks like a parameterized list, i instance, o object, @ fully qualified class name
functions =
{
"getPluginInfo:l<o@app.Plugin>:",
"exportMarkupToClipboard:i:o@db.Catalog"
}
}
-- can't assign inside declaration
props.name = props.name .. props.version
return props
end
function objectPluginInput(type)
return grtV.newObj("app.PluginObjectInput", {objectStructName = type})
end
function getPluginInfo()
-- list of plugins this module exposes (list object of type app.Plugin?)
local l = grtV.newList("object", "app.Plugin")
-- plugin instances
local plugin
local props = getModuleInfo()
-- new plugin: export to clipboard
plugin = createNewPlugin("wb.catalog.util.exportMarkupToClipboard" .. props.version,
"Confluence Markup Exporter " .. props.version .. ": Copy to Clipboard",
props.name,
"exportMarkupToClipboard",
{objectPluginInput("db.Catalog")},
{"Catalog/Utilities", "Menu/Catalog"})
-- append to list of plugins
grtV.insert(l, plugin)
return l
end
function createNewPlugin(name, caption, moduleName, moduleFunctionName, inputValues, groups)
-- create dictionary, Lua seems to handle keys and values right...
local props =
{
name = name,
caption = caption,
moduleName = moduleName,
pluginType = "normal",
moduleFunctionName = moduleFunctionName,
inputValues = inputValues,
rating = 100,
showProgress = 0,
groups = groups
}
local plugin = grtV.newObj("app.Plugin", props)
-- set owner???
plugin.inputValues[1].owner = plugin
return plugin
end
--
-- Print some version information and copyright to the output window
function printVersion()
print("\n\n\MySQL Schema to Confluence Markup Exporter v" .. getModuleInfo().version .. "\nCopyright (c) 2011 Ralf Schaeftlein - License: LGPLv3");
end
-- export function #1
function exportMarkupToClipboard(catalog)
printVersion()
local markup = generateConfluenceMarkup(catalog)
Workbench:copyToClipboard(markup)
print('\n > Confluence Markup copied to clipboard')
return 0
end
--
-- generates the confluence markup
function generateConfluenceMarkup(cat)
local i, j, schema, tbl
local markup = ""
local optionsSetFlag = false
for i = 1, grtV.getn(cat.schemata) do
schema = cat.schemata[i]
--print(schema)
for j = 1, grtV.getn(schema.tables) do
tbl = schema.tables[j]
--
-- do not export *_translation tables
if (tbl.name ~= nil and tbl.name ~= "" and string.ends(tbl.name, "_translation") == false ) then
markup = buildMarkupForSingleTable(tbl, schema, markup)
end
end
end
--print(markup)
return markup
end
function buildMarkupForSingleTable(tbl, schema, markup)
local k, l, col, index, column
local actAsPart = ""
local actAs = ""
--
-- start of adding a table
markup = markup .. "h4. Tablestructure for table " .. tbl.name .. "\n"
markup = markup .. "\n"
if ( tbl.comment ~= nil and tbl.comment ~= "" ) then
markup = markup .. tbl.comment .. "\n"
end
markup = markup .. "\n"
if ( tbl.tableEngine ~= nil and tbl.tableEngine ~= "") then
markup = markup .. " engine " .. tbl.tableEngine .. "\n"
end
if ( tbl.defaultCharacterSetName ~= nil and tbl.defaultCharacterSetName ~= "" ) then
markup = markup .. " charset: " .. tbl.defaultCharacterSetName .. "\n"
end
if ( tbl.defaultCollationName ~= nil and tbl.defaultCollationName ~= "" ) then
markup = markup .. " collation: " .. tbl.defaultCollationName .. "\n"
end
markup = markup .. "\n"
markup = markup .. "|| *Column* || *Type* || *Null* || *autoincrement* || *default* || *Primary* || *Unique* || *Description*||\n"
--
-- iterate through the table columns
for k = 1, grtV.getn(tbl.columns) do
col = tbl.columns[k]
markup = buildMarkupForSingleColumn(tbl, col, markup)
end
markup = markup .. "\n"
-- table index
local indexes = ""
for k = 1, grtV.getn(tbl.indices) do
index = tbl.indices[k]
if ( index.indexType == "INDEX" ) then
indexes = indexes .. "| " .. index.name .. " | "
for l = 1, grtV.getn(index.columns) do
column = index.columns[l]
indexes = indexes .. column.referencedColumn.name
if ( l < grtV.getn(index.columns) ) then
indexes = indexes .. ", "
end
end
indexes = indexes .. " | INDEX |\n"
end
end
if ( indexes ~= nil and indexes ~= "") then
markup = markup .. "h5. Indexes \n"
markup = markup .. "|| *Name* || *Columns* || *Type* ||\n"
markup = markup .. indexes
end
-- final line break
return markup .. "\n"
end
function buildMarkupForSingleColumn(tbl, col, markup)
local l, m, p, u, n
-- column name
markup = markup .. "| " .. col.name .. " | "
-- column type
markup = markup .. col.simpleType.name
if ( col.length ~= -1 ) then
markup = markup.. "(" ..col.length.. ")"
end
markup = markup .. " | "
-- column not null?
if ( col.isNotNull == 1 ) then
markup = markup .. " NOT NULL " .. " | "
else
markup = markup .. " NULL " .. " | "
end
-- autoincrement
if ( col.autoIncrement == 1 ) then
markup = markup .. " true |"
else
markup = markup .. " false |"
end
-- default
if ( col.defaultValue ~= '') then
markup = markup .. col.defaultValue .. " | "
else
markup = markup .. " | "
end
p = " false "
u = " false "
for m = 1, grtV.getn(tbl.indices) do
index = tbl.indices[m]
--
-- checking for primary index
if ( index.indexType == "PRIMARY" ) then
for l = 1, grtV.getn(index.columns) do
column = index.columns[l]
if ( column.referencedColumn.name == col.name ) then
p = " true "
end
end
end
--
-- checking for unique index
if ( index.indexType == "UNIQUE" ) then
-- check if just one column in index
if ( grtV.getn(index.columns) == 1 ) then
for l = 1, grtV.getn(index.columns) do
column = index.columns[l]
if ( column.referencedColumn.name == col.name ) then
u = " true "
end
end
end
end
end
-- primary key?
markup = markup .. p .. " | "
-- unique key?
markup = markup .. u .. " | "
-- description
if ( col.comment ~= nil and col.comment ~= '') then
markup = markup .. col.comment
end
-- fk?
for n = 1, grtV.getn(tbl.foreignKeys) do
fk = tbl.foreignKeys[n]
if ( fk.columns[1].name == col.name ) then
markup = markup .. " (foreign key to table " .. fk.referencedColumns[1].owner.name .. " column " .. fk.referencedColumns[1].name .. " ) "
end
end
markup = markup .. " | \n"
return markup
end
function string.ends(String,End)
return End=='' or string.sub(String,-string.len(End))==End
end