Skip to content

Commit cd98e5d

Browse files
authored
Merge pull request #33 from funktechno/f/lastlink
improved composite key pk lookup
2 parents 8f43052 + a86a0ce commit cd98e5d

File tree

4 files changed

+20565
-20284
lines changed

4 files changed

+20565
-20284
lines changed

README.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const models = parser
9393
"TableName": "humanresources_employeedepartmenthistory",
9494
"ForeignKey": [],
9595
"IsForeignKey": false,
96-
"IsPrimaryKey": false
96+
"IsPrimaryKey": true
9797
},
9898
{
9999
"Name": "departmentid",
@@ -109,23 +109,23 @@ const models = parser
109109
}
110110
],
111111
"IsForeignKey": true,
112-
"IsPrimaryKey": false
112+
"IsPrimaryKey": true
113113
},
114114
{
115115
"Name": "shiftid",
116116
"ColumnProperties": "int(2) NOT NULL",
117117
"TableName": "humanresources_employeedepartmenthistory",
118118
"ForeignKey": [],
119119
"IsForeignKey": false,
120-
"IsPrimaryKey": false
120+
"IsPrimaryKey": true
121121
},
122122
{
123123
"Name": "startdate",
124124
"ColumnProperties": "date NOT NULL",
125125
"TableName": "humanresources_employeedepartmenthistory",
126126
"ForeignKey": [],
127127
"IsForeignKey": false,
128-
"IsPrimaryKey": false
128+
"IsPrimaryKey": true
129129
},
130130
{
131131
"Name": "enddate",
@@ -170,7 +170,19 @@ const models = parser
170170
},
171171
{
172172
"PrimaryKeyTableName": "humanresources_employeedepartmenthistory",
173-
"PrimaryKeyName": "businessentityid,departmentid,shiftid,startdate"
173+
"PrimaryKeyName": "businessentityid"
174+
},
175+
{
176+
"PrimaryKeyTableName": "humanresources_employeedepartmenthistory",
177+
"PrimaryKeyName": "departmentid"
178+
},
179+
{
180+
"PrimaryKeyTableName": "humanresources_employeedepartmenthistory",
181+
"PrimaryKeyName": "shiftid"
182+
},
183+
{
184+
"PrimaryKeyTableName": "humanresources_employeedepartmenthistory",
185+
"PrimaryKeyName": "startdate"
174186
}
175187
]
176188
}

src/index.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export class SqlSimpleParser {
273273
);
274274

275275
//Add Primary Key to List
276-
this.primaryKeyList.push(primaryKeyModel);
276+
this.primaryKeyList = this.primaryKeyList.concat(primaryKeyModel);
277277
}
278278
} else {
279279
//Parse Primary Key
@@ -293,7 +293,7 @@ export class SqlSimpleParser {
293293
);
294294

295295
//Add Primary Key to List
296-
this.primaryKeyList.push(primaryKeyModel);
296+
this.primaryKeyList = this.primaryKeyList.concat(primaryKeyModel);
297297
} else {
298298
// let start = i + 2;
299299
// let end = 0;
@@ -312,7 +312,7 @@ export class SqlSimpleParser {
312312
);
313313

314314
//Add Primary Key to List
315-
this.primaryKeyList.push(primaryKeyModel);
315+
this.primaryKeyList = this.primaryKeyList.concat(primaryKeyModel);
316316
} else {
317317
const startIndex = name.toLocaleLowerCase().indexOf("(");
318318
const endIndex = name.indexOf(")") + 1;
@@ -343,7 +343,7 @@ export class SqlSimpleParser {
343343
);
344344

345345
//Add Primary Key to List
346-
this.primaryKeyList.push(primaryKeyModel);
346+
this.primaryKeyList = this.primaryKeyList.concat(primaryKeyModel);
347347
/*
348348
while (end === 0) {
349349
let primaryKeyRow = lines[start].trim();
@@ -503,13 +503,22 @@ export class SqlSimpleParser {
503503
private CreatePrimaryKey(
504504
primaryKeyName: string,
505505
primaryKeyTableName: string
506-
) {
507-
const primaryKey: PrimaryKeyModel = {
508-
PrimaryKeyTableName: primaryKeyTableName,
509-
PrimaryKeyName: this.RemoveNameQuantifiers(primaryKeyName),
510-
};
506+
):PrimaryKeyModel[] {
507+
const primaryKeyNames = this.RemoveNameQuantifiers(primaryKeyName)
508+
.split(",")
509+
.filter((n) => n)
510+
// remove multiple spaces
511+
.map((n) => n.replace(/\s+/g, " ").trim());
512+
const primaryKeys:PrimaryKeyModel[] = [];
513+
primaryKeyNames.forEach(name => {
514+
const primaryKey: PrimaryKeyModel = {
515+
PrimaryKeyTableName: primaryKeyTableName,
516+
PrimaryKeyName: name,
517+
};
518+
primaryKeys.push(primaryKey);
519+
});
511520

512-
return primaryKey;
521+
return primaryKeys;
513522
}
514523

515524
private CreateProperty(

0 commit comments

Comments
 (0)