Skip to content

Commit 3334739

Browse files
committed
handle different primary key
1 parent e8afc2e commit 3334739

3 files changed

Lines changed: 96 additions & 50 deletions

File tree

examples/adventureworks_mssql.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
*/
44
USE [AdventureWorksLT2019]
55
GO
6+
/****** Object: Table [dbo].[ErrorLog] Script Date: 10/7/2022 9:47:21 PM ******/
7+
SET ANSI_NULLS ON
8+
GO
9+
SET QUOTED_IDENTIFIER ON
10+
GO
11+
CREATE TABLE Persons (
12+
Personid int IDENTITY(1,1) PRIMARY KEY,
13+
LastName varchar(255) NOT NULL,
14+
FirstName varchar(255),
15+
Age int
16+
);
17+
GO
618
/****** Object: Table [dbo].[BuildVersion] Script Date: 10/7/2022 9:47:21 PM ******/
719
SET ANSI_NULLS ON
820
GO

src/index.ts

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ export class SqlSimpleParser {
180180
}
181181

182182
//Verify if this is a property that doesn't have a relationship (One minute of silence for the property)
183+
// TODO: make primary key check a regex match/ contains w/ () space or not
183184
var normalProperty =
184-
propertyType.indexOf(Primary_Key) == -1 &&
185+
!propertyType.match(/PRIMARY KEY\s?\(/gi) &&
185186
propertyType.indexOf(Foreign_Key) == -1 &&
186187
propertyType.indexOf(CONSTRAINT_Primary_Key) == -1 &&
187188
propertyType.indexOf(CONSTRAINT_Foreign_Key) == -1;
@@ -255,33 +256,26 @@ export class SqlSimpleParser {
255256

256257
//Add Property to table
257258
currentTableModel.Properties.push(propertyModel);
258-
}
259-
260-
//Parse Primary Key
261-
if (
262-
propertyType.indexOf(Primary_Key) != -1 ||
263-
propertyType.indexOf(CONSTRAINT_Primary_Key) != -1
264-
) {
265-
if (!this.MODE_SQLSERVER) {
266-
var primaryKey = name
267-
.replace(/PRIMARY KEY\s?\(/gi, "")
268-
.replace(")", "");
269259

260+
if (
261+
ExtendedProperties.toLocaleLowerCase().indexOf(Primary_Key) > -1
262+
) {
270263
//Create Primary Key
271264
var primaryKeyModel = this.CreatePrimaryKey(
272-
primaryKey,
265+
name,
273266
currentTableModel.Name
274267
);
275268

276269
//Add Primary Key to List
277270
this.primaryKeyList.push(primaryKeyModel);
278-
} else {
279-
// var start = i + 2;
280-
// var end = 0;
281-
if (
282-
propertyRow.indexOf(Primary_Key) !== -1 &&
283-
nameSkipCheck.indexOf("CLUSTERED") === -1
284-
) {
271+
}
272+
} else {
273+
//Parse Primary Key
274+
if (
275+
propertyType.indexOf(Primary_Key) != -1 ||
276+
propertyType.indexOf(CONSTRAINT_Primary_Key) != -1
277+
) {
278+
if (!this.MODE_SQLSERVER) {
285279
var primaryKey = name
286280
.replace(/PRIMARY KEY\s?\(/gi, "")
287281
.replace(")", "");
@@ -295,38 +289,56 @@ export class SqlSimpleParser {
295289
//Add Primary Key to List
296290
this.primaryKeyList.push(primaryKeyModel);
297291
} 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();
292+
// var start = i + 2;
293+
// var end = 0;
294+
if (
295+
propertyRow.indexOf(Primary_Key) !== -1 &&
296+
nameSkipCheck.indexOf("CLUSTERED") === -1
297+
) {
298+
var primaryKey = name
299+
.replace(/PRIMARY KEY\s?\(/gi, "")
300+
.replace(")", "");
320301

321-
//Create Primary Key
322-
var primaryKeyModel = this.CreatePrimaryKey(
323-
primaryKeyRow,
324-
currentTableModel.Name
325-
);
302+
//Create Primary Key
303+
var primaryKeyModel = this.CreatePrimaryKey(
304+
primaryKey,
305+
currentTableModel.Name
306+
);
326307

327-
//Add Primary Key to List
328-
this.primaryKeyList.push(primaryKeyModel);
329-
/*
308+
//Add Primary Key to List
309+
this.primaryKeyList.push(primaryKeyModel);
310+
} else {
311+
var startIndex = name.toLocaleLowerCase().indexOf("(");
312+
var endIndex = name.indexOf(")") + 1;
313+
var primaryKey = name
314+
.substring(startIndex, endIndex)
315+
.replace("(", "")
316+
.replace(")", "")
317+
.replace(/ASC/gi, "")
318+
.trim();
319+
320+
const columnQuantifiers = this.GetColumnQuantifiers();
321+
//Get delimiter of column name
322+
var firstSpaceIndex =
323+
primaryKey[0] == columnQuantifiers.Start &&
324+
primaryKey.indexOf(columnQuantifiers.End + " ") !== -1
325+
? primaryKey.indexOf(columnQuantifiers.End + " ")
326+
: primaryKey.indexOf(" ");
327+
328+
var primaryKeyRow =
329+
firstSpaceIndex == -1
330+
? primaryKey
331+
: primaryKey.substring(firstSpaceIndex + 1).trim();
332+
333+
//Create Primary Key
334+
var primaryKeyModel = this.CreatePrimaryKey(
335+
primaryKeyRow,
336+
currentTableModel.Name
337+
);
338+
339+
//Add Primary Key to List
340+
this.primaryKeyList.push(primaryKeyModel);
341+
/*
330342
while (end === 0) {
331343
var primaryKeyRow = lines[start].trim();
332344
@@ -352,6 +364,7 @@ export class SqlSimpleParser {
352364
this.primaryKeyList.push(primaryKeyModel);
353365
}
354366
*/
367+
}
355368
}
356369
}
357370
}

tests/examples_models.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,25 @@ describe("Example Sql Parsing", () => {
6565
await runSample();
6666
expect(1).toBeTruthy();
6767
});
68+
it("Run Parser postgres Ex", async () => {
69+
const filePath = "examples/adventureworks_mssql.sql";
70+
async function runSample() {
71+
// load sql
72+
var sql = await fs.readFileSync(filePath, "utf8");
73+
console.log(sql);
74+
75+
// run parser
76+
const parser = new SqlSimpleParser('sqlserver')
77+
78+
// get models
79+
const models = parser
80+
.feed(sql)
81+
.ToModel();
82+
83+
// write to json file
84+
await fs.writeFileSync("output-mssql.json", JSON.stringify(models, null, '\t'));
85+
}
86+
await runSample();
87+
expect(1).toBeTruthy();
88+
});
6889
});

0 commit comments

Comments
 (0)