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