1 function Query(action, method, timestamp, filters, params, fields, unique, uuid, aq, sq)
3 // get, update, delete, create
6 // slice, user, network... : Metadata
9 // timestamp, now, latest(cache) : date of the results queried
12 // key(field),op(=<>),value
18 // hostname, ip,... : Metadata
21 // 0,1 : list of element of an object or single object
24 // uuid : unique identifier of a query
27 // Query : root query (no sub-Query)
30 // {} : Assoc Table of sub-queries ["resources"->subQ1, "users"->subQ2]
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 -------------------------------------------------------------*/
42 this.clone = function() {
44 return jQuery.extend(true, q, this);
46 this.add_filter = function(key, op, value) {
47 this.filters.push(new Array(key, op, value));
49 this.update_filter = function(key, op, value) {
50 // Need to be improved...
51 // remove all occurrences of key if operation is not defined
53 this.filters = jQuery.grep(this.filters, function(val, i) {
56 // Else remove the key+op filters
58 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
60 this.filters.push(new Array(key, op, value));
62 this.remove_filter = function (key,op,value){
63 // if operator is null then remove all occurences of this key
65 this.filters = jQuery.grep(this.filters, function(val, i) {
69 this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
72 // FIXME These functions computing diff's between queries are meant to be shared
73 this.diff_fields = function (otherQuery)
76 var f2 = otherQuery.fields;
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 });
83 return {'added':added, 'removed':removed};
85 // FIXME Modify filter to filters
86 this.diff_filter = function (otherQuery)
88 var f1 = this.filters;
89 var f2 = otherQuery.filters;
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)});
96 return {'added':added, 'removed':removed};
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};
102 this.analyze_subqueries = function() {
103 /* adapted from the PHP function in com_tophat/includes/query.php */
106 q.action = this.action;
107 q.method = this.method;
108 q.timestamp = this.timestamp;
111 jQuery.each(this.filters, function(i, filter) {
115 var pos = k.indexOf('.');
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 Query();
121 q.subqueries[this.method].action = this.action;
122 q.subqueries[this.method].method = this.method;
123 q.subqueries[this.method].timestamp = this.timestamp;
125 q.subqueries[this.method].filters.push(Array(field, op, v));
127 q.filters.push(this.filter);
132 jQuery.each(this.params, function(param, value) {
133 var pos = param.indexOf('.');
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 Query();
139 q.subqueries[this.method].action = this.action;
140 q.subqueries[this.method].method = this.method;
141 q.subqueries[this.method].timestamp = this.timestamp;
143 q.subqueries[this.method].params[field] = value;
145 q.params[field] = value;
150 jQuery.each(this.fields, function(i, v) {
151 var pos = v.indexOf('.');
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 Query();
157 q.subqueries[this.method].action = this.action;
158 q.subqueries[this.method].method = this.method;
159 q.subqueries[this.method].timestamp = this.timestamp;
161 q.subqueries[this.method].fields.push(field);
166 this.analyzed_query = q;
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;
178 this.analyzed_query = aq;
179 this.subqueries = sq;