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

Commit 12970d17 by Sascha Depold

convert result of count, min and max query to int

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