Skip to content

Commit c50ebf8

Browse files
committed
Fix #687 : Negation doubled when using setRulesFromSQL
1 parent 87605e2 commit c50ebf8

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

src/plugins/not-group/plugin.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ QueryBuilder.define('not-group', function(options) {
6666

6767
// if the there is no sub-group, create one
6868
if (['AND', 'OR'].indexOf(e.value.operation.toUpperCase()) === -1) {
69-
e.value = {
70-
left: e.value,
71-
operation: self.settings.default_condition,
72-
right: null
73-
};
69+
e.value = new SQLParser.nodes.Op(
70+
self.settings.default_condition,
71+
e.value,
72+
null
73+
);
7474
}
7575

7676
e.value.not = true;
7777
}
7878
});
7979

8080
// Request to create sub-group if the "not" flag is set
81-
this.on('sqlGroupsDistinct.filter', function(e, group, data) {
82-
if (data.not) {
81+
this.on('sqlGroupsDistinct.filter', function(e, group, data, i) {
82+
if (data.not && i > 0) {
8383
e.value = true;
8484
}
8585
});

src/plugins/sql-support/plugin.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,13 @@ QueryBuilder.extend(/** @lends module:plugins.SqlSupport.prototype */ {
491491
* Given an existing group and an AST node, determines if a sub-group must be created
492492
* @event changer:sqlGroupsDistinct
493493
* @memberof module:plugins.SqlSupport
494-
* @param {boolean} create - try by default if the group condition is different
494+
* @param {boolean} create - true by default if the group condition is different
495495
* @param {object} group
496496
* @param {object} AST
497+
* @param {int} current group level
497498
* @returns {boolean}
498499
*/
499-
var createGroup = self.change('sqlGroupsDistinct', i > 0 && curr.condition != data.operation.toUpperCase(), curr, data);
500+
var createGroup = self.change('sqlGroupsDistinct', i > 0 && curr.condition != data.operation.toUpperCase(), curr, data, i);
500501

501502
if (createGroup) {
502503
/**

tests/plugins.not-group.module.js

+61-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ $(function () {
5959
sql,
6060
'Should export SQL with NOT function'
6161
);
62+
});
6263

63-
$b.queryBuilder('reset');
64+
QUnit.test('SQL import', function (assert) {
65+
$b.queryBuilder({
66+
filters: basic_filters,
67+
plugins: ['not-group']
68+
});
6469

6570
$b.queryBuilder('setRulesFromSQL', sql);
6671

@@ -85,6 +90,22 @@ $(function () {
8590
rules3,
8691
'Should parse NOT SQL function with same operation'
8792
);
93+
94+
$b.queryBuilder('setRulesFromSQL', sql4);
95+
96+
assert.rulesMatch(
97+
$b.queryBuilder('getRules'),
98+
rules4,
99+
'Should parse NOT SQL function with negated root group'
100+
);
101+
102+
$b.queryBuilder('setRulesFromSQL', sql5);
103+
104+
assert.rulesMatch(
105+
$b.queryBuilder('getRules'),
106+
rules5,
107+
'Should parse NOT SQL function with double negated root group'
108+
);
88109
});
89110

90111
QUnit.test('Mongo export', function (assert) {
@@ -113,6 +134,7 @@ $(function () {
113134

114135
var rules = {
115136
condition: 'OR',
137+
not: false,
116138
rules: [{
117139
id: 'name',
118140
operator: 'equal',
@@ -137,6 +159,7 @@ $(function () {
137159

138160
var rules2 = {
139161
condition: 'OR',
162+
not: false,
140163
rules: [{
141164
id: 'name',
142165
operator: 'equal',
@@ -156,6 +179,7 @@ $(function () {
156179

157180
var rules3 = {
158181
condition: 'OR',
182+
not: false,
159183
rules: [{
160184
id: 'name',
161185
operator: 'equal',
@@ -178,6 +202,42 @@ $(function () {
178202

179203
var sql3 = 'name = \'Mistic\' OR ( NOT ( price < 10.25 OR category IN(\'mo\', \'mu\') ) ) ';
180204

205+
var rules4 = {
206+
condition: 'AND',
207+
not: true,
208+
rules: [{
209+
id: 'price',
210+
operator: 'less',
211+
value: 10.25
212+
}]
213+
};
214+
215+
var sql4 = 'NOT ( price < 10.25 )';
216+
217+
var rules5 = {
218+
condition: 'AND',
219+
not: false,
220+
rules: [{
221+
condition: 'AND',
222+
not: true,
223+
rules: [{
224+
id: 'price',
225+
operator: 'less',
226+
value: 10.25
227+
}]
228+
}, {
229+
condition: 'AND',
230+
not: true,
231+
rules: [{
232+
id: 'price',
233+
operator: 'greater',
234+
value: 20.5
235+
}]
236+
}]
237+
};
238+
239+
var sql5 = 'NOT ( price < 10.25 ) AND NOT ( price > 20.5 )';
240+
181241
var mongo = {
182242
"$or": [{
183243
"name": "Mistic"

0 commit comments

Comments
 (0)