Commit 7e9921a4 authored by Tristan Cavelier's avatar Tristan Cavelier

Queries: Fix some parsing/serialization bug

- fixes a:( b OR c ) -> ( a:b OR a:c )
- fixes NOT ( b ) -> ( b )
- fixes query toString inconsistent output
parent 0500b645
...@@ -90,13 +90,13 @@ case 5: case 8: case 11: case 14: case 16: ...@@ -90,13 +90,13 @@ case 5: case 8: case 11: case 14: case 16:
this.$ = $$[$0]; this.$ = $$[$0];
break; break;
case 6: case 6:
this.$ = mkComplexQuery('AND', [$$[$0-1], $$[$0]]); this.$ = mkComplexQuery('', 'AND', [$$[$0-1], $$[$0]]);
break; break;
case 7: case 7:
this.$ = mkComplexQuery('OR', [$$[$0-2], $$[$0]]); this.$ = mkComplexQuery('', 'OR', [$$[$0-2], $$[$0]]);
break; break;
case 9: case 9:
this.$ = mkComplexQuery('AND', [$$[$0-2], $$[$0]]); this.$ = mkComplexQuery('', 'AND', [$$[$0-2], $$[$0]]);
break; break;
case 10: case 10:
this.$ = mkNotQuery($$[$0]); this.$ = mkNotQuery($$[$0]);
...@@ -105,7 +105,7 @@ case 12: ...@@ -105,7 +105,7 @@ case 12:
this.$ = $$[$0-1]; this.$ = $$[$0-1];
break; break;
case 13: case 13:
simpleQuerySetKey($$[$0], $$[$0-2]); this.$ = $$[$0]; querySetKey($$[$0], $$[$0-2]); this.$ = $$[$0];
break; break;
case 15: case 15:
$$[$0].operator = $$[$0-1] ; this.$ = $$[$0]; $$[$0].operator = $$[$0-1] ; this.$ = $$[$0];
......
...@@ -45,13 +45,13 @@ end ...@@ -45,13 +45,13 @@ end
search_text search_text
: and_expression { $$ = $1; } : and_expression { $$ = $1; }
| and_expression search_text { $$ = mkComplexQuery('AND', [$1, $2]); } | and_expression search_text { $$ = mkComplexQuery('', 'AND', [$1, $2]); }
| and_expression OR search_text { $$ = mkComplexQuery('OR', [$1, $3]); } | and_expression OR search_text { $$ = mkComplexQuery('', 'OR', [$1, $3]); }
; ;
and_expression and_expression
: boolean_expression { $$ = $1; } : boolean_expression { $$ = $1; }
| boolean_expression AND and_expression { $$ = mkComplexQuery('AND', [$1, $3]); } | boolean_expression AND and_expression { $$ = mkComplexQuery('', 'AND', [$1, $3]); }
; ;
boolean_expression boolean_expression
...@@ -61,7 +61,7 @@ boolean_expression ...@@ -61,7 +61,7 @@ boolean_expression
expression expression
: LEFT_PARENTHESE search_text RIGHT_PARENTHESE { $$ = $2; } : LEFT_PARENTHESE search_text RIGHT_PARENTHESE { $$ = $2; }
| WORD DEFINITION expression { simpleQuerySetKey($3, $1); $$ = $3; } | WORD DEFINITION expression { querySetKey($3, $1); $$ = $3; }
| value { $$ = $1; } | value { $$ = $1; }
; ;
......
...@@ -26,9 +26,9 @@ var arrayExtend = function () { ...@@ -26,9 +26,9 @@ var arrayExtend = function () {
if (query.operator === "NOT") { if (query.operator === "NOT") {
return query.query_list[0]; return query.query_list[0];
} }
return {"type": "complex", "operator": "NOT", "query_list": [query]}; return {"type": "complex", "key": "", "operator": "NOT", "query_list": [query]};
}, mkComplexQuery = function (operator, query_list) { }, mkComplexQuery = function (key, operator, query_list) {
var i, query_list2 = []; var i, query_list2 = [];
for (i = 0; i < query_list.length; i += 1) { for (i = 0; i < query_list.length; i += 1) {
if (query_list[i].operator === operator) { if (query_list[i].operator === operator) {
...@@ -37,17 +37,10 @@ var arrayExtend = function () { ...@@ -37,17 +37,10 @@ var arrayExtend = function () {
query_list2.push(query_list[i]); query_list2.push(query_list[i]);
} }
} }
return {type:"complex",operator:operator,query_list:query_list2}; return {type:"complex",key:key,operator:operator,query_list:query_list2};
}, simpleQuerySetKey = function (query, key) { }, querySetKey = function (query, key) {
var i; if (({simple: 1, complex: 1})[query.type] && !query.key) {
if (query.type === "complex") {
for (i = 0; i < query.query_list.length; ++i) {
simpleQuerySetKey (query.query_list[i],key);
}
return true;
}
if (query.type === "simple" && !query.key) {
query.key = key; query.key = key;
return true; return true;
} }
......
...@@ -519,6 +519,8 @@ ...@@ -519,6 +519,8 @@
*/ */
this.operator = spec.operator; this.operator = spec.operator;
this.key = spec.key || this.key;
/** /**
* The sub Query list which are used to query an item. * The sub Query list which are used to query an item.
* *
...@@ -538,6 +540,7 @@ ...@@ -538,6 +540,7 @@
ComplexQuery.prototype.operator = "AND"; ComplexQuery.prototype.operator = "AND";
ComplexQuery.prototype.type = "complex"; ComplexQuery.prototype.type = "complex";
ComplexQuery.prototype.key = "";
/** /**
* #crossLink "Query/match:method" * #crossLink "Query/match:method"
...@@ -554,21 +557,8 @@ ...@@ -554,21 +557,8 @@
* #crossLink "Query/toString:method" * #crossLink "Query/toString:method"
*/ */
ComplexQuery.prototype.toString = function () { ComplexQuery.prototype.toString = function () {
var str_list = [], this_operator = this.operator; /*global objectToSearchText */
if (this.operator === "NOT") { return objectToSearchText(this.toJSON());
str_list.push("NOT (");
str_list.push(this.query_list[0].toString());
str_list.push(")");
return str_list.join(" ");
}
this.query_list.forEach(function (query) {
str_list.push("(");
str_list.push(query.toString());
str_list.push(")");
str_list.push(this_operator);
});
str_list.length -= 1;
return str_list.join(" ");
}; };
/** /**
...@@ -578,6 +568,7 @@ ...@@ -578,6 +568,7 @@
var s = { var s = {
"type": "complex", "type": "complex",
"operator": this.operator, "operator": this.operator,
"key": this.key,
"query_list": [] "query_list": []
}; };
this.query_list.forEach(function (query) { this.query_list.forEach(function (query) {
...@@ -667,12 +658,26 @@ ...@@ -667,12 +658,26 @@
}; };
function objectToSearchText(query) { function objectToSearchText(query) {
var str_list = []; var str_list = [], operator = "", query_list = null;
if (query.type === "complex") { if (query.type === "complex") {
query_list = query.query_list || [];
if (query_list.length === 0) {
return "";
}
operator = query.operator;
if (operator === "NOT") {
str_list.push("NOT");
// fallback to AND operator if several queries are given
// i.e. `NOT ( a AND b )`
operator = "AND";
}
if (query.key) {
str_list.push(query.key + ":");
}
str_list.push("("); str_list.push("(");
(query.query_list || []).forEach(function (sub_query) { query_list.forEach(function (sub_query) {
str_list.push(objectToSearchText(sub_query)); str_list.push(objectToSearchText(sub_query));
str_list.push(query.operator); str_list.push(operator);
}); });
str_list.length -= 1; str_list.length -= 1;
str_list.push(")"); str_list.push(")");
...@@ -849,8 +854,7 @@ ...@@ -849,8 +854,7 @@
* #crossLink "Query/toString:method" * #crossLink "Query/toString:method"
*/ */
SimpleQuery.prototype.toString = function () { SimpleQuery.prototype.toString = function () {
return (this.key ? this.key + ":" : "") + return objectToSearchText(this.toJSON());
(this.operator ? " " + this.operator : "") + ' "' + this.value + '"';
}; };
/** /**
......
...@@ -258,9 +258,11 @@ ...@@ -258,9 +258,11 @@
{ {
"type": "complex", "type": "complex",
"operator": "NOT", "operator": "NOT",
"key": "",
"query_list": [{ "query_list": [{
"type": "complex", "type": "complex",
"operator": "OR", "operator": "OR",
"key": "",
"query_list": [{ "query_list": [{
"key": "a", "key": "a",
"operator": "=", "operator": "=",
...@@ -269,6 +271,7 @@ ...@@ -269,6 +271,7 @@
}, { }, {
"type": "complex", "type": "complex",
"operator": "AND", "operator": "AND",
"key": "",
"query_list": [{ "query_list": [{
"key": "c", "key": "c",
"type": "simple", "type": "simple",
...@@ -308,10 +311,23 @@ ...@@ -308,10 +311,23 @@
"NOT(a:=b OR c:% AND d:<2)" "NOT(a:=b OR c:% AND d:<2)"
) )
).toString(), ).toString(),
"NOT ( ( a: = \"b\" ) OR ( ( c: \"%\" ) AND ( d: < \"2\" ) ) )", "NOT ( ( a: = \"b\" OR ( c: \"%\" AND d: < \"2\" ) ) )",
"create(create(\"NOT(a:=b OR c:% AND d:<2)\")).toString();" "create(create(\"NOT(a:=b OR c:% AND d:<2)\")).toString();"
); );
deepEqual(
jIO.QueryFactory.create(jIO.Query.objectToSearchText(jsoned)).toJSON(),
jsoned,
"create( objectToSearchText(create(\"NOT(a:=b OR c:% AND d:<2)\")" +
".toJSON()) ).toJSON()"
);
deepEqual(
jIO.QueryFactory.create("a:(b OR c)").toString(),
"a: ( \"b\" OR \"c\" )",
"create( \"a:(b OR c)\" ).toString()"
);
}); });
test('Docs with space, tab, and newline', function () { test('Docs with space, tab, and newline', function () {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment