· 8 years ago · Feb 04, 2018, 06:36 PM
1[**
2 * Copyright (c) 2016-2017 GenMyModel
3 * See the file located at https://github.com/genmymodel/generators/blob/master/LICENSE for copying permission.
4 *
5 * -=( MYSQL Code Generator )=-
6 *
7 * (Mainly based on the postgresql generator)
8 * We can help you modifying this script so it can match your needs:
9 * send us a ticket, we will gladly help you.
10 *
11 * Author: Vincent Aranega - GenMyModel
12 */]
13[module mysql('http://com.genmymodel.rds.core/1.0')/]
14
15[template public generate(db : Database)]
16[comment @main/]
17[file (db.name + '.mysql', false, 'UTF-8')]
18[if (db.getDoc() <> null and db.getDoc() <> '')]
19# [db.getDoc().replaceAll('\n', '\n#')/]
20
21[/if]
22# Create schemas
23[for (g : Group | db.elements->filter(Group))]
24[g.genGroup()/]
25[/for]
26
27# Create tables
28[for (t : Table | db.eAllContents(Table))]
29[t.genTable()/]
30[/for]
31
32# Create FKs
33[for (r : Reference | db.eAllContents(Reference))]
34[r.genFKs()/]
35[/for]
36
37# Create Indexes
38[for (index : Index | db.eAllContents(Index))]
39[index.genIndexes()/]
40[/for]
41
42[/file]
43[/template]
44
45
46[template public genGroup(g : Group)]
47CREATE SCHEMA IF NOT EXISTS [g.qualName()/];
48[for (g.elements->filter(Group))]
49[self.genGroup()/][/for]
50[/template]
51
52
53[template public genTable(t : Table)]
54[if (t.getDoc() <> null and t.getDoc() <> '')]
55# [t.getDoc()/]
56[/if]
57CREATE TABLE IF NOT EXISTS [t.qualName().normalize()/]
58(
59 [for (t.columns) separator (',\n') after(if (t.columns->notEmpty() and t.columns->select(PK)->notEmpty()) then ',\n' else '' endif)]
60 [self.genCols()/][/for]
61 [if (t.columns->select(PK)->notEmpty())]PRIMARY KEY([t.columns->select(PK).name->sep(', ')/])[/if]
62);
63
64[/template]
65
66[template public genIndexes(i : Index)]
67CREATE INDEX [i.name.normalize()/] ON [i.eContainer(Table).qualName().normalize()/][i.genIndexColumns()/];
68[/template]
69
70[template public genIndexColumns(i : Index) ? (i.indexColumns->notEmpty())]
71 [i.indexColumns.column->select(n | n <> null).name.normalize()->sep('(', ', ', ')')/]
72[/template]
73
74
75[template public genFKs(r : Reference)]
76ALTER TABLE [r.foreignKeyColumns->at(1).table.qualName()/]
77 ADD[if (r.name <> null and r.name <> '')] CONSTRAINT FK_[r.name/]
78 [/if]
79 FOREIGN KEY ([r.foreignKeyColumns.name.normalize()->sep(',')/])
80 REFERENCES [r.primaryKeyColumns->at(1).table.qualName()/]([r.primaryKeyColumns.name.normalize()->sep(',')/])
81 [if (r.onDelete <> ReferenceActionType::NO_ACTION)]
82 ON DELETE [r.onDelete.toString().replace('_',' ')/]
83 [/if]
84 [if (r.onUpdate <> ReferenceActionType::NO_ACTION)]
85 ON UPDATE [r.onUpdate.toString().replace('_',' ')/]
86 [/if]
87;
88
89[/template]
90
91[**
92 * One line of the death
93 */]
94[template public genCols(c : Column) post(trim())]
95[c.name.normalize()/] [c.type.genType()/][c.genSize()/][if (c.defaultValue <> null)] DEFAULT [c.genDValue()/][/if][if (not c.nullable or PK)] NOT NULL[/if][if (c.unique)] UNIQUE[/if]
96[if (c.checkValue <> null)] CHECK ([c.checkValue/])[/if]
97[/template]
98
99
100[template public genType(t : Type) ? (name.isValid()) post(trim())]
101[if (t.name = 'VARBINARY')]LONGBLOB
102[elseif (t.name = 'NUMERIC')]DECIMAL
103[elseif (t.name = 'INTEGER')]INT
104[elseif (t.name = 'BOOLEAN')]TINYINT(1)
105[elseif (t.name = 'TIMESTAMP')]DATETIME
106[elseif (t.name = 'text')]LONGTEXT
107[elseif (t.name = 'XML')]LONGTEXT
108[else][t.name/]
109[/if]
110[/template]
111
112
113[template public genDValue(c : Column) ? (c.type.name.isValid()) post(trim())]
114[if (c.type.name = 'VARCHAR' or c.type.name = 'CHARACTER')]'[c.defaultValue/]'
115[else][c.defaultValue/]
116[/if]
117[/template]
118
119
120[comment quite ugly, a better solution would be prefered/]
121[template public genSize(t : Column) ? (t.type.hasSize or t.type.hasScale)]
122([if (t.type.hasSize)][t.size/][/if][if (t.type.hasSize and t.type.hasScale)], [/if][if (t.type.hasScale)][t.scale/][/if])
123[/template]
124
125
126[query public getDoc(a : ModelElement) : String =
127 if (a.getEAnnotation('genmymodel') = null
128 or not a.getEAnnotation('genmymodel').details->exists(key = 'gmm-documentation')) then
129 null
130 else
131 a.getEAnnotation('genmymodel').details->select(key = 'gmm-documentation')->first().value.replaceAll('\n', '\n#')
132 endif
133/]
134
135
136[query public isValid(s : String) : Boolean =
137 s <> null and s <> ''
138/]
139
140
141[query public normalize(s : String) : String =
142 if (s.contains(' ')) then '`' + s + '`' else s endif
143/]
144
145
146[query public qualName(m : ModelElement) : Sequence(String) =
147 m.ancestors(ModelElement)
148 ->reject(oclIsKindOf(Database))
149 ->reverse()
150 .name.normalize()->including(m.name.normalize())->sep('.')
151/]
152
153
154[query public hasProperty(emod : ModelElement, pname : String) : Boolean =
155 emod.getEAnnotation('genmymodel').details->exists(key = pname)
156/]