Skip to content

Commit 7208c86

Browse files
committed
reformat sql parser
1 parent 73672a5 commit 7208c86

File tree

2 files changed

+75
-54
lines changed

2 files changed

+75
-54
lines changed

src/generate-sql-ddl.ts

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import {
2+
DbDefinition,
3+
DbEntityDefinition,
4+
DbRelationshipDefinition,
5+
} from "./types";
16

27
interface Table {
3-
name: string;
4-
columns: string[];
8+
name: string;
9+
columns: string[];
510
}
611

712
class Node<T> {
@@ -10,57 +15,72 @@ class Node<T> {
1015
constructor(public data: T) {}
1116
}
1217

13-
export class Parser {
18+
export class DbParser {
19+
private symbols = ["INTEGER", "NVARCHAR", "DATETIME", "NUMERIC"];
20+
private db: DbDefinition;
21+
private dbType: string;
22+
private dataDefinitions!: Array<string>;
23+
private entities!: Record<string, DbEntityDefinition>;
24+
private relationships!: DbRelationshipDefinition[];
1425

15-
private symbols = ['INTEGER', 'NVARCHAR', 'DATETIME', 'NUMERIC'];
16-
private ast: string;
17-
private dataDefinitions! : Array<string>;
26+
constructor(dbType: string, db: DbDefinition) {
27+
this.db = db;
28+
this.dbType = dbType;
29+
}
1830

19-
constructor(abstractSyntaxTree: string){
20-
this.ast = abstractSyntaxTree;
21-
}
22-
23-
public getSQLDataDefinition(){
24-
const tokens = this.ast.trim().split(" ");
25-
console.log(tokens);
26-
if(tokens[0] !== 'erDiagram'){
27-
throw 'Expecting erDiagram keyword';
28-
}
29-
return this.lexer(tokens);
30-
}
31+
public getSQLDataDefinition() {
32+
this.entities = this.db.getEntities();
33+
this.relationships = this.db.getRelationships();
34+
// const tokens = this.ast.trim().split(" ");
35+
// console.log(tokens);
36+
// if(tokens[0] !== 'erDiagram'){
37+
// throw 'Expecting erDiagram keyword';
38+
// }
39+
return this.lexer();
40+
}
3141

32-
private lexer(tokens : Array<string>): string{
33-
let statementGeneration : Array<string> = [];
34-
for(let i = 1; i < tokens.length; i++){
35-
if(this.symbols.includes(tokens[i])){
36-
throw `Keyword reserved '${tokens[i]}'`;
37-
}
38-
const tableName = tokens[i];
39-
i++;
40-
if(tokens[i] !== "{"){
41-
throw `Expecting '{' got : '${tokens[i]}'`;
42-
}
43-
i++;
44-
while(tokens[i] !== '}' && i < tokens.length){
45-
if(!this.symbols.includes(tokens[i])){
46-
throw `Expecting keyword got : '${tokens[i]}'`;
47-
}
48-
const dataType = tokens[i];
49-
i++;
50-
statementGeneration.push(this.createColumn(tableName, tokens[i], dataType));
51-
i++;
52-
}
53-
statementGeneration.unshift(this.createTable(tableName));
42+
private lexer(): string {
43+
let statementGeneration: Array<string> = [];
44+
if (this.entities) {
45+
for (const key in this.entities) {
46+
if (Object.prototype.hasOwnProperty.call(this.entities, key)) {
47+
const element = this.entities[key];
5448
}
55-
return statementGeneration.join("");
49+
}
5650
}
51+
// for(let i = 1; i < tokens.length; i++){
52+
// if(this.symbols.includes(tokens[i])){
53+
// throw `Keyword reserved '${tokens[i]}'`;
54+
// }
55+
// const tableName = tokens[i];
56+
// i++;
57+
// if(tokens[i] !== "{"){
58+
// throw `Expecting '{' got : '${tokens[i]}'`;
59+
// }
60+
// i++;
61+
// while(tokens[i] !== '}' && i < tokens.length){
62+
// if(!this.symbols.includes(tokens[i])){
63+
// throw `Expecting keyword got : '${tokens[i]}'`;
64+
// }
65+
// const dataType = tokens[i];
66+
// i++;
67+
// statementGeneration.push(this.createColumn(tableName, tokens[i], dataType));
68+
// i++;
69+
// }
70+
// statementGeneration.unshift(this.createTable(tableName));
71+
// }
72+
return statementGeneration.join("");
73+
}
5774

58-
private createTable(tableName: string,){
59-
return `CREATE TABLE ${tableName}`;
60-
}
61-
62-
private createColumn(tableName: string, columnName: string, dataType: string){
63-
return `ALTER TABLE ${tableName } ADD ${columnName} ${dataType}`;
64-
}
75+
private createTable(tableName: string) {
76+
return `CREATE TABLE ${tableName}`;
77+
}
6578

66-
}
79+
private createColumn(
80+
tableName: string,
81+
columnName: string,
82+
dataType: string
83+
) {
84+
return `ALTER TABLE ${tableName} ADD ${columnName} ${dataType}`;
85+
}
86+
}

tests/generate-sql-ddl.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Parser } from "../src/generate-sql-ddl";
2-
1+
import { DbParser } from "../src/generate-sql-ddl";
2+
import erDb from "../src/mermaid/src/diagrams/er/erDb";
33
describe("Generate DDL examples", () => {
44
it("Should generate create table SQL syntax", () => {
5-
const abt = `erDiagram artists { }`;
6-
const ddlSyntax = new Parser(abt).getSQLDataDefinition();
7-
expect(ddlSyntax).toBe('CREATE TABLE artists');
5+
const db = erDb;
6+
// `erDiagram artists { }`;
7+
const ddlSyntax = new DbParser('sqlite', db).getSQLDataDefinition();
8+
// expect(ddlSyntax).toBe('CREATE TABLE artists');
89
});
910
});

0 commit comments

Comments
 (0)