-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathplugin.js
More file actions
672 lines (598 loc) · 22.5 KB
/
plugin.js
File metadata and controls
672 lines (598 loc) · 22.5 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
* @class SqlSupport
* @memberof module:plugins
* @description Allows to export rules as a SQL WHERE statement as well as populating the builder from an SQL query.
* @param {object} [options]
* @param {boolean} [options.boolean_as_integer=true] - `true` to convert boolean values to integer in the SQL output
*/
QueryBuilder.define('sql-support', function(options) {
}, {
boolean_as_integer: true
});
QueryBuilder.defaults({
// operators for internal -> SQL conversion
sqlOperators: {
equal: { op: '= ?' },
not_equal: { op: '!= ?' },
in: { op: 'IN(?)', sep: ', ' },
not_in: { op: 'NOT IN(?)', sep: ', ' },
less: { op: '< ?' },
less_or_equal: { op: '<= ?' },
greater: { op: '> ?' },
greater_or_equal: { op: '>= ?' },
between: { op: 'BETWEEN ?', sep: ' AND ' },
not_between: { op: 'NOT BETWEEN ?', sep: ' AND ' },
begins_with: { op: 'LIKE ?', mod: '{0}%', escape: '%_' },
not_begins_with: { op: 'NOT LIKE ?', mod: '{0}%', escape: '%_' },
contains: { op: 'LIKE ?', mod: '%{0}%', escape: '%_' },
not_contains: { op: 'NOT LIKE ?', mod: '%{0}%', escape: '%_' },
ends_with: { op: 'LIKE ?', mod: '%{0}', escape: '%_' },
not_ends_with: { op: 'NOT LIKE ?', mod: '%{0}', escape: '%_' },
is_empty: { op: '= \'\'' },
is_not_empty: { op: '!= \'\'' },
is_null: { op: 'IS NULL' },
is_not_null: { op: 'IS NOT NULL' }
},
// operators for SQL -> internal conversion
sqlRuleOperator: {
'=': function(v) {
return {
val: v,
op: v === '' ? 'is_empty' : 'equal'
};
},
'!=': function(v) {
return {
val: v,
op: v === '' ? 'is_not_empty' : 'not_equal'
};
},
'LIKE': function(v) {
if (v.slice(0, 1) == '%' && v.slice(-1) == '%') {
return {
val: v.slice(1, -1),
op: 'contains'
};
}
else if (v.slice(0, 1) == '%') {
return {
val: v.slice(1),
op: 'ends_with'
};
}
else if (v.slice(-1) == '%') {
return {
val: v.slice(0, -1),
op: 'begins_with'
};
}
else {
Utils.error('SQLParse', 'Invalid value for LIKE operator "{0}"', v);
}
},
'NOT LIKE': function(v) {
if (v.slice(0, 1) == '%' && v.slice(-1) == '%') {
return {
val: v.slice(1, -1),
op: 'not_contains'
};
}
else if (v.slice(0, 1) == '%') {
return {
val: v.slice(1),
op: 'not_ends_with'
};
}
else if (v.slice(-1) == '%') {
return {
val: v.slice(0, -1),
op: 'not_begins_with'
};
}
else {
Utils.error('SQLParse', 'Invalid value for NOT LIKE operator "{0}"', v);
}
},
'IN': function(v) {
return { val: v, op: 'in' };
},
'NOT IN': function(v) {
return { val: v, op: 'not_in' };
},
'<': function(v) {
return { val: v, op: 'less' };
},
'<=': function(v) {
return { val: v, op: 'less_or_equal' };
},
'>': function(v) {
return { val: v, op: 'greater' };
},
'>=': function(v) {
return { val: v, op: 'greater_or_equal' };
},
'BETWEEN': function(v) {
return { val: v, op: 'between' };
},
'NOT BETWEEN': function(v) {
return { val: v, op: 'not_between' };
},
'IS': function(v) {
if (v !== null) {
Utils.error('SQLParse', 'Invalid value for IS operator');
}
return { val: null, op: 'is_null' };
},
'IS NOT': function(v) {
if (v !== null) {
Utils.error('SQLParse', 'Invalid value for IS operator');
}
return { val: null, op: 'is_not_null' };
}
},
// statements for internal -> SQL conversion
sqlStatements: {
'question_mark': function() {
var params = [];
return {
add: function(rule, value) {
params.push(value);
return '?';
},
run: function() {
return params;
}
};
},
'numbered': function(char) {
if (!char || char.length > 1) char = '$';
var index = 0;
var params = [];
return {
add: function(rule, value) {
params.push(value);
index++;
return char + index;
},
run: function() {
return params;
}
};
},
'named': function(char) {
if (!char || char.length > 1) char = ':';
var indexes = {};
var params = {};
return {
add: function(rule, value) {
if (!indexes[rule.field]) indexes[rule.field] = 1;
var key = rule.field + '_' + (indexes[rule.field]++);
params[key] = value;
return char + key;
},
run: function() {
return params;
}
};
}
},
// statements for SQL -> internal conversion
sqlRuleStatement: {
'question_mark': function(values) {
var index = 0;
return {
parse: function(v) {
return v == '?' ? values[index++] : v;
},
esc: function(sql) {
return sql.replace(/\?/g, '\'?\'');
}
};
},
'numbered': function(values, char) {
if (!char || char.length > 1) char = '$';
var regex1 = new RegExp('^\\' + char + '[0-9]+$');
var regex2 = new RegExp('\\' + char + '([0-9]+)', 'g');
return {
parse: function(v) {
return regex1.test(v) ? values[v.slice(1) - 1] : v;
},
esc: function(sql) {
return sql.replace(regex2, '\'' + (char == '$' ? '$$' : char) + '$1\'');
}
};
},
'named': function(values, char) {
if (!char || char.length > 1) char = ':';
var regex1 = new RegExp('^\\' + char);
var regex2 = new RegExp('\\' + char + '(' + Object.keys(values).join('|') + ')\\b', 'g');
return {
parse: function(v) {
return regex1.test(v) ? values[v.slice(1)] : v;
},
esc: function(sql) {
return sql.replace(regex2, '\'' + (char == '$' ? '$$' : char) + '$1\'');
}
};
}
}
});
/**
* @typedef {object} SqlQuery
* @memberof module:plugins.SqlSupport
* @property {string} sql
* @property {object} params
*/
QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ {
/**
* Returns rules as a SQL query
* @param {boolean|string} [stmt] - use prepared statements: false, 'question_mark', 'numbered', 'numbered(@)', 'named', 'named(@)'
* @param {boolean} [nl=false] output with new lines
* @param {object} [data] - current rules by default
* @returns {module:plugins.SqlSupport.SqlQuery}
* @fires module:plugins.SqlSupport.changer:getSQLField
* @fires module:plugins.SqlSupport.changer:ruleToSQL
* @fires module:plugins.SqlSupport.changer:groupToSQL
* @throws UndefinedSQLConditionError, UndefinedSQLOperatorError
*/
getSQL: function(stmt, nl, data) {
data = (data === undefined) ? this.getRules() : data;
if (!data) {
return null;
}
nl = !!nl ? '\n' : ' ';
var boolean_as_integer = this.getPluginOptions('sql-support', 'boolean_as_integer');
if (stmt === true) {
stmt = 'question_mark';
}
if (typeof stmt == 'string') {
var config = getStmtConfig(stmt);
stmt = this.settings.sqlStatements[config[1]](config[2]);
}
var self = this;
var sql = (function parse(group) {
if (!group.condition) {
group.condition = self.settings.default_condition;
}
if (['AND', 'OR'].indexOf(group.condition.toUpperCase()) === -1) {
Utils.error('UndefinedSQLCondition', 'Unable to build SQL query with condition "{0}"', group.condition);
}
if (!group.rules) {
return '';
}
var parts = [];
group.rules.forEach(function(rule) {
if (rule.rules && rule.rules.length > 0) {
parts.push('(' + nl + parse(rule) + nl + ')' + nl);
}
else {
var sql = self.settings.sqlOperators[rule.operator];
var ope = self.getOperatorByType(rule.operator);
var value = '';
if (sql === undefined) {
Utils.error('UndefinedSQLOperator', 'Unknown SQL operation for operator "{0}"', rule.operator);
}
if (ope.nb_inputs !== 0) {
if (!(rule.value instanceof Array)) {
rule.value = [rule.value];
}
rule.value.forEach(function(v, i) {
if (i > 0) {
value += sql.sep;
}
if (rule.type == 'boolean' && boolean_as_integer) {
v = v ? 1 : 0;
}
else if (!stmt && rule.type !== 'integer' && rule.type !== 'double' && rule.type !== 'boolean') {
v = Utils.escapeString(v, sql.escape);
}
if (sql.mod) {
v = Utils.fmt(sql.mod, v);
}
if (stmt) {
value += stmt.add(rule, v);
}
else {
if (typeof v == 'string') {
v = '\'' + v + '\'';
}
value += v;
}
});
}
var sqlFn = function(v) {
return sql.op.replace('?', function() {
return v;
});
};
/**
* Modifies the SQL field used by a rule
* @event changer:getSQLField
* @memberof module:plugins.SqlSupport
* @param {string} field
* @param {Rule} rule
* @returns {string}
*/
var field = self.change('getSQLField', rule.field, rule);
var ruleExpression = field + ' ' + sqlFn(value);
/**
* Modifies the SQL generated for a rule
* @event changer:ruleToSQL
* @memberof module:plugins.SqlSupport
* @param {string} expression
* @param {Rule} rule
* @param {*} value
* @param {function} valueWrapper - function that takes the value and adds the operator
* @returns {string}
*/
parts.push(self.change('ruleToSQL', ruleExpression, rule, value, sqlFn));
}
});
var groupExpression = parts.join(' ' + group.condition + nl);
/**
* Modifies the SQL generated for a group
* @event changer:groupToSQL
* @memberof module:plugins.SqlSupport
* @param {string} expression
* @param {Group} group
* @returns {string}
*/
return self.change('groupToSQL', groupExpression, group);
}(data));
if (stmt) {
return {
sql: sql,
params: stmt.run()
};
}
else {
return {
sql: sql
};
}
},
/**
* Convert a SQL query to rules
* @param {string|module:plugins.SqlSupport.SqlQuery} query
* @param {boolean|string} stmt
* @returns {object}
* @fires module:plugins.SqlSupport.changer:parseSQLNode
* @fires module:plugins.SqlSupport.changer:getSQLFieldID
* @fires module:plugins.SqlSupport.changer:sqlToRule
* @fires module:plugins.SqlSupport.changer:sqlToGroup
* @throws MissingLibraryError, SQLParseError, UndefinedSQLOperatorError
*/
getRulesFromSQL: function(query, stmt) {
if (!('SQLParser' in window)) {
Utils.error('MissingLibrary', 'SQLParser is required to parse SQL queries. Get it here https://github.com/mistic100/sql-parser');
}
var self = this;
if (typeof query == 'string') {
query = { sql: query };
}
if (stmt === true) stmt = 'question_mark';
if (typeof stmt == 'string') {
var config = getStmtConfig(stmt);
stmt = this.settings.sqlRuleStatement[config[1]](query.params, config[2]);
}
if (stmt) {
query.sql = stmt.esc(query.sql);
}
if (query.sql.toUpperCase().indexOf('SELECT') !== 0) {
query.sql = 'SELECT * FROM table WHERE ' + query.sql;
}
var parsed = SQLParser.parse(query.sql);
if (!parsed.where) {
Utils.error('SQLParse', 'No WHERE clause found');
}
/**
* Custom parsing of an AST node generated by SQLParser, you can return a sub-part of the tree, or a well formed group or rule JSON
* @event changer:parseSQLNode
* @memberof module:plugins.SqlSupport
* @param {object} AST node
* @returns {object} tree, rule or group
*/
var data = self.change('parseSQLNode', parsed.where.conditions);
// a plugin returned a group
if ('rules' in data && 'condition' in data) {
return data;
}
// a plugin returned a rule
if ('id' in data && 'operator' in data && 'value' in data) {
return {
condition: this.settings.default_condition,
rules: [data]
};
}
// create root group
var out = self.change('sqlToGroup', {
condition: this.settings.default_condition,
rules: []
}, data);
// keep track of current group
var curr = out;
(function flatten(data, i) {
if (data === null) {
return;
}
// allow plugins to manually parse or handle special cases
data = self.change('parseSQLNode', data);
// a plugin returned a group
if ('rules' in data && 'condition' in data) {
curr.rules.push(data);
return;
}
// a plugin returned a rule
if ('id' in data && 'operator' in data && 'value' in data) {
curr.rules.push(data);
return;
}
// data must be a SQL parser node
if (!('left' in data) || !('right' in data) || !('operation' in data)) {
Utils.error('SQLParse', 'Unable to parse WHERE clause');
}
// it's a node
if (['AND', 'OR'].indexOf(data.operation.toUpperCase()) !== -1) {
// create a sub-group if the condition is not the same and it's not the first level
/**
* Given an existing group and an AST node, determines if a sub-group must be created
* @event changer:sqlGroupsDistinct
* @memberof module:plugins.SqlSupport
* @param {boolean} create - true by default if the group condition is different
* @param {object} group
* @param {object} AST
* @param {int} current group level
* @returns {boolean}
*/
var createGroup = self.change('sqlGroupsDistinct', i > 0 && curr.condition != data.operation.toUpperCase(), curr, data, i);
if (createGroup) {
/**
* Modifies the group generated from the SQL expression (this is called before the group is filled with rules)
* @event changer:sqlToGroup
* @memberof module:plugins.SqlSupport
* @param {object} group
* @param {object} AST
* @returns {object}
*/
var group = self.change('sqlToGroup', {
condition: self.settings.default_condition,
rules: []
}, data);
curr.rules.push(group);
curr = group;
}
curr.condition = data.operation.toUpperCase();
i++;
// some magic !
var next = curr;
flatten(data.left, i);
curr = next;
flatten(data.right, i);
}
// it's a leaf
else {
if ($.isPlainObject(data.right.value)) {
Utils.error('SQLParse', 'Value format not supported for {0}.', data.left.value);
}
// convert array
var value;
if ($.isArray(data.right.value)) {
value = data.right.value.map(function(v) {
return v.value;
});
}
else {
value = data.right.value;
}
// get actual values
if (stmt) {
if ($.isArray(value)) {
value = value.map(stmt.parse);
}
else {
value = stmt.parse(value);
}
}
// convert operator
var operator = data.operation.toUpperCase();
if (operator == '<>') {
operator = '!=';
}
var sqlrl = self.settings.sqlRuleOperator[operator];
if (sqlrl === undefined) {
Utils.error('UndefinedSQLOperator', 'Invalid SQL operation "{0}".', data.operation);
}
var opVal = sqlrl.call(this, value, data.operation);
// find field name
var field;
if ('values' in data.left) {
field = data.left.values.join('.');
}
else if ('value' in data.left) {
field = data.left.value;
}
else {
Utils.error('SQLParse', 'Cannot find field name in {0}', JSON.stringify(data.left));
}
// unescape chars declared by the operator
var finalValue = opVal.val;
var sql = self.settings.sqlOperators[opVal.op];
if (!stmt && sql && sql.escape) {
var searchChars = sql.escape.split('').map(function(c) {
return '\\\\' + c;
}).join('|');
finalValue = finalValue
.replace(new RegExp('(' + searchChars + ')', 'g'), function(s) {
return s[1];
});
}
var id = self.getSQLFieldID(field, value);
/**
* Modifies the rule generated from the SQL expression
* @event changer:sqlToRule
* @memberof module:plugins.SqlSupport
* @param {object} rule
* @param {object} AST
* @returns {object}
*/
var rule = self.change('sqlToRule', {
id: id,
field: field,
operator: opVal.op,
value: finalValue
}, data);
curr.rules.push(rule);
}
}(data, 0));
return out;
},
/**
* Sets the builder's rules from a SQL query
* @see module:plugins.SqlSupport.getRulesFromSQL
*/
setRulesFromSQL: function(query, stmt) {
this.setRules(this.getRulesFromSQL(query, stmt));
},
/**
* Returns a filter identifier from the SQL field.
* Automatically use the only one filter with a matching field, fires a changer otherwise.
* @param {string} field
* @param {*} value
* @fires module:plugins.SqlSupport:changer:getSQLFieldID
* @returns {string}
* @private
*/
getSQLFieldID: function(field, value) {
var matchingFilters = this.filters.filter(function(filter) {
return filter.field.toLowerCase() === field.toLowerCase();
});
var id;
if (matchingFilters.length === 1) {
id = matchingFilters[0].id;
}
else {
/**
* Returns a filter identifier from the SQL field
* @event changer:getSQLFieldID
* @memberof module:plugins.SqlSupport
* @param {string} field
* @param {*} value
* @returns {string}
*/
id = this.change('getSQLFieldID', field, value);
}
return id;
}
});
/**
* Parses the statement configuration
* @memberof module:plugins.SqlSupport
* @param {string} stmt
* @returns {Array} null, mode, option
* @private
*/
function getStmtConfig(stmt) {
var config = stmt.match(/(question_mark|numbered|named)(?:\((.)\))?/);
if (!config) config = [null, 'question_mark', undefined];
return config;
}