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