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