2 * This file is included in tophat_render.php
5 function Query(action, method, ts, filter, params, fields, unique, uuid, aq, sq)
7 // get, update, delete, create
10 // slice, user, network... : Metadata
13 // timestamp, now, latest(cache) : date of the results queried
16 // key(field),op(=<>),value
22 // hostname, ip,... : Metadata
25 // 0,1 : list of element of an object or single object
28 // uuid : unique identifier of a query
31 // Query : root query (no sub-Query)
34 // {} : Assoc Table of sub-queries ["resources"->subQ1, "users"->subQ2]
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 -------------------------------------------------------------*/
46 this.clone = function() {
48 return jQuery.extend(true, q, this);
50 this.add_filter = function(key, op, value) {
51 this.filter.push(new Array(key, op, value));
53 this.update_filter = function(key, op, value) {
54 // Need to be improved...
55 // remove all occurrences of key if operation is not defined
57 this.filter = jQuery.grep(this.filter, function(val, i) {
60 // Else remove the key+op filter
62 this.filter = jQuery.grep(this.filter, function(val, i) {return (val[0] != key || val[1] != op);});
64 this.filter.push(new Array(key, op, value));
66 this.remove_filter = function (key,op,value){
67 // if operator is null then remove all occurences of this key
69 this.filter = jQuery.grep(this.filter, function(val, i) {
73 this.filter = jQuery.grep(this.filter, function(val, i) {return (val[0] != key || val[1] != op);});
76 // FIXME These functions computing diff's between queries are meant to be shared
77 this.diff_fields = function (otherQuery)
80 var f2 = otherQuery.fields;
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 });
87 return {'added':added, 'removed':removed};
89 // FIXME Modify filter to filters
90 this.diff_filter = function (otherQuery)
93 var f2 = otherQuery.filter;
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)});
100 return {'added':added, 'removed':removed};
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};
106 this.analyze_subqueries = function() {
107 /* adapted from the PHP function in com_tophat/includes/query.php */
110 q.action = this.action;
111 q.method = this.method;
115 jQuery.each(this.filters, function(i, filter) {
119 var pos = k.indexOf('.');
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;
129 q.subqueries[this.method].filters.push(Array(field, op, v));
131 q.filters.push(this.filter);
136 jQuery.each(this.params, function(param, value) {
137 var pos = param.indexOf('.');
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;
147 q.subqueries[this.method].params[field] = value;
149 q.params[field] = value;
154 jQuery.each(this.fields, function(i, v) {
155 var pos = v.indexOf('.');
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;
165 q.subqueries[this.method].fields.push(field);
170 this.analyzed_query = q;
174 this.action = action;
175 this.method = method;
177 this.filter = filter;
178 this.params = params;
179 this.fields = fields;
180 this.unique = unique;
182 this.analyzed_query = aq;
183 this.subqueries = sq;