1 function ManifoldQuery(action, method, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {
2 // get, update, delete, create
4 // slice, user, network...
6 // timestamp, now, latest(cache) : date of the results queried
8 // key(field),op(=<>),value
14 // 0,1 : list of element of an object or single object
16 // query_uuid : unique identifier of a query
18 // Query : root query (no sub-Query)
20 // {} : Assoc Table of sub-queries ["resources"->subQ1, "users"->subQ2]
23 /*-------------------------------------------------------------
24 Query properties are SQL like :
25 ---------------------------------------------------------------
26 SELECT fields FROM method WHERE filter;
27 UPDATE method SET field=value WHERE filter; / returns SELECT
28 DELETE FROM method WHERE filter
29 INSERT INTO method VALUES(field=value)
30 -------------------------------------------------------------*/
32 this.clone = function() {
33 q = new ManifoldQuery();
34 return jQuery.extend(true, q, this);
36 this.add_filter = function(key, op, value) {
37 this.filters.push(new Array(key, op, value));
39 this.update_filter = function(key, op, value) {
40 // Need to be improved...
41 // remove all occurrences of key if operation is not defined
43 this.filters = jQuery.grep(this.filters, function(val, i) {
46 // Else remove the key+op filters
48 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
50 this.filters.push(new Array(key, op, value));
52 this.remove_filter = function (key,op,value){
53 // if operator is null then remove all occurences of this key
55 this.filters = jQuery.grep(this.filters, function(val, i) {
59 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
62 // FIXME These functions computing diff's between queries are meant to be shared
63 this.diff_fields = function (otherQuery)
66 var f2 = otherQuery.fields;
68 /* added elements are the ones in f2 not in f1 */
69 var added = jQuery.grep(f2, function (x) { return jQuery.inArray(x, f1) == -1 });
70 /* removed elements are the ones in f1 not in f2 */
71 var removed = jQuery.grep(f1, function (x) { return jQuery.inArray(x, f2) == -1 });
73 return {'added':added, 'removed':removed};
75 // FIXME Modify filter to filters
76 this.diff_filter = function (otherQuery)
78 var f1 = this.filters;
79 var f2 = otherQuery.filters;
81 /* added elements are the ones in f2 not in f1 */
82 var added = jQuery.grep(f2, function (x) { return !arrayInArray(x, f1)});
83 /* removed elements are the ones in f1 not in f2 */
84 var removed = jQuery.grep(f1, function (x) { return !arrayInArray(x, f2)});
86 return {'added':added, 'removed':removed};
88 this.to_hash = function() {
89 return {'action': this.action, 'method': this.method, 'timestamp': this.timestamp,
90 'filters': this.filters, 'params': this.params, 'fields': this.fields};
92 this.analyze_subqueries = function() {
93 /* adapted from the PHP function in com_tophat/includes/query.php */
94 var q = new ManifoldQuery();
95 q.query_uuid = this.query_uuid;
96 q.action = this.action;
97 q.method = this.method;
98 q.timestamp = this.timestamp;
101 jQuery.each(this.filters, function(i, filter) {
105 var pos = k.indexOf('.');
107 var method = k.substr(0, pos);
108 var field = k.substr(pos+1);
109 if (jQuery.inArray(this.method, q.subqueries) == -1) {
110 q.subqueries[this.method] = new ManifoldQuery();
111 q.subqueries[this.method].action = this.action;
112 q.subqueries[this.method].method = this.method;
113 q.subqueries[this.method].timestamp = this.timestamp;
115 q.subqueries[this.method].filters.push(Array(field, op, v));
117 q.filters.push(this.filter);
122 jQuery.each(this.params, function(param, value) {
123 var pos = param.indexOf('.');
125 var method = param.substr(0, pos);
126 var field = param.substr(pos+1);
127 if (jQuery.inArray(this.method, q.subqueries) == -1) {
128 q.subqueries[this.method] = new ManifoldQuery();
129 q.subqueries[this.method].action = this.action;
130 q.subqueries[this.method].method = this.method;
131 q.subqueries[this.method].timestamp = this.timestamp;
133 q.subqueries[this.method].params[field] = value;
135 q.params[field] = value;
140 jQuery.each(this.fields, function(i, v) {
141 var pos = v.indexOf('.');
143 var method = v.substr(0, pos);
144 var field = v.substr(pos+1);
145 if (jQuery.inArray(this.method, q.subqueries) == -1) {
146 q.subqueries[this.method] = new ManifoldQuery();
147 q.subqueries[this.method].action = this.action;
148 q.subqueries[this.method].method = this.method;
149 q.subqueries[this.method].timestamp = this.timestamp;
151 q.subqueries[this.method].fields.push(field);
156 this.analyzed_query = q;
160 this.action = action;
161 this.method = method;
162 this.timestamp = timestamp;
163 this.filters = filters;
164 this.params = params;
165 this.fields = fields;
166 this.unique = unique;
167 this.query_uuid = query_uuid;
168 this.analyzed_query = aq;
169 this.subqueries = sq;