improved subquery support
[myslice.git] / manifold / js / manifold-query.js
index 09ba089..4d1ec0d 100644 (file)
@@ -1,44 +1,46 @@
-function ManifoldQuery(action, method, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {  
-
+function ManifoldQuery(action, subject, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {  
     // get, update, delete, create
     var action;
-    
-    // slice, user, network... : Metadata
-    var method; 
-    
+    // slice, user, network... 
+    var subject; 
     // timestamp, now, latest(cache) : date of the results queried    
     var timestamp;
-    
     // key(field),op(=<>),value
     var filters;
-
     // todo
     var params;
-    
-    // hostname, ip,... : Metadata
+    // hostname, ip,... 
     var fields;
-    
     // 0,1 : list of element of an object or single object  
     var unique;
-    
     // query_uuid : unique identifier of a query
     var query_uuid;
-    
     // Query : root query (no sub-Query)
     var analyzed_query;
-    
     // {} : Assoc Table of sub-queries ["resources"->subQ1, "users"->subQ2]
     var subqueries;
 
 /*-------------------------------------------------------------
               Query properties are SQL like : 
 ---------------------------------------------------------------
-SELECT fields FROM method WHERE filter;
-UPDATE method SET field=value WHERE filter; / returns SELECT 
-DELETE FROM method WHERE filter
-INSERT INTO method VALUES(field=value)
+SELECT fields FROM subject WHERE filter;
+UPDATE subject SET field=value WHERE filter; / returns SELECT 
+DELETE FROM subject WHERE filter
+INSERT INTO subject VALUES(field=value)
 -------------------------------------------------------------*/
     
+    this.__repr = function () {
+       res  = "ManyfoldQuery ";
+       res += " id=" + this.query_uuid;
+       res += " a=" + this.action;
+       res += " s=" + this.subject;
+       res += " ts=" + this.timestamp;
+       res += " flts=" + this.filters;
+       res += " flds=" + this.fields;
+       res += " prms=" + this.params;
+       return res;
+    }  
+
     this.clone = function() {
         q = new ManifoldQuery();
         return jQuery.extend(true, q, this);
@@ -59,7 +61,8 @@ INSERT INTO method VALUES(field=value)
         }
         this.filters.push(new Array(key, op, value));
     }
-    this.remove_filter = function (key,op,value){
+
+    this.remove_filter = function (key,op,value) {
         // if operator is null then remove all occurences of this key
         if(!op){
             this.filters = jQuery.grep(this.filters, function(val, i) { 
@@ -69,9 +72,9 @@ INSERT INTO method VALUES(field=value)
             this.filters = jQuery.grep(this.filters, function(val, i) {return (val[0] != key || val[1] != op);});
         }
     }
+
     // FIXME These functions computing diff's between queries are meant to be shared
-    this.diff_fields = function (otherQuery)
-    {
+    this.diff_fields = function (otherQuery) {
         var f1 = this.fields;
         var f2 = otherQuery.fields;
 
@@ -81,10 +84,10 @@ INSERT INTO method VALUES(field=value)
         var removed = jQuery.grep(f1, function (x) { return jQuery.inArray(x, f2) == -1 }); 
         
         return {'added':added, 'removed':removed};
-    }    
+    }
+
     // FIXME Modify filter to filters
-    this.diff_filter = function (otherQuery)
-    {
+    this.diff_filter = function (otherQuery) {
         var f1 = this.filters;
         var f2 = otherQuery.filters;
         
@@ -95,16 +98,17 @@ INSERT INTO method VALUES(field=value)
         
         return {'added':added, 'removed':removed};
     } 
-    this.to_hash = function() {
-        return {'action': this.action, 'method': this.method, 'timestamp': this.timestamp,
-               'filters': this.filters, 'params': this.params, 'fields': this.fields};
-    }
+// we send queries as a json string now 
+//    this.as_POST = function() {
+//        return {'action': this.action, 'subject': this.subject, 'timestamp': this.timestamp,
+//             'filters': this.filters, 'params': this.params, 'fields': this.fields};
+//    }
     this.analyze_subqueries = function() {
         /* adapted from the PHP function in com_tophat/includes/query.php */
         var q = new ManifoldQuery();
         q.query_uuid = this.query_uuid;
         q.action = this.action;
-        q.method = this.method;
+        q.subject = this.subject;
         q.timestamp = this.timestamp;
 
         /* Filters */
@@ -114,15 +118,15 @@ INSERT INTO method VALUES(field=value)
             var v = filter[2];
             var pos = k.indexOf('.');
             if (pos != -1) {
-                var method = k.substr(0, pos);
+                var subject = k.substr(0, pos);
                 var field = k.substr(pos+1);
-                if (jQuery.inArray(this.method, q.subqueries) == -1) {
-                    q.subqueries[this.method] = new ManifoldQuery();
-                    q.subqueries[this.method].action = this.action;
-                    q.subqueries[this.method].method = this.method;
-                    q.subqueries[this.method].timestamp = this.timestamp;
+                if (!q.subqueries[subject]) {
+                    q.subqueries[subject] = new ManifoldQuery();
+                    q.subqueries[subject].action = this.action;
+                    q.subqueries[subject].subject = this.subject;
+                    q.subqueries[subject].timestamp = this.timestamp;
                 }
-                q.subqueries[this.method].filters.push(Array(field, op, v));
+                q.subqueries[subject].filters.push(Array(field, op, v));
             } else {
                 q.filters.push(this.filter);
             }
@@ -132,15 +136,15 @@ INSERT INTO method VALUES(field=value)
         jQuery.each(this.params, function(param, value) {
             var pos = param.indexOf('.');
             if (pos != -1) {
-                var method = param.substr(0, pos);
+                var subject = param.substr(0, pos);
                 var field = param.substr(pos+1);
-                if (jQuery.inArray(this.method, q.subqueries) == -1) {
-                    q.subqueries[this.method] = new ManifoldQuery();
-                    q.subqueries[this.method].action = this.action;
-                    q.subqueries[this.method].method = this.method;
-                    q.subqueries[this.method].timestamp = this.timestamp;
+                if (!q.subqueries[subject]) {
+                    q.subqueries[subject] = new ManifoldQuery();
+                    q.subqueries[subject].action = this.action;
+                    q.subqueries[subject].subject = this.subject;
+                    q.subqueries[subject].timestamp = this.timestamp;
                 }
-                q.subqueries[this.method].params[field] = value;
+                q.subqueries[subject].params[field] = value;
             } else {
                 q.params[field] = value;
             }
@@ -150,15 +154,15 @@ INSERT INTO method VALUES(field=value)
         jQuery.each(this.fields, function(i, v) {
             var pos = v.indexOf('.');
             if (pos != -1) {
-                var method = v.substr(0, pos);
+                var subject = v.substr(0, pos);
                 var field = v.substr(pos+1);
-                if (jQuery.inArray(this.method, q.subqueries) == -1) {
-                    q.subqueries[this.method] = new ManifoldQuery();
-                    q.subqueries[this.method].action = this.action;
-                    q.subqueries[this.method].method = this.method;
-                    q.subqueries[this.method].timestamp = this.timestamp;
+                if (!q.subqueries[subject]) {
+                    q.subqueries[subject] = new ManifoldQuery();
+                    q.subqueries[subject].action = this.action;
+                    q.subqueries[subject].subject = this.subject;
+                    q.subqueries[subject].timestamp = this.timestamp;
                 }
-                q.subqueries[this.method].fields.push(field);
+                q.subqueries[subject].fields.push(field);
             } else {
                 q.fields.push(v);
             }
@@ -167,14 +171,49 @@ INSERT INTO method VALUES(field=value)
     }
  
     /* constructor */
-    this.action = action;
-    this.method = method;
-    this.timestamp = timestamp;
-    this.filters = filters;
-    this.params = params;
-    this.fields = fields;
-    this.unique = unique;
+    if (typeof action == "undefined")
+        this.action = "get";
+    else
+        this.action = action;
+    
+    if (typeof subject == "undefined")
+        this.subject = null;
+    else
+        this.subject = subject;
+
+    if (typeof timestamp == "undefined")
+        this.timestamp = "now";
+    else
+        this.timestamp = timestamp;
+
+    if (typeof filters == "undefined")
+        this.filters = [];
+    else
+        this.filters = filters;
+
+    if (typeof params == "undefined")
+        this.params = {};
+    else
+        this.params = params;
+
+    if (typeof fields == "undefined")
+        this.fields = [];
+    else
+        this.fields = fields;
+
+    if (typeof unique == "undefined")
+        this.unique = false;
+    else
+        this.unique = unique;
+
     this.query_uuid = query_uuid;
-    this.analyzed_query = aq;
-    this.subqueries = sq;
+    if (typeof analyzed_query == "undefined")
+        this.analyzed_query = null;
+    else
+        this.analyzed_query = aq;
+
+    if (typeof subqueries == "undefined")
+        this.subqueries = {};
+    else
+        this.subqueries = sq;
 }