Commit 83d022d4 by Tristan Cavelier Committed by Romain Courteaud

Queries: Fix some parsing/serialization issues

- fixes query toString inconsistent output
- change serialization behavior
  ( a:b OR a:c ) -> a:( b OR c )
  a:( b OR c )   -> a:( b OR c )
- rename simpleQuerySetKey to querySetKey
  as it also applies on complex queries.
1 parent 1868b249
......@@ -105,7 +105,7 @@ case 12:
this.$ = $$[$0-1];
break;
case 13:
simpleQuerySetKey($$[$0], $$[$0-2]); this.$ = $$[$0];
querySetKey($$[$0], $$[$0-2]); this.$ = $$[$0];
break;
case 15:
$$[$0].operator = $$[$0-1] ; this.$ = $$[$0];
......
......@@ -61,7 +61,7 @@ boolean_expression
expression
: LEFT_PARENTHESE search_text RIGHT_PARENTHESE { $$ = $2; }
| WORD DEFINITION expression { simpleQuerySetKey($3, $1); $$ = $3; }
| WORD DEFINITION expression { querySetKey($3, $1); $$ = $3; }
| value { $$ = $1; }
;
......
......@@ -58,11 +58,11 @@ var arrayExtend = function () {
}
return {type:"complex",operator:operator,query_list:query_list2};
}, simpleQuerySetKey = function (query, key) {
}, querySetKey = function (query, key) {
var i;
if (query.type === "complex") {
for (i = 0; i < query.query_list.length; ++i) {
simpleQuerySetKey (query.query_list[i],key);
querySetKey(query.query_list[i], key);
}
return true;
}
......
......@@ -573,21 +573,8 @@
* #crossLink "Query/toString:method"
*/
ComplexQuery.prototype.toString = function () {
var str_list = [], this_operator = this.operator;
if (this.operator === "NOT") {
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(" ");
/*global objectToSearchText */
return objectToSearchText(this.toJSON());
};
/**
......@@ -686,21 +673,60 @@
};
function objectToSearchText(query) {
var str_list = [];
if (query.type === "complex") {
str_list.push("(");
(query.query_list || []).forEach(function (sub_query) {
str_list.push(objectToSearchText(sub_query));
str_list.push(query.operator);
});
str_list.length -= 1;
str_list.push(")");
return str_list.join(" ");
}
var i = 0,
query_list = null,
string_list = null,
operator = "",
common_key = "";
if (query.type === "simple") {
return (query.key ? query.key + ": " : "") +
(query.operator || "") + ' "' + query.value + '"';
}
if (query.type === "complex") {
query_list = query.query_list;
if (!query_list || query_list.length === 0) {
return "";
}
operator = query.operator || "";
if (operator === "NOT") {
// fallback to AND operator if several queries are given
// i.e. `NOT ( a AND b )`
return "NOT ( " + objectToSearchText(
{type: "complex", operator: "AND", query_list: query_list}
) + " )";
}
if (query_list.length === 1) {
return objectToSearchText(query_list[0]);
}
common_key = query_list[i].key;
for (i = 1; i < query_list.length; i += 1) {
if (query_list[i].type !== "simple" ||
query_list[i].key !== common_key) {
break;
}
}
string_list = [];
if (i === query_list.length) {
for (i = 0; i < query_list.length; i += 1) {
string_list.push(
(query_list[i].operator || "") +
' "' + query_list[i].value + '"'
);
}
} else {
common_key = "";
for (i = 0; i < query_list.length; i += 1) {
string_list.push(objectToSearchText(query_list[i]));
}
}
if (string_list.length > 1) {
return (common_key ? common_key + ": " : "") +
"( " + string_list.join(" " + operator + " ") + " )";
}
return (common_key ? common_key + ": " : "") +
string_list[0];
}
throw new TypeError("This object is not a query");
}
......@@ -868,8 +894,7 @@
* #crossLink "Query/toString:method"
*/
SimpleQuery.prototype.toString = function () {
return (this.key ? this.key + ":" : "") +
(this.operator ? " " + this.operator : "") + ' "' + this.value + '"';
return objectToSearchText(this.toJSON());
};
/**
......
......@@ -1670,7 +1670,7 @@
test("extract complex AND single local_roles", function () {
var search_url = domain + "?mode=search&" +
"query=%28%20portal_type%3A%20%20%22Person%22%20%29&" +
"query=portal_type%3A%20%20%22Person%22&" +
"select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee",
search_hateoas = JSON.stringify({
......@@ -1774,7 +1774,7 @@
test("extract sub multiple local_roles", function () {
var search_url = domain + "?mode=search&" +
"query=%28%20portal_type%3A%20%20%22Person%22%20%29&" +
"query=portal_type%3A%20%20%22Person%22&" +
"select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee&local_roles=Assignor",
search_hateoas = JSON.stringify({
......@@ -1879,7 +1879,7 @@
test("extract complex AND single domains", function () {
var search_url = domain + "?mode=search&" +
"query=%28%20portal_type%3A%20%20%22Person%22%20%29&" +
"query=portal_type%3A%20%20%22Person%22&" +
"select_list=destination&select_list=source&limit=5&" +
"selection_domain=%7B%22group%22%3A%22bar%2Ffoo%22%7D",
search_hateoas = JSON.stringify({
......
......@@ -321,10 +321,44 @@
"NOT(a:=b OR c:% AND d:<2)"
)
).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();"
);
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()"
);
deepEqual(
jIO.QueryFactory.create("(a:b OR a:c)").toString(),
"a: ( \"b\" OR \"c\" )",
"create( \"(a:b OR a:c)\" ).toString()"
);
deepEqual(
jIO.QueryFactory.create({
"type": "complex",
"query_list": [{
"type": "simple",
"value": "a"
}, {
"type": "simple",
"value": "b"
}]
}).toString(),
"( \"a\" \"b\" )",
"{complex query without operator}.toString()"
);
});
test('Docs with space, tab, and newline', function () {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!