Animal Cat Fish
Planet Mars Venus
-Finally, blocks must be separated by new lines. For a usage example, see example.html (it should render as seen in example\_\*.svg)
+Finally, blocks must be separated by new lines. For a usage example, see example.html (it should render as seen in example\_\*.svg);
+or example2.html for a bigger, more realistic illustration (small social network).
Note that the "drawMcd" method can take a second argument, which indicates the type of graph.
- * "bubble" draws the standard graph, as seen [here](https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#/media/File:ER_Diagram_MMORPG.png) for example
+ * "bubble" draws the standard graph, as seen [here](https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model#/media/File:ER_Diagram_MMORPG.png)
* "compact" (default) use the same box for an entity and its attributes
-----
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]});
+ attr.qualifiers = attr.qualifiers.replace(/references [^\s]+/, "");
+ }
newTable.push(newField);
});
this.tables[name] = newTable;
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 + ")",
});
});
});
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 + ")",
});
}
});
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 + ")",
});
});
});
mldDot += '<tr><td port="' + f.name + '"' + ' BGCOLOR="#FFFFFF" BORDER="0" ALIGN="LEFT"><font COLOR="#000000" >' + label + '</font></td></tr>\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;';
}
});
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;