this.sqlText = "";
}
- static get CARDINAL()
+ static CARDINAL(symbol)
{
- return {
- "*": "0,n",
- "+": "1,n",
- "?": "0,1",
- "1": "1,1",
- "?R": "(0,1)",
- "1R": "(1,1)",
- };
+ let res = { "*": "0,n", "+": "1,n", "?": "0,1", "1": "1,1" } [ symbol[0] ];
+ if (symbol.length >= 2)
+ {
+ if (symbol[1] == 'R')
+ res = '(' + res + ')';
+ else if (['>','<'].includes(symbol[1]))
+ res += symbol[1];
+ }
+ return res;
}
///////////////////////////////
{
let field = { };
let line = lines[i];
- if (line.charAt(0) == '#')
+ if (line.charAt(0) == '+')
{
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);
}
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(/foreign/i))
+ Object.assign(newField, {ref: attr.qualifiers.match(/references ([^\s]+)/i)[1]});
+ newTable.push(newField);
});
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 = [ ];
+ let hasZeroOne = false;
a.entities.forEach( e => {
if (['?','1'].includes(e.card[0]))
{
+ hasZeroOne = true;
// Foreign key apparition (for each entity in association minus current one, for each identifying attribute)
a.entities.forEach( e2 => {
if (e2.name == e.name)
});
}
});
- if (newTableAttrs.length > 1)
+ if (!hasZeroOne && newTableAttrs.length > 1)
{
// Ok, really create a new table
let newTable = {
});
});
});
+ // Check for duplicates (in case of self-relationship), rename if needed
+ newTable.fields.forEach( (f,i) => {
+ const idx = newTable.fields.findIndex( item => { return item.name == f.name; });
+ if (idx < i)
+ {
+ // Current field is a duplicate
+ let suffix = 2;
+ let newName = f.name + suffix;
+ while (newTable.fields.findIndex( item => { return item.name == newName; }) >= 0)
+ {
+ suffix++;
+ newName = f.name + suffix;
+ }
+ f.name = newName;
+ }
+ });
// Add relationship potential own attributes
(a.attributes || [ ]).forEach( attr => {
newTable.fields.push({
mcdDot += '"' + e.name + '":name -- "' + name + '"';
else
mcdDot += '"' + name + '" -- "' + e.name + '":name';
- mcdDot += '[label="' + ErDiags.CARDINAL[e.card] + '"];\n';
+ mcdDot += '[label="' + ErDiags.CARDINAL(e.card) + '"];\n';
});
});
mcdDot += '}';
mldDot += '"' + name + '" [label=<<table BORDER="1" ALIGN="LEFT" CELLPADDING="5" CELLSPACING="0">\n';
mldDot += '<tr><td BGCOLOR="#ae7d4e" BORDER="0"><font COLOR="#FFFFFF">' + name + '</font></td></tr>\n';
this.tables[name].forEach( f => {
- let label = (f.isKey ? '<u>' : '') + (!!f.qualifiers && f.qualifiers.indexOf("foreign")>=0 ? '#' : '') + f.name + (f.isKey ? '</u>' : '');
+ let label = (f.isKey ? '<u>' : '') + (!!f.ref ? '#' : '') + f.name + (f.isKey ? '</u>' : '');
mldDot += '<tr><td port="' + f.name + '"' + ' BGCOLOR="#FFFFFF" BORDER="0" ALIGN="LEFT"><font COLOR="#000000" >' + label + '</font></td></tr>\n';
if (!!f.ref)
{
sqlText += "CREATE TABLE " + name + " (\n";
let key = "";
this.tables[name].forEach( f => {
- sqlText += "\t" + f.name + " " + (f.type || "TEXT") + " " + (f.qualifiers || "") + ",\n";
+ let type = f.type || (f.isKey ? "INTEGER" : "TEXT");
+ sqlText += "\t" + f.name + " " + type + " " + (f.qualifiers || "") + ",\n";
if (f.isKey)
key += (key.length>0 ? "," : "") + f.name;
});