Skip to content

Commit b4b2c85

Browse files
committed
some progress
1 parent 0c9d1f1 commit b4b2c85

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

src/index.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
import { DbEntity } from "./types";
2+
13
/**
24
* Main Parser class
35
*/
46
export class SqlSimpleParser {
57
private dialect: string;
8+
private entities: DbEntity[] = [];
9+
10+
/**
11+
* Parsed statements.
12+
*/
13+
private statements: string[] = [];
14+
15+
/**
16+
* Remains of string feed, after last parsed statement.
17+
*/
18+
private remains = "";
19+
20+
/**
21+
* Whether preparser is currently escaped.
22+
*/
23+
private escaped = false;
24+
25+
/**
26+
* Current quote char of preparser.
27+
*/
28+
private quoted = "";
629
/**
730
* Parser constructor.
831
* Default dialect is 'sqlite'.
@@ -22,10 +45,52 @@ export class SqlSimpleParser {
2245
*/
2346
feed(chunk: string): SqlSimpleParser {
2447
//
48+
let i;
49+
let char;
50+
let parsed = "";
51+
let lastStatementIndex = 0;
52+
53+
for (i = 0; i < chunk.length; i += 1) {
54+
char = chunk[i];
55+
parsed += char;
56+
57+
if (char === "\\") {
58+
this.escaped = !this.escaped;
59+
} else {
60+
if (!this.escaped && SqlSimpleParser.isQuoteChar(char)) {
61+
if (this.quoted) {
62+
if (this.quoted === char) {
63+
this.quoted = "";
64+
}
65+
} else {
66+
this.quoted = char;
67+
}
68+
} else if (char === ";" && !this.quoted) {
69+
const statement =
70+
this.remains + parsed.substr(lastStatementIndex, i + 1);
71+
this.statements.push(statement);
72+
this.remains = "";
73+
lastStatementIndex = i + 1;
74+
}
75+
76+
this.escaped = false;
77+
}
78+
}
79+
80+
this.remains += parsed.substr(lastStatementIndex);
2581
return this;
2682
}
2783

84+
/**
85+
* Checks whether character is a quotation character.
86+
*
87+
* @param char Character to be evaluated.
88+
*/
89+
private static isQuoteChar(char: string): boolean {
90+
return char === '"' || char === "'" || char === "`";
91+
}
92+
2893
ToModel(): any {
29-
return {}
94+
return {};
3095
}
3196
}

src/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
export interface DbEntity {
4+
name: string;
5+
6+
}

tests/example_sqlite.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe("Example Sqlite", () => {
1818
.ToModel();
1919

2020
// write to json file
21-
await fs.writeFileSync("sqliteOutput.json", JSON.stringify(models));
21+
await fs.writeFileSync("output-sqlite.json", JSON.stringify(models));
2222
}
2323
await runSample();
2424
expect(1).toBeTruthy();

0 commit comments

Comments
 (0)