Merge pull request #114 from lehni/fix/count-transaction
Support transactions in count()
This commit is contained in:
commit
fa6f51d198
|
@ -1518,7 +1518,7 @@ SQLConnector.prototype.count = function(model, where, options, cb) {
|
||||||
this.tableEscaped(model));
|
this.tableEscaped(model));
|
||||||
stmt = stmt.merge(this.buildWhere(model, where));
|
stmt = stmt.merge(this.buildWhere(model, where));
|
||||||
stmt = this.parameterize(stmt);
|
stmt = this.parameterize(stmt);
|
||||||
this.execute(stmt.sql, stmt.params,
|
this.execute(stmt.sql, stmt.params, options,
|
||||||
function(err, res) {
|
function(err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
|
|
@ -212,28 +212,20 @@ TestConnector.prototype.rollback = function(tx, cb) {
|
||||||
TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
|
TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
|
||||||
var transaction = options.transaction;
|
var transaction = options.transaction;
|
||||||
var model = options.model;
|
var model = options.model;
|
||||||
if (transaction && transaction.connector === this && transaction.connection) {
|
var useTransaction = transaction && transaction.connector === this &&
|
||||||
|
transaction.connection;
|
||||||
|
var data = useTransaction ? transaction.connection.data : this.data;
|
||||||
|
var name = useTransaction ? transaction.connection.name : undefined;
|
||||||
if (sql.indexOf('INSERT') === 0) {
|
if (sql.indexOf('INSERT') === 0) {
|
||||||
transaction.connection.data[model] =
|
data[model] = data[model] || [];
|
||||||
transaction.connection.data[model] || [];
|
data[model].push({sql: sql, params: params});
|
||||||
transaction.connection.data[model].push({sql: sql, params: params});
|
debug('INSERT', data, sql, name);
|
||||||
debug('INSERT', transaction.connection.data, sql,
|
|
||||||
transaction.connection.name);
|
|
||||||
callback(null, 1);
|
callback(null, 1);
|
||||||
} else {
|
} else {
|
||||||
debug('SELECT', transaction.connection.data, sql,
|
debug('SELECT', data, sql, name);
|
||||||
transaction.connection.name);
|
var instances = data[model] || [];
|
||||||
callback(null, transaction.connection.data[model] || []);
|
var result = sql.indexOf('count(*) as "cnt"') !== -1 ?
|
||||||
}
|
[{cnt: instances.length}] : instances;
|
||||||
} else {
|
callback(null, result);
|
||||||
if (sql.indexOf('INSERT') === 0) {
|
|
||||||
this.data[model] = this.data[model] || [];
|
|
||||||
this.data[model].push({sql: sql, params: params});
|
|
||||||
debug('INSERT', this.data, sql);
|
|
||||||
callback(null, 1);
|
|
||||||
} else {
|
|
||||||
debug('SELECT', this.data, sql);
|
|
||||||
callback(null, this.data[model] || []);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -92,6 +92,11 @@ describe('transactions', function() {
|
||||||
function(err, posts) {
|
function(err, posts) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
expect(posts.length).to.be.eql(count);
|
expect(posts.length).to.be.eql(count);
|
||||||
|
// Make sure both find() and count() behave the same way
|
||||||
|
Post.count(where, options,
|
||||||
|
function(err, result) {
|
||||||
|
if (err) return done(err);
|
||||||
|
expect(result).to.be.eql(count);
|
||||||
if (count) {
|
if (count) {
|
||||||
// Find related reviews
|
// Find related reviews
|
||||||
options.model = 'Review';
|
options.model = 'Review';
|
||||||
|
@ -106,6 +111,7 @@ describe('transactions', function() {
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue