Skip to content

Commit

Permalink
feat: count in query
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Sep 30, 2016
1 parent 87a6882 commit 2b72ede
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/adapters/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,13 @@ class MySQLAdapter extends Adapter {
}

makeSql(type, model, options) {
if(type === "count") {
options.count = true;
}

switch(type) {
case "find": return this.makeFind(model, options);
case "count": return this.makeFind(model, options);
case "update": return this.makeUpdate(model, options);
case "delete": return this.makeDelete(model, options);
default: return this.makeFind(model, options);
Expand Down Expand Up @@ -679,6 +684,15 @@ class MySQLAdapter extends Adapter {
return _options;
}

count(query, callback) {
const _options = this.queryToOptions(query, {});
const sql = this.makeSet("count", query.model, _options);
this.execute(sql, function(err, rows) {
if(err) return callback(err, undefined, sql);
return callback(undefined, (rows || [ { "COUNT(0)": 0 } ])[0]["COUNT(0)"], sql);
});
}

find(query, callback, options) {
const _options = this.queryToOptions(query, options);
if(!query.cache || options.noCache) {
Expand Down
6 changes: 6 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class ToshihikoQuery {
return this;
}

count(callback) {
this.adapter.count(this, function(err, count, extra) {
return callback(err, count, extra);
});
}

find(callback, toJSON, options) {
if(typeof toJSON === "object") {
options = toJSON;
Expand Down

0 comments on commit 2b72ede

Please sign in to comment.