Skip to content

Commit

Permalink
Changes drivers.count() to be able to pass options (related to #68)
Browse files Browse the repository at this point in the history
This is required when using the countAccessor from hasMany associations
to be able to JOIN tables
  • Loading branch information
dresende committed Mar 11, 2013
1 parent 77641a8 commit 049e747
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/ChainFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function ChainFind(opts) {
return this;
},
count: function (cb) {
opts.driver.count(opts.table, opts.conditions, function (err, data) {
opts.driver.count(opts.table, opts.conditions, opts, function (err, data) {
if (err || data.length === 0) {
return cb(err);
}
Expand Down
17 changes: 13 additions & 4 deletions lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,21 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
}
};

Driver.prototype.count = function (table, conditions, cb) {
Driver.prototype.count = function (table, conditions, opts, cb) {
var q = this.query.select()
.from(table)
.count(null, 'c')
.where(conditions)
.build();
.count(opts.id, 'c');

if (opts.merge) {
q.from(opts.merge.from.table, opts.merge.from.field, opts.merge.to.field);
if (opts.merge.where && Object.keys(opts.merge.where[1]).length) {
q = q.where(opts.merge.where[0], opts.merge.where[1], conditions).build();
} else {
q = q.where(conditions).build();
}
} else {
q = q.where(conditions).build();
}

if (this.opts.pool) {
this.poolQuery(q, cb);
Expand Down
17 changes: 13 additions & 4 deletions lib/Drivers/DML/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,21 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
this.db.query(q, handleQuery(cb));
};

Driver.prototype.count = function (table, conditions, cb) {
Driver.prototype.count = function (table, conditions, opts, cb) {
var q = this.query.select()
.from(table)
.count(null, 'c')
.where(conditions)
.build();
.count(opts.id, 'c');

if (opts.merge) {
q.from(opts.merge.from.table, opts.merge.from.field, opts.merge.to.field);
if (opts.merge.where && Object.keys(opts.merge.where[1]).length) {
q = q.where(opts.merge.where[0], opts.merge.where[1], conditions).build();
} else {
q = q.where(conditions).build();
}
} else {
q = q.where(conditions).build();
}

if (this.opts.debug) {
require("../../Debug").sql('postgres', q);
Expand Down
17 changes: 13 additions & 4 deletions lib/Drivers/DML/postgresaxomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,21 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
}
this.hijackQuery(q, handleQuery(cb));
};
Driver.prototype.count = function (table, conditions, cb) {
Driver.prototype.count = function (table, conditions, opts, cb) {
var q = this.query.select()
.from(table)
.count(null, 'c')
.where(conditions)
.build();
.count(opts.id, 'c');

if (opts.merge) {
q.from(opts.merge.from.table, opts.merge.from.field, opts.merge.to.field);
if (opts.merge.where && Object.keys(opts.merge.where[1]).length) {
q = q.where(opts.merge.where[0], opts.merge.where[1], conditions).build();
} else {
q = q.where(conditions).build();
}
} else {
q = q.where(conditions).build();
}

if (this.opts.debug) {
require("../../Debug").sql('postgres', q);
Expand Down
17 changes: 13 additions & 4 deletions lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,21 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
this.db.all(q, cb);
};

Driver.prototype.count = function (table, conditions, cb) {
Driver.prototype.count = function (table, conditions, opts, cb) {
var q = this.query.select()
.from(table)
.count(null, 'c')
.where(conditions)
.build();
.count(opts.id, 'c');

if (opts.merge) {
q.from(opts.merge.from.table, opts.merge.from.field, opts.merge.to.field);
if (opts.merge.where && Object.keys(opts.merge.where[1]).length) {
q = q.where(opts.merge.where[0], opts.merge.where[1], conditions).build();
} else {
q = q.where(conditions).build();
}
} else {
q = q.where(conditions).build();
}

if (this.opts.debug) {
require("../../Debug").sql('sqlite', q);
Expand Down
6 changes: 3 additions & 3 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function Model(opts) {
if (options.hasOwnProperty("__merge")) {
merge = options.__merge;
delete options.__merge;
}
}
if (options.hasOwnProperty("order")) {
order = options.order;
delete options.order;
Expand Down Expand Up @@ -267,7 +267,7 @@ function Model(opts) {
throw new Error("Missing Model.count() callback");
}

opts.driver.count(opts.table, conditions, function (err, data) {
opts.driver.count(opts.table, conditions, {}, function (err, data) {
if (err || data.length === 0) {
return cb(err);
}
Expand All @@ -284,7 +284,7 @@ function Model(opts) {
var conditions = {};
conditions[opts.id] = id;

opts.driver.count(opts.table, conditions, function (err, data) {
opts.driver.count(opts.table, conditions, {}, function (err, data) {
if (err || data.length === 0) {
return cb(err);
}
Expand Down

0 comments on commit 049e747

Please sign in to comment.