Permalink
Browse files

Added `count` call to query collections. Fixed a little bug in

`destroyAll`.
  • Loading branch information...
1 parent a2c07b5 commit a9fa1897fcd1629f593a16e780b5f0c1bad7abdf @zefhemel zefhemel committed Jun 23, 2010
Showing with 41 additions and 5 deletions.
  1. +7 −4 README.md
  2. +34 −1 persistence.js
View
@@ -290,6 +290,10 @@ A `QueryCollection` has the following methods:
Returns a new `QueryCollection` that prefetches entities linked
through relationship `rel`, note that this only works for one-to-one
and many-to-one relationships.
+* `add(obj)`
+ Adds object `obj` to the collection.
+* `remove(obj)`
+ Removes object `obj` from the collection.
* `list(tx, callback)`
Asynchronously fetches the results matching the formulated query.
Once retrieved, the callback function is invoked with an array of
@@ -298,15 +302,14 @@ A `QueryCollection` has the following methods:
Asynchronously fetches the results matching the formulated query.
Once retrieved, the `eachCallback` function is invoked on each
element of the result objects.
-* `add(obj)`
- Adds object `obj` to the collection.
-* `remove(obj)`
- Removes object `obj` from the collection.
* `one(tx, callback)`
Asynchronously fetches the first element of the collection, or `null` if none.
* `destroyAll(tx, callback)`
Asynchronously removes all the items in the collection. __Important__: this does
not only remove the items from the collection, but removes the items themselves!
+* `count(tx, callback)`
+ Asynchronously counts the number of items in the collection. The arguments passed
+ to the `callback` function is the number of items.
Query collections are returned by:
View
@@ -1461,7 +1461,7 @@ var persistence = (window && window.persistence) ? window.persistence : {};
}
if(!tx) { // no transaction supplied
persistence.transaction(function(tx) {
- that.clear(tx, callback);
+ that.destroyAll(tx, callback);
});
return;
}
@@ -1480,6 +1480,39 @@ var persistence = (window && window.persistence) ? window.persistence : {};
};
/**
+ * Asynchronous call to count the number of items in the collection.
+ * @param tx transaction to use
+ * @param callback function to be called when clearing has completed
+ */
+ DbQueryCollection.prototype.count = function (tx, callback) {
+ var that = this;
+ if(tx && !tx.executeSql) { // provided callback as first argument
+ callback = tx;
+ tx = null;
+ }
+ if(!tx) { // no transaction supplied
+ persistence.transaction(function(tx) {
+ that.count(tx, callback);
+ });
+ return;
+ }
+ var entityName = this._entityName;
+
+ var args = [];
+ var whereSql = "WHERE "
+ + [ this._filter.sql("", args) ].concat(
+ this._additionalWhereSqls).join(' AND ');
+
+ var sql = "SELECT COUNT(*) AS cnt FROM `" + entityName + "` " + whereSql;
+
+ persistence.flush(tx, function () {
+ tx.executeSql(sql, args, function(results) {
+ callback(results[0].cnt);
+ });
+ });
+ };
+
+ /**
* An implementation of QueryCollection, that is used
* to represent all instances of an entity type
* @constructor

0 comments on commit a9fa189

Please sign in to comment.