不要怂,就是干,撸起袖子干!

Commit f15dcc21 by Sascha Depold

moved count query to queryinterface

1 parent e6bae76f
Showing with 30 additions and 8 deletions
......@@ -68,14 +68,7 @@ module.exports = (function() {
}
ModelFactory.prototype.count = function(options) {
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).success(function(obj) {
emitter.emit('success', obj['count(*)'])
})
})
return emitter.run()
return this.QueryInterface.count(this, this.tableName, options)
}
ModelFactory.prototype.max = function(field, options) {
......
......@@ -58,6 +58,35 @@ module.exports = (function() {
return query.call(this, this.QueryGenerator.selectQuery(tableName, options), factory)
}
QueryInterface.prototype.count = function(factory, tableName, options) {
var self = this
, countOption = "count(*) as count"
options = options || {}
options.attributes = options.attributes || []
options.attributes.push('count(*) as count')
return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options).replace('`' + countOption + '`', countOption)
query.call(self, sql, factory, { plain: true }).success(function(data) {
emitter.emit('success', data.count)
}).error(function(err) {
emitter.emit('failure', err)
})
}).run()
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
query.call(self, self.QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).success(function(obj) {
emitter.emit('success', obj['count(*)'])
})
})
return emitter.run()
}
// private
var query = function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!