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

Commit 0701b1f9 by Jan Aagaard Meier

Added api docs for count, changed count to use aggregate, changed rawselet to use promises

1 parent 07698882
...@@ -743,17 +743,10 @@ module.exports = (function() { ...@@ -743,17 +743,10 @@ module.exports = (function() {
* @return {Promise} * @return {Promise}
*/ */
DAOFactory.prototype.aggregate = function(field, aggregateFunction, 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([aggregateFunction + '(' + tableField + ')', aggregateFunction])
options.attributes.push([this.sequelize.fn(aggregateFunction, this.sequelize.col(field)), aggregateFunction])
if (!options.dataType) { if (!options.dataType) {
if (this.rawAttributes[field]) { if (this.rawAttributes[field]) {
options.dataType = this.rawAttributes[field] options.dataType = this.rawAttributes[field]
...@@ -765,30 +758,33 @@ module.exports = (function() { ...@@ -765,30 +758,33 @@ module.exports = (function() {
options = paranoidClause.call(this, options) options = paranoidClause.call(this, options)
return this.QueryInterface.rawSelect(this.getTableName(), options, aggregateFunction) return this.QueryInterface.rawSelect(this.getTableName(), options, aggregateFunction, this)
} }
/**
* Count the number of records matching the provided where clause.
*
* If you provide an `include` option, the number of matching associations will be counted instead.
*
* @param {Object} [options]
* @param {Object} [options.include] Include options. See `find` for details
*
* @return {Promise}
*/
DAOFactory.prototype.count = function(options) { DAOFactory.prototype.count = function(options) {
options = Utils._.clone(options || {}) options = Utils._.clone(options || {})
var col = this.sequelize.col('*') var col = '*'
if (options.include) { if (options.include) {
col = this.sequelize.col(this.name+'.'+(this.primaryKeyAttribute)) col = this.name+'.'+this.primaryKeyAttribute
validateIncludedElements.call(this, options)
} }
options.attributes = [ options.dataType = DataTypes.INTEGER
[this.sequelize.fn('COUNT', col), 'count']
]
options.includeIgnoreAttributes = false options.includeIgnoreAttributes = false
options.limit = null options.limit = null
return this.find(options, { return this.aggregate(col, 'COUNT', options)
raw: true,
transaction: options.transaction
}).then(function (result) {
return parseInt(result.count, 10)
})
} }
/** /**
......
...@@ -609,41 +609,33 @@ module.exports = (function() { ...@@ -609,41 +609,33 @@ module.exports = (function() {
return queryAndEmit.call(this, [sql, dao, options], 'increment') return queryAndEmit.call(this, [sql, dao, options], 'increment')
} }
QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector) { QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector, Model) {
var self = this var sql = this.QueryGenerator.selectQuery(tableName, options, Model)
, queryOptions = Utils._.extend({ transaction: options.transaction }, { plain: true, raw: true, type: QueryTypes.SELECT })
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 this.sequelize.query(sql, null, queryOptions).then(function (data) {
var sql = self.QueryGenerator.selectQuery(tableName, options) var result = data ? data[attributeSelector] : null
, queryOptions = Utils._.extend({ transaction: options.transaction }, { plain: true, raw: true, type: QueryTypes.SELECT })
, query = self.sequelize.query(sql, null, queryOptions)
query
.success(function(data) {
var result = data ? data[attributeSelector] : null
if (options && options.dataType) {
var dataType = options.dataType;
if (dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT) {
result = parseFloat(result);
} else if (dataType === DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) {
result = parseInt(result, 10);
} else if (dataType === DataTypes.DATE) {
result = new Date(result + 'Z');
} else if (dataType === DataTypes.STRING) {
// Nothing to do, result is already a string.
}
}
self.emit('rawSelect', null) if (options && options.dataType) {
emitter.emit('success', result) var dataType = options.dataType;
})
.proxy(emitter, { events: ['error', 'sql']}) if (dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT) {
}).run() result = parseFloat(result);
} else if (dataType === DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) {
result = parseInt(result, 10);
} else if (dataType === DataTypes.DATE) {
result = new Date(result + 'Z');
} else if (dataType === DataTypes.STRING) {
// Nothing to do, result is already a string.
}
}
return result
})
} }
QueryInterface.prototype.createTrigger = function(tableName, triggerName, timingType, fireOnArray, QueryInterface.prototype.createTrigger = function(tableName, triggerName, timingType, fireOnArray,
......
...@@ -663,10 +663,11 @@ module.exports = (function() { ...@@ -663,10 +663,11 @@ module.exports = (function() {
* @see {DAOFactory#findAll} * @see {DAOFactory#findAll}
* @see {DAOFactory#define} * @see {DAOFactory#define}
* @see {Sequelize#col} * @see {Sequelize#col}
*
* @method fn * @method fn
*
* @param {String} fn The function you want to call * @param {String} fn The function you want to call
* @param {any} args All further arguments will be passed as arguments to the function * @param {any} args All further arguments will be passed as arguments to the function
*
* @since v2.0.0-dev3 * @since v2.0.0-dev3
* @return {Sequelize.fn} * @return {Sequelize.fn}
*/ */
......
...@@ -596,7 +596,7 @@ Utils.fn.prototype.toString = function(queryGenerator, parentModel) { ...@@ -596,7 +596,7 @@ Utils.fn.prototype.toString = function(queryGenerator, parentModel) {
Utils.col.prototype.toString = function (queryGenerator, parentModel) { Utils.col.prototype.toString = function (queryGenerator, parentModel) {
if (Array.isArray(this.col)) { if (Array.isArray(this.col)) {
if (!parent) { if (!parentModel) {
throw new Error('Cannot call Sequelize.col() with array outside of order / group clause') throw new Error('Cannot call Sequelize.col() with array outside of order / group clause')
} }
} else if (this.col.indexOf('*') === 0) { } else if (this.col.indexOf('*') === 0) {
......
...@@ -1612,14 +1612,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1612,14 +1612,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
afterEach(function() {
var self = this
return this.sequelize.getQueryInterface().dropTable('posts', { force: true }).then(function() {
return self.sequelize.getQueryInterface().dropTable('authors', { force: true })
})
})
it('uses an existing dao factory and references the author table', function(done) { it('uses an existing dao factory and references the author table', function(done) {
var self = this var self = this
, Post = this.sequelize.define('post', { , Post = this.sequelize.define('post', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!