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

Commit 1c7e685b by sdepold

refactored count and max method

1 parent 36d748f4
...@@ -68,16 +68,17 @@ module.exports = (function() { ...@@ -68,16 +68,17 @@ module.exports = (function() {
} }
ModelFactory.prototype.count = function(options) { ModelFactory.prototype.count = function(options) {
return this.QueryInterface.count(this, this.tableName, options) options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['count(*)', 'count'])
return this.QueryInterface.rawSelect(this.tableName, options, 'count')
} }
ModelFactory.prototype.max = function(field, options) { ModelFactory.prototype.max = function(field, options) {
var emitter = new Utils.CustomEventEmitter(function() { options = Utils._.extend({ attributes: [] }, options || {})
query.call(self, self.QueryGenerator.maxQuery(self.tableName, field,options), self, {plain: true}).success(function(obj) { options.attributes.push(['max(' + field + ')', 'max'])
emitter.emit('success', obj['max'])
}) return this.QueryInterface.rawSelect(this.tableName, options, 'max')
})
return emitter.run()
} }
ModelFactory.prototype.min = function(field, options) { ModelFactory.prototype.min = function(field, options) {
var self = this var self = this
......
...@@ -58,7 +58,25 @@ module.exports = (function() { ...@@ -58,7 +58,25 @@ module.exports = (function() {
return query.call(this, this.QueryGenerator.selectQuery(tableName, options), factory) return query.call(this, this.QueryGenerator.selectQuery(tableName, options), factory)
} }
QueryInterface.prototype.count = function(factory, tableName, options) { QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector) {
var self = this
if(attributeSelector == undefined)
throw new Error('Please pass an attribute selector!')
return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options)
query.call(self, sql, null, { plain: true, raw: true }).success(function(data) {
emitter.emit('success', data[attributeSelector])
}).error(function(err) {
emitter.emit('failure', err)
})
}).run()
}
QueryInterface.prototype.count = function(tableName, options) {
var self = this var self = this
, countOption = "count(*) as count" , countOption = "count(*) as count"
......
...@@ -62,7 +62,13 @@ module.exports = (function() { ...@@ -62,7 +62,13 @@ module.exports = (function() {
// transform results into real model instances // transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find) // return the first real model instance if options.plain is set (e.g. Model.find)
if (this.sql.indexOf('SELECT') == 0) { if (this.sql.indexOf('SELECT') == 0) {
result = results.map(function(result) { return self.callee.build(result, {isNewRecord: false}) }) if(this.options.raw) {
result = results
} else {
result = results.map(function(result) {
return self.callee.build(result, { isNewRecord: false })
})
}
if(this.options.plain) if(this.options.plain)
result = (result.length == 0) ? null : result[0] result = (result.length == 0) ? null : result[0]
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!