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

Commit c80079ae by Martin Blumenstingl

Introduced a aggregate() function in the DAOFactory so code can be shared.

Also made rawSelect's data-types options more extensible and implemented
the "DATE" case.
1 parent 4e0d4884
Showing with 42 additions and 20 deletions
...@@ -535,14 +535,37 @@ module.exports = (function() { ...@@ -535,14 +535,37 @@ module.exports = (function() {
}, queryOptions)) }, queryOptions))
} }
DAOFactory.prototype.count = function(options) { DAOFactory.prototype.aggregate = function(field, aggregateFunction, options) {
var tableField;
if (field == '*') {
tableField = field
} else {
tableField = this.QueryInterface.QueryGenerator.quoteIdentifier(field)
}
options = Utils._.extend({ attributes: [] }, options || {}) options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['count(*)', 'count']) options.attributes.push([aggregateFunction + '(' + tableField + ')', aggregateFunction])
options.parseInt = true
if (!options.dataType) {
if (this.rawAttributes[field]) {
options.dataType = this.rawAttributes[field]
} else {
// Use FLOAT as fallback
options.dataType = DataTypes.FLOAT
}
}
options = paranoidClause.call(this, options) options = paranoidClause.call(this, options)
return this.QueryInterface.rawSelect(this.getTableName(), options, 'count') return this.QueryInterface.rawSelect(this.getTableName(), options, aggregateFunction)
}
DAOFactory.prototype.count = function(options) {
options = Utils._.extend({ attributes: [] }, options || {})
options.dataType = DataTypes.INTEGER
return this.aggregate('*', 'count', options)
} }
DAOFactory.prototype.findAndCountAll = function(options, queryOptions) { DAOFactory.prototype.findAndCountAll = function(options, queryOptions) {
...@@ -586,18 +609,11 @@ module.exports = (function() { ...@@ -586,18 +609,11 @@ module.exports = (function() {
} }
DAOFactory.prototype.max = function(field, options) { DAOFactory.prototype.max = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {}) return this.aggregate(field, 'max', options)
options.attributes.push(['max(' + this.QueryInterface.QueryGenerator.quoteIdentifier(field) + ')', 'max'])
options.parseFloat = true
return this.QueryInterface.rawSelect(this.getTableName(), options, 'max')
} }
DAOFactory.prototype.min = function(field, options) {
options = Utils._.extend({ attributes: [] }, options || {})
options.attributes.push(['min(' + this.QueryInterface.QueryGenerator.quoteIdentifier(field) + ')', 'min'])
options.parseFloat = true
return this.QueryInterface.rawSelect(this.getTableName(), options, 'min') DAOFactory.prototype.min = function(field, options) {
return this.aggregate(field, 'min', options)
} }
DAOFactory.prototype.build = function(values, options) { DAOFactory.prototype.build = function(values, options) {
......
...@@ -686,12 +686,18 @@ module.exports = (function() { ...@@ -686,12 +686,18 @@ module.exports = (function() {
.success(function(data) { .success(function(data) {
var result = data ? data[attributeSelector] : null var result = data ? data[attributeSelector] : null
if (options && options.parseInt) { if (options && options.dataType) {
result = parseInt(result, 10) switch (options.dataType) {
} case DataTypes.INTEGER:
result = parseInt(result, 10);
if (options && options.parseFloat) { break;
result = parseFloat(result) case DataTypes.FLOAT:
result = parseFloat(result);
break;
case DataTypes.DATE:
result = new Date(result + 'Z');
break;
}
} }
self.emit('rawSelect', null) self.emit('rawSelect', null)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!