forked from martin-pabst/SQL-IDE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementCleaner.ts
More file actions
164 lines (137 loc) · 5.81 KB
/
StatementCleaner.ts
File metadata and controls
164 lines (137 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { TokenType } from "../lexer/Token.js";
import { ASTNode, ConstantNode, CreateTableColumnNode, CreateTableNode, ForeignKeyInfo, InsertNode } from "./AST.js";
import { SQLStatement } from "./Parser.js";
export class StatementCleaner {
MaxRowsPerInsert: number = 300;
clean(statement: SQLStatement): string {
switch (statement.ast.type) {
case TokenType.keywordCreate: // Create Table statement
statement.sqlCleaned = this.cleanCreateTableStatement(statement.ast);
return statement.sqlCleaned;
case TokenType.keywordInsert:
if(statement.ast.select != null){
return statement.sql;
} else {
statement.sqlCleaned = this.cleanInsertStatement(statement.ast);
return statement.sqlCleaned;
}
default:
return statement.sql;
break;
}
}
cleanInsertStatement(ast: InsertNode): string {
let statementHeader: string = `insert into ${ast.table.identifier}`;
if (ast.columnList != null && ast.columnList.length > 0) {
statementHeader += `(${ast.columnList.map(c => this.escapeIdentifier(c.identifier)).join(", ")})`;
}
statementHeader += '\nvalues';
let st = "";
let lines: string[] = [];
if (ast.values != null && ast.values.length > 0) {
for (let vline of ast.values) {
lines.push(`\n(${vline.map(v => this.cleanValue(v)).join(", ")})`);
}
//st += lines.join(",\n");
}
while (lines.length > 0) {
st += statementHeader;
st += lines.splice(0, Math.max(this.MaxRowsPerInsert, lines.length)).join(",\n");
st += ";\n";
}
return st;
}
cleanValue(v: ConstantNode) {
if (v.constantType == TokenType.stringConstant) {
let s: string = v.constant;
s = s.replace(/'/g, "''");
return `'${s}'`;
} else {
return v.constant;
}
}
cleanCreateTableStatement(ast: CreateTableNode): string {
let st: string = `create table ${ast.ifNotExists ? " if not exists" : ""} ${this.escapeIdentifier(ast.identifier)}(\n `;
st += ast.columnList.map(column => this.cleanColumnDef(column)).join(",\n ");
if (ast.foreignKeyInfoList != null && ast.foreignKeyInfoList.length > 0) {
st += ",\n " + ast.foreignKeyInfoList.map(fki => this.cleanForeignKeyInfo(fki)).join(",\n ");
}
let pkc = ast.combinedPrimaryKeyColumns.slice(); //.map(s => s.toLocaleLowerCase());
for (let column of ast.columnList) {
let c = column.identifier;
if (column.isAutoIncrement && pkc.indexOf(c) >= 0) {
pkc.splice(pkc.indexOf(c), 1);
}
}
if (pkc.length > 0) {
st += `,\n primary key(${this.escapeIdentifiers(pkc).join(", ")})`;
}
if (ast.uniqueConstraints.length > 0) {
st += ",\n " + ast.uniqueConstraints.map(uk => 'unique(' + this.escapeIdentifiers(uk).join(", ") + ")").join(",\n ");
}
st += '\n);';
return st;
}
cleanForeignKeyInfo(fki: ForeignKeyInfo): string {
let fkiString = `foreign key (${this.escapeIdentifier(fki.column)}) references ${this.escapeIdentifier(fki.referencesTable)}(${this.escapeIdentifier(fki.referencesColumn)})`;
if (fki.onDelete) {
fkiString += ` on delete ` + fki.onDelete;
}
if (fki.onUpdate) {
fkiString += ` on update ` + fki.onUpdate;
}
return fkiString;
}
cleanColumnDef(column: CreateTableColumnNode): string {
let type = column.baseType.getSQLiteType();
if (type == "int" && column.isAutoIncrement) {
type = "integer";
}
let st: string = `${this.escapeIdentifier(column.identifier)} ${type}`;
if (column.parameters != null && column.parameters.length > 0 && !column.isAutoIncrement) {
st += `(${column.parameters.join(", ")})`;
}
if (column.notNull) {
st += " not null";
}
if (column.defaultValue != null) {
st += " default " + column.defaultValue;
}
if (column.isPrimary || column.isAutoIncrement) {
st += " primary key";
}
if (column.isAutoIncrement) {
st += " autoincrement";
}
if (column.foreignKeyInfo != null) {
let fki = column.foreignKeyInfo;
let c: string = fki.referencesColumn;
if (fki.referencesTable) c = this.escapeIdentifier(fki.referencesTable) + "(" + this.escapeIdentifier(c) + ")";
st += " references " + c;
if(fki.onDelete){
st += " on delete " + fki.onDelete;
}
if(fki.onUpdate){
st += " on update " + fki.onUpdate;
}
}
if (column.collate != null) {
let collate = column.collate.toLocaleLowerCase();
if (["binary", "nocase", "rtrim"].indexOf(collate) >= 0) {
st += " collate " + collate;
}
}
let parameters = column.parameters ? column.parameters : [0, 0];
let checkFunction = column.baseType.checkFunction(this.escapeIdentifier(column.identifier), parameters);
if (checkFunction != "") {
st += " " + checkFunction;
}
return st;
}
escapeIdentifier(id: string){
return '"' + id + '"';
}
escapeIdentifiers(list: string[]):string[] {
return list.map(this.escapeIdentifier);
}
}