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() {
45 q = new ManifoldQuery();
46 return jQuery.extend(true, q, this);
48 this.add_filter = function(key, op, value) {
49 this.filters.push(new Array(key, op, value));
51 this.update_filter = function(key, op, value) {
52 // Need to be improved...
53 // remove all occurrences of key if operation is not defined
55 this.filters = jQuery.grep(this.filters, function(val, i) {
58 // Else remove the key+op filters
60 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
62 this.filters.push(new Array(key, op, value));
65 this.remove_filter = function (key,op,value) {
66 // if operator is null then remove all occurences of this key
68 this.filters = jQuery.grep(this.filters, function(val, i) {
72 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
76 // FIXME These functions computing diff's between queries are meant to be shared
77 this.diff_fields = function(otherQuery) {
79 var f2 = otherQuery.fields;
81 /* added elements are the ones in f2 not in f1 */
82 var added = jQuery.grep(f2, function (x) { return jQuery.inArray(x, f1) == -1 });
83 /* removed elements are the ones in f1 not in f2 */
84 var removed = jQuery.grep(f1, function (x) { return jQuery.inArray(x, f2) == -1 });
86 return {'added':added, 'removed':removed};
89 // FIXME Modify filter to filters
90 this.diff_filter = function(otherQuery) {
91 var f1 = this.filters;
92 var f2 = otherQuery.filters;
94 /* added elements are the ones in f2 not in f1 */
95 var added = jQuery.grep(f2, function (x) { return !arrayInArray(x, f1)});
96 /* removed elements are the ones in f1 not in f2 */
97 var removed = jQuery.grep(f1, function (x) { return !arrayInArray(x, f2)});
99 return {'added':added, 'removed':removed};
102 this.iter_subqueries = function(callback, data)
104 rec = function(query, callback, data) {
105 jQuery.each(query.subqueries, function(object, subquery) {
106 rec(subquery, callback);
108 callback(query, data);
110 if (this.analyzed_query !== undefined)
111 query = this.analyzed_query;
114 rec(query, callback, data);
117 // we send queries as a json string now
118 // this.as_POST = function() {
119 // return {'action': this.action, 'object': this.object, 'timestamp': this.timestamp,
120 // 'filters': this.filters, 'params': this.params, 'fields': this.fields};
122 this.analyze_subqueries = function() {
123 /* adapted from the PHP function in com_tophat/includes/query.php */
124 var q = new ManifoldQuery();
125 q.query_uuid = this.query_uuid;
126 q.action = this.action;
127 q.object = this.object;
128 q.timestamp = this.timestamp;
131 jQuery.each(this.filters, function(i, filter) {
135 var pos = k.indexOf('.');
137 var object = k.substr(0, pos);
138 var field = k.substr(pos+1);
139 if (!q.subqueries[object]) {
140 q.subqueries[object] = new ManifoldQuery();
141 q.subqueries[object].action = q.action;
142 q.subqueries[object].object = object;
143 q.subqueries[object].timestamp = q.timestamp;
145 q.subqueries[object].filters.push(Array(field, op, v));
147 q.filters.push(filter);
152 jQuery.each(this.params, function(param, value) {
153 var pos = param.indexOf('.');
155 var object = param.substr(0, pos);
156 var field = param.substr(pos+1);
157 if (!q.subqueries[object]) {
158 q.subqueries[object] = new ManifoldQuery();
159 q.subqueries[object].action = q.action;
160 q.subqueries[object].object = object;
161 q.subqueries[object].timestamp = q.timestamp;
163 q.subqueries[object].params[field] = value;
165 q.params[field] = value;
170 jQuery.each(this.fields, function(i, v) {
171 var pos = v.indexOf('.');
173 var object = v.substr(0, pos);
174 var field = v.substr(pos+1);
175 if (!q.subqueries[object]) {
176 q.subqueries[object] = new ManifoldQuery();
177 q.subqueries[object].action = q.action;
178 q.subqueries[object].object = object;
179 q.subqueries[object].timestamp = q.timestamp;
181 q.subqueries[object].fields.push(field);
186 this.analyzed_query = q;
190 if (typeof action == "undefined")
193 this.action = action;
195 if (typeof object == "undefined")
198 this.object = object;
200 if (typeof timestamp == "undefined")
201 this.timestamp = "now";
203 this.timestamp = timestamp;
205 if (typeof filters == "undefined")
208 this.filters = filters;
210 if (typeof params == "undefined")
213 this.params = params;
215 if (typeof fields == "undefined")
218 this.fields = fields;
220 if (typeof unique == "undefined")
223 this.unique = unique;
225 this.query_uuid = query_uuid;
227 if (typeof aq == "undefined")
228 this.analyzed_query = null;
230 this.analyzed_query = aq;
232 if (typeof sq == "undefined")
233 this.subqueries = {};
235 this.subqueries = sq;