1 function ManifoldQuery(action, object, 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 object WHERE filter;
27 UPDATE object SET field=value WHERE filter; / returns SELECT
28 DELETE FROM object WHERE filter
29 INSERT INTO object VALUES(field=value)
30 -------------------------------------------------------------*/
32 this.__repr = function () {
33 res = "ManifoldQuery ";
34 // res += " id=" + this.query_uuid;
35 res += " a=" + this.action;
36 res += " o=" + this.object;
37 res += " ts=" + this.timestamp;
38 res += " flts=" + this.filters;
39 res += " flds=" + this.fields;
40 res += " prms=" + this.params;
44 this.clone = function() {
46 var q = new ManifoldQuery();
47 q.action = this.action;
48 q.object = this.object;
49 q.timestamp = this.timestamp;
50 q.filters = this.filters.slice();
51 q.fields = this.fields.slice();
52 q.query_uuid = this.query_uuid;
54 if (this.analyzed_query)
55 q.analyzed_query = this.analyzed_query.clone();
57 q.analyzed_query = null;
59 if (this.subqueries) {
61 for (method in this.subqueries)
62 q.subqueries[method] = this.subqueries[method].clone();
65 // deep extend not working for custom objects
66 // $.extend(true, q, this);
70 this.add_filter = function(key, op, value) {
71 this.filters.push(new Array(key, op, value));
73 this.update_filter = function(key, op, value) {
74 // Need to be improved...
75 // remove all occurrences of key if operation is not defined
77 this.filters = jQuery.grep(this.filters, function(val, i) {
80 // Else remove the key+op filters
82 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
84 this.filters.push(new Array(key, op, value));
87 this.remove_filter = function (key,op,value) {
88 // if operator is null then remove all occurences of this key
90 this.filters = jQuery.grep(this.filters, function(val, i) {
94 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
98 // FIXME These functions computing diff's between queries are meant to be shared
99 this.diff_fields = function(otherQuery) {
100 var f1 = this.fields;
101 var f2 = otherQuery.fields;
103 /* added elements are the ones in f2 not in f1 */
104 var added = jQuery.grep(f2, function (x) { return jQuery.inArray(x, f1) == -1 });
105 /* removed elements are the ones in f1 not in f2 */
106 var removed = jQuery.grep(f1, function (x) { return jQuery.inArray(x, f2) == -1 });
108 return {'added':added, 'removed':removed};
111 // FIXME Modify filter to filters
112 this.diff_filter = function(otherQuery) {
113 var f1 = this.filters;
114 var f2 = otherQuery.filters;
116 /* added elements are the ones in f2 not in f1 */
117 var added = jQuery.grep(f2, function (x) { return !arrayInArray(x, f1)});
118 /* removed elements are the ones in f1 not in f2 */
119 var removed = jQuery.grep(f1, function (x) { return !arrayInArray(x, f2)});
121 return {'added':added, 'removed':removed};
124 // Callaback received 3 parameters: query, data, parent_query
125 this.iter_subqueries = function(callback, data)
127 rec = function(query, callback, data, parent_query) {
128 callback(query, data, parent_query);
129 jQuery.each(query.subqueries, function(object, subquery) {
130 rec(subquery, callback, data, query);
134 if (this.analyzed_query !== undefined)
135 query = this.analyzed_query;
139 rec(query, callback, data, null);
142 this.select = function(field)
144 this.fields.push(field);
147 this.unselect = function(field)
149 this.fields = $.grep(this.fields, function(x) { return x != field; });
152 // we send queries as a json string now
153 // this.as_POST = function() {
154 // return {'action': this.action, 'object': this.object, 'timestamp': this.timestamp,
155 // 'filters': this.filters, 'params': this.params, 'fields': this.fields};
157 this.analyze_subqueries = function() {
158 /* adapted from the PHP function in com_tophat/includes/query.php */
159 var q = new ManifoldQuery();
160 q.query_uuid = this.query_uuid;
161 q.action = this.action;
162 q.object = this.object;
163 q.timestamp = this.timestamp;
166 jQuery.each(this.filters, function(i, filter) {
170 var pos = k.indexOf('.');
172 var object = k.substr(0, pos);
173 var field = k.substr(pos+1);
174 if (!q.subqueries[object]) {
175 q.subqueries[object] = new ManifoldQuery();
176 q.subqueries[object].action = q.action;
177 q.subqueries[object].object = object;
178 q.subqueries[object].timestamp = q.timestamp;
180 q.subqueries[object].filters.push(Array(field, op, v));
182 q.filters.push(filter);
187 jQuery.each(this.params, function(param, value) {
188 var pos = param.indexOf('.');
190 var object = param.substr(0, pos);
191 var field = param.substr(pos+1);
192 if (!q.subqueries[object]) {
193 q.subqueries[object] = new ManifoldQuery();
194 q.subqueries[object].action = q.action;
195 q.subqueries[object].object = object;
196 q.subqueries[object].timestamp = q.timestamp;
198 q.subqueries[object].params[field] = value;
200 q.params[field] = value;
205 jQuery.each(this.fields, function(i, v) {
206 var pos = v.indexOf('.');
208 var object = v.substr(0, pos);
209 var field = v.substr(pos+1);
210 if (!q.subqueries[object]) {
211 q.subqueries[object] = new ManifoldQuery();
212 q.subqueries[object].action = q.action;
213 q.subqueries[object].object = object;
214 q.subqueries[object].timestamp = q.timestamp;
216 q.subqueries[object].fields.push(field);
221 this.analyzed_query = q;
225 if (typeof action == "undefined")
228 this.action = action;
230 if (typeof object == "undefined")
233 this.object = object;
235 if (typeof timestamp == "undefined")
236 this.timestamp = "now";
238 this.timestamp = timestamp;
240 if (typeof filters == "undefined")
243 this.filters = filters;
245 if (typeof params == "undefined")
248 this.params = params;
250 if (typeof fields == "undefined")
253 this.fields = fields;
255 if (typeof unique == "undefined")
258 this.unique = unique;
260 this.query_uuid = query_uuid;
262 if (typeof aq == "undefined")
263 this.analyzed_query = null;
265 this.analyzed_query = aq;
267 if (typeof sq == "undefined")
268 this.subqueries = {};
270 this.subqueries = sq;