rename js Query into ManifoldQuery
[myslice.git] / unfold / js / manifold-query.js
1 function ManifoldQuery(action, method, timestamp, filters, params, fields, unique, uuid, aq, sq)
2 {  
3     // get, update, delete, create
4     var action;
5     
6     // slice, user, network... : Metadata
7     var method; 
8     
9     // timestamp, now, latest(cache) : date of the results queried    
10     var timestamp;
11     
12     // key(field),op(=<>),value
13     var filters;
14
15     // todo
16     var params;
17     
18     // hostname, ip,... : Metadata
19     var fields;
20     
21     // 0,1 : list of element of an object or single object  
22     var unique;
23     
24     // uuid : unique identifier of a query
25     var uuid;
26     
27     // Query : root query (no sub-Query)
28     var analyzed_query;
29     
30     // {} : Assoc Table of sub-queries ["resources"->subQ1, "users"->subQ2]
31     var subqueries;
32
33 /*-------------------------------------------------------------
34               Query properties are SQL like : 
35 ---------------------------------------------------------------
36 SELECT fields FROM method WHERE filter;
37 UPDATE method SET field=value WHERE filter; / returns SELECT 
38 DELETE FROM method WHERE filter
39 INSERT INTO method VALUES(field=value)
40 -------------------------------------------------------------*/
41     
42     this.clone = function() {
43         q = new ManifoldQuery();
44         return jQuery.extend(true, q, this);
45     }
46     this.add_filter = function(key, op, value) {
47         this.filters.push(new Array(key, op, value));
48     }
49     this.update_filter = function(key, op, value) {
50         // Need to be improved...
51         // remove all occurrences of key if operation is not defined
52         if(!op){
53             this.filters = jQuery.grep(this.filters, function(val, i) {
54                 return val[0] != key; 
55             });
56         // Else remove the key+op filters
57         }else{
58             this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
59         }
60         this.filters.push(new Array(key, op, value));
61     }
62     this.remove_filter = function (key,op,value){
63         // if operator is null then remove all occurences of this key
64         if(!op){
65             this.filters = jQuery.grep(this.filters, function(val, i) { 
66                 return val[0] != key; 
67             });
68         }else{
69             this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
70         }
71     }
72     // FIXME These functions computing diff's between queries are meant to be shared
73     this.diff_fields = function (otherQuery)
74     {
75         var f1 = this.fields;
76         var f2 = otherQuery.fields;
77
78         /* added elements are the ones in f2 not in f1 */
79         var added   = jQuery.grep(f2, function (x) { return jQuery.inArray(x, f1) == -1 }); 
80         /* removed elements are the ones in f1 not in f2 */
81         var removed = jQuery.grep(f1, function (x) { return jQuery.inArray(x, f2) == -1 }); 
82         
83         return {'added':added, 'removed':removed};
84     }    
85     // FIXME Modify filter to filters
86     this.diff_filter = function (otherQuery)
87     {
88         var f1 = this.filters;
89         var f2 = otherQuery.filters;
90         
91         /* added elements are the ones in f2 not in f1 */
92         var added   = jQuery.grep(f2, function (x) { return !arrayInArray(x, f1)}); 
93         /* removed elements are the ones in f1 not in f2 */
94         var removed = jQuery.grep(f1, function (x) { return !arrayInArray(x, f2)}); 
95         
96         return {'added':added, 'removed':removed};
97     } 
98     this.to_hash = function() {
99         return {'action': this.action, 'method': this.method, 'timestamp': this.timestamp,
100                 'filters': this.filters, 'params': this.params, 'fields': this.fields};
101     }
102     this.analyze_subqueries = function() {
103         /* adapted from the PHP function in com_tophat/includes/query.php */
104         var q = new ManifoldQuery();
105         q.uuid = this.uuid;
106         q.action = this.action;
107         q.method = this.method;
108         q.timestamp = this.timestamp;
109
110         /* Filters */
111         jQuery.each(this.filters, function(i, filter) {
112             var k = filter[0];
113             var op = filter[1];
114             var v = filter[2];
115             var pos = k.indexOf('.');
116             if (pos != -1) {
117                 var method = k.substr(0, pos);
118                 var field = k.substr(pos+1);
119                 if (jQuery.inArray(this.method, q.subqueries) == -1) {
120                     q.subqueries[this.method] = new ManifoldQuery();
121                     q.subqueries[this.method].action = this.action;
122                     q.subqueries[this.method].method = this.method;
123                     q.subqueries[this.method].timestamp = this.timestamp;
124                 }
125                 q.subqueries[this.method].filters.push(Array(field, op, v));
126             } else {
127                 q.filters.push(this.filter);
128             }
129         });
130
131         /* Params */
132         jQuery.each(this.params, function(param, value) {
133             var pos = param.indexOf('.');
134             if (pos != -1) {
135                 var method = param.substr(0, pos);
136                 var field = param.substr(pos+1);
137                 if (jQuery.inArray(this.method, q.subqueries) == -1) {
138                     q.subqueries[this.method] = new ManifoldQuery();
139                     q.subqueries[this.method].action = this.action;
140                     q.subqueries[this.method].method = this.method;
141                     q.subqueries[this.method].timestamp = this.timestamp;
142                 }
143                 q.subqueries[this.method].params[field] = value;
144             } else {
145                 q.params[field] = value;
146             }
147         });
148
149         /* Fields */
150         jQuery.each(this.fields, function(i, v) {
151             var pos = v.indexOf('.');
152             if (pos != -1) {
153                 var method = v.substr(0, pos);
154                 var field = v.substr(pos+1);
155                 if (jQuery.inArray(this.method, q.subqueries) == -1) {
156                     q.subqueries[this.method] = new ManifoldQuery();
157                     q.subqueries[this.method].action = this.action;
158                     q.subqueries[this.method].method = this.method;
159                     q.subqueries[this.method].timestamp = this.timestamp;
160                 }
161                 q.subqueries[this.method].fields.push(field);
162             } else {
163                 q.fields.push(v);
164             }
165         });
166         this.analyzed_query = q;
167     }
168  
169     /* constructor */
170     this.action = action;
171     this.method = method;
172     this.timestamp = timestamp;
173     this.filters = filters;
174     this.params = params;
175     this.fields = fields;
176     this.unique = unique;
177     this.uuid = uuid;
178     this.analyzed_query = aq;
179     this.subqueries = sq;
180 }