Skip to content

Commit e8afc2e

Browse files
committed
mssql improve primary key check
1 parent be7bb87 commit e8afc2e

1 file changed

Lines changed: 44 additions & 10 deletions

File tree

src/index.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class SqlSimpleParser {
173173

174174
if (
175175
propertyType.indexOf(CONSTRAINT) !== -1 &&
176-
propertyType.indexOf(Primary_Key) !== -1
176+
propertyType.indexOf(Foreign_Key) !== -1
177177
) {
178178
propertyType = CONSTRAINT_Foreign_Key;
179179
}
@@ -218,7 +218,7 @@ export class SqlSimpleParser {
218218
//TODO: check for space? or end quantifier
219219
var firstSpaceIndex =
220220
name[0] == "[" && name.indexOf("]" + " ") !== -1
221-
? name.indexOf("]" + " ")
221+
? name.indexOf("]" + " ")
222222
: name.indexOf(" ");
223223

224224
ExtendedProperties = name.substring(firstSpaceIndex + 1).trim();
@@ -228,10 +228,11 @@ export class SqlSimpleParser {
228228

229229
name = this.RemoveNameQuantifiers(name);
230230
} else {
231-
const columnQuantifiers = this.GetColumnQuantifiers()
231+
const columnQuantifiers = this.GetColumnQuantifiers();
232232
//Get delimiter of column name
233233
var firstSpaceIndex =
234-
name[0] == columnQuantifiers.Start && name.indexOf(columnQuantifiers.End + " ") !== -1
234+
name[0] == columnQuantifiers.Start &&
235+
name.indexOf(columnQuantifiers.End + " ") !== -1
235236
? name.indexOf(columnQuantifiers.End + " ")
236237
: name.indexOf(" ");
237238

@@ -275,8 +276,8 @@ export class SqlSimpleParser {
275276
//Add Primary Key to List
276277
this.primaryKeyList.push(primaryKeyModel);
277278
} else {
278-
var start = i + 2;
279-
var end = 0;
279+
// var start = i + 2;
280+
// var end = 0;
280281
if (
281282
propertyRow.indexOf(Primary_Key) !== -1 &&
282283
nameSkipCheck.indexOf("CLUSTERED") === -1
@@ -294,6 +295,38 @@ export class SqlSimpleParser {
294295
//Add Primary Key to List
295296
this.primaryKeyList.push(primaryKeyModel);
296297
} else {
298+
var startIndex =
299+
name.toLocaleLowerCase().indexOf("(");
300+
var endIndex = name.indexOf(")") + 1;
301+
var primaryKey = name
302+
.substring(startIndex, endIndex)
303+
.replace("(", "")
304+
.replace(")", "")
305+
.replace(/ASC/gi, "")
306+
.trim();
307+
308+
const columnQuantifiers = this.GetColumnQuantifiers();
309+
//Get delimiter of column name
310+
var firstSpaceIndex =
311+
primaryKey[0] == columnQuantifiers.Start &&
312+
primaryKey.indexOf(columnQuantifiers.End + " ") !== -1
313+
? primaryKey.indexOf(columnQuantifiers.End + " ")
314+
: primaryKey.indexOf(" ");
315+
316+
var primaryKeyRow =
317+
firstSpaceIndex == -1
318+
? primaryKey
319+
: primaryKey.substring(firstSpaceIndex + 1).trim();
320+
321+
//Create Primary Key
322+
var primaryKeyModel = this.CreatePrimaryKey(
323+
primaryKeyRow,
324+
currentTableModel.Name
325+
);
326+
327+
//Add Primary Key to List
328+
this.primaryKeyList.push(primaryKeyModel);
329+
/*
297330
while (end === 0) {
298331
var primaryKeyRow = lines[start].trim();
299332
@@ -318,6 +351,7 @@ export class SqlSimpleParser {
318351
//Add Primary Key to List
319352
this.primaryKeyList.push(primaryKeyModel);
320353
}
354+
*/
321355
}
322356
}
323357
}
@@ -438,11 +472,11 @@ export class SqlSimpleParser {
438472
}
439473
return new RegExp("//(.+)/.*/", "//.+/(.*)/");
440474
}
441-
private GetColumnQuantifiers(){
442-
let chars:ColumnQuantifiers={
475+
private GetColumnQuantifiers() {
476+
let chars: ColumnQuantifiers = {
443477
Start: '"',
444-
End: '"'
445-
}
478+
End: '"',
479+
};
446480
if (this.dialect == "mysql") {
447481
chars.Start = "`";
448482
chars.End = "`";

0 commit comments

Comments
 (0)