Skip to content

Commit 19b8914

Browse files
Yufaneighbor-peacegeistnine01001101CKatu816
authored andcommitted
Fix parser (front end and back end)
Co-authored-by: Michael Costello <[email protected]> Co-authored-by: Steven Geiger <[email protected]> Co-authored-by: Yufa Li <[email protected]> Co-authored-by: Alexander Tu <[email protected]>
1 parent dddb4d1 commit 19b8914

File tree

4 files changed

+91
-71
lines changed

4 files changed

+91
-71
lines changed

server/controllers/postgresData.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const util = require('util');
55
const exec = util.promisify(require('child_process').exec);
66
const { Pool } = require('pg');
77

8-
const parseSql = require('../../src/parse.js');
8+
const parseSql = require('../../src/parse');
99

1010
/**
1111
* Postgres Dump Query

src/components/ReactFlow/TableNode.jsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import useSettingsStore from '../../store/settingsStore';
77

88
export default function TableNode({ data }) {
99
const tableName = data.table[0];
10+
console.log('tableName', tableName)
1011

1112
// columnData is an array of objects with each column in the table as an element
1213
const columnData = Object.values(data.table[1]);
@@ -18,12 +19,13 @@ export default function TableNode({ data }) {
1819
const tableHandles = [];
1920
for (let i = 0; i < data.edges.length; i++) {
2021
if (data.edges[i].source === tableName) {
22+
console.log('Source.tableName', data.edges[i].source)
2123
//make handle placement dynamic, we need to know the column of our source
2224
let columnNumberSource =
2325
columnData.findIndex((obj) => obj.Name === data.edges[i].sourceHandle) + 1;
2426
if (columnNumberSource === 0) columnNumberSource = 1;
25-
console.log('columnNumberSource', columnNumberSource);
26-
console.log('data.edges[i].sourceHandle', data.edges[i].sourceHandle);
27+
// console.log('columnNumberSource', columnNumberSource);
28+
// console.log('data.edges[i].sourceHandle', data.edges[i].sourceHandle);
2729
tableHandles.push(
2830
<Handle
2931
key={`${data.edges[i]}-source-${[i]}`}
@@ -32,19 +34,20 @@ export default function TableNode({ data }) {
3234
id={data.edges[i].sourceHandle}
3335
style={{
3436
background: 'transparent',
35-
top: 96 + columnNumberSource * 21,
36-
bottom: 'auto',
37+
top: 77 + columnNumberSource * 21,
38+
// bottom: 'auto',
3739
}}
3840
/>
3941
);
4042
}
4143
if (data.edges[i].target === tableName) {
44+
console.log('target.tableName', data.edges[i].target)
4245
//make handle placement dynamic, we need to know the column of our target
4346
let columnNumberTarget =
4447
columnData.findIndex((obj) => obj.Name === data.edges[i].targetHandle) + 1;
4548
if (columnNumberTarget === 0) columnNumberTarget = 1;
46-
console.log('columnNumberTarget', columnNumberTarget);
47-
console.log('data.edges[i].targetHandle', data.edges[i].targetHandle);
49+
// console.log('columnNumberTarget', columnNumberTarget);
50+
// console.log('data.edges[i].targetHandle', data.edges[i].targetHandle);
4851
tableHandles.push(
4952
<Handle
5053
key={`${data.edges[i]}-target-${[i]}`}
@@ -53,8 +56,8 @@ export default function TableNode({ data }) {
5356
id={data.edges[i].targetHandle}
5457
style={{
5558
background: 'transparent',
56-
top: 96 + columnNumberTarget * 21,
57-
bottom: 'auto',
59+
top: 77 + columnNumberTarget * 21,
60+
// bottom: 'auto',
5861
}}
5962
/>
6063
);

src/components/ReactFlow/createEdges.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export default function createEdges(schemaObject: SchemaStore) {
2525
const table = schemaObject[tableKey];
2626
for (const rowKey in table) {
2727
const row = table[rowKey];
28+
if (row.IsForeignKey) {
29+
console.log('row', row)
30+
};
2831
if (row.IsForeignKey) {
2932
edges.push({
3033
id: `${row.References[0].ReferencesTableName}-to-${row.References[0].PrimaryKeyTableName}`,

src/parse.js

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
//objSchema controller allows the user to obj data to more
2-
//usable format for front-end
1+
// Creating global empty arrays to hold foreign keys, primary keys, and tableList
2+
let foreignKeyList = [];
3+
let primaryKeyList = [];
4+
let primaryKeyListArray = [];
5+
let tableList = [];
6+
let exportedTables = 0;
7+
/**
8+
* objSchema
9+
* Iterates through testdata array of tables and * Iterates through properties array and assigns field name as key for properties.
10+
*/
311
const objSchema = (testdata) => {
412
const results = {};
513
// Iterate through the testdata Array
@@ -46,6 +54,9 @@ const objSchema = (testdata) => {
4654

4755
return results;
4856
};
57+
////////////////////
58+
//// SQL PARSER ////
59+
////////////////////
4960

5061
function TableModel() {
5162
this.Name = null;
@@ -76,13 +87,11 @@ function PrimaryKeyModel() {
7687
}
7788

7889
// Creating global empty arrays to hold foreign keys, primary keys, and tableList
79-
let foreignKeyList = [];
80-
let primaryKeyList = [];
81-
let tableList = [];
82-
let exportedTables = 0;
90+
foreignKeyList = [];
91+
primaryKeyList = [];
92+
tableList = [];
8393

84-
/* Function
85-
Section */
94+
/* Function Section */
8695

8796
// Creates propertyModel and assigns properties to arguments passed in
8897
function createProperty(name, tableName, foreignKey, isPrimaryKey) {
@@ -185,7 +194,7 @@ function parseSQLServerForeignKey(name, currentTableModel, propertyType) {
185194
currentTableModel.Properties.push(propertyModel);
186195
}
187196

188-
function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
197+
function parseMySQLForeignKey(name, currentTableModel, constrainName = null) {
189198
name = name.replace(/\"/g, '');
190199

191200
let foreignKeyName = name
@@ -194,6 +203,7 @@ function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
194203
const referencedTableName = name.match(
195204
/(?<=REFERENCES\s)([A-Za-z0-9_]+\.[A-Za-z0-9_]+)+/
196205
)[0];
206+
// let constraintname = name.match(/(?<=CONSTRAINT\s)([A-Za-z0-9_]+)/)[0];
197207
let referencedPropertyName = name
198208
.match(/(?<=REFERENCES\s)([A-Za-z0-9_]+\.[A-Za-z0-9_()]+)+/)[0]
199209
.match(/\(([^()]+)\)/g)[0]
@@ -209,39 +219,32 @@ function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
209219

210220
let primaryTableModel = null;
211221

212-
for (const i in tableList) {
213-
if (tableList[i].Name == referencedTableName) {
214-
primaryTableModel = tableList[i];
222+
const tlKeys = Object.keys(tableList);
223+
for (let i = 0; i < tlKeys.length; i++) {
224+
if (tableList[tlKeys[i]].Name === referencedTableName) {
225+
primaryTableModel = tableList[tlKeys[i]];
215226
break;
216227
}
217228
}
218229

219-
for (const k in primaryTableModel) {
220-
for (const l in primaryTableModel[k])
221-
if (primaryTableModel[k][l].Name !== undefined) {
222-
if (primaryTableModel[k][l].Name.indexOf(referencedPropertyName) !== -1) {
223-
referencedPropertyName = primaryTableModel[k][l].Name;
224-
break;
225-
}
230+
const ptmKeys = Object.keys(primaryTableModel);
231+
for (let i = 0; i < ptmKeys.length; i++) {
232+
const ptmSubKeys = Object.keys(primaryTableModel[ptmKeys[i]]);
233+
for (let j = 0; j < ptmSubKeys.length; j++) {
234+
if (
235+
primaryTableModel[ptmKeys[i]][ptmSubKeys[j]].Name !== undefined &&
236+
primaryTableModel[ptmKeys[i]][ptmSubKeys[j]].Name.indexOf(
237+
referencedPropertyName
238+
) !== -1
239+
) {
240+
referencedPropertyName = primaryTableModel[ptmKeys[i]][ptmSubKeys[j]].Name;
241+
break;
226242
}
243+
}
227244
}
228245

229-
// Create ForeignKey
230-
let foreignKeyOriginModel = createForeignKey(
231-
foreignKeyName,
232-
currentTableModel.Name,
233-
referencedPropertyName,
234-
referencedTableName,
235-
true
236-
);
237-
238-
foreignKeyOriginModel.constraintName = constraintName;
239-
240-
// Add ForeignKey Origin
241-
foreignKeyList.push(foreignKeyOriginModel);
242-
243-
//Add PrimaryKey Origin
244-
//foreignKeyList.push(primaryKeyOriginModel);
246+
// Add PrimaryKey Origin
247+
// foreignKeyList.push(primaryKeyOriginModel);
245248

246249
// Create ForeignKey
247250
let foreignKeyDestinationModel = createForeignKey(
@@ -252,7 +255,7 @@ function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
252255
false
253256
);
254257

255-
foreignKeyDestinationModel.constraintName = constraintName;
258+
foreignKeyDestinationModel.constrainName = constrainName;
256259
// Add ForeignKey Destination
257260
foreignKeyList.push(foreignKeyDestinationModel);
258261
}
@@ -286,6 +289,20 @@ function processForeignKey() {
286289
}
287290
});
288291
}
292+
if (tableModel.Name == foreignKeyModel.PrimaryKeyTableName) {
293+
tableModel.Properties.forEach(function (propertyModel) {
294+
if (propertyModel.Name === foreignKeyModel.PrimaryKeyName) {
295+
propertyModel.References.push({
296+
PrimaryKeyName: foreignKeyModel.PrimaryKeyName,
297+
ReferencesPropertyName: foreignKeyModel.ReferencesPropertyName,
298+
PrimaryKeyTableName: foreignKeyModel.PrimaryKeyTableName,
299+
ReferencesTableName: foreignKeyModel.ReferencesTableName,
300+
IsDestination: true,
301+
constrainName: foreignKeyModel.constrainName,
302+
});
303+
}
304+
});
305+
}
289306
});
290307
});
291308
}
@@ -326,11 +343,8 @@ function parseTableName(name) {
326343
}
327344

328345
function parseAlterTable(tableName, constraint) {
329-
// const tableName = tmp.match(/(?<=ALTER\sTABLE\s)([a-zA-Z_]+)(?=\sADD\sCONSTRAINT)/)[0];
330346
const regexConstraint = /(?<=CONSTRAINT\s)([a-zA-Z_]+)/;
331-
const constraintName = constraint.match(regexConstraint);
332-
333-
// if (constraintName !== null) console.log('constraintName', constraintName[0]);
347+
const constrainName = constraint.match(regexConstraint);
334348

335349
tableName = tableName.trim();
336350
let currentTableModel;
@@ -341,24 +355,20 @@ function parseAlterTable(tableName, constraint) {
341355
});
342356

343357
if (constraint.indexOf('FOREIGN KEY') !== -1) {
344-
// console.log('Alter foreign:', constraint)
345358
const name = constraint.substring(
346359
constraint.indexOf('FOREIGN KEY'),
347360
constraint.length - 1
348361
);
349-
350362
parseMySQLForeignKey(
351363
name,
352364
currentTableModel,
353-
constraintName !== null ? constraintName[0] : null
365+
constrainName !== null ? constrainName[0] : null
354366
);
355367
} else if (constraint.indexOf('PRIMARY KEY') !== -1) {
356-
// console.log('Alter constraint:', constraint)
357368
const name = constraint.substring(
358369
constraint.indexOf('PRIMARY KEY'),
359370
constraint.length - 1
360371
);
361-
// console.log(currentTableModel, "\n", name)
362372
parseMYSQLPrimaryKey(name, currentTableModel);
363373
}
364374
}
@@ -389,6 +399,7 @@ function parseSQLServerPrimaryKey(name, currentTableModel, propertyType) {
389399

390400
function parseMYSQLPrimaryKey(name, currentTableModel) {
391401
const primaryKeyName = name.slice(13).replace(')', '').replace(/\"/g, '');
402+
392403
currentTableModel.Properties.forEach((property) => {
393404
if (property.Name.split(' ')[0] === primaryKeyName) {
394405
property.IsPrimaryKey = true;
@@ -401,46 +412,50 @@ function parseMYSQLPrimaryKey(name, currentTableModel) {
401412
export default function parseSql(text) {
402413
const lines = text.split('\n');
403414
let tableCell = null;
404-
const cells = [];
415+
let cells = [];
405416
exportedTables = 0;
406417
tableList = [];
407418
foreignKeyList = [];
408419
primaryKeyList = [];
409420

410421
let currentTableModel = null;
411422

412-
//Parse SQL to objects
423+
// Parse SQL to objects
413424
for (let i = 0; i < lines.length; i++) {
414425
let rowCell = null;
415426

416427
const tmp = lines[i].trim();
417428

418429
const propertyRow = tmp.substring(0, 12).toLowerCase().trim();
430+
419431
if (currentTableModel !== null && tmp.includes(');')) {
420432
tableList.push(currentTableModel);
421433
currentTableModel = null;
422434
}
423435

424-
//Parse Table
436+
// Parse Table
425437
if (propertyRow === 'create table') {
426-
//Parse row
438+
// Parse row
427439
let name = tmp.substring(12).trim();
428-
//Parse Table Name
440+
441+
// Parse Table Name
429442
name = parseTableName(name);
430-
//Create Table
443+
444+
// Create Table
431445
currentTableModel = createTable(name);
432446
}
433447
// tmp === 'ALTER TABLE'
434448
else if (propertyRow == 'alter table') {
435449
let alterQuerySplit = tmp.toLowerCase().trim();
436450
let tname = null;
451+
437452
for (let i = 0; i < tableList.length; i++) {
438453
if (alterQuerySplit.indexOf(tableList[i].Name) !== -1) {
439454
tname = tableList[i].Name;
440455
}
441456
}
442-
443-
parseAlterTable(tname, lines[i + 1]);
457+
//check for TableName and following line with constraint bound on database
458+
if (tname !== null && lines[i + 1] !== null) parseAlterTable(tname, lines[i + 1]);
444459
i += 3;
445460
}
446461

@@ -450,15 +465,15 @@ export default function parseSql(text) {
450465
currentTableModel !== null &&
451466
propertyRow !== 'alter table '
452467
) {
453-
//Parse the row
468+
// Parse the row
454469
let name = tmp.substring(
455470
0,
456471
tmp.charAt(tmp.length - 1) === ',' ? tmp.length - 1 : tmp.length
457472
);
458473
// Check if first 10 characters are 'constraint'
459474
const constraint = name.substring(0, 10).toLowerCase();
460475
if (constraint === 'constraint') {
461-
//double checking for constraints here
476+
// double checking for constraints here
462477
if (name.indexOf('PRIMARY KEY') !== -1) {
463478
name = name
464479
.substring(name.indexOf('PRIMARY KEY'), name.length)
@@ -470,9 +485,9 @@ export default function parseSql(text) {
470485
}
471486
}
472487

473-
//Attempt to get the Key Type
488+
// Attempt to get the Key Type
474489
let propertyType = name.substring(0, 11).toLowerCase();
475-
//Add special constraints
490+
// Add special constraints
476491
if (propertyType !== 'primary key' && propertyType !== 'foreign key') {
477492
if (tmp.indexOf('PRIMARY KEY') !== -1 && tmp.indexOf('FOREIGN KEY') !== -1) {
478493
propertyType = 'SQLServer both';
@@ -489,7 +504,6 @@ export default function parseSql(text) {
489504
propertyType !== 'SQLServer primary key' &&
490505
propertyType !== 'SQLServer foreign key' &&
491506
propertyType !== 'SQLServer both';
492-
493507
// Parse properties that don't have relationships
494508
if (normalProperty) {
495509
// For now, skip lines with these commands
@@ -585,8 +599,8 @@ export default function parseSql(text) {
585599
// Process Foreign Keys
586600
processForeignKey();
587601

588-
for (const i in tableList) {
589-
for (const k in tableList[i].Properties) {
602+
for (let i in tableList) {
603+
for (let k in tableList[i].Properties) {
590604
if (tableList[i].Properties[k] !== undefined) {
591605
let composite = tableList[i].Properties[k].Name.match(/^(\S+)\s(.*)/).slice(1);
592606

@@ -613,12 +627,12 @@ export default function parseSql(text) {
613627
}
614628

615629
/* Function
616-
Section */
630+
Section */
617631

618632
function createTableUI() {
619633
tableList.forEach(function (tableModel) {
620634
// Push in string code to d3tables array to render table name as a row
621-
for (const ref in tableModel.Properties);
635+
for (let ref in tableModel.Properties);
622636
});
623637

624638
return tableList;

0 commit comments

Comments
 (0)