forked from mazenovi/PropelExport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropelExport.grt.lua
executable file
·869 lines (718 loc) · 29.9 KB
/
PropelExport.grt.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
-- PropelExport v0.6
--
-- The PropelExport Module allows you to export a catalog as propel xml-schema.
-- Currently Propel 1.2 and 1.3 is fully supported. It should also work with Propel 1.4, 1.5, 1.6
-- now 1.4-specific features behaviors and single inheritance are supported:
-- * behaviors can be set in table's comment as <behaviors>propel xml code for behaviors</behaviors>
-- * single inheritance can be set in columns's comment as <inheritances>propel xml code for single inheritance</inheritances>
--
--
-- This Module is Copyright (c) 2008-2009 CN-Consult GmbH
-- Historic Author: Daniel Haas <[email protected]>
-- Author: Vincent Mazenod <[email protected]>
--
--
-- This module is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; version 2 of the License.
--
--
-- The module is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-- without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-- See the GNU General Public License for more details.
--
--
-- * IMPORTANT:
-- * If you find BUGS in this module or have ideas for IMPROVEMENTS or PATCHES, dont hesitate
-- * to contact us at [email protected]! Thanks!
--
-- CHANGELOG:
-- Version 0.1: Internal Version
-- Version 0.2: First released Version
-- Version 0.3: Added support for *TEXT columns (are converted to VARCHAR/LONGVARCHAR/CLOBs for propel)
-- Version 0.4:
-- * Added support to set database baseClass manually
-- * Added support to set phpName per table manually
-- * Added support for onDelete and onUpdate foreign key constraints (based on work by Antoine Noal aka Hvannentir)
-- * DATETIME type is converted to TIMESTAMP type
-- * size and scale support for DECIMAL columns (based on work by Antoine Noal aka Hvannentir)
-- * support for multiple foreign key references (thanks for pointing that out Antoine!)
-- Version 0.4.1: Fixed MEDIUMINT not being converted to INT for propel
-- Version 0.5:
-- * Works with Workbench 5.1
-- * Added support for workbenchs user data types (This also fixes BOOLEANS appearing als UNKNOWN in WB 5.1)
-- (Note: This does not work with user-types defining precision and scale, due to a Bug in Workbench)
-- * Added support for Propel 1.3 defaultExpr
-- * Add name-attribute to unique-tags and index-tags
-- * Add size-attribute to unique- and index-tags only if a size is available
-- Note: This is the last Version that will support Propel 1.2, newer releases will only support >=Propel 1.3 (default= will be replaced with defaultValue=)
-- Version 0.6:
-- * Works with Workbench 5.2
-- * Added behaviors can be set in table's comment as <behaviors>...</behaviors>
-- * Added single inheritance can be set in columns's comment as <inheritances>...</inheritances>
-- * Added support to set package manually
-- * Fix some indent bugs in XMLWriter:addContent()
--
-- standard module/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()
return {
name= "PropelExport", -- put your module name here; must be a valid identifier unique among
-- all other plugin names
author= "CN-Consult GmbH", -- put your company name here
version= "1.0", -- put module version string in form major.minor
implements= "PluginInterface", -- don't change this
functions= {
"getPluginInfo:l<[email protected]>:", -- don't change this
"exportPropelSchemaToClipboard:i:[email protected]", -- list all your plugin function names and accepted argument types,
"exportPropelSchemaToFile:i:[email protected]",
"setPropelExportBaseClass:i:[email protected]",
"setPropelExportPackage:i:[email protected]",
"setPropelExportPhpName:i:[email protected]"
-- keeping the rest unchanged; in this example there's only one
-- function, function name is PluginFunctionName and argument type
-- is db.Catalog
}
}
end
-- helper function to create a descriptor for an argument of a specific type of object
-- you don't need to change here anything
function objectPluginInput(type)
return grtV.newObj("app.PluginObjectInput", {objectStructName= type})
end
-- this function is called by MySQL Workbench core after a successful call to getModuleInfo()
-- to gather information about the plugins in this module and the functions that the plugins expose;
-- a plugin should expose only one function that will handle a menu command for a class of objects
-- see the comments in the function body and adjust the parameters as appropriate
function getPluginInfo()
local l
local plugin
-- create the list of plugins that this module exports
l= grtV.newList("object", "app.Plugin")
-- create a new app.Plugin object for every plugin
plugin= grtV.newObj("app.Plugin", {
name= "wb.catalog.util.exportPropelSchemaToClipboard", -- plugin namespace
caption= "PropelExport: Copy Catalog as Propel-Schema to Clipboard", -- plugin textual description (will appear as menu item name)
moduleName= "PropelExport", -- this should be in sync with what you sepcified previously for module
-- name in getModuleInfo()
pluginType= "normal", -- don't change this
moduleFunctionName= "exportPropelSchemaToClipboard", -- the function that this plugin exposes
inputValues= {objectPluginInput("db.Catalog")}, -- the type of object
rating= 100, -- don't change this
showProgress= 0, -- don't change this
groups= {"Catalog/Utilities", "Menu/Catalog"} -- use "Catalog/Utilities" to show the menu item on the overview page,
-- or "Model/Utilities" to show the menu item on the canvas;
-- the "Menu/*" entries control how the plugin will appear in main menu
-- the possible values for it are "Menu/Model", "Menu/Catalog", "Menu/Objects",
-- "Menu/Database", "Menu/Utilities"
})
-- fixup owner
plugin.inputValues[1].owner= plugin
-- add to the list of plugins
grtV.insert(l, plugin)
-- create a new app.Plugin object for every plugin
plugin= grtV.newObj("app.Plugin", {
name= "wb.catalog.util.exportPropelSchemaToFile", -- plugin namespace
caption= "PropelExport: Export Catalog as Propel-Schema to File", -- plugin textual description (will appear as menu item name)
moduleName= "PropelExport", -- this should be in sync with what you sepcified previously for module
-- name in getModuleInfo()
pluginType= "normal", -- don't change this
moduleFunctionName= "exportPropelSchemaToFile", -- the function that this plugin exposes
inputValues= {objectPluginInput("db.Catalog")}, -- the type of object
rating= 100, -- don't change this
showProgress= 0, -- don't change this
groups= {"Catalog/Utilities", "Menu/Catalog"} -- use "Catalog/Utilities" to show the menu item on the overview page,
-- or "Model/Utilities" to show the menu item on the canvas;
-- the "Menu/*" entries control how the plugin will appear in main menu
-- the possible values for it are "Menu/Model", "Menu/Catalog", "Menu/Objects",
-- "Menu/Database", "Menu/Utilities"
})
-- fixup owner
plugin.inputValues[1].owner= plugin
-- add to the list of plugins
grtV.insert(l, plugin)
-- create a new app.Plugin object for every plugin
plugin= grtV.newObj("app.Plugin", {
name= "wb.catalog.util.setPropelExportBaseClass",
caption= "PropelExport: Set custom baseClass",
moduleName= "PropelExport",
pluginType= "normal",
moduleFunctionName= "setPropelExportBaseClass",
inputValues= {objectPluginInput("db.Catalog")},
rating= 100,
showProgress= 0,
groups= {"Catalog/Utilities", "Menu/Catalog"}
})
-- fixup owner
plugin.inputValues[1].owner= plugin
-- add to the list of plugins
grtV.insert(l, plugin)
-- create a new app.Plugin object for every plugin
plugin= grtV.newObj("app.Plugin", {
name= "wb.catalog.util.setPropelExportPackage",
caption= "PropelExport: Set custom package",
moduleName= "PropelExport",
pluginType= "normal",
moduleFunctionName= "setPropelExportPackage",
inputValues= {objectPluginInput("db.Catalog")},
rating= 100,
showProgress= 0,
groups= {"Catalog/Utilities", "Menu/Catalog"}
})
-- fixup owner
plugin.inputValues[1].owner= plugin
-- add to the list of plugins
grtV.insert(l, plugin)
-- create a new app.Plugin object for every plugin
plugin= grtV.newObj("app.Plugin", {
name= "wb.table.util.setPropelExportPhpName",
caption= "PropelExport: Set custom phpName",
moduleName= "PropelExport",
pluginType= "normal",
moduleFunctionName= "setPropelExportPhpName",
inputValues= {objectPluginInput("db.Table")},
rating= 100,
showProgress= 0,
groups= {"Catalog/Utilities", "Menu/Table"}
})
-- fixup owner
plugin.inputValues[1].owner= plugin
-- add to the list of plugins
grtV.insert(l, plugin)
return l
end
-- Some needed definitions for the XMLWriter "class" (metatable)
XMLWriter = {}
XMLWriter_mt = {}
XMLWriter_mt.__index = XMLWriter
-- This is a very simplistic xml-serializer "class" in lua
-- With this method you create a new xmlwriter.
-- You have to at least pass the _rootTag
function XMLWriter:new(_rootTag,_encoding)
xmlwriter={
rootTag=_rootTag,
encoding = _encoding or 'UTF-8',
xml='',
state='',
openedTags={},
newline='',
indent='',
useIndent=false
}
setmetatable(xmlwriter,XMLWriter_mt)
xmlwriter:start()
return xmlwriter
end
-- Start a new xml document
function XMLWriter:start()
self.xml='<?xml version="1.0" encoding="'..self.encoding..'"?>\n'
self.state='documentOpen'
self.xml=self.xml..'<'..self.rootTag
end
-- Use this method to enable indenting the generated xml-code
-- Otherwise the xml is generated all on one line
function XMLWriter:enableIndent()
self.useIndent=true
self.newline='\n'
end
-- Internal method for pretty indenting
function XMLWriter:increaseIndent()
if (self.useIndent==true) then
local indent=string.len(self.indent)
self.indent=string.rep(' ',indent+2)
end
end
-- Internal method for pretty indenting
function XMLWriter:decreaseIndent()
if (self.useIndent==true) then
local indent=string.len(self.indent)
if (indent<2) then indent=2 end
self.indent=string.rep(' ',indent-2)
end
end
-- begin a new tag with the given name
-- To add attributes to the tag use addAttribute().
-- Use addContent() to add content to the tag, or openTag() to add subtags.
function XMLWriter:openTag(_tagname)
if (self.state~='tagClosed' and self.state~='insideTag') then self.increaseIndent(self) end
if (_tagname) then
if (self.state=='documentOpen' or self.state=='tagOpen' or self.state=='subTagOpen') then
self.xml=self.xml..'>'..self.newline
end
self.xml=self.xml..self.indent..'<'.._tagname..''
self.openedTags[#self.openedTags+1]=_tagname
if (self.state=='tagOpen') then
self.state='subTagOpen'
else
self.state='tagOpen'
end
else
print("You have to pass a tagname when you open a tag!")
end
end
-- Add an attribute to the currently open tag
-- Note: this method may not be called when you added content to the tag already
function XMLWriter:addAttribute(_name, _value)
if (self.state=='tagOpen' or self.state=='subTagOpen' or self.state=='documentOpen') then
self.xml=self.xml..' '.._name..'="'.._value..'"'
end
end
-- Add content (cdata) to the currently open tag
-- Note: When adding content to a tag, you may not add attributes anymore to the tag
function XMLWriter:addContent(_content)
if (self.state=='tagOpen' or self.state=='subTagOpen') then
self.xml=self.xml..'>'
self.state='insideTag'
self.increaseIndent(self)
end
self.xml=self.xml..self.newline..self.indent.._content..self.newline
end
-- Add xml text to the currently open tag
-- Note: When adding content to a tag, you may not add attributes anymore to the tag
function XMLWriter:addXml(_content)
if (self.state=='tagOpen' or self.state=='subTagOpen') then
self.xml=self.xml..'>'
self.state='insideTag'
self.increaseIndent(self)
end
for tag in string.gmatch(_content, "<%/?[%w:]+.-%/?>") do
if (string.sub(tag, string.len(tag)-1, string.len(tag)-1) == '/') then
typeTag = "singleTag "
elseif(string.sub(tag, 2, 2)=='/') then
typeTag = "closeTag "
else
typeTag = "openTag "
end
if (typeTag == "closeTag ") then
self.decreaseIndent(self)
end
self.xml=self.xml..self.newline..self.indent..tag
if (typeTag == "openTag ") then
self.increaseIndent(self)
end
end
self.xml=self.xml..self.newline
end
-- Close a previously opened tag
function XMLWriter:closeTag()
if (self.state=='tagOpen' or self.state=='subTagOpen') then
self.xml=self.xml..'/>'..self.newline
self.openedTags[#self.openedTags]=nil
if (#self.openedTags==0) then
self.state='tagClosed'
else
self.state='insideTag'
end
else if (self.state=='insideTag') then
self.decreaseIndent(self)
self.xml=self.xml..self.indent..'</'..self.openedTags[#self.openedTags]..'>'..self.newline
self.openedTags[#self.openedTags]=nil
if (#self.openedTags==0) then self.state='tagClosed' end
end
end
end
-- Finishes the document
-- This means closing the root tag
function XMLWriter:finishDocument()
if (#self.openedTags > 0) then
print("You still have opened tags!")
else
if (self.state=='documentOpen') then self.xml=self.xml..'>'..self.newline end
self.xml=self.xml..'</'..self.rootTag..'>\n'
end
end
-- Return the XML of the generated document
function XMLWriter:getXML()
return self.xml
end
-- Test method which tests the XML Serializer
function testXml(catalog)
print('This is lua Version '.._VERSION.."\n")
local xml=XMLWriter:new('database')
xml:enableIndent()
xml:addAttribute("cool","ness")
xml:openTag('test')
xml:closeTag()
xml:openTag('cool')
xml:addAttribute('master','commander')
xml:closeTag()
xml:openTag('multi')
xml:addAttribute('master','commander')
xml:addContent('Mycontent')
xml:closeTag()
xml:openTag('aaaargh')
xml:addAttribute('thelast','test')
xml:openTag('subtag')
xml:addAttribute('subattrib','value')
xml:addXml("<behaviors name=\"fabulousable\">\n \t<parameter />\n </behaviors>\n")
xml:closeTag()
xml:closeTag()
xml:finishDocument()
print (xml:getXML())
end
--testXml(catalog)
------------------------------
--- PropelExport util methods:
------------------------------
--
-- Print some version information and copyright to the output window
function printVersion()
print("\n\n\nThis is PropelExport v0.6\nCopyright (c) 2008-2009 CN-Consult GmbH");
end
--
-- Convert workbench simple types to propel types
function wbSimpleType2PropelDatatype(simpleType)
-- local propelType="**UNKNOWN** ("..simpleType.name..")"
-- We assume that the simpleType corresponds to the propel type by default
-- This is correct 95% of the time
if (simpleType~=nil) then
local propelType=simpleType.name
-- convert INT to INTEGER
if (simpleType.name=="INT" or simpleType.name=="MEDIUMINT") then
propelType = "INTEGER"
end
-- convert text types to CLOBs
if (simpleType.name=="TINYTEXT") then
propelType = "VARCHAR"
end
if (simpleType.name=="TEXT") then
propelType = "LONGVARCHAR"
end
if (simpleType.name=="MEDIUMTEXT") then
propelType = "CLOB"
end
if (simpleType.name=="LONGTEXT") then
propelType = "CLOB"
end
-- convert DATETIME TO TIMESTAMP (this will be converted back to DATETIME by Propel 1.3)
if (simpleType.name=="DATETIME") then
propelType = "TIMESTAMP"
end
return propelType
else
return "EMPTY SIMPLETYPE"
end
end
--
-- Tries to convert workbench user types to propel types
function wbUserType2PropelDatatype(userType)
-- local propelType="**UNKNOWN** ("..simpleType.name..")"
-- We assume that the simpleType corresponds to the propel type by default
-- This is correct 95% of the time
if (userType~=nil) then
local propelType=""
-- convert MySQL Workbench defined user-types to Propel-Types
if (userType.name=="BOOL") then
propelType = "BOOLEAN"
end
if (userType.name=="BOOLEAN") then
propelType = "BOOLEAN"
end
-- if you have custom mappings you could add cases for them here:
-- Check if we found a conversion, if not use the simple-type converter with the actual definition of the user-type
if (propelType=="") then
propelType=wbSimpleType2PropelDatatype(userType.actualType)
end
return propelType
else
return "EMPTY USERTYPE"
end
end
--
-- converts unusable characters to underscores
function sanitizeName(name)
local newName= name.gsub(name,'%-','_')
local newName2=string.gsub(newName,'%s+',"_")
return newName2;
end
--
-- Takes a catalog object and converts it to a propel xml-schema
-- This method is called from the two plugin entry-points to generate the actual schema
function geneneratePropelSchemaFromCatalog(catalog)
local xml=XMLWriter:new('database')
xml:enableIndent()
local firstSchema = catalog.schemata[1]
xml:addAttribute("defaultIdMethod","native")
xml:addAttribute("name",firstSchema.name)
if (catalog.customData["propelExportBaseClass"]~= nil and catalog.customData["propelExportBaseClass"]~="") then
xml:addAttribute("baseClass",catalog.customData["propelExportBaseClass"]);
end
if (catalog.customData["propelExportPackage"]~= nil and catalog.customData["propelExportPackage"]~="") then
xml:addAttribute("package",catalog.customData["propelExportPackage"]);
end
-- go through all tables:
for i = 1, grtV.getn(catalog.schemata) do
schema = catalog.schemata[i]
for j = 1, grtV.getn(schema.tables) do
currentTable = schema.tables[j]
xml:openTag('table')
xml:addAttribute('name',currentTable.name)
if (currentTable.customData["phpName"]~=nil)
then
xml:addAttribute('phpName',currentTable.customData["phpName"]);
end
if (string.match(currentTable.comment,".*<behaviors>.*</behaviors>.*"))
then
-- add behaviors
if ( currentTable.comment ~= nil and currentTable.comment ~= "" ) then
for behaviors in string.gmatch(currentTable.comment, ".*<behaviors>(.+)</behaviors>.*") do
if ( behaviors ~= "" and behaviors ~= nil ) then
xml:addXml(behaviors)
end
end
end
end
-- now fetch all columns:
for k = 1, grtV.getn(currentTable.columns) do
currentColumn=currentTable.columns[k]
local propelType=''
xml:openTag('column')
xml:addAttribute('name',currentColumn.name)
if (currentColumn.simpleType~=nil)
then
propelType=wbSimpleType2PropelDatatype(currentColumn.simpleType)
else
propelType=wbUserType2PropelDatatype(currentColumn.userType)
end
xml:addAttribute('type',propelType)
if (currentColumn.length~=-1) then
xml:addAttribute('size',currentColumn.length)
end
if (string.match(currentColumn.comment,".*<inheritances>.*</inheritances>.*"))
then
-- add single inheritances
for inheritances in string.gmatch(currentColumn.comment, ".*<inheritances>(.*)</inheritances>.*") do
if ( inheritances ~= "" and inheritances ~= nil ) then
xml:addXml(inheritances)
end
end
end
local columnType=nil;
if (currentColumn.simpleType~=nil)
then columnType=currentColumn.simpleType
else columnType=currentColumn.userType.actualType end
if (propelType=="CLOB" or propelType=="VARCHAR") then
if (columnType.name=="TINYTEXT") then xml:addAttribute('size',255);
elseif (columnType.name=="MEDIUMTEXT")then xml:addAttribute('size',16777215);
elseif (columnType.name=="LONGTEXT") then xml:addAttribute('size',4294967295); end
elseif (columnType.name=="DECIMAL") then
xml:addAttribute('size',currentColumn.precision);
xml:addAttribute('scale',currentColumn.scale);
end
-- try to find out if this is the primary key column
for k = 1, grtV.getn(currentTable.indices) do
index=currentTable.indices[k]
if (index.indexType=="PRIMARY") then
for l=1, grtV.getn(index.columns) do
column=index.columns[l]
if (column.referencedColumn.name==currentColumn.name) then
xml:addAttribute('primaryKey','true')
end
end
end
end
if (currentColumn.isNotNull==1) then
xml:addAttribute('required','true')
end
-- add a default value if available
if (currentColumn.defaultValue~='' and currentColumn.defaultValue~='CURRENT_TIMESTAMP') then
xml:addAttribute('default',currentColumn.defaultValue)
end
-- add Propel 1.3 defaultExpr
if (currentColumn.defaultValue~="") then
xml:addAttribute('defaultExpr',currentColumn.defaultValue)
-- print ("Default Value:"..currentColumn.defaultValue.."\n")
end
if (currentColumn.autoIncrement==1) then
xml:addAttribute('autoIncrement','true')
end
-- if (currentColumn.comment~='') then
-- xml:addAttribute('description',currentColumn.comment)
-- end
xml:closeTag()
end
-- add foreign keys:
for k = 1, grtV.getn(currentTable.foreignKeys) do
foreignKey=currentTable.foreignKeys[k]
xml:openTag('foreign-key')
xml:addAttribute('name',sanitizeName(foreignKey.name))
xml:addAttribute('foreignTable',foreignKey.referencedTable.name)
if(foreignKey.deleteRule~="" and foreignKey.deleteRule~="NO ACTION") then
xml:addAttribute('onDelete',string.lower(foreignKey.deleteRule))
end
if(foreignKey.updateRule~="" and foreignKey.updateRule~="NO ACTION") then
xml:addAttribute('onUpdate',string.lower(foreignKey.updateRule))
end
for l=1, grtV.getn(foreignKey.columns) do
xml:openTag('reference')
xml:addAttribute('local',foreignKey.columns[l].name)
xml:addAttribute('foreign',foreignKey.referencedColumns[l].name)
xml:closeTag()
end
xml:closeTag()
end
-- add unique keys:
for k = 1, grtV.getn(currentTable.indices) do
index=currentTable.indices[k]
if (index.indexType=="UNIQUE") then
xml:openTag('unique')
xml:addAttribute('name',sanitizeName(index.name))
for l=1, grtV.getn(index.columns) do
column=index.columns[l]
xml:openTag('unique-column')
xml:addAttribute('name',column.referencedColumn.name)
if (column.referencedColumn.length>0) then xml:addAttribute('size',column.referencedColumn.length) end
xml:closeTag()
end
xml:closeTag()
end
end
-- add remaining indices
for k = 1, grtV.getn(currentTable.indices) do
index=currentTable.indices[k]
if (index.indexType=="INDEX") then
xml:openTag('index')
xml:addAttribute('name',sanitizeName(index.name))
for l=1, grtV.getn(index.columns) do
column=index.columns[l]
xml:openTag('index-column')
xml:addAttribute('name',column.referencedColumn.name)
xml:closeTag()
end
xml:closeTag()
end
end
xml:closeTag()
end
end
xml:finishDocument()
return xml:getXML();
end
------------------
-- Plugin methods:
------------------
--
-- Export the propel-schema of the selected catalog to the clipboard
-- This is the main plugin method which is called from the menu
function exportPropelSchemaToClipboard(catalog)
printVersion();
local propelSchema=geneneratePropelSchemaFromCatalog(catalog);
-- print ("Propel-Schema looks like this:\n")
-- print(propelSchema);
Workbench:copyToClipboard(propelSchema)
print('\n > Propel-Schema was copied to clipboard');
return 0
end
--
-- Export the propel-schema of the selected catalog to the a filename
-- This is the main plugin method which is called from the menu
function exportPropelSchemaToFile(catalog)
printVersion();
local propelSchema=geneneratePropelSchemaFromCatalog(catalog);
-- print ("Propel-Schema looks like this:\n")
-- print(propelSchema);
if (catalog.customData["propelExportPath"] ~= nil) then
-- print("\nFilepath is: "..catalog.customData["propelExportPath"]);
if (Workbench:confirm("Proceed?","Do you want to overwrite previously exported file "..catalog.customData["propelExportPath"].." ?") == 1)
then
propelExportPath=catalog.customData["propelExportPath"];
else
propelExportPath=Workbench:input('Filename? Please enter Filename to export the propel schema to');
if (propelExportPath~="")
then
-- Try to save the filepath for the next time:
catalog.customData["propelExportPath"]=propelExportPath;
end
end
else
propelExportPath=Workbench:input('Filename? Please enter Filename to export the propel schema to');
if (propelExportPath~="")
then
-- Try to save the filepath for the next time:
catalog.customData["propelExportPath"]=propelExportPath;
end
end
if propelExportPath~='' then
f = io.open(propelExportPath,"w");
if (f~=nil) then
f.write(f,propelSchema);
f.close(f);
print('\n > Propel-Schema was exported to '..propelExportPath);
else
print('\n > Could not open file '..propelExportPath..'!');
end
else
print('\n > Propel-Schema was NOT exported as no path was given!');
end
return 0
end
--
-- Set a custom baseClass to be added to the database tag of the resulting schema file.
-- This is saved inside the workbench file, so on subsequent exports it is reused.
function setPropelExportBaseClass(catalog)
printVersion();
print("Setting baseClass...");
local question="New baseClass?";
if (catalog.customData["propelExportBaseClass"]~=nil) then
question = question .. " (" .. catalog.customData["propelExportBaseClass"] ..")"
end
propelExportBaseClass=Workbench:input(question);
if (propelExportBaseClass==" ")
then
-- Remove the previously set base-class
catalog.customData["propelExportBaseClass"]="";
elseif (propelExportBaseClass~="")
then
-- Try to save the base-class
catalog.customData["propelExportBaseClass"]=propelExportBaseClass;
end
print ("done");
end
--
-- Set a custom package to be added to the database tag of the resulting schema file.
-- This is saved inside the workbench file, so on subsequent exports it is reused.
function setPropelExportPackage(catalog)
printVersion();
print("Setting package...");
local question="New package?";
if (catalog.customData["propelExportPackage"]~=nil) then
question = question .. " (" .. catalog.customData["propelExportPackage"] ..")"
end
propelExportPackage=Workbench:input(question);
if (propelExportPackage==" ")
then
-- Remove the previously set package
catalog.customData["propelExportPackage"]="";
elseif (propelExportPackage~="")
then
-- Try to save the package
catalog.customData["propelExportPackage"]=propelExportPackage;
end
print ("done");
end
--
-- Sets a custom phpName for a table.
-- If you want to unset the phpName, enter a single space as phpName
function setPropelExportPhpName(_table)
printVersion();
print("Setting phpName of " .. _table.name);
local question="New phpName?";
if (_table.customData["phpName"]~=nil) then
question = question .. " (" .. _table.customData["phpName"] ..")";
end
phpName=Workbench:input(question);
if (phpName==" ")
then
-- Remove the previously set phpName
_table.customData["phpName"]=nil;
elseif (phpName~="")
then
-- Try to save the phpName
_table.customData["phpName"]=phpName;
end
--this is a trick to let MySQL Workbench believe there are changes in the file:
_table.owner.owner.owner.customData["phpName".._table.name]="set";
print ("done");
end