Commit a9ae8a4c authored by Tristan Cavelier's avatar Tristan Cavelier

WIP: complexquery.js

parent d45aeb6f
......@@ -10,17 +10,38 @@ var ComplexQuery = newClass(Query, function (spec) {
*/
this.operator = spec.operator || "AND";
this.query_list = todo;
this.query_list = spec.query_list || [];
/**
* Filter the item list only if all the sub queries match this item according
* to the logical operator.
* See {{#crossLink "Query/exec:method"}}{{/crossLink}}
*/
this.exec = function (item_list, option) {
// XXX
this.match = function () {
todo
};
// XXX
this.toString = function () {
var str_list = ["("];
this.query_list.forEach(function (query) {
str_list.push(query.toString());
str_list.push(this.operator);
});
str_list.pop(); // remove last operator
str_list.push(")");
retrun str_list.join(" ");
};
// XXX
this.serialized = function () {
var s = {
"type": "complex",
"operator": this.operator,
"query_list": []
};
this.query_list.forEach(function (query) {
s.query_list.push(query.serialized());
});
return s;
};
// XXX
this["AND"] = function (item, wildcard_character) {
var i;
......
......@@ -5,9 +5,18 @@
* @class Query
* @constructor
*/
var Query = newClass(function() {
var Query = newClass(function(spec) {
/**
* Creates a new item list with matching item only
* The wildcard character used to extend comparison action
*
* @property wildcard_character
* @type String
*/
this.wildcard_character = spec.wildcard_character || "%";
/**
* Filter the item list with matching item only
*
* @method exec
* @param {Array} item_list The list of object
......@@ -18,9 +27,29 @@ var Query = newClass(function() {
* and "ascending" or "descending"
* @param {Array} [option.limit=undefined] Couple of integer, first is an
* index and second is the length.
* @return {Array} The new item list
*/
this.exec = function (item_list, option) {};
this.exec = function (item_list, option) {
var i;
for (i = 0; i < item_list.length;) {
if (!this.match(item, option.wildcard_character)) {
item_list.splice(i, 1);
} else {
i += 1;
}
}
if (option.sort_on) {
Query.sortOn(option.sort_on, item_list);
}
if (option.limit) {
new_item_list = item_list.slice(
option.limit[0],
option.limit[1] + option.limit[0] + 1
);
}
if (option.select_list) {
Query.filterListSelect(option.select_list, item_list);
}
};
/**
* Test if an item matches this query
......
......@@ -34,39 +34,6 @@ var SimpleQuery = newClass(Query, function (spec) {
*/
this.value = spec.value;
/**
* The wildcard character used to extend comparison action
*
* @property wildcard_character
* @type String
*/
this.wildcard_character = spec.wildcard_character || "%";
/**
* #crossLink "Query/exec:method"
*/
this.exec = function (item_list, option) {
var new_item_list = [];
item_list.forEach(function (item) {
if (!this.match(item, option.wildcard_character)) {
new_item_list.push(item);
}
});
if (option.sort_on) {
Query.sortOn(option.sort_on, new_item_list);
}
if (option.limit) {
new_item_list = new_item_list.slice(
option.limit[0],
option.limit[1] + option.limit[0] + 1
);
}
if (option.select_list) {
Query.filterListSelect(option.select_list, new_item_list);
}
return new_item_list;
};
/**
* #crossLink "Query/match:method"
*/
......
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