Commit 882b089a authored by Tristan Cavelier's avatar Tristan Cavelier

Queries: Change serialization behavior

( a:b OR a:c ) -> a:( b OR c )
parent b345214e
...@@ -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]);
...@@ -637,4 +637,4 @@ function Parser () { ...@@ -637,4 +637,4 @@ function Parser () {
} }
Parser.prototype = parser;parser.Parser = Parser; Parser.prototype = parser;parser.Parser = Parser;
return new Parser; return new Parser;
})(); })();
\ No newline at end of file
...@@ -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
......
...@@ -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", "key": "", "operator": "NOT", "query_list": [query]}; return {"type": "complex", "operator": "NOT", "query_list": [query]};
}, mkComplexQuery = function (key, operator, query_list) { }, mkComplexQuery = function (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,10 +37,17 @@ var arrayExtend = function () { ...@@ -37,10 +37,17 @@ var arrayExtend = function () {
query_list2.push(query_list[i]); query_list2.push(query_list[i]);
} }
} }
return {type:"complex",key:key,operator:operator,query_list:query_list2}; return {type:"complex",operator:operator,query_list:query_list2};
}, querySetKey = function (query, key) { }, querySetKey = function (query, key) {
if (({simple: 1, complex: 1})[query.type] && !query.key) { var i;
if (query.type === "complex") {
for (i = 0; i < query.query_list.length; ++i) {
querySetKey(query.query_list[i], key);
}
return true;
}
if (query.type === "simple" && !query.key) {
query.key = key; query.key = key;
return true; return true;
} }
......
...@@ -519,8 +519,6 @@ ...@@ -519,8 +519,6 @@
*/ */
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.
* *
...@@ -540,7 +538,6 @@ ...@@ -540,7 +538,6 @@
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"
...@@ -568,7 +565,6 @@ ...@@ -568,7 +565,6 @@
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) {
...@@ -658,34 +654,60 @@ ...@@ -658,34 +654,60 @@
}; };
function objectToSearchText(query) { function objectToSearchText(query) {
var str_list = [], operator = "", query_list = null; 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") { if (query.type === "complex") {
query_list = query.query_list || []; query_list = query.query_list;
if (query_list.length === 0) { if (!query_list || query_list.length === 0) {
return ""; return "";
} }
operator = query.operator; operator = query.operator;
if (operator === "NOT") { if (operator === "NOT") {
str_list.push("NOT");
// fallback to AND operator if several queries are given // fallback to AND operator if several queries are given
// i.e. `NOT ( a AND b )` // i.e. `NOT ( a AND b )`
operator = "AND"; return "NOT ( " + objectToSearchText(
{type: "complex", operator: "AND", query_list: query_list}
) + " )";
} }
if (query.key) { if (query_list.length === 1) {
str_list.push(query.key + ":"); return objectToSearchText(query_list[0]);
} }
str_list.push("(");
query_list.forEach(function (sub_query) { common_key = query_list[i].key;
str_list.push(objectToSearchText(sub_query)); for (i = 1; i < query_list.length; i += 1) {
str_list.push(operator); if (query_list[i].type !== "simple" ||
}); query_list[i].key !== common_key) {
str_list.length -= 1; break;
str_list.push(")"); }
return str_list.join(" "); }
} string_list = [];
if (query.type === "simple") { if (i === query_list.length) {
return (query.key ? query.key + ": " : "") + for (i = 0; i < query_list.length; i += 1) {
(query.operator || "") + ' "' + query.value + '"'; 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"); throw new TypeError("This object is not a query");
} }
......
...@@ -1281,7 +1281,7 @@ ...@@ -1281,7 +1281,7 @@
test("extract complex AND single local_roles", function () { test("extract complex AND single local_roles", function () {
var search_url = domain + "?mode=search&" + 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&" + "select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee", "local_roles=Assignee",
search_hateoas = JSON.stringify({ search_hateoas = JSON.stringify({
...@@ -1385,7 +1385,7 @@ ...@@ -1385,7 +1385,7 @@
test("extract sub multiple local_roles", function () { test("extract sub multiple local_roles", function () {
var search_url = domain + "?mode=search&" + 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&" + "select_list=destination&select_list=source&limit=5&" +
"local_roles=Assignee&local_roles=Assignor", "local_roles=Assignee&local_roles=Assignor",
search_hateoas = JSON.stringify({ search_hateoas = JSON.stringify({
...@@ -1490,7 +1490,7 @@ ...@@ -1490,7 +1490,7 @@
test("extract complex AND single domains", function () { test("extract complex AND single domains", function () {
var search_url = domain + "?mode=search&" + 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&" + "select_list=destination&select_list=source&limit=5&" +
"selection_domain=%7B%22group%22%3A%22bar%2Ffoo%22%7D", "selection_domain=%7B%22group%22%3A%22bar%2Ffoo%22%7D",
search_hateoas = JSON.stringify({ search_hateoas = JSON.stringify({
......
...@@ -258,11 +258,9 @@ ...@@ -258,11 +258,9 @@
{ {
"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": "=",
...@@ -271,7 +269,6 @@ ...@@ -271,7 +269,6 @@
}, { }, {
"type": "complex", "type": "complex",
"operator": "AND", "operator": "AND",
"key": "",
"query_list": [{ "query_list": [{
"key": "c", "key": "c",
"type": "simple", "type": "simple",
...@@ -328,6 +325,12 @@ ...@@ -328,6 +325,12 @@
"create( \"a:(b OR c)\" ).toString()" "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()"
);
}); });
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