manifold: now managing update queries
[myslice.git] / manifold / js / manifold-query.js
1 function ManifoldQuery(action, object, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {  
2     // get, update, delete, create
3     var action;
4     // slice, user, network... 
5     var object; 
6     // timestamp, now, latest(cache) : date of the results queried    
7     var timestamp;
8     // key(field),op(=<>),value
9     var filters;
10     // todo
11     var params;
12     // hostname, ip,... 
13     var fields;
14     // 0,1 : list of element of an object or single object  
15     var unique;
16     // query_uuid : unique identifier of a query
17     var query_uuid;
18     // Query : root query (no sub-Query)
19     var analyzed_query;
20     // {} : Assoc Table of sub-queries ["resources"->subQ1, "users"->subQ2]
21     var subqueries;
22
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 -------------------------------------------------------------*/
31     
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;
41         return res;
42     }   
43
44     this.clone = function() {
45         q = new ManifoldQuery();
46         return jQuery.extend(true, q, this);
47     }
48     this.add_filter = function(key, op, value) {
49         this.filters.push(new Array(key, op, value));
50     }
51     this.update_filter = function(key, op, value) {
52         // Need to be improved...
53         // remove all occurrences of key if operation is not defined
54         if(!op){
55             this.filters = jQuery.grep(this.filters, function(val, i) {
56                 return val[0] != key; 
57             });
58         // Else remove the key+op filters
59         }else{
60             this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
61         }
62         this.filters.push(new Array(key, op, value));
63     }
64
65     this.remove_filter = function (key,op,value) {
66         // if operator is null then remove all occurences of this key
67         if(!op){
68             this.filters = jQuery.grep(this.filters, function(val, i) { 
69                 return val[0] != key; 
70             });
71         }else{
72             this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
73         }
74     }
75
76     // FIXME These functions computing diff's between queries are meant to be shared
77     this.diff_fields = function(otherQuery) {
78         var f1 = this.fields;
79         var f2 = otherQuery.fields;
80
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 }); 
85         
86         return {'added':added, 'removed':removed};
87     }
88
89     // FIXME Modify filter to filters
90     this.diff_filter = function(otherQuery) {
91         var f1 = this.filters;
92         var f2 = otherQuery.filters;
93         
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)}); 
98         
99         return {'added':added, 'removed':removed};
100     } 
101
102     // Callaback received 3 parameters: query, data, parent_query
103     this.iter_subqueries = function(callback, data)
104     {
105         rec = function(query, callback, data, parent_query) {
106             callback(query, data, parent_query);
107             jQuery.each(query.subqueries, function(object, subquery) {
108                 rec(subquery, callback, data, query);
109             });
110         };
111
112         if (this.analyzed_query !== undefined)
113             query = this.analyzed_query;
114         else
115             query = this;
116
117         rec(query, callback, data, null);
118     }
119
120     this.select = function(field)
121     {
122         this.fields.push(field);
123     }
124
125     this.unselect = function(field)
126     {   
127         this.fields = $.grep(this.fields, function(x) { return x != field; });
128     }
129
130 // we send queries as a json string now 
131 //    this.as_POST = function() {
132 //        return {'action': this.action, 'object': this.object, 'timestamp': this.timestamp,
133 //              'filters': this.filters, 'params': this.params, 'fields': this.fields};
134 //    }
135     this.analyze_subqueries = function() {
136         /* adapted from the PHP function in com_tophat/includes/query.php */
137         var q = new ManifoldQuery();
138         q.query_uuid = this.query_uuid;
139         q.action = this.action;
140         q.object = this.object;
141         q.timestamp = this.timestamp;
142
143         /* Filters */
144         jQuery.each(this.filters, function(i, filter) {
145             var k = filter[0];
146             var op = filter[1];
147             var v = filter[2];
148             var pos = k.indexOf('.');
149             if (pos != -1) {
150                 var object = k.substr(0, pos);
151                 var field = k.substr(pos+1);
152                 if (!q.subqueries[object]) {
153                     q.subqueries[object] = new ManifoldQuery();
154                     q.subqueries[object].action = q.action;
155                     q.subqueries[object].object = object;
156                     q.subqueries[object].timestamp = q.timestamp;
157                 }
158                 q.subqueries[object].filters.push(Array(field, op, v));
159             } else {
160                 q.filters.push(filter);
161             }
162         });
163
164         /* Params */
165         jQuery.each(this.params, function(param, value) {
166             var pos = param.indexOf('.');
167             if (pos != -1) {
168                 var object = param.substr(0, pos);
169                 var field = param.substr(pos+1);
170                 if (!q.subqueries[object]) {
171                     q.subqueries[object] = new ManifoldQuery();
172                     q.subqueries[object].action = q.action;
173                     q.subqueries[object].object = object;
174                     q.subqueries[object].timestamp = q.timestamp;
175                 }
176                 q.subqueries[object].params[field] = value;
177             } else {
178                 q.params[field] = value;
179             }
180         });
181
182         /* Fields */
183         jQuery.each(this.fields, function(i, v) {
184             var pos = v.indexOf('.');
185             if (pos != -1) {
186                 var object = v.substr(0, pos);
187                 var field = v.substr(pos+1);
188                 if (!q.subqueries[object]) {
189                     q.subqueries[object] = new ManifoldQuery();
190                     q.subqueries[object].action = q.action;
191                     q.subqueries[object].object = object;
192                     q.subqueries[object].timestamp = q.timestamp;
193                 }
194                 q.subqueries[object].fields.push(field);
195             } else {
196                 q.fields.push(v);
197             }
198         });
199         this.analyzed_query = q;
200     }
201  
202     /* constructor */
203     if (typeof action == "undefined")
204         this.action = "get";
205     else
206         this.action = action;
207     
208     if (typeof object == "undefined")
209         this.object = null;
210     else
211         this.object = object;
212
213     if (typeof timestamp == "undefined")
214         this.timestamp = "now";
215     else
216         this.timestamp = timestamp;
217
218     if (typeof filters == "undefined")
219         this.filters = [];
220     else
221         this.filters = filters;
222
223     if (typeof params == "undefined")
224         this.params = {};
225     else
226         this.params = params;
227
228     if (typeof fields == "undefined")
229         this.fields = [];
230     else
231         this.fields = fields;
232
233     if (typeof unique == "undefined")
234         this.unique = false;
235     else
236         this.unique = unique;
237
238     this.query_uuid = query_uuid;
239
240     if (typeof aq == "undefined")
241         this.analyzed_query = null;
242     else
243         this.analyzed_query = aq;
244
245     if (typeof sq == "undefined")
246         this.subqueries = {};
247     else
248         this.subqueries = sq;
249 }