+ //////////////////////////////
+ // 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;
+ });
+ // 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 = e.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
+ /////////////////////////////////