Attempts for a simpler plugin API: both coexist, tests with hazelnut plugin
[myslice.git] / manifold / js / manifold-query.js
index 7d885ae..c6ebaab 100644 (file)
@@ -1,8 +1,8 @@
-function ManifoldQuery(action, subject, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {  
+function ManifoldQuery(action, object, timestamp, filters, params, fields, unique, query_uuid, aq, sq) {  
     // get, update, delete, create
     var action;
     // slice, user, network... 
-    var subject; 
+    var object; 
     // timestamp, now, latest(cache) : date of the results queried    
     var timestamp;
     // key(field),op(=<>),value
@@ -23,17 +23,17 @@ function ManifoldQuery(action, subject, timestamp, filters, params, fields, uniq
 /*-------------------------------------------------------------
               Query properties are SQL like : 
 ---------------------------------------------------------------
-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)
+SELECT fields FROM object WHERE filter;
+UPDATE object SET field=value WHERE filter; / returns SELECT 
+DELETE FROM object WHERE filter
+INSERT INTO object VALUES(field=value)
 -------------------------------------------------------------*/
     
     this.__repr = function () {
-       res  = "ManyfoldQuery ";
+       res  = "ManifoldQuery ";
        res += " id=" + this.query_uuid;
        res += " a=" + this.action;
-       res += " s=" + this.subject;
+       res += " o=" + this.object;
        res += " ts=" + this.timestamp;
        res += " flts=" + this.filters;
        res += " flds=" + this.fields;
@@ -74,7 +74,7 @@ INSERT INTO subject VALUES(field=value)
     }
 
     // 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;
 
@@ -87,7 +87,7 @@ INSERT INTO subject VALUES(field=value)
     }
 
     // FIXME Modify filter to filters
-    this.diff_filter = function (otherQuery) {
+    this.diff_filter = function(otherQuery) {
         var f1 = this.filters;
         var f2 = otherQuery.filters;
         
@@ -98,9 +98,25 @@ INSERT INTO subject VALUES(field=value)
         
         return {'added':added, 'removed':removed};
     } 
+
+    this.iter_subqueries = function(callback, data)
+    {
+        rec = function(query, callback, data) {
+            jQuery.each(query.subqueries, function(object, subquery) {
+                rec(subquery, callback);
+            });
+            callback(query, data);
+        };
+        if (this.analyzed_query !== undefined)
+            query = this.analyzed_query;
+        else
+            query = this
+        rec(query, callback, data);
+    }
+
 // we send queries as a json string now 
 //    this.as_POST = function() {
-//        return {'action': this.action, 'subject': this.subject, 'timestamp': this.timestamp,
+//        return {'action': this.action, 'object': this.object, 'timestamp': this.timestamp,
 //             'filters': this.filters, 'params': this.params, 'fields': this.fields};
 //    }
     this.analyze_subqueries = function() {
@@ -108,7 +124,7 @@ INSERT INTO subject VALUES(field=value)
         var q = new ManifoldQuery();
         q.query_uuid = this.query_uuid;
         q.action = this.action;
-        q.subject = this.subject;
+        q.object = this.object;
         q.timestamp = this.timestamp;
 
         /* Filters */
@@ -118,17 +134,17 @@ INSERT INTO subject VALUES(field=value)
             var v = filter[2];
             var pos = k.indexOf('.');
             if (pos != -1) {
-                var subject = k.substr(0, pos);
+                var object = k.substr(0, pos);
                 var field = k.substr(pos+1);
-                if (jQuery.inArray(this.subject, q.subqueries) == -1) {
-                    q.subqueries[this.subject] = new ManifoldQuery();
-                    q.subqueries[this.subject].action = this.action;
-                    q.subqueries[this.subject].subject = this.subject;
-                    q.subqueries[this.subject].timestamp = this.timestamp;
+                if (!q.subqueries[object]) {
+                    q.subqueries[object] = new ManifoldQuery();
+                    q.subqueries[object].action = q.action;
+                    q.subqueries[object].object = object;
+                    q.subqueries[object].timestamp = q.timestamp;
                 }
-                q.subqueries[this.subject].filters.push(Array(field, op, v));
+                q.subqueries[object].filters.push(Array(field, op, v));
             } else {
-                q.filters.push(this.filter);
+                q.filters.push(filter);
             }
         });
 
@@ -136,15 +152,15 @@ INSERT INTO subject VALUES(field=value)
         jQuery.each(this.params, function(param, value) {
             var pos = param.indexOf('.');
             if (pos != -1) {
-                var subject = param.substr(0, pos);
+                var object = param.substr(0, pos);
                 var field = param.substr(pos+1);
-                if (jQuery.inArray(this.subject, q.subqueries) == -1) {
-                    q.subqueries[this.subject] = new ManifoldQuery();
-                    q.subqueries[this.subject].action = this.action;
-                    q.subqueries[this.subject].subject = this.subject;
-                    q.subqueries[this.subject].timestamp = this.timestamp;
+                if (!q.subqueries[object]) {
+                    q.subqueries[object] = new ManifoldQuery();
+                    q.subqueries[object].action = q.action;
+                    q.subqueries[object].object = object;
+                    q.subqueries[object].timestamp = q.timestamp;
                 }
-                q.subqueries[this.subject].params[field] = value;
+                q.subqueries[object].params[field] = value;
             } else {
                 q.params[field] = value;
             }
@@ -154,15 +170,15 @@ INSERT INTO subject VALUES(field=value)
         jQuery.each(this.fields, function(i, v) {
             var pos = v.indexOf('.');
             if (pos != -1) {
-                var subject = v.substr(0, pos);
+                var object = v.substr(0, pos);
                 var field = v.substr(pos+1);
-                if (jQuery.inArray(this.subject, q.subqueries) == -1) {
-                    q.subqueries[this.subject] = new ManifoldQuery();
-                    q.subqueries[this.subject].action = this.action;
-                    q.subqueries[this.subject].subject = this.subject;
-                    q.subqueries[this.subject].timestamp = this.timestamp;
+                if (!q.subqueries[object]) {
+                    q.subqueries[object] = new ManifoldQuery();
+                    q.subqueries[object].action = q.action;
+                    q.subqueries[object].object = object;
+                    q.subqueries[object].timestamp = q.timestamp;
                 }
-                q.subqueries[this.subject].fields.push(field);
+                q.subqueries[object].fields.push(field);
             } else {
                 q.fields.push(v);
             }
@@ -171,14 +187,50 @@ INSERT INTO subject VALUES(field=value)
     }
  
     /* constructor */
-    this.action = action;
-    this.subject = subject;
-    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 object == "undefined")
+        this.object = null;
+    else
+        this.object = object;
+
+    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 aq == "undefined")
+        this.analyzed_query = null;
+    else
+        this.analyzed_query = aq;
+
+    if (typeof sq == "undefined")
+        this.subqueries = {};
+    else
+        this.subqueries = sq;
 }