Skip to content

setRulesFromSQL does not work well with negations #641

Closed
@Zanzi92

Description

@Zanzi92

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIdentified bug which needs a fix

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions