Skip to content

Commit 3d28b9c

Browse files
committed
improvements on parser
1 parent 6a035bf commit 3d28b9c

2 files changed

Lines changed: 68 additions & 7 deletions

File tree

src/index.ts

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,65 @@ export class SqlSimpleParser {
325325
}
326326
}
327327
}
328+
// parse fk and primary keys
329+
if (this.primaryKeyList.length > 0) {
330+
this.primaryKeyList.forEach((pk) => {
331+
// find table index
332+
var pkTableIndex = this.tableList.findIndex(
333+
(t) => t.Name == pk.PrimaryKeyTableName
334+
);
335+
336+
// find property index
337+
if (pkTableIndex > -1) {
338+
var propertyIndex = this.tableList[pkTableIndex].Properties.findIndex(
339+
(p) => p.Name == pk.PrimaryKeyName
340+
);
341+
if (propertyIndex > -1) {
342+
this.tableList[pkTableIndex].Properties[
343+
propertyIndex
344+
].IsPrimaryKey = true;
345+
}
346+
}
347+
});
348+
}
349+
if (this.foreignKeyList.length > 0) {
350+
this.foreignKeyList.forEach((fk) => {
351+
// find table index
352+
var pkTableIndex = this.tableList.findIndex(
353+
(t) => t.Name == fk.ReferencesTableName
354+
);
355+
356+
// var fkTableIndex = this.tableList.findIndex(
357+
// (t) => t.Name == fk.PrimaryKeyTableName
358+
// );
359+
360+
// find property index
361+
if (pkTableIndex > -1) {
362+
var propertyIndex = this.tableList[pkTableIndex].Properties.findIndex(
363+
(p) => p.Name == fk.PrimaryKeyName
364+
);
365+
if (propertyIndex > -1) {
366+
this.tableList[pkTableIndex].Properties[
367+
propertyIndex
368+
].ForeignKey.push(fk);
369+
if (!fk.IsDestination) {
370+
this.tableList[pkTableIndex].Properties[
371+
propertyIndex
372+
].IsForeignKey = true;
373+
}
374+
}
375+
}
376+
377+
// if (fkTableIndex > -1) {
378+
// var propertyIndex = this.tableList[fkTableIndex].Properties.findIndex(
379+
// (p) => p.Name == fk.PrimaryKeyName
380+
// );
381+
// if (propertyIndex > -1) {
382+
// this.tableList[fkTableIndex].Properties[propertyIndex].ForeignKey.push(fk)
383+
// }
384+
// }
385+
});
386+
}
328387

329388
return this;
330389
}
@@ -351,7 +410,7 @@ export class SqlSimpleParser {
351410
) {
352411
var primaryKey: PrimaryKeyModel = {
353412
PrimaryKeyTableName: primaryKeyTableName,
354-
PrimaryKeyName: primaryKeyName,
413+
PrimaryKeyName: this.RemoveNameQuantifiers(primaryKeyName),
355414
};
356415

357416
return primaryKey;
@@ -369,7 +428,7 @@ export class SqlSimpleParser {
369428
Name: name,
370429
ColumnProperties: columnProps,
371430
TableName: tableName,
372-
ForeignKey: isForeignKey ? foreignKey : [],
431+
ForeignKey: foreignKey || [],
373432
IsForeignKey: isForeignKey,
374433
IsPrimaryKey: isPrimaryKey,
375434
};
@@ -535,10 +594,12 @@ export class SqlSimpleParser {
535594
isDestination: boolean
536595
) {
537596
var foreignKey: ForeignKeyModel = {
538-
PrimaryKeyTableName: primaryKeyTableName,
539-
PrimaryKeyName: primaryKeyName,
540-
ReferencesPropertyName: referencesPropertyName,
541-
ReferencesTableName: referencesTableName,
597+
PrimaryKeyTableName: this.RemoveNameQuantifiers(primaryKeyTableName),
598+
PrimaryKeyName: this.RemoveNameQuantifiers(primaryKeyName),
599+
ReferencesPropertyName: this.RemoveNameQuantifiers(
600+
referencesPropertyName
601+
),
602+
ReferencesTableName: this.RemoveNameQuantifiers(referencesTableName),
542603
IsDestination:
543604
isDestination !== undefined && isDestination !== null
544605
? isDestination

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface PropertyModel {
1515
Value?: string;
1616
ColumnProperties: string;
1717
TableName: string;
18-
ForeignKey: ForeignKeyModel[] | null;
18+
ForeignKey: ForeignKeyModel[];
1919
IsPrimaryKey: boolean;
2020
IsForeignKey: boolean;
2121
}

0 commit comments

Comments
 (0)