1
|
|
|
import {parse} from 'node-sqlparser' |
2
|
|
|
import {Promise} from 'es6-promise' |
3
|
|
|
import path from 'path' |
4
|
|
|
import { |
5
|
|
|
config, |
6
|
|
|
Manager, |
7
|
|
|
FileParser, |
8
|
|
|
getAttr |
9
|
|
|
} from '../../' |
10
|
|
|
|
11
|
|
|
export default class Sql { |
12
|
|
|
|
13
|
|
|
constructor() { |
14
|
|
|
|
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
static cleanRequest(str, jsonPage) { |
|
|
|
|
18
|
|
|
var matchFrom = /from .(.*?) / |
19
|
|
|
var matchVariable = /{{([a-zA-Z]*)}}/ |
20
|
|
|
|
21
|
|
|
var matchFromExec = matchFrom.exec(str) |
22
|
|
|
if(typeof matchFromExec !== 'undefined' && matchFromExec !== null |
23
|
|
|
&& typeof matchFromExec[1] !== 'undefined' && matchFromExec[1] !== null) { |
24
|
|
|
|
25
|
|
|
var fromMatch |
26
|
|
|
var toReplace = matchFromExec[1] |
27
|
|
|
while (fromMatch = matchVariable.exec(toReplace)) { |
28
|
|
|
try { |
29
|
|
|
var value = eval('jsonPage.' + fromMatch[1]) |
|
|
|
|
30
|
|
|
if(typeof value !== 'undefined' && value !== null) { |
31
|
|
|
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', value) |
32
|
|
|
}else { |
33
|
|
|
toReplace = toReplace.replace('{{' + fromMatch[1] + '}}', '') |
34
|
|
|
} |
35
|
|
|
}catch(e) { |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
str = str.replace(matchFromExec[1], toReplace) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
var from = /from ([\S\s]+)/.exec(str) |
43
|
|
|
|
44
|
|
|
var matches = from |
45
|
|
|
if(matches[1]) { |
46
|
|
|
var res = matches[1] |
47
|
|
|
var splitAttr = [' where ', ' order by ', ' limit ', ' WHERE ', ' ORDER BY ', ' LIMIT '] |
48
|
|
|
for(var i = 0; i < splitAttr.length; i++) { |
49
|
|
|
if(res.indexOf(splitAttr[i]) > -1) { |
50
|
|
|
res = res.substring(0, res.indexOf(splitAttr[i])) |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
var escapedFrom = res.replace(/\//g, '___abe___') |
54
|
|
|
escapedFrom = escapedFrom.replace(/\./g, '___abe_dot___') |
55
|
|
|
escapedFrom = escapedFrom.replace(/-/g, '___abe_dash___') |
56
|
|
|
str = str.replace(res, escapedFrom) |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
str = str.replace(/``/g, '\'\'') |
60
|
|
|
|
61
|
|
|
return str |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
static handleSqlRequest(str, jsonPage) { |
65
|
|
|
var cleanRequest = Sql.cleanRequest(str, jsonPage) |
66
|
|
|
var request = parse(cleanRequest) |
67
|
|
|
var reconstructSql = '' |
68
|
|
|
|
69
|
|
|
// SQL TYPE |
70
|
|
|
var type = '' |
71
|
|
|
if(typeof request.type !== 'undefined' && request.type !== null) { |
72
|
|
|
type = request.type |
73
|
|
|
} |
74
|
|
|
reconstructSql += `${type} ` |
75
|
|
|
|
76
|
|
|
// SQL COLUMNS |
77
|
|
|
var columns = [] |
78
|
|
|
if(typeof request.columns !== 'undefined' && request.columns !== null) { |
79
|
|
|
if(request.columns === '*') { |
80
|
|
|
columns.push('*') |
81
|
|
|
}else { |
82
|
|
|
Array.prototype.forEach.call(request.columns, (item) => { |
83
|
|
|
columns.push(item.expr.column) |
84
|
|
|
}) |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
reconstructSql += `${JSON.stringify(columns)} ` |
88
|
|
|
|
89
|
|
|
// SQL FROM |
90
|
|
|
var from = [] |
91
|
|
|
if(typeof request.from !== 'undefined' && request.from !== null) { |
92
|
|
|
|
93
|
|
|
Array.prototype.forEach.call(request.from, (item) => { |
94
|
|
|
from.push(item.table) |
95
|
|
|
}) |
96
|
|
|
}else { |
97
|
|
|
from.push('*') |
98
|
|
|
} |
99
|
|
|
reconstructSql += `from ${JSON.stringify(from)} ` |
100
|
|
|
|
101
|
|
|
var where |
102
|
|
|
if(typeof request.where !== 'undefined' && request.where !== null) { |
103
|
|
|
where = request.where |
104
|
|
|
// where = Sql.recurseWhere(request.where) |
105
|
|
|
// reconstructSql += 'where ' |
106
|
|
|
// Array.prototype.forEach.call(where, (w) => { |
107
|
|
|
// reconstructSql += `${w.operator} ${w.left} ${w.compare} ${w.right} ` |
108
|
|
|
// }) |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
var limit = -1 |
112
|
|
|
if(typeof request.limit !== 'undefined' && request.limit !== null) { |
113
|
|
|
limit = request.limit[request.limit.length - 1].value |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
var orderby |
117
|
|
|
if(typeof request.orderby !== 'undefined' && request.orderby !== null && request.orderby.length > 0) { |
118
|
|
|
orderby = { |
119
|
|
|
column: request.orderby[0].expr.column, |
120
|
|
|
type: request.orderby[0].type |
121
|
|
|
} |
122
|
|
|
reconstructSql += `ORDER BY ${orderby.column} ${orderby.type} ` |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return { |
126
|
|
|
type: type, |
127
|
|
|
columns: columns, |
128
|
|
|
from: from, |
129
|
|
|
where: where, |
|
|
|
|
130
|
|
|
string: reconstructSql, |
131
|
|
|
limit: limit, |
132
|
|
|
orderby: orderby |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
static sortByDateDesc(a, b) { |
137
|
|
|
var dateA = new Date(a.date) |
138
|
|
|
var dateB = new Date(b.date) |
139
|
|
|
if(dateA < dateB) { |
140
|
|
|
return 1 |
141
|
|
|
}else if(dateA > dateB) { |
142
|
|
|
return -1 |
143
|
|
|
} |
144
|
|
|
return 0 |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
static shuffle(array) { |
148
|
|
|
var currentIndex = array.length, temporaryValue, randomIndex |
149
|
|
|
|
150
|
|
|
// While there remain elements to shuffle... |
151
|
|
|
while (0 !== currentIndex) { |
152
|
|
|
|
153
|
|
|
// Pick a remaining element... |
154
|
|
|
randomIndex = Math.floor(Math.random() * currentIndex) |
155
|
|
|
currentIndex -= 1 |
156
|
|
|
|
157
|
|
|
// And swap it with the current element. |
158
|
|
|
temporaryValue = array[currentIndex] |
159
|
|
|
array[currentIndex] = array[randomIndex] |
160
|
|
|
array[randomIndex] = temporaryValue |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return array |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
static sortByDateAsc(a, b) { |
167
|
|
|
var dateA = new Date(a.date) |
168
|
|
|
var dateB = new Date(b.date) |
169
|
|
|
if(dateA > dateB) { |
170
|
|
|
return 1 |
171
|
|
|
}else if(dateA < dateB) { |
172
|
|
|
return -1 |
173
|
|
|
} |
174
|
|
|
return 0 |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
static getDataSource(str) { |
178
|
|
|
var res = str.substring(str.indexOf('source=') + 8, str.length) |
179
|
|
|
|
180
|
|
|
var reg = /([^'"]*=[\s\S]*?}})/g |
181
|
|
|
var matches = res.match(reg) |
182
|
|
|
if(typeof matches !== 'undefined' && matches !== null) { |
183
|
|
|
Array.prototype.forEach.call(matches, (match) => { |
184
|
|
|
res = res.replace(match, '') |
185
|
|
|
}) |
186
|
|
|
}else { |
187
|
|
|
res = res.replace('}}', '') |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return res.substring(0, res.length-1) |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* replaces escaped characters with the right ones |
195
|
|
|
* @param {String} statement the from clause |
196
|
|
|
* @return {String} the from sanitized |
197
|
|
|
*/ |
198
|
|
|
static sanitizeFromStatement(statement){ |
199
|
|
|
var from = '' |
200
|
|
|
|
201
|
|
|
if(typeof statement !== 'undefined' && statement !== null) { |
202
|
|
|
from = statement[0].replace(/___abe_dot___/g, '.') |
203
|
|
|
from = from.replace(/___abe___/g, '/') |
204
|
|
|
from = from.replace(/___abe_dash___/g, '-') |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return from |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* calculate the directory to analyze from the from clause |
212
|
|
|
* @param {String} statement the from clause |
213
|
|
|
* @param {String} tplPath the path from the template originator |
214
|
|
|
* @return {string} the directory to analyze |
215
|
|
|
*/ |
216
|
|
|
static getFromDirectory(statement, tplPath){ |
217
|
|
|
var pathFromDir = '' |
218
|
|
|
if(typeof tplPath === 'undefined' || tplPath === null || tplPath === ''){ |
219
|
|
|
tplPath = '/' |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if(statement === '' || statement === '*' || statement === '/') { |
223
|
|
|
pathFromDir = path.join(config.root, config.data.url) |
224
|
|
|
}else if(statement === './') { |
225
|
|
|
pathFromDir = path.join(config.root, config.data.url, tplPath) |
226
|
|
|
}else if(statement.indexOf('/') === 0) { |
227
|
|
|
pathFromDir = path.join(config.root, config.data.url, statement) |
228
|
|
|
}else if(statement.indexOf('/') !== 0) { |
229
|
|
|
pathFromDir = path.join(config.root, config.data.url, tplPath, statement) |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return pathFromDir |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
static executeOrderByClause(files, orderby){ |
236
|
|
|
if(typeof orderby !== 'undefined' && orderby !== null) { |
237
|
|
|
if(orderby.column.toLowerCase() === 'random') { |
238
|
|
|
Sql.shuffle(files) |
239
|
|
|
}else if(orderby.column.toLowerCase() === 'date') { |
240
|
|
|
if(orderby.type === 'ASC') { |
241
|
|
|
files.sort(Sql.sortByDateAsc) |
242
|
|
|
}else if(orderby.type === 'DESC') { |
243
|
|
|
files.sort(Sql.sortByDateDesc) |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return files |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
static executeFromClause(statement, pathFromClause){ |
252
|
|
|
var from = Sql.sanitizeFromStatement(statement) |
253
|
|
|
|
254
|
|
|
// if the from clause ends with a dot, we won't recurse the directory analyze |
255
|
|
|
if(from.slice(-1) === '.'){ |
256
|
|
|
from = from.slice(0, -1) |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
var fromDirectory = Sql.getFromDirectory(from, pathFromClause) |
260
|
|
|
|
261
|
|
|
var list = Manager.instance.getList() |
262
|
|
|
var files_array = list.filter((element, index) => { |
|
|
|
|
263
|
|
|
if(element.publish) { |
264
|
|
|
if (element.path.indexOf(fromDirectory) > -1) { |
265
|
|
|
return true |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
return false |
269
|
|
|
}) |
270
|
|
|
return files_array |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
static execQuery(pathQuery, match, jsonPage) { |
274
|
|
|
var res |
275
|
|
|
var files |
276
|
|
|
var request = Sql.handleSqlRequest(getAttr(match, 'source'), jsonPage) |
277
|
|
|
|
278
|
|
|
files = Sql.executeFromClause(request.from, pathQuery) |
279
|
|
|
files = Sql.executeOrderByClause(files, request.orderby) |
280
|
|
|
res = Sql.executeWhereClause(files, request.where, request.limit, request.columns, jsonPage) |
281
|
|
|
|
282
|
|
|
return res |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
static executeQuerySync(pathQuerySync, match, jsonPage) { |
286
|
|
|
return Sql.execQuery(pathQuerySync, match, jsonPage) |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
static executeQuery(pathexecuteQuery, match, jsonPage) { |
290
|
|
|
var p = new Promise((resolve) => { |
291
|
|
|
var res = Sql.execQuery(pathexecuteQuery, match, jsonPage) |
292
|
|
|
resolve(res) |
293
|
|
|
}).catch(function(e) { |
294
|
|
|
console.error(e) |
295
|
|
|
}) |
296
|
|
|
|
297
|
|
|
return p |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* check if a given string an url, string json, file url, abe sql request |
302
|
|
|
* |
303
|
|
|
* Sql.get('http://google.com') |
304
|
|
|
* Sql.get('{"test":"test"}') |
305
|
|
|
* Sql.get('select * from ../') |
306
|
|
|
* Sql.get('test') |
307
|
|
|
* |
308
|
|
|
* @param {String} str |
309
|
|
|
* @return {String} url | request | value | file | other |
310
|
|
|
*/ |
311
|
|
|
static getSourceType(str) { |
312
|
|
|
if(/http:\/\/|https:\/\//.test(str)) { |
313
|
|
|
return 'url' |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
if(/select[\S\s]*?from/.test(str)) { |
317
|
|
|
return 'request' |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
try { |
321
|
|
|
JSON.parse(str) |
322
|
|
|
return 'value' |
323
|
|
|
}catch(e) { |
|
|
|
|
324
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if(/\.json/.test(str)) { |
328
|
|
|
return 'file' |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return 'other' |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
static executeWhereClause(files, wheres, maxLimit, columns, jsonPage){ |
335
|
|
|
var res = [] |
336
|
|
|
var limit = 0 |
337
|
|
|
|
338
|
|
|
for(let file of files) { |
339
|
|
|
if(limit < maxLimit || maxLimit === -1) { |
340
|
|
|
if(typeof wheres !== 'undefined' && wheres !== null) { |
341
|
|
|
|
342
|
|
|
if(Sql.recurseWhere(wheres, file.publish, jsonPage)) { |
343
|
|
|
var json = JSON.parse(JSON.stringify(file.publish)) |
344
|
|
|
var jsonValues = {} |
345
|
|
|
|
346
|
|
|
if(typeof columns !== 'undefined' && columns !== null && columns.length > 0 && columns[0] !== '*') { |
347
|
|
|
|
348
|
|
|
Array.prototype.forEach.call(columns, (column) => { |
349
|
|
|
if(typeof json[column] !== 'undefined' && json[column] !== null) { |
|
|
|
|
350
|
|
|
jsonValues[column] = json[column] |
|
|
|
|
351
|
|
|
} |
352
|
|
|
}) |
353
|
|
|
jsonValues['abe_meta'] = json['abe_meta'] |
354
|
|
|
}else { |
355
|
|
|
jsonValues = json |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
res.push(jsonValues) |
359
|
|
|
limit++ |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} else { |
363
|
|
|
break |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return res |
368
|
|
|
} |
369
|
|
|
|
370
|
|
View Code Duplication |
static whereEquals(where, value, compare) { |
|
|
|
|
371
|
|
|
var shouldAdd = true |
372
|
|
|
if(where === 'template' || where === 'abe_meta.template') { |
373
|
|
|
if(value && value.indexOf('/') > -1 && value !== compare) { |
374
|
|
|
shouldAdd = false |
375
|
|
|
}else if(value && value.indexOf('/') === -1 && compare && compare.indexOf(value) === -1) { |
376
|
|
|
shouldAdd = false |
377
|
|
|
} |
378
|
|
|
}else { |
379
|
|
|
if(value !== compare) { // only none is Array |
380
|
|
|
shouldAdd = false |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
return shouldAdd |
384
|
|
|
} |
385
|
|
|
|
386
|
|
View Code Duplication |
static whereNotEquals(where, value, compare, json) { |
|
|
|
|
387
|
|
|
var shouldAdd = true |
388
|
|
|
if(where.left === 'template' || where.left === 'abe_meta.template') { |
389
|
|
|
if (value && value.indexOf('/') > -1 && value === compare) { |
390
|
|
|
shouldAdd = false |
391
|
|
|
} else if (value && value.indexOf('/') === -1 && compare && compare.indexOf(value) !== -1) { |
392
|
|
|
shouldAdd = false |
393
|
|
|
} |
394
|
|
|
}else { |
395
|
|
|
if(value === compare) { // only none is Array |
396
|
|
|
shouldAdd = false |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
return shouldAdd |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
static whereLike(where, value, compare, json) { |
|
|
|
|
403
|
|
|
var shouldAdd = true |
404
|
|
|
if(where.left === 'template' || where.left === 'abe_meta.template') { |
405
|
|
|
if(value && value.indexOf(compare) === -1) { |
406
|
|
|
shouldAdd = false |
407
|
|
|
} |
408
|
|
|
}else { |
409
|
|
|
if(value && value.indexOf(compare) === -1) { |
410
|
|
|
shouldAdd = false |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
return shouldAdd |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
static whereNotLike(where, value, compare, json) { |
|
|
|
|
417
|
|
|
var shouldAdd = true |
418
|
|
|
if(where.left === 'template' || where.left === 'abe_meta.template') { |
419
|
|
|
if(value && value.indexOf(compare) >= -1) { |
420
|
|
|
shouldAdd = false |
421
|
|
|
} |
422
|
|
|
}else { |
423
|
|
|
if(value && value.indexOf(compare) > -1) { |
424
|
|
|
shouldAdd = false |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
return shouldAdd |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
static getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) { |
|
|
|
|
431
|
|
|
var value |
432
|
|
|
var compare |
433
|
|
|
|
434
|
|
|
if((where.left.column === 'template' || where.left.column === 'abe_meta.template') |
435
|
|
|
&& typeof jsonDoc['abe_meta'] !== 'undefined' && jsonDoc['abe_meta'] !== null) { |
436
|
|
|
value = FileParser.getTemplate(jsonDoc['abe_meta'].template) |
437
|
|
|
}else { |
438
|
|
|
try { |
439
|
|
|
value = eval('jsonDoc.' + where.left.column) |
|
|
|
|
440
|
|
|
}catch(e) { |
441
|
|
|
// console.log('e', e) |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
compare = where.right.column |
445
|
|
|
|
446
|
|
|
var matchVariable = /^{{(.*)}}$/.exec(compare) |
447
|
|
|
if(typeof matchVariable !== 'undefined' && matchVariable !== null && matchVariable.length > 0) { |
448
|
|
|
try { |
449
|
|
|
var shouldCompare = eval('jsonOriginalDoc.' + matchVariable[1]) |
|
|
|
|
450
|
|
|
if(typeof shouldCompare !== 'undefined' && shouldCompare !== null) { |
451
|
|
|
compare = shouldCompare |
452
|
|
|
}else { |
453
|
|
|
compare = null |
454
|
|
|
} |
455
|
|
|
}catch(e) { |
456
|
|
|
compare = null |
457
|
|
|
// console.log('e', e) |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return { |
462
|
|
|
left: value, |
463
|
|
|
right: compare |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
static recurseWhere(where, jsonDoc, jsonOriginalDoc) { |
468
|
|
|
var shouldAdd = true |
|
|
|
|
469
|
|
|
var isLeftCorrect = false |
|
|
|
|
470
|
|
|
var isRightCorrect = false |
|
|
|
|
471
|
|
|
var isCorrect = false |
|
|
|
|
472
|
|
|
|
473
|
|
|
switch(where.operator) { |
|
|
|
|
474
|
|
|
case '=': |
475
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
476
|
|
|
isCorrect = Sql.whereEquals(where.left.column, values.left, values.right) |
477
|
|
|
break |
478
|
|
|
case '!=': |
479
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
480
|
|
|
isCorrect = Sql.whereNotEquals(where.left.column, values.left, values.right) |
481
|
|
|
break |
482
|
|
|
case '>': |
483
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
484
|
|
|
isCorrect = (values.left > values.right) |
485
|
|
|
break |
486
|
|
|
case '>=': |
487
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
488
|
|
|
isCorrect = (values.left >= values.right) |
489
|
|
|
break |
490
|
|
|
case '<': |
491
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
492
|
|
|
isCorrect = (values.left < values.right) |
493
|
|
|
break |
494
|
|
|
case '<=': |
495
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
496
|
|
|
isCorrect = (values.left <= values.right) |
497
|
|
|
break |
498
|
|
|
case 'LIKE': |
499
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
500
|
|
|
isCorrect = Sql.whereLike(where.left.column, values.left, values.right) |
501
|
|
|
break |
502
|
|
|
case 'NOT LIKE': |
503
|
|
|
var values = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
504
|
|
|
isCorrect = Sql.whereNotLike(where.left.column, values.left, values.right) |
505
|
|
|
break |
506
|
|
|
case 'AND': |
507
|
|
|
isLeftCorrect = Sql.recurseWhere(where.left, jsonDoc, jsonOriginalDoc) |
508
|
|
|
isRightCorrect = Sql.recurseWhere(where.right, jsonDoc, jsonOriginalDoc) |
509
|
|
|
isCorrect = isLeftCorrect && isRightCorrect |
510
|
|
|
break |
511
|
|
|
case 'OR': |
512
|
|
|
isLeftCorrect = Sql.recurseWhere(where.left, jsonDoc, jsonOriginalDoc) |
513
|
|
|
isRightCorrect = Sql.recurseWhere(where.right, jsonDoc, jsonOriginalDoc) |
514
|
|
|
isCorrect = isLeftCorrect || isRightCorrect |
515
|
|
|
break |
516
|
|
|
case 'IN': |
517
|
|
|
var valuesLeft = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
518
|
|
|
Array.prototype.forEach.call(where.right.value, (right) => { |
519
|
|
|
if (Sql.whereEquals(where.left.column, valuesLeft.left, right.column)) { |
520
|
|
|
isCorrect = true |
521
|
|
|
} |
522
|
|
|
}) |
523
|
|
|
break |
524
|
|
|
case 'NOT IN': |
525
|
|
|
var valuesLeft = Sql.getWhereValuesToCompare(where, jsonDoc, jsonOriginalDoc) |
|
|
|
|
526
|
|
|
isCorrect = true |
527
|
|
|
Array.prototype.forEach.call(where.right.value, (right) => { |
528
|
|
|
if (Sql.whereEquals(where.left.column, valuesLeft.left, right.column)) { |
529
|
|
|
isCorrect = false |
530
|
|
|
} |
531
|
|
|
}) |
532
|
|
|
break |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
return isCorrect |
536
|
|
|
} |
537
|
|
|
} |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.