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);
49 this.add_filter = function(key, op, value) {
50 this.filters.push(new Array(key, op, value));
52 this.update_filter = function(key, op, value) {
53 // Need to be improved...
54 // remove all occurrences of key if operation is not defined
56 this.filters = jQuery.grep(this.filters, function(val, i) {
59 // Else remove the key+op filters
61 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
63 this.filters.push(new Array(key, op, value));
66 this.remove_filter = function (key,op,value) {
67 // if operator is null then remove all occurences of this key
69 this.filters = jQuery.grep(this.filters, function(val, i) {
73 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
77 // FIXME These functions computing diff's between queries are meant to be shared
78 this.diff_fields = function(otherQuery) {
80 var f2 = otherQuery.fields;
82 /* added elements are the ones in f2 not in f1 */
83 var added = jQuery.grep(f2, function (x) { return jQuery.inArray(x, f1) == -1 });
84 /* removed elements are the ones in f1 not in f2 */
85 var removed = jQuery.grep(f1, function (x) { return jQuery.inArray(x, f2) == -1 });
87 return {'added':added, 'removed':removed};
90 // FIXME Modify filter to filters
91 this.diff_filter = function(otherQuery) {
92 var f1 = this.filters;
93 var f2 = otherQuery.filters;
95 /* added elements are the ones in f2 not in f1 */
96 var added = jQuery.grep(f2, function (x) { return !arrayInArray(x, f1)});
97 /* removed elements are the ones in f1 not in f2 */
98 var removed = jQuery.grep(f1, function (x) { return !arrayInArray(x, f2)});
100 return {'added':added, 'removed':removed};
103 // Callaback received 3 parameters: query, data, parent_query
104 this.iter_subqueries = function(callback, data)
106 rec = function(query, callback, data, parent_query) {
107 callback(query, data, parent_query);
108 jQuery.each(query.subqueries, function(object, subquery) {
109 rec(subquery, callback, data, query);
113 if (this.analyzed_query !== undefined)
114 query = this.analyzed_query;
118 rec(query, callback, data, null);
121 this.select = function(field)
123 this.fields.push(field);
126 this.unselect = function(field)
128 this.fields = $.grep(this.fields, function(x) { return x != field; });
131 // we send queries as a json string now
132 // this.as_POST = function() {
133 // return {'action': this.action, 'object': this.object, 'timestamp': this.timestamp,
134 // 'filters': this.filters, 'params': this.params, 'fields': this.fields};
136 this.analyze_subqueries = function() {
137 /* adapted from the PHP function in com_tophat/includes/query.php */
138 var q = new ManifoldQuery();
139 q.query_uuid = this.query_uuid;
140 q.action = this.action;
141 q.object = this.object;
142 q.timestamp = this.timestamp;
145 jQuery.each(this.filters, function(i, filter) {
149 var pos = k.indexOf('.');
151 var object = k.substr(0, pos);
152 var field = k.substr(pos+1);
153 if (!q.subqueries[object]) {
154 q.subqueries[object] = new ManifoldQuery();
155 q.subqueries[object].action = q.action;
156 q.subqueries[object].object = object;
157 q.subqueries[object].timestamp = q.timestamp;
159 q.subqueries[object].filters.push(Array(field, op, v));
161 q.filters.push(filter);
166 jQuery.each(this.params, function(param, value) {
167 var pos = param.indexOf('.');
169 var object = param.substr(0, pos);
170 var field = param.substr(pos+1);
171 if (!q.subqueries[object]) {
172 q.subqueries[object] = new ManifoldQuery();
173 q.subqueries[object].action = q.action;
174 q.subqueries[object].object = object;
175 q.subqueries[object].timestamp = q.timestamp;
177 q.subqueries[object].params[field] = value;
179 q.params[field] = value;
184 jQuery.each(this.fields, function(i, v) {
185 var pos = v.indexOf('.');
187 var object = v.substr(0, pos);
188 var field = v.substr(pos+1);
189 if (!q.subqueries[object]) {
190 q.subqueries[object] = new ManifoldQuery();
191 q.subqueries[object].action = q.action;
192 q.subqueries[object].object = object;
193 q.subqueries[object].timestamp = q.timestamp;
195 q.subqueries[object].fields.push(field);
200 this.analyzed_query = q;
204 if (typeof action == "undefined")
207 this.action = action;
209 if (typeof object == "undefined")
212 this.object = object;
214 if (typeof timestamp == "undefined")
215 this.timestamp = "now";
217 this.timestamp = timestamp;
219 if (typeof filters == "undefined")
222 this.filters = filters;
224 if (typeof params == "undefined")
227 this.params = params;
229 if (typeof fields == "undefined")
232 this.fields = fields;
234 if (typeof unique == "undefined")
237 this.unique = unique;
239 this.query_uuid = query_uuid;
241 if (typeof aq == "undefined")
242 this.analyzed_query = null;
244 this.analyzed_query = aq;
246 if (typeof sq == "undefined")
247 this.subqueries = {};
249 this.subqueries = sq;