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
+ */
3
11
const objSchema = ( testdata ) => {
4
12
const results = { } ;
5
13
// Iterate through the testdata Array
@@ -46,6 +54,9 @@ const objSchema = (testdata) => {
46
54
47
55
return results ;
48
56
} ;
57
+ ////////////////////
58
+ //// SQL PARSER ////
59
+ ////////////////////
49
60
50
61
function TableModel ( ) {
51
62
this . Name = null ;
@@ -76,13 +87,11 @@ function PrimaryKeyModel() {
76
87
}
77
88
78
89
// 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 = [ ] ;
83
93
84
- /* Function
85
- Section */
94
+ /* Function Section */
86
95
87
96
// Creates propertyModel and assigns properties to arguments passed in
88
97
function createProperty ( name , tableName , foreignKey , isPrimaryKey ) {
@@ -185,7 +194,7 @@ function parseSQLServerForeignKey(name, currentTableModel, propertyType) {
185
194
currentTableModel . Properties . push ( propertyModel ) ;
186
195
}
187
196
188
- function parseMySQLForeignKey ( name , currentTableModel , constraintName = null ) {
197
+ function parseMySQLForeignKey ( name , currentTableModel , constrainName = null ) {
189
198
name = name . replace ( / \" / g, '' ) ;
190
199
191
200
let foreignKeyName = name
@@ -194,6 +203,7 @@ function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
194
203
const referencedTableName = name . match (
195
204
/ (?< = R E F E R E N C E S \s ) ( [ A - Z a - z 0 - 9 _ ] + \. [ A - Z a - z 0 - 9 _ ] + ) + /
196
205
) [ 0 ] ;
206
+ // let constraintname = name.match(/(?<=CONSTRAINT\s)([A-Za-z0-9_]+)/)[0];
197
207
let referencedPropertyName = name
198
208
. match ( / (?< = R E F E R E N C E S \s ) ( [ A - Z a - z 0 - 9 _ ] + \. [ A - Z a - z 0 - 9 _ ( ) ] + ) + / ) [ 0 ]
199
209
. match ( / \( ( [ ^ ( ) ] + ) \) / g) [ 0 ]
@@ -209,39 +219,32 @@ function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
209
219
210
220
let primaryTableModel = null ;
211
221
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 ] ] ;
215
226
break ;
216
227
}
217
228
}
218
229
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 ;
226
242
}
243
+ }
227
244
}
228
245
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);
245
248
246
249
// Create ForeignKey
247
250
let foreignKeyDestinationModel = createForeignKey (
@@ -252,7 +255,7 @@ function parseMySQLForeignKey(name, currentTableModel, constraintName = null) {
252
255
false
253
256
) ;
254
257
255
- foreignKeyDestinationModel . constraintName = constraintName ;
258
+ foreignKeyDestinationModel . constrainName = constrainName ;
256
259
// Add ForeignKey Destination
257
260
foreignKeyList . push ( foreignKeyDestinationModel ) ;
258
261
}
@@ -286,6 +289,20 @@ function processForeignKey() {
286
289
}
287
290
} ) ;
288
291
}
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
+ }
289
306
} ) ;
290
307
} ) ;
291
308
}
@@ -326,11 +343,8 @@ function parseTableName(name) {
326
343
}
327
344
328
345
function parseAlterTable ( tableName , constraint ) {
329
- // const tableName = tmp.match(/(?<=ALTER\sTABLE\s)([a-zA-Z_]+)(?=\sADD\sCONSTRAINT)/)[0];
330
346
const regexConstraint = / (?< = C O N S T R A I N T \s ) ( [ a - z A - Z _ ] + ) / ;
331
- const constraintName = constraint . match ( regexConstraint ) ;
332
-
333
- // if (constraintName !== null) console.log('constraintName', constraintName[0]);
347
+ const constrainName = constraint . match ( regexConstraint ) ;
334
348
335
349
tableName = tableName . trim ( ) ;
336
350
let currentTableModel ;
@@ -341,24 +355,20 @@ function parseAlterTable(tableName, constraint) {
341
355
} ) ;
342
356
343
357
if ( constraint . indexOf ( 'FOREIGN KEY' ) !== - 1 ) {
344
- // console.log('Alter foreign:', constraint)
345
358
const name = constraint . substring (
346
359
constraint . indexOf ( 'FOREIGN KEY' ) ,
347
360
constraint . length - 1
348
361
) ;
349
-
350
362
parseMySQLForeignKey (
351
363
name ,
352
364
currentTableModel ,
353
- constraintName !== null ? constraintName [ 0 ] : null
365
+ constrainName !== null ? constrainName [ 0 ] : null
354
366
) ;
355
367
} else if ( constraint . indexOf ( 'PRIMARY KEY' ) !== - 1 ) {
356
- // console.log('Alter constraint:', constraint)
357
368
const name = constraint . substring (
358
369
constraint . indexOf ( 'PRIMARY KEY' ) ,
359
370
constraint . length - 1
360
371
) ;
361
- // console.log(currentTableModel, "\n", name)
362
372
parseMYSQLPrimaryKey ( name , currentTableModel ) ;
363
373
}
364
374
}
@@ -389,6 +399,7 @@ function parseSQLServerPrimaryKey(name, currentTableModel, propertyType) {
389
399
390
400
function parseMYSQLPrimaryKey ( name , currentTableModel ) {
391
401
const primaryKeyName = name . slice ( 13 ) . replace ( ')' , '' ) . replace ( / \" / g, '' ) ;
402
+
392
403
currentTableModel . Properties . forEach ( ( property ) => {
393
404
if ( property . Name . split ( ' ' ) [ 0 ] === primaryKeyName ) {
394
405
property . IsPrimaryKey = true ;
@@ -401,46 +412,50 @@ function parseMYSQLPrimaryKey(name, currentTableModel) {
401
412
export default function parseSql ( text ) {
402
413
const lines = text . split ( '\n' ) ;
403
414
let tableCell = null ;
404
- const cells = [ ] ;
415
+ let cells = [ ] ;
405
416
exportedTables = 0 ;
406
417
tableList = [ ] ;
407
418
foreignKeyList = [ ] ;
408
419
primaryKeyList = [ ] ;
409
420
410
421
let currentTableModel = null ;
411
422
412
- //Parse SQL to objects
423
+ // Parse SQL to objects
413
424
for ( let i = 0 ; i < lines . length ; i ++ ) {
414
425
let rowCell = null ;
415
426
416
427
const tmp = lines [ i ] . trim ( ) ;
417
428
418
429
const propertyRow = tmp . substring ( 0 , 12 ) . toLowerCase ( ) . trim ( ) ;
430
+
419
431
if ( currentTableModel !== null && tmp . includes ( ');' ) ) {
420
432
tableList . push ( currentTableModel ) ;
421
433
currentTableModel = null ;
422
434
}
423
435
424
- //Parse Table
436
+ // Parse Table
425
437
if ( propertyRow === 'create table' ) {
426
- //Parse row
438
+ // Parse row
427
439
let name = tmp . substring ( 12 ) . trim ( ) ;
428
- //Parse Table Name
440
+
441
+ // Parse Table Name
429
442
name = parseTableName ( name ) ;
430
- //Create Table
443
+
444
+ // Create Table
431
445
currentTableModel = createTable ( name ) ;
432
446
}
433
447
// tmp === 'ALTER TABLE'
434
448
else if ( propertyRow == 'alter table' ) {
435
449
let alterQuerySplit = tmp . toLowerCase ( ) . trim ( ) ;
436
450
let tname = null ;
451
+
437
452
for ( let i = 0 ; i < tableList . length ; i ++ ) {
438
453
if ( alterQuerySplit . indexOf ( tableList [ i ] . Name ) !== - 1 ) {
439
454
tname = tableList [ i ] . Name ;
440
455
}
441
456
}
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 ] ) ;
444
459
i += 3 ;
445
460
}
446
461
@@ -450,15 +465,15 @@ export default function parseSql(text) {
450
465
currentTableModel !== null &&
451
466
propertyRow !== 'alter table '
452
467
) {
453
- //Parse the row
468
+ // Parse the row
454
469
let name = tmp . substring (
455
470
0 ,
456
471
tmp . charAt ( tmp . length - 1 ) === ',' ? tmp . length - 1 : tmp . length
457
472
) ;
458
473
// Check if first 10 characters are 'constraint'
459
474
const constraint = name . substring ( 0 , 10 ) . toLowerCase ( ) ;
460
475
if ( constraint === 'constraint' ) {
461
- //double checking for constraints here
476
+ // double checking for constraints here
462
477
if ( name . indexOf ( 'PRIMARY KEY' ) !== - 1 ) {
463
478
name = name
464
479
. substring ( name . indexOf ( 'PRIMARY KEY' ) , name . length )
@@ -470,9 +485,9 @@ export default function parseSql(text) {
470
485
}
471
486
}
472
487
473
- //Attempt to get the Key Type
488
+ // Attempt to get the Key Type
474
489
let propertyType = name . substring ( 0 , 11 ) . toLowerCase ( ) ;
475
- //Add special constraints
490
+ // Add special constraints
476
491
if ( propertyType !== 'primary key' && propertyType !== 'foreign key' ) {
477
492
if ( tmp . indexOf ( 'PRIMARY KEY' ) !== - 1 && tmp . indexOf ( 'FOREIGN KEY' ) !== - 1 ) {
478
493
propertyType = 'SQLServer both' ;
@@ -489,7 +504,6 @@ export default function parseSql(text) {
489
504
propertyType !== 'SQLServer primary key' &&
490
505
propertyType !== 'SQLServer foreign key' &&
491
506
propertyType !== 'SQLServer both' ;
492
-
493
507
// Parse properties that don't have relationships
494
508
if ( normalProperty ) {
495
509
// For now, skip lines with these commands
@@ -585,8 +599,8 @@ export default function parseSql(text) {
585
599
// Process Foreign Keys
586
600
processForeignKey ( ) ;
587
601
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 ) {
590
604
if ( tableList [ i ] . Properties [ k ] !== undefined ) {
591
605
let composite = tableList [ i ] . Properties [ k ] . Name . match ( / ^ ( \S + ) \s ( .* ) / ) . slice ( 1 ) ;
592
606
@@ -613,12 +627,12 @@ export default function parseSql(text) {
613
627
}
614
628
615
629
/* Function
616
- Section */
630
+ Section */
617
631
618
632
function createTableUI ( ) {
619
633
tableList . forEach ( function ( tableModel ) {
620
634
// 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 ) ;
622
636
} ) ;
623
637
624
638
return tableList ;
0 commit comments