X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=parser.js;h=2b75709dddeae00dddf82d8787b9d686a7d70033;hb=8edb29ffa12fda61a939295ce00a6d22133131d1;hp=da8a1d56d7f3e39e4d1dabfd7795c5f666170f46;hpb=6a430a228d953ec4e9fe1b7fe324becb27d9ebbf;p=erdiag.git diff --git a/parser.js b/parser.js index da8a1d5..2b75709 100644 --- a/parser.js +++ b/parser.js @@ -15,16 +15,17 @@ class ErDiags this.sqlText = ""; } - static get CARDINAL() + static CARDINAL(symbol) { - return { - "*": "0,n", - "+": "1,n", - "?": "0,1", - "1": "1,1", - "?R": "(0,1)", - "1R": "(1,1)", - }; + let res = { "*": "0,n", "+": "1,n", "?": "0,1", "1": "1,1" } [ symbol[0] ]; + if (symbol.length >= 2) + { + if (symbol[1] == 'R') + res = '(' + res + ')'; + else if (['>','<'].includes(symbol[1])) + res += symbol[1]; + } + return res; } /////////////////////////////// @@ -99,13 +100,12 @@ class ErDiags field.isKey = true; line = line.slice(1); } - field.name = line.match(/[^()"\s]+/)[0]; - let parenthesis = line.match(/\((.+)\)/); - if (parenthesis !== null) + field.name = line.match(/[^"\s]+/)[0]; + let sqlClues = line.substring(field.name.length).trim(); + if (sqlClues.length > 0) { - let sqlClues = parenthesis[1]; field.type = sqlClues.match(/[^\s]+/)[0]; //type is always the first indication (mandatory) - field.qualifiers = sqlClues.substring(field.type.length).trim(); + field.qualifiers = sqlClues.substring(field.type.length); } attributes.push(field); } @@ -166,12 +166,15 @@ class ErDiags Object.keys(this.entities).forEach( name => { let newTable = [ ]; //array of fields this.entities[name].attributes.forEach( attr => { - newTable.push({ + let newField = { name: attr.name, type: attr.type, isKey: attr.isKey, qualifiers: attr.qualifiers, - }); + }; + if (!!attr.qualifiers && !!attr.qualifiers.match(/foreign/i)) + Object.assign(newField, {ref: attr.qualifiers.match(/references ([^\s]+)/i)[1]}); + newTable.push(newField); }); this.tables[name] = newTable; }); @@ -192,9 +195,11 @@ class ErDiags // Pass 2: parse associations, add foreign keys when cardinality is 0,1 or 1,1 this.associations.forEach( a => { let newTableAttrs = [ ]; + let hasZeroOne = false; a.entities.forEach( e => { if (['?','1'].includes(e.card[0])) { + hasZeroOne = true; // Foreign key apparition (for each entity in association minus current one, for each identifying attribute) a.entities.forEach( e2 => { if (e2.name == e.name) @@ -223,7 +228,7 @@ class ErDiags }); } }); - if (newTableAttrs.length > 1) + if (!hasZeroOne && newTableAttrs.length > 1) { // Ok, really create a new table let newTable = { @@ -241,6 +246,22 @@ class ErDiags }); }); }); + // Check for duplicates (in case of self-relationship), rename if needed + newTable.fields.forEach( (f,i) => { + const idx = newTable.fields.findIndex( item => { return item.name == f.name; }); + if (idx < i) + { + // Current field is a duplicate + let suffix = 2; + let newName = f.name + suffix; + while (newTable.fields.findIndex( item => { return item.name == newName; }) >= 0) + { + suffix++; + newName = f.name + suffix; + } + f.name = newName; + } + }); // Add relationship potential own attributes (a.attributes || [ ]).forEach( attr => { newTable.fields.push({ @@ -390,7 +411,7 @@ class ErDiags mcdDot += '"' + e.name + '":name -- "' + name + '"'; else mcdDot += '"' + name + '" -- "' + e.name + '":name'; - mcdDot += '[label="' + ErDiags.CARDINAL[e.card] + '"];\n'; + mcdDot += '[label="' + ErDiags.CARDINAL(e.card) + '"];\n'; }); }); mcdDot += '}'; @@ -420,7 +441,7 @@ class ErDiags mldDot += '"' + name + '" [label=<\n'; mldDot += '\n'; this.tables[name].forEach( f => { - let label = (f.isKey ? '' : '') + (!!f.qualifiers && f.qualifiers.indexOf("foreign")>=0 ? '#' : '') + f.name + (f.isKey ? '' : ''); + let label = (f.isKey ? '' : '') + (!!f.ref ? '#' : '') + f.name + (f.isKey ? '' : ''); mldDot += '\n'; if (!!f.ref) { @@ -465,7 +486,8 @@ class ErDiags sqlText += "CREATE TABLE " + name + " (\n"; let key = ""; this.tables[name].forEach( f => { - sqlText += "\t" + f.name + " " + (f.type || "TEXT") + " " + (f.qualifiers || "") + ",\n"; + let type = f.type || (f.isKey ? "INTEGER" : "TEXT"); + sqlText += "\t" + f.name + " " + type + " " + (f.qualifiers || "") + ",\n"; if (f.isKey) key += (key.length>0 ? "," : "") + f.name; });
' + name + '
' + label + '