Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for skip and limit in DbQueryCollection::count() #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/persistence.store.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,19 @@ function config(persistence, dialect) {
var args = [];
var whereSql = "WHERE " + [ this._filter.sql(meta, "root", args) ].concat(additionalWhereSqls).join(' AND ');

var sql = "SELECT COUNT(*) AS cnt FROM `" + entityName + "` AS `root` " + joinSql + " " + whereSql;
var skipLimitSql = '';
if(this._limit >= 0) {
skipLimitSql += " LIMIT " + this._limit;
}
if(this._skip > 0) {
skipLimitSql += " OFFSET " + this._skip;
}

if (skipLimitSql === '') {
var sql = "SELECT COUNT(*) AS cnt FROM `" + entityName + "` AS `root` " + joinSql + " " + whereSql;
} else {
var sql = "SELECT COUNT(*) AS cnt FROM (SELECT id FROM `" + entityName + "` AS `root` " + joinSql + " " + whereSql + skipLimitSql + ")";
}

session.flush(tx, function () {
tx.executeSql(sql, args, function(results) {
Expand Down
30 changes: 18 additions & 12 deletions test/browser/test.persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,15 @@ $(document).ready(function(){
tasks.push(t);
coll.add(t);
}
coll.order("counter", true).limit(5).list(function(results) {
equals(results.length, 5, "Result length check");
for(var i = 0; i < 5; i++) {
equals(results[i].id, tasks[i].id, "limit check");
}
start();
coll.order("counter", true).limit(5).count(function(count) {
equals(count, 5, "Count check");
coll.order("counter", true).limit(5).list(function(results) {
equals(results.length, 5, "Result length check");
for(var i = 0; i < 5; i++) {
equals(results[i].id, tasks[i].id, "limit check");
}
start();
});
});
}

Expand All @@ -548,12 +551,15 @@ $(document).ready(function(){
tasks.push(t);
coll.add(t);
}
coll.order("counter", true).skip(5).limit(5).list(function(results) {
equals(results.length, 5, "Result length check");
for(var i = 5; i < 10; i++) {
equals(results[i-5].id, tasks[i].id, "skip check");
}
start();
coll.order("counter", true).skip(5).limit(5).count(function(count) {
equals(count, 5, "Count check");
coll.order("counter", true).skip(5).limit(5).list(function(results) {
equals(results.length, 5, "Result length check");
for(var i = 5; i < 10; i++) {
equals(results[i-5].id, tasks[i].id, "skip check");
}
start();
});
});
}

Expand Down