db70f7f282e074c415a990ac1ad422b6c2683624
[erdiag.git] / parser.js
1 // ER diagram description parser
2 class ErDiags
3 {
4 constructor(description)
5 {
6 this.entities = { };
7 this.inheritances = [ ];
8 this.associations = [ ];
9 this.tables = { };
10 this.mcdParsing(description);
11 this.mldParsing();
12
13 console.log(this.tables);
14
15 // Cache SVG graphs returned by server (in addition to server cache = good perfs)
16 this.mcdGraph = "";
17 this.mldGraph = "";
18 this.sqlText = "";
19 }
20
21 static get CARDINAL()
22 {
23 return {
24 "*": "0,n",
25 "+": "1,n",
26 "?": "0,1",
27 "1": "1,1",
28 "?R": "(0,1)",
29 "1R": "(1,1)",
30 };
31 }
32
33 ///////////////////////////////
34 // PARSING STAGE 1: text to MCD
35 ///////////////////////////////
36
37 // Parse a textual description into a json object
38 mcdParsing(text)
39 {
40 let lines = text.split("\n");
41 lines.push(""); //easier parsing: always empty line at the end
42 let start = -1;
43 for (let i=0; i < lines.length; i++)
44 {
45 lines[i] = lines[i].trim();
46 // Empty line ?
47 if (lines[i].length == 0)
48 {
49 if (start >= 0) //there is some group of lines to parse
50 {
51 this.parseThing(lines, start, i);
52 start = -1;
53 }
54 }
55 else //not empty line: just register starting point
56 {
57 if (start < 0)
58 start = i;
59 }
60 }
61 }
62
63 // Parse a group of lines into entity, association, ...
64 parseThing(lines, start, end) //start included, end excluded
65 {
66 switch (lines[start].charAt(0))
67 {
68 case '[':
69 // Entity = { name: { attributes, [weak] } }
70 let name = lines[start].match(/[^\[\]"\s]+/)[0];
71 let entity = { attributes: this.parseAttributes(lines, start+1, end) };
72 if (lines[start].charAt(1) == '[')
73 entity.weak = true;
74 this.entities[name] = entity;
75 break;
76 case 'i': //inheritance (arrows)
77 this.inheritances = this.inheritances.concat(this.parseInheritance(lines, start+1, end));
78 break;
79 case '{': //association
80 // Association = { [name], [attributes], [weak], entities: ArrayOf entity indices }
81 let relationship = { };
82 let nameRes = lines[start].match(/[^{}"\s]+/);
83 if (nameRes !== null)
84 relationship.name = nameRes[0];
85 if (lines[start].charAt(1) == '{')
86 relationship.weak = true;
87 this.associations.push(Object.assign({}, relationship, this.parseAssociation(lines, start+1, end)));
88 break;
89 }
90 }
91
92 // attributes: ArrayOf {name, [isKey], [type], [qualifiers]}
93 parseAttributes(lines, start, end)
94 {
95 let attributes = [];
96 for (let i=start; i<end; i++)
97 {
98 let field = { };
99 let line = lines[i];
100 if (line.charAt(0) == '#')
101 {
102 field.isKey = true;
103 line = line.slice(1);
104 }
105 field.name = line.match(/[^()"\s]+/)[0];
106 let parenthesis = line.match(/\((.+)\)/);
107 if (parenthesis !== null)
108 {
109 let sqlClues = parenthesis[1];
110 field.type = sqlClues.match(/[^\s]+/)[0]; //type is always the first indication (mandatory)
111 field.qualifiers = sqlClues.substring(field.type.length).trim();
112 }
113 attributes.push(field);
114 }
115 return attributes;
116 }
117
118 // GroupOf Inheritance: { parent, children: ArrayOf entity indices }
119 parseInheritance(lines, start, end)
120 {
121 let inheritance = [];
122 for (let i=start; i<end; i++)
123 {
124 let lineParts = lines[i].split(" ");
125 let children = [];
126 for (let j=1; j<lineParts.length; j++)
127 children.push(lineParts[j]);
128 inheritance.push({ parent:lineParts[0], children: children });
129 }
130 return inheritance;
131 }
132
133 // Association (parsed here): {
134 // entities: ArrayOf entity names + cardinality,
135 // [attributes: ArrayOf {name, [isKey], [type], [qualifiers]}]
136 // }
137 parseAssociation(lines, start, end)
138 {
139 let assoce = { };
140 let entities = [];
141 let i = start;
142 while (i < end)
143 {
144 if (lines[i].charAt(0) == '-')
145 {
146 assoce.attributes = this.parseAttributes(lines, i+1, end);
147 break;
148 }
149 else
150 {
151 // Read entity name + cardinality
152 let lineParts = lines[i].split(" ");
153 entities.push({ name:lineParts[0], card:lineParts[1] });
154 }
155 i++;
156 }
157 assoce.entities = entities;
158 return assoce;
159 }
160
161 //////////////////////////////
162 // PARSING STAGE 2: MCD to MLD
163 //////////////////////////////
164
165 // From entities + relationships to tables
166 mldParsing()
167 {
168 // Pass 1: initialize tables
169 Object.keys(this.entities).forEach( name => {
170 let newTable = [ ]; //array of fields
171 this.entities[name].attributes.forEach( attr => {
172 newTable.push({
173 name: attr.name,
174 type: attr.type,
175 isKey: attr.isKey,
176 qualifiers: attr.qualifiers,
177 });
178 });
179 this.tables[name] = newTable;
180 });
181 // Pass 2: parse associations, add foreign keys when cardinality is 0,1 or 1,1
182 this.associations.forEach( a => {
183 let newTableAttrs = [ ];
184 a.entities.forEach( e => {
185 if (['?','1'].includes(e.card[0]))
186 {
187 // Foreign key apparition (for each entity in association minus current one, for each identifying attribute)
188 a.entities.forEach( e2 => {
189 if (e2.name == e.name)
190 return;
191 e2.attributes.forEach( attr => {
192 if (attr.isKey)
193 {
194 this.tables[e.name].push({
195 isKey: e.card.length >= 2 && e.card[1] == 'R', //"weak tables" foreign keys become part of the key
196 name: "#" + e2.name + "_" + attr.name,
197 type: attr.type,
198 qualifiers: "foreign key references " + e2.name + " " + (e.card[0]=='1' ? "not null" : ""),
199 ref: e2.name, //easier drawMld function (fewer regexps)
200 });
201 }
202 });
203 });
204 }
205 else
206 {
207 // Add all keys in current entity
208 let fields = this.entities[e.name].attributes.filter( attr => { return attr.isKey; });
209 newTableAttrs.push({
210 fields: fields,
211 entity: e.name,
212 });
213 }
214 });
215 if (newTableAttrs.length > 1)
216 {
217 // Ok, really create a new table
218 let newTable = {
219 name: a.name || newTableAttrs.map( item => { return item.entity; }).join("_"),
220 fields: [ ],
221 };
222 newTableAttrs.forEach( item => {
223 item.fields.forEach( f => {
224 newTable.fields.push({
225 name: item.entity + "_" + f.name,
226 isKey: true,
227 type: f.type,
228 qualifiers: (f.qualifiers+" " || "") + "foreign key references " + item.entity + " not null",
229 ref: item.entity,
230 });
231 });
232 });
233 // Add relationship potential own attributes
234 a.attributes.forEach( attr => {
235 newTable.fields.push({
236 name: attr.name,
237 isKey: false,
238 type: attr.type,
239 qualifiers: attr.qualifiers,
240 });
241 });
242 this.tables[newTable.name] = newTable.fields;
243 }
244 });
245 }
246
247 /////////////////////////////////
248 // DRAWING + GET SQL FROM PARSING
249 /////////////////////////////////
250
251 static AjaxGet(dotInput, callback)
252 {
253 let xhr = new XMLHttpRequest();
254 xhr.onreadystatechange = function() {
255 if (this.readyState == 4 && this.status == 200)
256 callback(this.responseText);
257 };
258 xhr.open("GET", "scripts/getGraphSvg.php?dot=" + encodeURIComponent(dotInput), true);
259 xhr.send();
260 }
261
262 // "Modèle conceptuel des données". TODO: option for graph size
263 // NOTE: randomizing helps to obtain better graphs (sometimes)
264 drawMcd(id, mcdStyle) //mcdStyle: bubble, or compact
265 {
266 let element = document.getElementById(id);
267 mcdStyle = mcdStyle || "compact";
268 if (this.mcdGraph.length > 0)
269 {
270 element.innerHTML = this.mcdGraph;
271 return;
272 }
273 // Build dot graph input
274 let mcdDot = 'graph {\n';
275 mcdDot += 'rankdir="LR";\n';
276 // Nodes:
277 if (mcdStyle == "compact")
278 mcdDot += 'node [shape=plaintext];\n';
279 _.shuffle(Object.keys(this.entities)).forEach( name => {
280 if (mcdStyle == "bubble")
281 {
282 mcdDot += '"' + name + '" [shape=rectangle, label="' + name + '"';
283 if (this.entities[name].weak)
284 mcdDot += ', peripheries=2';
285 mcdDot += '];\n';
286 if (!!this.entities[name].attributes)
287 {
288 this.entities[name].attributes.forEach( a => {
289 let label = (a.isKey ? '#' : '') + a.name;
290 let attrName = name + '_' + a.name;
291 mcdDot += '"' + attrName + '" [shape=ellipse, label="' + label + '"];\n';
292 if (Math.random() < 0.5)
293 mcdDot += '"' + attrName + '" -- "' + name + '";\n';
294 else
295 mcdDot += '"' + name + '" -- "' + attrName + '";\n';
296 });
297 }
298 }
299 else
300 {
301 mcdDot += '"' + name + '" [label=<';
302 if (this.entities[name].weak)
303 {
304 mcdDot += '<table port="name" BORDER="1" ALIGN="LEFT" CELLPADDING="0" CELLSPACING="3" CELLBORDER="0">' +
305 '<tr><td><table BORDER="1" ALIGN="LEFT" CELLPADDING="5" CELLSPACING="0">\n';
306 }
307 else
308 mcdDot += '<table port="name" BORDER="1" ALIGN="LEFT" CELLPADDING="5" CELLSPACING="0">\n';
309 mcdDot += '<tr><td BGCOLOR="#ae7d4e" BORDER="0"><font COLOR="#FFFFFF">' + name + '</font></td></tr>\n';
310 if (!!this.entities[name].attributes)
311 {
312 this.entities[name].attributes.forEach( a => {
313 let label = (a.isKey ? '<u>' : '') + a.name + (a.isKey ? '</u>' : '');
314 mcdDot += '<tr><td BGCOLOR="#FFFFFF" BORDER="0" ALIGN="LEFT"><font COLOR="#000000" >' + label + '</font></td></tr>\n';
315 });
316 }
317 mcdDot += '</table>';
318 if (this.entities[name].weak)
319 mcdDot += '</td></tr></table>';
320 mcdDot += '>];\n';
321 }
322 });
323 // Inheritances:
324 _.shuffle(this.inheritances).forEach( i => {
325 // TODO: node shape = triangle fill yellow. See
326 // https://merise.developpez.com/faq/?page=MCD#CIF-ou-dependance-fonctionnelle-de-A-a-Z
327 // https://merise.developpez.com/faq/?page=MLD#Comment-transformer-un-MCD-en-MLD
328 // https://www.developpez.net/forums/d1088964/general-developpement/alm/modelisation/structure-agregation-l-association-d-association/
329 _.shuffle(i.children).forEach( c => {
330 if (Math.random() < 0.5)
331 mcdDot += '"' + c + '":name -- "' + i.parent;
332 else
333 mcdDot += '"' + i.parent + '":name -- "' + c;
334 mcdDot += '":name [dir="forward", arrowhead="vee", style="dashed"];\n';
335 });
336 });
337 // Relationships:
338 if (mcdStyle == "compact")
339 mcdDot += 'node [shape=rectangle, style=rounded];\n';
340 let assoceCounter = 0;
341 _.shuffle(this.associations).forEach( a => {
342 let name = a.name || "_assoce" + assoceCounter++;
343 if (mcdStyle == "bubble")
344 {
345 mcdDot += '"' + name + '" [shape="diamond", style="filled", color="lightgrey", label="' + name + '"';
346 if (a.weak)
347 mcdDot += ', peripheries=2';
348 mcdDot += '];\n';
349 if (!!a.attributes)
350 {
351 a.attributes.forEach( attr => {
352 let label = (attr.isKey ? '#' : '') + attr.name;
353 mcdDot += '"' + name + '_' + attr.name + '" [shape=ellipse, label="' + label + '"];\n';
354 let attrName = name + '_' + attr.name;
355 if (Math.random() < 0.5)
356 mcdDot += '"' + attrName + '" -- "' + name + '";\n';
357 else
358 mcdDot += '"' + name + '" -- "' + attrName + '";\n';
359 });
360 }
361 }
362 else
363 {
364 let label = '<' + name + '>';
365 if (!!a.attributes)
366 {
367 a.attributes.forEach( attr => {
368 let attrLabel = (attr.isKey ? '#' : '') + attr.name;
369 label += '\\n' + attrLabel;
370 });
371 }
372 mcdDot += '"' + name + '" [color="lightgrey", label="' + label + '"';
373 if (a.weak)
374 mcdDot += ', peripheries=2';
375 mcdDot += '];\n';
376 }
377 _.shuffle(a.entities).forEach( e => {
378 if (Math.random() < 0.5)
379 mcdDot += '"' + e.name + '":name -- "' + name + '"';
380 else
381 mcdDot += '"' + name + '" -- "' + e.name + '":name';
382 mcdDot += '[label="' + ErDiags.CARDINAL[e.card] + '"];\n';
383 });
384 });
385 mcdDot += '}';
386 console.log(mcdDot);
387 ErDiags.AjaxGet(mcdDot, graphSvg => {
388 this.mcdGraph = graphSvg;
389 element.innerHTML = graphSvg;
390 });
391 }
392
393 // "Modèle logique des données", from MCD without anomalies
394 // TODO: this one should draw links from foreign keys to keys (port=... in <TD>)
395 drawMld(id)
396 {
397 let element = document.getElementById(id);
398 if (this.mldGraph.length > 0)
399 {
400 element.innerHTML = this.mcdGraph;
401 return;
402 }
403 // Build dot graph input (assuming foreign keys not already present...)
404 let mldDot = 'graph {\n';
405 mldDot += 'node [shape=plaintext];\n';
406 let links = "";
407 _.shuffle(Object.keys(this.tables)).forEach( name => {
408 mldDot += '"' + name + '" [label=<<table BORDER="1" ALIGN="LEFT" CELLPADDING="5" CELLSPACING="0">\n';
409 mldDot += '<tr><td BGCOLOR="#ae7d4e" BORDER="0"><font COLOR="#FFFFFF">' + name + '</font></td></tr>\n';
410 this.tables[name].forEach( f => {
411 let label = (f.isKey ? '<u>' : '') + (!!f.qualifiers && f.qualifiers.indexOf("foreign")>=0 ? '#' : '') + f.name + (f.isKey ? '</u>' : '');
412 mldDot += '<tr><td port="' + f.name + '"' + (f.isKey ? ' port="__key"' : '')
413 + ' BGCOLOR="#FFFFFF" BORDER="0" ALIGN="LEFT"><font COLOR="#000000" >' + label + '</font></td></tr>\n';
414 if (!!f.ref)
415 {
416 if (Math.random() < 0.5)
417 links += '"' + f.ref + '":__key -- "' + name+'":"'+f.name+'"\n';
418 else
419 links += '"'+name+'":"'+f.name+'" -- "' + f.ref + '":__key\n';
420 }
421 });
422 mldDot += '</table>>];\n';
423 });
424 mldDot += links + '\n';
425 mldDot += '}\n';
426 console.log(mldDot);
427 ErDiags.AjaxGet(mldDot, graphSvg => {
428 this.mldGraph = graphSvg;
429 element.innerHTML = graphSvg;
430 });
431 }
432
433 fillSql(id)
434 {
435 let element = document.getElementById(id);
436 if (this.sqlText.length > 0)
437 {
438 element.innerHTML = this.sqlText;
439 return;
440 }
441 let sqlText = "";
442 Object.keys(this.tables).forEach( name => {
443 sqlText += "CREATE TABLE " + name + " (\n";
444 let key = "";
445 this.tables[name].forEach( f => {
446 sqlText += f.name + " " + (f.type || "TEXT") + (" "+f.qualifiers || "") + ",\n";
447 if (f.isKey)
448 key += (key.length>0 ? "," : "") + f.name;
449 });
450 sqlText += "PRIMARY KEY (" + key + ")\n";
451 sqlText += ");\n";
452 });
453 //console.log(sqlText);
454 this.sqlText = sqlText;
455 element.innerHTML = "<pre><code>" + sqlText + "</code></pre>";
456 }
457 }