X-Git-Url: https://git.auder.net/pieces/Cwda/c_white_knight.svg?a=blobdiff_plain;f=parser.js;h=218906c5cb32483f132ac37d9648cceba92c3f92;hb=04ccd4f6ea87ab6791efff26f2b61806777294ac;hp=6d43d4f68ee77be1a778fa0fed71fb4259a730f3;hpb=7a80e6db5860033f0a922d54cc0ba3e950f4f268;p=erdiag.git diff --git a/parser.js b/parser.js index 6d43d4f..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 = ""; } @@ -100,13 +104,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); } @@ -167,12 +170,17 @@ 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(/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; }); @@ -185,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 + ")", }); }); }); @@ -205,12 +213,14 @@ class ErDiags this.entities[e2.name].attributes.forEach( attr => { if (attr.isKey) { + // For "weak tables", foreign keys become part of the key + const isKey = e.card.length >= 2 && e.card[1] == 'R'; this.tables[e.name].push({ - isKey: e.card.length >= 2 && e.card[1] == 'R', //"weak tables" foreign keys become part of the key + isKey: isKey, 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) + qualifiers: !isKey && e.card[0]=='1' ? "not null" : "", + ref: e2.name + "(" + attr.name + ")", }); } }); @@ -239,8 +249,8 @@ class ErDiags name: item.entity + "_" + f.name, isKey: true, type: f.type, - qualifiers: (f.qualifiers || "") + " foreign key references " + item.entity + " not null", - ref: item.entity, + qualifiers: f.qualifiers || "", + ref: item.entity + "(" + f.name + ")", }); }); }); @@ -295,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; @@ -413,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
' + name + ' |
' + label + ' |
" + sqlText + "
";
}