Commit 5d3af8bd authored by Tristan Cavelier's avatar Tristan Cavelier

WIP: Great changes on complex queries

Change to a module compatible with nodejs, requirejs, web workers and classic
browsers. Provides classes Query, SimpleQuery, ComplexQuery. Documentation
comming soon.
parent 1c0444c6
......@@ -4,11 +4,27 @@
* http://www.gnu.org/licenses/lgpl.html
*/
(function (scope) {
/**
* Provides some function to use complex queries with item list
*
* @module complex_queries
*/
var complex_queries;
(function () {
"use strict";
Object.defineProperty(scope, "ComplexQueries", {
configurable: false,
enumerable: false,
writable: false,
value: {}
});
var to_export = {}, module_name = "complex_queries";
/**
* Add a secured (write permission denied) property to an object.
* @method defineProperty
* @param {Object} object The object to fill
* @param {String} key The object key where to store the property
* @param {Any} value The value to store
*/
function _export(key, value) {
Object.defineProperty(to_export, key, {
"configurable": false,
"enumerable": true,
"writable": false,
"value": value
});
}
// XXX
var ComplexQuery = newClass(Query, function () {
/**
* 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 () {
todo
};
});
todo
query_class_dict.complex = ComplexQuery;
_export("ComplexQuery", ComplexQuery);
}(jIO));
if (typeof define === "function" && define.amd) {
define(to_export);
} else if (typeof window === "object") {
Object.defineProperty(window, module_name, {
configurable: false,
enumerable: true,
writable: false,
value: to_export
});
} else if (typeof exports === "object") {
var i;
for (i in to_export) {
if (to_export.hasOwnProperty(i)) {
exports[i] = to_export[i];
}
}
} else {
complex_queries = to_export;
}
}());
Object.defineProperty(scope.ComplexQueries, "parse", {
configurable: false,
enumerable: false,
writable: false,
value: function (string) {
/**
* Parse a text request to a json query tree
* @method parseStringToObject
* @param {String} string The string to parse
* @return {Object} The json query tree
*/
function parseStringToObject(string) {
return result;
}
}
});
}; // parseStringToQuery
This diff is collapsed.
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global _export: true, ComplexQuery: true, SimpleQuery: true,
newClass: true,
sortFunction: true, convertSearchTextToRegExp: true */
// XXX
var query_class_dict = {}, query_factory = {};
newClass.apply(query_factory, [{
"secure_methods": true
}, function () {
// XXX
this.create = function (object) {
if (typeof object.type === "string" &&
query_class_dict[object.type]) {
return new query_class_dict[object.type](object);
}
return null;
};
}]); // end QueryFactory
_export("factory", query_factory);
Object.defineProperty(scope.ComplexQueries,"serialize",{
configurable:false,enumerable:false,writable:false,value:function(query){
var str_list = [], i;
if (query.type === 'complex') {
str_list.push ( '(' );
for (i=0; i<query.query_list.length; ++i) {
str_list.push( scope.ComplexQueries.serialize(query.query_list[i]) );
str_list.push( query.operator );
}
str_list.length --;
str_list.push ( ')' );
return str_list.join(' ');
} else if (query.type === 'simple') {
return query.id + (query.id?': ':'') + query.operator + ' "' + query.value + '"';
}
return query;
}
});
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global _export: true, to_export: true */
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(" ");
}
if (query.type === "simple") {
return query.id + (query.id ? ": " : "") + (query.operator || "=") + ' "' +
query.value + '"';
}
throw new TypeError("This object is not a query");
}
_export("objectToSearchText", objectToSearchText);
/**
* The SimpleQuery inherits from Query, and compares one metadata value
*
* @class SimpleQuery
* @param {Object} [spec={}] The specifications
* @param {String} [spec.operator="="] The compare method to use
* @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare
* @param {String} [spec.wildcard_character="%"] The wildcard character
*/
var SimpleQuery = newClass(Query, function (spec) {
/**
* Operator to use to compare object values
*
* @property operator
* @type String
* @default "="
*/
this.operator = spec.operator || "=";
/**
* Key of the object which refers to the value to compare
*
* @property key
* @type String
*/
this.key = spec.key;
/**
* Value is used to do the comparison with the object value
*
* @property value
* @type String
*/
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"
*/
this.match = function (item, wildcard_character) {
this[this.operator](item[this.key], this.value, wildcard_character);
};
/**
* #crossLink "Query/toString:method"
*/
this.toString = function () {
return (this.key ? this.key + ": " : "") + (this.operator || "=") + ' "' +
this.value + '"';
};
/**
* #crossLink "Query/serialized:method"
*/
this.serialized = function () {
return {
"type": "simple",
"operator": this.operator,
"key": this.key,
"value": this.value
};
};
// XXX
this["="] = function (object_value, comparison_value,
wildcard_character) {
return convertSearchTextToRegExp(
comparison_value.toString(),
wildcard_character || this.wildcard_character
).test(object_value.toString());
};
// XXX
this["!="] = function (object_value, comparison_value,
wildcard_character) {
return !convertSearchTextToRegExp(
comparison_value.toString(),
wildcard_character || this.wildcard_character
).test(object_value.toString());
};
// XXX
this["<"] = function (object_value, comparison_value) {
return object_value < comparison_value;
};
// XXX
this["<="] = function (object_value, comparison_value) {
return object_value <= comparison_value;
};
// XXX
this[">"] = function (object_value, comparison_value) {
return object_value > comparison_value;
};
// XXX
this[">="] = function (object_value, comparison_value) {
return object_value >= comparison_value;
};
});
query_class_dict.simple = SimpleQuery;
_export("SimpleQuery", SimpleQuery);
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global _export: true */
#!/usr/bin/env node
// keywords: js, javascript, new class creator, generator
/**
* Create a class, manage inheritance, static methods,
* protected attributes and can hide methods or/and secure methods
*
* @method newClass
* @param {Class} Class Classes to inherit from (0..n)
* @param {Object} option Class option (0..n)
* @param {Boolean} [option.secure_methods=false] Make methods not configurable
* and not writable
* @param {Boolean} [option.hide_methods=false] Make methods not enumerable
* @param {Object} [option.static_methods={}] Add static methods
* @param {Function} constructor The new class constructor
* @return {Class} The new class
*/
function newClass() {
var j, k, constructors = [], option, new_class;
for (j = 0; j < arguments.length; j += 1) {
if (typeof arguments[j] === "function") {
constructors.push(arguments[j]);
} else if (typeof arguments[j] === "object") {
option = option || {};
for (k in arguments[j]) {
if (arguments[j].hasOwnProperty(k)) {
option[j] = arguments[j][k];
}
}
}
}
function postCreate(that) {
// modify the object according to 'option'
var key;
if (option) {
for (key in that) {
if (that.hasOwnProperty(key)) {
if (typeof that[key] === "function") {
Object.defineProperty(that, key, {
configurable: option.secure_methods ? false : true,
enumerable: option.hide_methods ? false : true,
writable: option.secure_methods ? false : true,
value: that[key]
});
}
}
}
}
}
new_class = function (spec, my) {
var i;
spec = spec || {};
my = my || {};
// don't use forEach !
for (i = 0; i < constructors.length; i += 1) {
constructors[i].apply(this, [spec, my]);
}
postCreate(this);
return this;
};
for (j in (option.static_methods || {})) {
if (option.static_methods.hasOwnProperty(j)) {
new_class[j] = option.static_methods[j];
}
}
postCreate(new_class);
return new_class;
}
/**
* Escapes regexp special chars from a string.
* @method regexpEscapeString
* @param {String} string The string to escape
* @return {String} The escaped string
*/
function regexpEscapeString(string) {
if (typeof string === "string") {
return string.replace(/([\\\.\$\[\]\(\)\{\}\^\?\*\+\-])/g, "\\$1");
}
}
_export("regexpEscapeString", regexpEscapeString);
/**
* Convert a search text to a regexp.
* @method convertSearchTextToRegExp
* @param {String} string The string to convert
* @param {String} [wildcard_character=undefined] The wildcard chararter
* @return {RegExp} The search text regexp
*/
function convertSearchTextToRegExp(string, wildcard_character) {
return new RegExp("^" + regexpEscapeString(string).replace(
regexpEscapeString(wildcard_character),
'.*'
) + "$");
}
_export("convertSearchTextToRegExp", convertSearchTextToRegExp);
// XXX
function sortFunction(key, way) {
if (way === 'descending') {
return function (a,b) {
return a[key] < b[key] ? 1 : a[key] > b[key] ? -1 : 0;
};
}
return function (a,b) {
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0;
};
}
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