From 8edb29ffa12fda61a939295ce00a6d22133131d1 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Sat, 3 Feb 2018 01:00:51 +0100
Subject: [PATCH] Better foreign keys handling, slightly change SQL clues
 syntax

---
 example.html |  6 +++---
 parser.js    | 21 ++++++++++++---------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/example.html b/example.html
index 4094835..3ab2458 100644
--- a/example.html
+++ b/example.html
@@ -13,14 +13,14 @@
 	let er =
 		new ErDiags(`
 			[Musician]
-			+id (integer)
+			+id integer
 			name
 			band
-			role (varchar not null)
+			role varchar not null
 
 			[Instrument]
 			+name
-			family (varchar not null default "Brass")
+			family varchar not null default "Brass"
 
 			[Piano]
 			type
diff --git a/parser.js b/parser.js
index 6d43d4f..2b75709 100644
--- a/parser.js
+++ b/parser.js
@@ -100,13 +100,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 +166,15 @@ 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(/foreign/i))
+					Object.assign(newField, {ref: attr.qualifiers.match(/references ([^\s]+)/i)[1]});
+				newTable.push(newField);
 			});
 			this.tables[name] = newTable;
 		});
@@ -439,7 +441,7 @@ class ErDiags
 			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)
 				{
@@ -484,7 +486,8 @@ class ErDiags
 			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;
 			});
-- 
2.44.0