Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setRulesFromSQL does not work well with negations #641

Closed
Zanzi92 opened this issue Feb 20, 2018 · 1 comment
Closed

setRulesFromSQL does not work well with negations #641

Zanzi92 opened this issue Feb 20, 2018 · 1 comment
Labels
bug Identified bug which needs a fix
Milestone

Comments

@Zanzi92
Copy link

Zanzi92 commented Feb 20, 2018

We found and solved two issues when using the setRulesFromSQL functionality:

  1. When a "NOT" group contains a single statament, the "NOT" is lost after calling setRulesFromSQL
  2. When a "NOT" group uses the same operator as the external group it is simplified and the "NOT" is lost again.

To replicate these issues we suggest to go to the demo page http://querybuilder.js.org/demo.html and executing this javascript in the inspector console:
var sql_import_export = 'name LIKE "%Johnny%" AND ( NOT (category = 2))'; $('#builder-import_export').queryBuilder('setRulesFromSQL', sql_import_export);

The above was the first issue. The second is:
var sql_import_export = 'name LIKE "%Johnny%" AND ( NOT (category = 2 AND category != 4))'; $('#builder-import_export').queryBuilder('setRulesFromSQL', sql_import_export);

The problem is in the getRulesFromSQL function, in its recursive "flatten" function.
The first problem is solved by adding those lines about at line 5960 where there is the "it's a leaf" comment:

// it's a leaf
            else {
            	// FIX START
            	if (data.not==true) {
					var group = self.change('sqlToGroup', {
                        condition: self.settings.default_condition,
                        rules: []
                    }, data);
                    curr.rules.push(group);
                    curr = group;

                    i++;
                    data.not=false;
                    flatten(data, i);
                    return;
	}
// FIX END

 if ($.isPlainObject(data.right.value)) {
                    Utils.error('SQLParse', 'Value format not supported for {0}.', data.left.value);
                }

The second one is solved by adding the statement ( || data.not==true) at around line 5930:

// 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 if (i > 0 && (curr.condition != data.operation.toUpperCase() || data.not==true)) {

Best regards

@mistic100 mistic100 added the bug Identified bug which needs a fix label Feb 26, 2018
@mistic100 mistic100 added this to the 2.4.6 milestone Feb 26, 2018
@ben-luck
Copy link

There seems to be another issue when using the setRulesFromSQL with the not-group plugin. For example,
$('#queryBuilderGoesHere').queryBuilder('setRulesFromSQL', 'NOT (price>100)'); would change the query to NOT ( ( NOT ( price > 100 ) ) ).

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identified bug which needs a fix
Projects
None yet
Development

No branches or pull requests

3 participants