improved subquery support
[myslice.git] / manifold / js / manifold-query.js
1 function ManifoldQuery(action, subject, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {  
2     // get, update, delete, create
3     var action;
4     // slice, user, network... 
5     var subject; 
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 subject WHERE filter;
27 UPDATE subject SET field=value WHERE filter; / returns SELECT 
28 DELETE FROM subject WHERE filter
29 INSERT INTO subject VALUES(field=value)
30 -------------------------------------------------------------*/
31     
32     this.__repr = function () {
33         res  = "ManyfoldQuery ";
34         res += " id=" + this.query_uuid;
35         res += " a=" + this.action;
36         res += " s=" + this.subject;
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 // we send queries as a json string now 
102 //    this.as_POST = function() {
103 //        return {'action': this.action, 'subject': this.subject, 'timestamp': this.timestamp,
104 //              'filters': this.filters, 'params': this.params, 'fields': this.fields};
105 //    }
106     this.analyze_subqueries = function() {
107         /* adapted from the PHP function in com_tophat/includes/query.php */
108         var q = new ManifoldQuery();
109         q.query_uuid = this.query_uuid;
110         q.action = this.action;
111         q.subject = this.subject;
112         q.timestamp = this.timestamp;
113
114         /* Filters */
115         jQuery.each(this.filters, function(i, filter) {
116             var k = filter[0];
117             var op = filter[1];
118             var v = filter[2];
119             var pos = k.indexOf('.');
120             if (pos != -1) {
121                 var subject = k.substr(0, pos);
122                 var field = k.substr(pos+1);
123                 if (!q.subqueries[subject]) {
124                     q.subqueries[subject] = new ManifoldQuery();
125                     q.subqueries[subject].action = this.action;
126                     q.subqueries[subject].subject = this.subject;
127                     q.subqueries[subject].timestamp = this.timestamp;
128                 }
129                 q.subqueries[subject].filters.push(Array(field, op, v));
130             } else {
131                 q.filters.push(this.filter);
132             }
133         });
134
135         /* Params */
136         jQuery.each(this.params, function(param, value) {
137             var pos = param.indexOf('.');
138             if (pos != -1) {
139                 var subject = param.substr(0, pos);
140                 var field = param.substr(pos+1);
141                 if (!q.subqueries[subject]) {
142                     q.subqueries[subject] = new ManifoldQuery();
143                     q.subqueries[subject].action = this.action;
144                     q.subqueries[subject].subject = this.subject;
145                     q.subqueries[subject].timestamp = this.timestamp;
146                 }
147                 q.subqueries[subject].params[field] = value;
148             } else {
149                 q.params[field] = value;
150             }
151         });
152
153         /* Fields */
154         jQuery.each(this.fields, function(i, v) {
155             var pos = v.indexOf('.');
156             if (pos != -1) {
157                 var subject = v.substr(0, pos);
158                 var field = v.substr(pos+1);
159                 if (!q.subqueries[subject]) {
160                     q.subqueries[subject] = new ManifoldQuery();
161                     q.subqueries[subject].action = this.action;
162                     q.subqueries[subject].subject = this.subject;
163                     q.subqueries[subject].timestamp = this.timestamp;
164                 }
165                 q.subqueries[subject].fields.push(field);
166             } else {
167                 q.fields.push(v);
168             }
169         });
170         this.analyzed_query = q;
171     }
172  
173     /* constructor */
174     if (typeof action == "undefined")
175         this.action = "get";
176     else
177         this.action = action;
178     
179     if (typeof subject == "undefined")
180         this.subject = null;
181     else
182         this.subject = subject;
183
184     if (typeof timestamp == "undefined")
185         this.timestamp = "now";
186     else
187         this.timestamp = timestamp;
188
189     if (typeof filters == "undefined")
190         this.filters = [];
191     else
192         this.filters = filters;
193
194     if (typeof params == "undefined")
195         this.params = {};
196     else
197         this.params = params;
198
199     if (typeof fields == "undefined")
200         this.fields = [];
201     else
202         this.fields = fields;
203
204     if (typeof unique == "undefined")
205         this.unique = false;
206     else
207         this.unique = unique;
208
209     this.query_uuid = query_uuid;
210     if (typeof analyzed_query == "undefined")
211         this.analyzed_query = null;
212     else
213         this.analyzed_query = aq;
214
215     if (typeof subqueries == "undefined")
216         this.subqueries = {};
217     else
218         this.subqueries = sq;
219 }