X-Git-Url: https://git.auder.net/?p=erdiag.git;a=blobdiff_plain;f=parser.js;h=218906c5cb32483f132ac37d9648cceba92c3f92;hp=65ac9ec1fcb9ed8c3f2823707cb476650fcf9b1a;hb=0f208fab135078d0c6b778b7e7df7976256ab74f;hpb=2a12fea9e4863a6d6d800c7d35fc31f4bbc87e4f diff --git a/parser.js b/parser.js index 65ac9ec..218906c 100644 --- a/parser.js +++ b/parser.js @@ -1,7 +1,7 @@ // ER diagram description parser class ErDiags { - constructor(description) + constructor(description, output) { this.entities = { }; this.inheritances = [ ]; @@ -9,9 +9,13 @@ class ErDiags 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.output = output; + if (output == "graph") + { + // Cache SVG graphs returned by server (in addition to server cache = good perfs) + this.mcdGraph = ""; + this.mldGraph = ""; + } this.sqlText = ""; } @@ -170,10 +174,12 @@ class ErDiags name: attr.name, type: attr.type, isKey: attr.isKey, - qualifiers: attr.qualifiers, }; - if (!!attr.qualifiers && !!attr.qualifiers.match(/foreign/i)) + if (!!attr.qualifiers && !!attr.qualifiers.match(/references/i)) + { Object.assign(newField, {ref: attr.qualifiers.match(/references ([^\s]+)/i)[1]}); + newField.qualifiers = attr.qualifiers.replace(/references [^\s]+/i, ""); + } newTable.push(newField); }); this.tables[name] = newTable; @@ -187,8 +193,8 @@ class ErDiags 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, + qualifiers: this.tables[inh.parent][idx].qualifiers || "", + ref: inh.parent + "(" + this.tables[inh.parent][idx].name + ")", }); }); }); @@ -213,8 +219,8 @@ class ErDiags isKey: isKey, name: e2.name + "_" + attr.name, type: attr.type, - qualifiers: "foreign key references " + e2.name + " " + (!isKey && e.card[0]=='1' ? "not null" : ""), - ref: e2.name, //easier drawMld function (fewer regexps) + qualifiers: !isKey && e.card[0]=='1' ? "not null" : "", + ref: e2.name + "(" + attr.name + ")", }); } }); @@ -243,8 +249,8 @@ class ErDiags name: item.entity + "_" + f.name, isKey: true, type: f.type, - qualifiers: (f.qualifiers || "") + " foreign key references " + item.entity, - ref: item.entity, + qualifiers: f.qualifiers || "", + ref: item.entity + "(" + f.name + ")", }); }); }); @@ -299,7 +305,7 @@ class ErDiags { let element = document.getElementById(id); mcdStyle = mcdStyle || "compact"; - if (this.mcdGraph.length > 0) + if (!!this.mcdGraph) { element.innerHTML = this.mcdGraph; return; @@ -417,19 +423,23 @@ class ErDiags }); }); mcdDot += '}'; - //console.log(mcdDot); - ErDiags.AjaxGet(mcdDot, graphSvg => { - this.mcdGraph = graphSvg; - element.innerHTML = graphSvg; - }); + if (this.output == "graph") + { + // Draw graph in element + ErDiags.AjaxGet(mcdDot, graphSvg => { + this.mcdGraph = graphSvg; + element.innerHTML = graphSvg; + }); + } + else //just show dot input + element.innerHTML = mcdDot.replace(//g,">"); } // "Modèle logique des données", from MCD without anomalies - // TODO: this one should draw links from foreign keys to keys (port=... in ) drawMld(id) { let element = document.getElementById(id); - if (this.mldGraph.length > 0) + if (!!this.mldGraph) { element.innerHTML = this.mcdGraph; return; @@ -447,38 +457,33 @@ class ErDiags mldDot += '' + label + '\n'; if (!!f.ref) { - // Need to find a key attribute in reference entity (the first...) - let keyInRef = ""; - for (let field of this.tables[f.ref]) - { - if (field.isKey) - { - keyInRef = field.name; - break; - } - } + const refPort = f.ref.slice(0,-1).replace('(',':'); if (Math.random() < 0.5) - links += '"' + f.ref + '":"' + keyInRef + '" -- "' + name+'":"'+f.name + '" [dir="forward",arrowhead="dot"'; + links += refPort + ' -- "' + name+'":"'+f.name + '" [dir="forward",arrowhead="dot"'; else - links += '"'+name+'":"'+f.name+'" -- "' + f.ref + '":"' + keyInRef + '" [dir="back",arrowtail="dot"'; + links += '"'+name+'":"'+f.name+'" -- ' + refPort + ' [dir="back",arrowtail="dot"'; links += ']\n;'; } }); mldDot += '>];\n'; }); mldDot += links + '\n'; - mldDot += '}\n'; - //console.log(mldDot); - ErDiags.AjaxGet(mldDot, graphSvg => { - this.mldGraph = graphSvg; - element.innerHTML = graphSvg; - }); + mldDot += '}'; + if (this.output == "graph") + { + ErDiags.AjaxGet(mldDot, graphSvg => { + this.mldGraph = graphSvg; + element.innerHTML = graphSvg; + }); + } + else + element.innerHTML = mldDot.replace(//g,">"); } fillSql(id) { let element = document.getElementById(id); - if (this.sqlText.length > 0) + if (!!this.sqlText) { element.innerHTML = this.sqlText; return; @@ -487,16 +492,24 @@ class ErDiags Object.keys(this.tables).forEach( name => { sqlText += "CREATE TABLE " + name + " (\n"; let key = ""; + let foreignKey = [ ]; this.tables[name].forEach( f => { let type = f.type || (f.isKey ? "INTEGER" : "TEXT"); + if (!!f.ref) + foreignKey.push({name: f.name, ref: f.ref}); sqlText += "\t" + f.name + " " + type + " " + (f.qualifiers || "") + ",\n"; if (f.isKey) key += (key.length>0 ? "," : "") + f.name; }); - sqlText += "\tPRIMARY KEY (" + key + ")\n"; - sqlText += ");\n"; + sqlText += "\tPRIMARY KEY (" + key + ")"; + foreignKey.forEach( f => { + let refParts = f.ref.split("("); + const table = refParts[0]; + const field = refParts[1].slice(0,-1); //remove last parenthesis + sqlText += ",\n\tFOREIGN KEY (" + f.name + ") REFERENCES " + table + "(" + field + ")"; + }); + sqlText += "\n);\n"; }); - //console.log(sqlText); this.sqlText = sqlText; element.innerHTML = "
" + sqlText + "
"; }