Improve cardinality, detect duplicated fields after creating tables
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 2 Feb 2018 22:54:36 +0000 (23:54 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 2 Feb 2018 22:54:36 +0000 (23:54 +0100)
README.md
parser.js

index f4e75d1..ef279e6 100644 (file)
--- a/README.md
+++ b/README.md
@@ -34,6 +34,12 @@ Cardinality dictionary:
 
 Special cardinalities are also available to indicate relative identification: `?R` and `1R`.
 
+And, in case of a self-relationship, symbols '>' and '<' can indicate the sense, as in
+
+       {manage}
+       Users *>
+       Users 1<
+
 To mark a weak entity, just surround its name by extra-brackets
 
        [[WeakEntity]]
index 6d028fd..6d43d4f 100644 (file)
--- a/parser.js
+++ b/parser.js
@@ -15,16 +15,17 @@ class ErDiags
                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;
        }
 
        ///////////////////////////////
@@ -243,6 +244,22 @@ class ErDiags
                                                });
                                        });
                                });
+                               // 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({
@@ -392,7 +409,7 @@ class ErDiags
                                        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 += '}';