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