X-Git-Url: https://git.auder.net/?p=erdiag.git;a=blobdiff_plain;f=parser.js;h=0ab6a4321a15f59a62718d39cbfc699d17563e39;hp=5ef2b3f25ba7ebb72fe58a6e0c93e3b30fdcc8e9;hb=HEAD;hpb=3789126f0dc7d0b751d99ded86a97c2b258afe80 diff --git a/parser.js b/parser.js index 5ef2b3f..0ab6a43 100644 --- a/parser.js +++ b/parser.js @@ -1,7 +1,7 @@ // ER diagram description parser class ErDiags { - constructor(description) + constructor(description, output, image) { this.entities = { }; this.inheritances = [ ]; @@ -9,10 +9,8 @@ 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.sqlText = ""; + this.output = output || "graph"; + this.image = image || "svg"; } static CARDINAL(symbol) @@ -170,12 +168,11 @@ class ErDiags name: attr.name, type: attr.type, isKey: attr.isKey, - qualifiers: attr.qualifiers, }; if (!!attr.qualifiers && !!attr.qualifiers.match(/references/i)) { Object.assign(newField, {ref: attr.qualifiers.match(/references ([^\s]+)/i)[1]}); - attr.qualifiers = attr.qualifiers.replace(/references [^\s]+/, ""); + newField.qualifiers = attr.qualifiers.replace(/references [^\s]+/i, ""); } newTable.push(newField); }); @@ -285,28 +282,12 @@ class ErDiags // DRAWING + GET SQL FROM PARSING ///////////////////////////////// - static AjaxGet(dotInput, callback) - { - let xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) - callback(this.responseText); - }; - xhr.open("GET", "scripts/getGraphSvg.php?dot=" + encodeURIComponent(dotInput), true); - xhr.send(); - } - // "Modèle conceptuel des données". TODO: option for graph size // NOTE: randomizing helps to obtain better graphs (sometimes) drawMcd(id, mcdStyle) //mcdStyle: bubble, or compact { let element = document.getElementById(id); mcdStyle = mcdStyle || "compact"; - if (this.mcdGraph.length > 0) - { - element.innerHTML = this.mcdGraph; - return; - } // Build dot graph input let mcdDot = 'graph {\n'; mcdDot += 'rankdir="LR";\n'; @@ -420,23 +401,16 @@ class ErDiags }); }); mcdDot += '}'; - //console.log(mcdDot); - ErDiags.AjaxGet(mcdDot, graphSvg => { - this.mcdGraph = graphSvg; - element.innerHTML = graphSvg; - }); + if (this.output == "graph") //draw graph in element + element.innerHTML = ""; + else //output = "text": 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) - { - element.innerHTML = this.mcdGraph; - return; - } // Build dot graph input (assuming foreign keys not already present...) let mldDot = 'graph {\n'; mldDot += 'rankdir="LR";\n'; @@ -461,18 +435,17 @@ class ErDiags 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") + element.innerHTML = ""; + 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; @@ -499,7 +472,6 @@ class ErDiags }); sqlText += "\n);\n"; }); - //console.log(sqlText); this.sqlText = sqlText; element.innerHTML = "
" + sqlText + "
"; }