slice page working. added temporarily core and util classes from manifold core
[unfold.git] / manifold / js / manifold-query.js
index 7578c0a..2a1a162 100644 (file)
@@ -29,15 +29,15 @@ DELETE FROM subject WHERE filter
 INSERT INTO subject VALUES(field=value)
 -------------------------------------------------------------*/
     
-    this.__repr = function (q) {
+    this.__repr = function () {
        res  = "ManyfoldQuery ";
-       res += " id=" + q.query_uuid;
-       res += " a=" + q.action;
-       res += " s=" + q.subject;
-       res += " ts=" + q.timestamp;
-       res += " flts=" + q.filters;
-       res += " flds=" + q.fields;
-       res += " prms=" + q.params;
+       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;
     }  
 
@@ -61,7 +61,8 @@ INSERT INTO subject 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) { 
@@ -71,9 +72,9 @@ INSERT INTO subject 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;
 
@@ -83,10 +84,10 @@ INSERT INTO subject 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;
         
@@ -119,15 +120,15 @@ INSERT INTO subject VALUES(field=value)
             if (pos != -1) {
                 var subject = 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[subject]) {
+                    q.subqueries[subject] = new ManifoldQuery();
+                    q.subqueries[subject].action = q.action;
+                    q.subqueries[subject].subject = subject;
+                    q.subqueries[subject].timestamp = q.timestamp;
                 }
-                q.subqueries[this.subject].filters.push(Array(field, op, v));
+                q.subqueries[subject].filters.push(Array(field, op, v));
             } else {
-                q.filters.push(this.filter);
+                q.filters.push(filter);
             }
         });
 
@@ -137,13 +138,13 @@ INSERT INTO subject VALUES(field=value)
             if (pos != -1) {
                 var subject = 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[subject]) {
+                    q.subqueries[subject] = new ManifoldQuery();
+                    q.subqueries[subject].action = q.action;
+                    q.subqueries[subject].subject = subject;
+                    q.subqueries[subject].timestamp = q.timestamp;
                 }
-                q.subqueries[this.subject].params[field] = value;
+                q.subqueries[subject].params[field] = value;
             } else {
                 q.params[field] = value;
             }
@@ -155,13 +156,13 @@ INSERT INTO subject VALUES(field=value)
             if (pos != -1) {
                 var subject = 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[subject]) {
+                    q.subqueries[subject] = new ManifoldQuery();
+                    q.subqueries[subject].action = q.action;
+                    q.subqueries[subject].subject = subject;
+                    q.subqueries[subject].timestamp = q.timestamp;
                 }
-                q.subqueries[this.subject].fields.push(field);
+                q.subqueries[subject].fields.push(field);
             } else {
                 q.fields.push(v);
             }
@@ -170,14 +171,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 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 aq == "undefined")
+        this.analyzed_query = null;
+    else
+        this.analyzed_query = aq;
+
+    if (typeof sq == "undefined")
+        this.subqueries = {};
+    else
+        this.subqueries = sq;
 }