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

Commit 12970d17 by Sascha Depold

convert result of count, min and max query to int

1 parent b0c586c1
Showing with 17 additions and 8 deletions
......@@ -141,6 +141,7 @@ module.exports = (function() {
DAOFactory.prototype.count = function(options) {
options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['count(*)', 'count'])
options.parseInt = true
return this.QueryInterface.rawSelect(this.tableName, options, 'count')
}
......@@ -148,12 +149,14 @@ module.exports = (function() {
DAOFactory.prototype.max = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['max(' + field + ')', 'max'])
options.parseInt = true
return this.QueryInterface.rawSelect(this.tableName, options, 'max')
}
DAOFactory.prototype.min = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['min(' + field + ')', 'min'])
options.parseInt = true
return this.QueryInterface.rawSelect(this.tableName, options, 'min')
}
......
......@@ -202,26 +202,32 @@ module.exports = (function() {
QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector) {
var self = this
if(attributeSelector == undefined)
if(attributeSelector == undefined) {
throw new Error('Please pass an attribute selector!')
}
return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options)
, qry = self.sequelize.query(sql, null, { plain: true, raw: true })
var qry = self.sequelize
.query(sql, null, { plain: true, raw: true })
qry
.success(function(data) {
var result = data[attributeSelector]
if (options && options.parseInt) {
result = parseInt(result)
}
qry.success(function(data) {
self.emit('rawSelect', null)
emitter.emit('success', data[attributeSelector])
emitter.emit('success', result)
})
.error(function(err) {
self.emit('rawSelect', err)
emitter.emit('error', err)
})
qry.on('sql', function(sql) {
emitter.emit('sql', sql)
})
.on('sql', function(sql) {
emitter.emit('sql', sql)
})
}).run()
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!