+
+// Constants that should be somehow moved to a plugin.js file
+var FILTER_ADDED = 1;
+var FILTER_REMOVED = 2;
+var CLEAR_FILTERS = 3;
+var FIELD_ADDED = 4;
+var FIELD_REMOVED = 5;
+var CLEAR_FIELDS = 6;
+var NEW_RECORD = 7;
+var CLEAR_RECORDS = 8;
+var FIELD_STATE_CHANGED = 9;
+
+var IN_PROGRESS = 101;
+var DONE = 102;
+
+/* Update requests related to subqueries */
+var SET_ADD = 201;
+var SET_REMOVED = 202;
+
+// request
+var FIELD_REQUEST_CHANGE = 301;
+var FIELD_REQUEST_ADD = 302;
+var FIELD_REQUEST_REMOVE = 303;
+var FIELD_REQUEST_RESET = 304;
+// status
+var FIELD_REQUEST_PENDING = 301;
+var FIELD_REQUEST_SUCCESS = 302;
+var FIELD_REQUEST_FAILURE = 303;
+
+/* Query status */
+var STATUS_NONE = 500; // Query has not been started yet
+var STATUS_GET_IN_PROGRESS = 501; // Query has been sent, no result has been received
+var STATUS_GET_RECEIVED = 502; // Success
+var STATUS_GET_ERROR = 503; // Error
+var STATUS_UPDATE_PENDING = 504;
+var STATUS_UPDATE_IN_PROGRESS = 505;
+var STATUS_UPDATE_RECEIVED = 506;
+var STATUS_UPDATE_ERROR = 507;
+
+/* Requests for query cycle */
+var RUN_UPDATE = 601;
+
+// A structure for storing queries
+
+
+
+function QueryExt(query, parent_query_ext, main_query_ext, update_query_ext, disabled) {
+
+ /* Constructor */
+ if (typeof query == "undefined")
+ throw "Must pass a query in QueryExt constructor";
+ this.query = query
+ this.parent_query_ext = (typeof parent_query_ext == "undefined") ? null : parent_query_ext
+ this.main_query_ext = (typeof main_query_ext == "undefined") ? null : main_query_ext
+ this.update_query_ext = (typeof update_query_ext == "undefined") ? null : update_query_ext
+ this.disabled = (typeof update_query_ext == "undefined") ? false : disabled
+
+ this.status = null;
+ this.results = null;
+ // update_query null unless we are a main_query (aka parent_query == null); only main_query_fields can be updated...
+}
+
+function QueryStore() {
+
+ this.main_queries = {};
+ this.analyzed_queries = {};
+
+ /* Insertion */
+
+ this.insert = function(query)
+ {
+ // We expect only main_queries are inserted
+
+ /* If the query has not been analyzed, then we analyze it */
+ if (query.analyzed_query == null) {
+ query.analyze_subqueries();
+ }
+
+ /* We prepare the update query corresponding to the main query and store both */
+ /* Note: they have the same UUID */
+
+ // XXX query.change_action() should become deprecated
+ update_query = query.clone();
+ update_query.action = 'update';
+ update_query.analyzed_query.action = 'update';
+ update_query.params = {};
+ update_query_ext = new QueryExt(update_query);
+ console.log("Update query created from Get query", update_query);
+
+ /* We store the main query */
+ query_ext = new QueryExt(query, null, null, update_query_ext, false);
+ manifold.query_store.main_queries[query.query_uuid] = query_ext;
+ /* Note: the update query does not have an entry! */
+
+
+ // The query is disabled; since it is incomplete until we know the content of the set of subqueries
+ // XXX unless we have no subqueries ???
+ // we will complete with params when records are received... this has to be done by the manager
+ // SET_ADD, SET_REMOVE will change the status of the elements of the set
+ // UPDATE will change also, etc.
+ // XXX We need a proper structure to store this information...
+
+ // We also need to insert all queries and subqueries from the analyzed_query
+ // XXX We need the root of all subqueries
+ query.iter_subqueries(function(sq, data, parent_query) {
+ if (parent_query)
+ parent_query_ext = manifold.query_store.find_analyzed_query_ext(parent_query.query_uuid);
+ else
+ parent_query_ext = null;
+ // XXX parent_query_ext == false
+ // XXX main.subqueries = {} # Normal, we need analyzed_query
+ sq_ext = new QueryExt(sq, parent_query_ext, query_ext)
+ manifold.query_store.analyzed_queries[sq.query_uuid] = sq_ext;
+ });
+
+ // XXX We have spurious update queries...
+ }
+
+ /* Searching */
+
+ this.find_query_ext = function(query_uuid)
+ {
+ return this.main_queries[query_uuid];
+ }
+
+ this.find_query = function(query_uuid)
+ {
+ return this.find_query_ext(query_uuid).query;
+ }
+
+ this.find_analyzed_query_ext = function(query_uuid)
+ {
+ return this.analyzed_queries[query_uuid];
+ }
+
+ this.find_analyzed_query = function(query_uuid)
+ {
+ return this.find_analyzed_query_ext(query_uuid).query;
+ }
+}
+
+/*!
+ * This namespace holds functions for globally managing query objects
+ * \Class Manifold
+ */