Skip to content

Commit fda0fe8

Browse files
authored
Merge pull request #12 from funktechno/f/lastlink
handle tables with new line (
2 parents 9dd7865 + f44cd3a commit fda0fe8

3 files changed

Lines changed: 165 additions & 1 deletion

File tree

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ export class SqlSimpleParser {
9292
var lines: string[] = [];
9393
var insertSameLine = false;
9494
cleanedLines.forEach((n) => {
95-
if (n[0] == "(" || insertSameLine) {
95+
if (
96+
(lines.length > 0 &&
97+
n[0] == "(" &&
98+
lines[lines.length - 1].toLocaleLowerCase().indexOf(CreateTable) ==
99+
-1) ||
100+
insertSameLine
101+
) {
96102
if (lines.length > 0) {
97103
insertSameLine = true;
98104
lines[lines.length - 1] += n;

tests/SqlSimpleParser/examples_models.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,51 @@ describe("Example Sql Parsing", () => {
127127
expect(1).toBeTruthy();
128128
});
129129

130+
it("Run Parser mssql simple", async () => {
131+
// load sql
132+
var sql = `CREATE TABLE Persons
133+
(
134+
PersonID int NOT NULL,
135+
LastName varchar(255),
136+
FirstName varchar(255),
137+
Address varchar(255),
138+
City varchar(255),
139+
Primary Key(PersonId)
140+
);
141+
142+
CREATE TABLE Orders
143+
(
144+
OrderID int NOT NULL PRIMARY KEY,
145+
PersonID int NOT NULL,
146+
FOREIGN KEY ([PersonID]) REFERENCES [Persons]([PersonID])
147+
);`;
148+
// console.log(sql);
149+
150+
// run parser
151+
const parser = new SqlSimpleParser("postgres");
152+
153+
// get models
154+
const result = parser.feed(sql).ToModel();
155+
156+
// write to json file
157+
const dataKey = expect.getState().currentTestName || "unknown";
158+
159+
const expectedResult = await getExpected(dataSource, dataKey);
160+
161+
if (result != expectedResult) {
162+
await updateExpected(dataSource, dataKey, result);
163+
}
164+
165+
// console.log(result);
166+
expect(result).toStrictEqual(expectedResult);
167+
168+
// write to json file
169+
// await fs.writeFileSync(
170+
// "output-pg_simple.json",
171+
// JSON.stringify(models, null, "\t")
172+
// );
173+
expect(1).toBeTruthy();
174+
});
130175
it("Run Parser postgres simple", async () => {
131176
// load sql
132177
var sql = `CREATE TABLE "humanresources_department" (

tests/data/results/ToModel.json

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20518,5 +20518,118 @@
2051820518
"PrimaryKeyName": "departmentid"
2051920519
}
2052020520
]
20521+
},
20522+
"Example Sql Parsing Run Parser mssql simple": {
20523+
"TableList": [
20524+
{
20525+
"Name": "Persons",
20526+
"Properties": [
20527+
{
20528+
"Name": "PersonID",
20529+
"ColumnProperties": "int NOT NULL",
20530+
"TableName": "Persons",
20531+
"ForeignKey": [
20532+
{
20533+
"PrimaryKeyTableName": "Orders",
20534+
"PrimaryKeyName": "PersonID",
20535+
"ReferencesPropertyName": "PersonID",
20536+
"ReferencesTableName": "Persons",
20537+
"IsDestination": true
20538+
}
20539+
],
20540+
"IsForeignKey": false,
20541+
"IsPrimaryKey": false
20542+
},
20543+
{
20544+
"Name": "LastName",
20545+
"ColumnProperties": "varchar(255)",
20546+
"TableName": "Persons",
20547+
"ForeignKey": [],
20548+
"IsForeignKey": false,
20549+
"IsPrimaryKey": false
20550+
},
20551+
{
20552+
"Name": "FirstName",
20553+
"ColumnProperties": "varchar(255)",
20554+
"TableName": "Persons",
20555+
"ForeignKey": [],
20556+
"IsForeignKey": false,
20557+
"IsPrimaryKey": false
20558+
},
20559+
{
20560+
"Name": "Address",
20561+
"ColumnProperties": "varchar(255)",
20562+
"TableName": "Persons",
20563+
"ForeignKey": [],
20564+
"IsForeignKey": false,
20565+
"IsPrimaryKey": false
20566+
},
20567+
{
20568+
"Name": "City",
20569+
"ColumnProperties": "varchar(255)",
20570+
"TableName": "Persons",
20571+
"ForeignKey": [],
20572+
"IsForeignKey": false,
20573+
"IsPrimaryKey": false
20574+
}
20575+
]
20576+
},
20577+
{
20578+
"Name": "Orders",
20579+
"Properties": [
20580+
{
20581+
"Name": "OrderID",
20582+
"ColumnProperties": "int NOT NULL PRIMARY KEY",
20583+
"TableName": "Orders",
20584+
"ForeignKey": [],
20585+
"IsForeignKey": false,
20586+
"IsPrimaryKey": true
20587+
},
20588+
{
20589+
"Name": "PersonID",
20590+
"ColumnProperties": "int NOT NULL",
20591+
"TableName": "Orders",
20592+
"ForeignKey": [
20593+
{
20594+
"PrimaryKeyTableName": "Persons",
20595+
"PrimaryKeyName": "PersonID",
20596+
"ReferencesPropertyName": "PersonID",
20597+
"ReferencesTableName": "Orders",
20598+
"IsDestination": false
20599+
}
20600+
],
20601+
"IsForeignKey": true,
20602+
"IsPrimaryKey": false
20603+
}
20604+
]
20605+
}
20606+
],
20607+
"Dialect": "postgres",
20608+
"ForeignKeyList": [
20609+
{
20610+
"PrimaryKeyTableName": "Orders",
20611+
"PrimaryKeyName": "PersonID",
20612+
"ReferencesPropertyName": "PersonID",
20613+
"ReferencesTableName": "Persons",
20614+
"IsDestination": true
20615+
},
20616+
{
20617+
"PrimaryKeyTableName": "Persons",
20618+
"PrimaryKeyName": "PersonID",
20619+
"ReferencesPropertyName": "PersonID",
20620+
"ReferencesTableName": "Orders",
20621+
"IsDestination": false
20622+
}
20623+
],
20624+
"PrimaryKeyList": [
20625+
{
20626+
"PrimaryKeyTableName": "Persons",
20627+
"PrimaryKeyName": "PersonId"
20628+
},
20629+
{
20630+
"PrimaryKeyTableName": "Orders",
20631+
"PrimaryKeyName": "OrderID"
20632+
}
20633+
]
2052120634
}
2052220635
}

0 commit comments

Comments
 (0)