Skip to content

Commit be7bb87

Browse files
committed
better primary key and spaces in column names handled
1 parent 5c9ba7a commit be7bb87

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlsimpleparser",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "",
55
"main": "index.js",
66
"engines": {

src/index.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Primary_Key,
99
} from "./contants";
1010
import {
11+
ColumnQuantifiers,
1112
DatabaseModel,
1213
ForeignKeyModel,
1314
PrimaryKeyModel,
@@ -80,12 +81,27 @@ export class SqlSimpleParser {
8081
// remove database comments, multiline, --, and //
8182
.replace(/\/\*[\s\S]*?\*\/|\/\/|--.*/g, "")
8283
.trim();
83-
var lines = removedComments
84+
var cleanedLines = removedComments
8485
.split("\n")
8586
// remove empty lines
8687
.filter((n) => n)
8788
// remove multiple spaces
8889
.map((n) => n.replace(/\s+/g, " ").trim());
90+
91+
// combine lines that are in parenthesis
92+
var lines: string[] = [];
93+
var insertSameLine = false;
94+
cleanedLines.forEach((n) => {
95+
if (n[0] == "(" || insertSameLine) {
96+
if (lines.length > 0) {
97+
insertSameLine = true;
98+
lines[lines.length - 1] += n;
99+
if (n[0] == ")") insertSameLine = false;
100+
}
101+
} else {
102+
lines.push(n);
103+
}
104+
});
89105
// dx = 0,
90106
// tableCell = null,
91107
// cells = [],
@@ -103,14 +119,13 @@ export class SqlSimpleParser {
103119

104120
var propertyRow = tmp.toLowerCase().trim();
105121

106-
if(propertyRow[0] == ")"){
122+
if (propertyRow[0] == ")") {
107123
// close table
108-
if(currentTableModel){
124+
if (currentTableModel) {
109125
this.tableList.push(currentTableModel);
110126
currentTableModel = null;
111127
}
112128
continue;
113-
114129
}
115130

116131
//Parse Table
@@ -201,7 +216,10 @@ export class SqlSimpleParser {
201216
}
202217
//Get delimiter of column name
203218
//TODO: check for space? or end quantifier
204-
var firstSpaceIndex = name.indexOf(" ");
219+
var firstSpaceIndex =
220+
name[0] == "[" && name.indexOf("]" + " ") !== -1
221+
? name.indexOf("]" + " ")
222+
: name.indexOf(" ");
205223

206224
ExtendedProperties = name.substring(firstSpaceIndex + 1).trim();
207225

@@ -210,8 +228,12 @@ export class SqlSimpleParser {
210228

211229
name = this.RemoveNameQuantifiers(name);
212230
} else {
231+
const columnQuantifiers = this.GetColumnQuantifiers()
213232
//Get delimiter of column name
214-
var firstSpaceIndex = name.indexOf(" ");
233+
var firstSpaceIndex =
234+
name[0] == columnQuantifiers.Start && name.indexOf(columnQuantifiers.End + " ") !== -1
235+
? name.indexOf(columnQuantifiers.End + " ")
236+
: name.indexOf(" ");
215237

216238
ExtendedProperties = name.substring(firstSpaceIndex + 1).trim();
217239

@@ -416,6 +438,20 @@ export class SqlSimpleParser {
416438
}
417439
return new RegExp("//(.+)/.*/", "//.+/(.*)/");
418440
}
441+
private GetColumnQuantifiers(){
442+
let chars:ColumnQuantifiers={
443+
Start: '"',
444+
End: '"'
445+
}
446+
if (this.dialect == "mysql") {
447+
chars.Start = "`";
448+
chars.End = "`";
449+
} else if (this.dialect == "sqlserver") {
450+
chars.Start = "[";
451+
chars.End = "]";
452+
}
453+
return chars;
454+
}
419455

420456
private CreatePrimaryKey(
421457
primaryKeyName: string,

src/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ export interface PrimaryKeyModel {
3232
PrimaryKeyName: string;
3333
PrimaryKeyTableName: string;
3434
}
35+
36+
export interface ColumnQuantifiers {
37+
Start: string;
38+
End: string;
39+
}

0 commit comments

Comments
 (0)