-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathvisitor.ts
More file actions
411 lines (368 loc) · 12.2 KB
/
visitor.ts
File metadata and controls
411 lines (368 loc) · 12.2 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import { Token } from "odata-v4-parser/lib/lexer";
import { Literal } from "odata-v4-literal";
import { SqlOptions } from "./index";
export class SQLLiteral extends Literal{
static convert(type:string, value:string):any {
return (new SQLLiteral(type, value)).valueOf();
}
'Edm.String'(value:string){ return "'" + decodeURIComponent(value).slice(1, -1).replace(/''/g, "'") + "'"; }
'Edm.Guid'(value:string){ return "'" + decodeURIComponent(value) + "'"; }
'Edm.Date'(value:string){ return "'" + value + "'"; }
'Edm.DateTimeOffset'(value:string):any{ return "'" + value.replace("T", " ").replace("Z", " ").trim() + "'"; }
'Edm.Boolean'(value:string):any{
value = value || '';
switch (value.toLowerCase()){
case 'true': return 1;
case 'false': return 0;
default: return "NULL";
}
}
'null'(value:string){ return "NULL"; }
}
export enum SQLLang{
ANSI,
MsSql,
MySql,
PostgreSql,
Oracle
}
export class Visitor{
protected options:SqlOptions
type:SQLLang;
select:string = "";
where:string = "";
orderby:string = "";
skip:number
limit:number
inlinecount:boolean
navigationProperty:string
includes:Visitor[] = [];
parameters:any = new Map();
protected parameterSeed:number = 0;
protected originalWhere:string;
ast:Token
constructor(options = <SqlOptions>{}){
this.options = options;
if (this.options.useParameters != false) this.options.useParameters = true;
this.type = options.type || SQLLang.ANSI;
}
from(table:string){
let sql = `SELECT ${this.select} FROM [${table}] WHERE ${this.where} ORDER BY ${this.orderby}`;
switch (this.type){
case SQLLang.Oracle:
case SQLLang.MsSql:
if (typeof this.skip == "number") sql += ` OFFSET ${this.skip} ROWS`;
if (typeof this.limit == "number"){
if (typeof this.skip != "number") sql += " OFFSET 0 ROWS";
sql += ` FETCH NEXT ${this.limit} ROWS ONLY`;
}
break;
case SQLLang.MySql:
case SQLLang.PostgreSql:
default:
if (typeof this.limit == "number") sql += ` LIMIT ${this.limit}`;
if (typeof this.skip == "number") sql += ` OFFSET ${this.skip}`;
break;
}
return sql;
}
asMsSql(){
this.type = SQLLang.MsSql;
let rx = new RegExp("\\?", "g");
let keys = this.parameters.keys();
this.originalWhere = this.where;
this.where = this.where.replace(rx, () => `@${keys.next().value}`);
this.includes.forEach((item) => item.asMsSql());
return this;
}
asOracleSql(){
this.type = SQLLang.Oracle;
let rx = new RegExp("\\?", "g");
let keys = this.parameters.keys();
this.originalWhere = this.where;
this.where = this.where.replace(rx, () => `:${keys.next().value}`);
this.includes.forEach((item) => item.asOracleSql());
return this;
}
asAnsiSql(){
this.type = SQLLang.ANSI;
this.where = this.originalWhere || this.where;
this.includes.forEach((item) => item.asAnsiSql());
return this;
}
asType(){
switch (this.type){
case SQLLang.MsSql: return this.asMsSql();
case SQLLang.ANSI:
case SQLLang.MySql:
case SQLLang.PostgreSql: return this.asAnsiSql();
case SQLLang.Oracle: return this.asOracleSql();
default: return this;
}
}
Visit(node:Token, context?:any){
this.ast = this.ast || node;
context = context || { target: "where" };
if (node){
var visitor = this[`Visit${node.type}`];
if (visitor) visitor.call(this, node, context);
else console.log(`Unhandled node type: ${node.type}`, node);
}
if (node == this.ast){
if (!this.select) this.select = `*`;
if (!this.where) this.where = "1 = 1";
if (!this.orderby) this.orderby = "1";
}
return this;
}
protected VisitODataUri(node:Token, context:any){
this.Visit(node.value.resource, context);
this.Visit(node.value.query, context);
}
protected VisitExpand(node: Token, context: any) {
node.value.items.forEach((item) => {
let expandPath = item.value.path.raw;
let visitor = this.includes.filter(v => v.navigationProperty == expandPath)[0];
if (!visitor){
visitor = new Visitor(this.options);
visitor.parameterSeed = this.parameterSeed;
this.includes.push(visitor);
}
visitor.Visit(item);
this.parameterSeed = visitor.parameterSeed;
});
}
protected VisitExpandItem(node: Token, context: any) {
this.Visit(node.value.path, context);
if (node.value.options) node.value.options.forEach((item) => this.Visit(item, context));
}
protected VisitExpandPath(node: Token, context: any) {
this.navigationProperty = node.raw;
}
protected VisitQueryOptions(node:Token, context:any){
node.value.options.forEach((option) => this.Visit(option, context));
}
protected VisitInlineCount(node:Token, context:any){
this.inlinecount = Literal.convert(node.value.value, node.value.raw);
}
protected VisitFilter(node:Token, context:any){
context.target = "where";
this.Visit(node.value, context);
if (!this.where) this.where = "1 = 1";
}
protected VisitOrderBy(node:Token, context:any){
context.target = "orderby";
node.value.items.forEach((item, i) => {
this.Visit(item, context);
if (i < node.value.items.length - 1) this.orderby += ", ";
});
}
protected VisitOrderByItem(node:Token, context:any){
this.Visit(node.value.expr, context);
this.orderby += node.value.direction > 0 ? " ASC" : " DESC";
}
protected VisitSkip(node:Token, context:any){
this.skip = +node.value.raw;
}
protected VisitTop(node:Token, context:any){
this.limit = +node.value.raw;
}
protected VisitSelect(node:Token, context:any){
context.target = "select";
node.value.items.forEach((item, i) => {
this.Visit(item, context);
if (i < node.value.items.length - 1) this.select += ", ";
});
}
protected VisitSelectItem(node:Token, context:any){
let item = node.raw.replace(/\//g, '.');
this.select += `[${item}]`;
}
protected VisitAndExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " AND ";
this.Visit(node.value.right, context);
}
protected VisitOrExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " OR ";
this.Visit(node.value.right, context);
}
protected VisitBoolParenExpression(node:Token, context:any){
this.where += "(";
this.Visit(node.value, context);
this.where += ")";
}
protected VisitCommonExpression(node:Token, context:any){
this.Visit(node.value, context);
}
protected VisitFirstMemberExpression(node:Token, context:any){
this.Visit(node.value, context);
}
protected VisitMemberExpression(node:Token, context:any){
this.Visit(node.value, context);
}
protected VisitPropertyPathExpression(node:Token, context:any){
if (node.value.current && node.value.next){
this.Visit(node.value.current, context);
context.identifier += ".";
this.Visit(node.value.next, context);
}else this.Visit(node.value, context);
}
protected VisitSingleNavigationExpression(node:Token, context:any){
if (node.value.current && node.value.next){
this.Visit(node.value.current, context);
this.Visit(node.value.next, context);
}else this.Visit(node.value, context);
}
protected VisitODataIdentifier(node:Token, context:any){
this[context.target] += `[${node.value.name}]`;
context.identifier = node.value.name;
}
protected VisitEqualsExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " = ";
this.Visit(node.value.right, context);
if (this.options.useParameters && context.literal == null){
this.where = this.where.replace(/= \?$/, "IS NULL").replace(new RegExp(`\\? = \\[${context.identifier}\\]$`), `[${context.identifier}] IS NULL`);
}else if (context.literal == "NULL"){
this.where = this.where.replace(/= NULL$/, "IS NULL").replace(new RegExp(`NULL = \\[${context.identifier}\\]$`), `[${context.identifier}] IS NULL`);
}
}
protected VisitNotEqualsExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " <> ";
this.Visit(node.value.right, context);
if (this.options.useParameters && context.literal == null){
this.where = this.where.replace(/<> \?$/, "IS NOT NULL").replace(new RegExp(`\\? <> \\[${context.identifier}\\]$`), `[${context.identifier}] IS NOT NULL`);
}else if (context.literal == "NULL"){
this.where = this.where.replace(/<> NULL$/, "IS NOT NULL").replace(new RegExp(`NULL <> \\[${context.identifier}\\]$`), `[${context.identifier}] IS NOT NULL`);
}
}
protected VisitLesserThanExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " < ";
this.Visit(node.value.right, context);
}
protected VisitLesserOrEqualsExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " <= ";
this.Visit(node.value.right, context);
}
protected VisitGreaterThanExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " > ";
this.Visit(node.value.right, context);
}
protected VisitGreaterOrEqualsExpression(node:Token, context:any){
this.Visit(node.value.left, context);
this.where += " >= ";
this.Visit(node.value.right, context);
}
protected VisitLiteral(node:Token, context:any){
if (this.options.useParameters){
let name = `p${this.parameterSeed++}`;
let value = Literal.convert(node.value, node.raw);
context.literal = value;
this.parameters.set(name, value);
this.where += "?";
}else this.where += (context.literal = SQLLiteral.convert(node.value, node.raw));
}
protected VisitMethodCallExpression(node:Token, context:any){
var method = node.value.method;
var params = node.value.parameters || [];
switch (method){
case "contains":
this.Visit(params[0], context);
if (this.options.useParameters){
let name = `p${this.parameterSeed++}`;
let value = Literal.convert(params[1].value, params[1].raw);
this.parameters.set(name, `%${value}%`);
this.where += " like ?";
}else this.where += ` like '%${SQLLiteral.convert(params[1].value, params[1].raw).slice(1, -1)}%'`;
break;
case "endswith":
this.Visit(params[0], context);
if (this.options.useParameters){
let name = `p${this.parameterSeed++}`;
let value = Literal.convert(params[1].value, params[1].raw);
this.parameters.set(name, `%${value}`);
this.where += " like ?";
}else this.where += ` like '%${SQLLiteral.convert(params[1].value, params[1].raw).slice(1, -1)}'`;
break;
case "startswith":
this.Visit(params[0], context);
if (this.options.useParameters){
let name = `p${this.parameterSeed++}`;
let value = Literal.convert(params[1].value, params[1].raw);
this.parameters.set(name, `${value}%`);
this.where += " like ?";
}else this.where += ` like '${SQLLiteral.convert(params[1].value, params[1].raw).slice(1, -1)}%'`;
break;
case "indexof":
let fn = "";
switch (this.type) {
case SQLLang.MsSql:
fn = "CHARINDEX";
break;
case SQLLang.ANSI:
case SQLLang.MySql:
case SQLLang.PostgreSql:
default:
fn = "INSTR";
break;
}
if (fn === "CHARINDEX"){
const tmp = params[0];
params[0] = params[1];
params[1] = tmp;
}
this.where += `${fn}(`;
this.Visit(params[0], context);
this.where += ', ';
this.Visit(params[1], context);
this.where += ") - 1";
break;
case "round":
this.where += "ROUND(";
this.Visit(params[0], context);
this.where += ")";
break;
case "length":
this.where += "LEN(";
this.Visit(params[0], context);
this.where += ")";
break;
case "tolower":
this.where += this.type === SQLLang.MsSql ? "LOWER(" : "LCASE(";
this.Visit(params[0], context);
this.where += ")";
break;
case "toupper":
this.where += this.type === SQLLang.MsSql ? "UPPER(" :"UCASE(";
this.Visit(params[0], context);
this.where += ")";
break;
case "floor":
case "ceiling":
case "year":
case "month":
case "day":
case "hour":
case "minute":
case "second":
this.where += `${method.toUpperCase()}(`;
this.Visit(params[0], context);
this.where += ")";
break;
case "now":
this.where += "NOW()";
break;
case "trim":
this.where += "TRIM(' ' FROM ";
this.Visit(params[0], context);
this.where += ")";
break;
}
}
}