X-Git-Url: https://git.auder.net/?a=blobdiff_plain;ds=sidebyside;f=parser.js;h=41eff934592d31c99143611465a7e1f001ae4f63;hb=3ca1e50c87c462e39ba19b5c7720715353315080;hp=0d52ee00005bb6889b298d40ee312ab0c68a4956;hpb=4ef6cdeda1d92848df61a92f8fa4af20761b99bb;p=erdiag.git diff --git a/parser.js b/parser.js index 0d52ee0..41eff93 100644 --- a/parser.js +++ b/parser.js @@ -6,19 +6,15 @@ class ErDiags this.entities = { }; this.inheritances = [ ]; this.associations = [ ]; - this.txt2json(description); + this.tables = { }; + this.mcdParsing(description); + this.mldParsing(); // Cache SVG graphs returned by server (in addition to server cache = good perfs) this.mcdGraph = ""; this.mldGraph = ""; this.sqlText = ""; } - static get TYPES() - { - // SQLite storage classes without null - return ["integer","real","text","blob"]; - } - static get CARDINAL() { return { @@ -31,12 +27,12 @@ class ErDiags }; } - ////////////////// - // PARSING STAGE 1 - ////////////////// + /////////////////////////////// + // PARSING STAGE 1: text to MCD + /////////////////////////////// // Parse a textual description into a json object - txt2json(text) + mcdParsing(text) { let lines = text.split("\n"); lines.push(""); //easier parsing: always empty line at the end @@ -108,14 +104,8 @@ class ErDiags if (parenthesis !== null) { let sqlClues = parenthesis[1]; - let qualifiers = sqlClues; - let firstWord = sqlClues.match(/[^\s]+/)[0]; - if (ErDiags.TYPES.includes(firstWord)) - { - field.type = firstWord; - qualifiers = sqlClues.substring(firstWord.length).trim(); - } - field.qualifiers = qualifiers; + field.type = sqlClues.match(/[^\s]+/)[0]; //type is always the first indication (mandatory) + field.qualifiers = sqlClues.substring(field.type.length).trim(); } attributes.push(field); } @@ -165,9 +155,109 @@ class ErDiags return assoce; } - ////////////////// - // PARSING STAGE 2 - ////////////////// + ////////////////////////////// + // PARSING STAGE 2: MCD to MLD + ////////////////////////////// + + // From entities + relationships to tables + mldParsing() + { + // Pass 1: initialize tables + Object.keys(this.entities).forEach( name => { + let newTable = [ ]; //array of fields + this.entities[name].attributes.forEach( attr => { + newTable.push({ + name: attr.name, + type: attr.type, + isKey: attr.isKey, + qualifiers: attr.qualifiers, + }); + }); + this.tables[name] = newTable; + }); + // Add foreign keys information for children (inheritance). TODO: allow several levels + // NOTE: modelisation assume each child has its own table, refering parent (other options exist) + this.inheritances.forEach( inh => { + let idx = this.tables[inh.parent].findIndex( item => { return item.isKey; }); + inh.children.forEach( c => { + this.tables[c].push({ + name: inh.parent + "_id", + type: this.tables[inh.parent][idx].type, + isKey: true, + qualifiers: (this.tables[inh.parent][idx].qualifiers || "") + " foreign key references " + inh.parent, + ref: inh.parent, + }); + }); + }); + // Pass 2: parse associations, add foreign keys when cardinality is 0,1 or 1,1 + this.associations.forEach( a => { + let newTableAttrs = [ ]; + a.entities.forEach( e => { + if (['?','1'].includes(e.card[0])) + { + // Foreign key apparition (for each entity in association minus current one, for each identifying attribute) + a.entities.forEach( e2 => { + if (e2.name == e.name) + return; + e2.attributes.forEach( attr => { + if (attr.isKey) + { + this.tables[e.name].push({ + isKey: e.card.length >= 2 && e.card[1] == 'R', //"weak tables" foreign keys become part of the key + name: e2.name + "_" + attr.name, + type: attr.type, + qualifiers: "foreign key references " + e2.name + " " + (e.card[0]=='1' ? "not null" : ""), + ref: e2.name, //easier drawMld function (fewer regexps) + }); + } + }); + }); + } + else + { + // Add all keys in current entity + let fields = this.entities[e.name].attributes.filter( attr => { return attr.isKey; }); + newTableAttrs.push({ + fields: fields, + entity: e.name, + }); + } + }); + if (newTableAttrs.length > 1) + { + // Ok, really create a new table + let newTable = { + name: a.name || newTableAttrs.map( item => { return item.entity; }).join("_"), + fields: [ ], + }; + newTableAttrs.forEach( item => { + item.fields.forEach( f => { + newTable.fields.push({ + name: item.entity + "_" + f.name, + isKey: true, + type: f.type, + qualifiers: (f.qualifiers || "") + " foreign key references " + item.entity + " not null", + ref: item.entity, + }); + }); + }); + // Add relationship potential own attributes + a.attributes.forEach( attr => { + newTable.fields.push({ + name: attr.name, + isKey: false, + type: attr.type, + qualifiers: attr.qualifiers, + }); + }); + this.tables[newTable.name] = newTable.fields; + } + }); + } + + ///////////////////////////////// + // DRAWING + GET SQL FROM PARSING + ///////////////////////////////// static AjaxGet(dotInput, callback) { @@ -249,10 +339,10 @@ class ErDiags // https://www.developpez.net/forums/d1088964/general-developpement/alm/modelisation/structure-agregation-l-association-d-association/ _.shuffle(i.children).forEach( c => { if (Math.random() < 0.5) - mcdDot += '"' + c + '":name -- "' + i.parent; + mcdDot += '"' + c + '":name -- "' + i.parent + '":name [dir="forward",arrowhead="vee",'; else - mcdDot += '"' + i.parent + '":name -- "' + c; - mcdDot += '":name [dir="forward", arrowhead="vee", style="dashed"];\n'; + mcdDot += '"' + i.parent + '":name -- "' + c + '":name [dir="back",arrowtail="vee",'; + mcdDot += 'style="dashed"];\n'; }); }); // Relationships: @@ -260,9 +350,7 @@ class ErDiags mcdDot += 'node [shape=rectangle, style=rounded];\n'; let assoceCounter = 0; _.shuffle(this.associations).forEach( a => { - let name = !!a.name && a.name.length > 0 - ? a.name - : '_assoce' + assoceCounter++; + let name = a.name || "_assoce" + assoceCounter++; if (mcdStyle == "bubble") { mcdDot += '"' + name + '" [shape="diamond", style="filled", color="lightgrey", label="' + name + '"'; @@ -306,14 +394,14 @@ class ErDiags }); }); mcdDot += '}'; - console.log(mcdDot); + //console.log(mcdDot); ErDiags.AjaxGet(mcdDot, graphSvg => { this.mcdGraph = graphSvg; element.innerHTML = graphSvg; }); } - // "Modèle logique des données" + // "Modèle logique des données", from MCD without anomalies // TODO: this one should draw links from foreign keys to keys (port=... in
' + name + ' |
' + label + ' |
" + sqlText + "
";
}
}