Skip to content

Commit

Permalink
Affected rows
Browse files Browse the repository at this point in the history
  • Loading branch information
janmeier committed Jan 25, 2014
1 parent 4ad706a commit b21db19
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 14 deletions.
11 changes: 7 additions & 4 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Utils = require("./utils")
, sql = require('sql')
, SqlString = require('./sql-string')
, Transaction = require('./transaction')
, QueryTypes = require('./query-types')

module.exports = (function() {
var DAOFactory = function(name, attributes, options) {
Expand Down Expand Up @@ -101,7 +102,7 @@ module.exports = (function() {
result.exec = function(options) {
options = Utils._.extend({
transaction: null,
type: 'SELECT'
type: QueryTypes.SELECT
}, options || {})

return self.QueryInterface.queryAndEmit([result.toSql(), self, options], 'snafu')
Expand Down Expand Up @@ -439,7 +440,7 @@ module.exports = (function() {
options = paranoidClause.call(this, options)

return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({
type: 'SELECT',
type: QueryTypes.SELECT,
hasJoin: hasJoin
}, queryOptions, { transaction: (options || {}).transaction }))
}
Expand All @@ -453,7 +454,7 @@ module.exports = (function() {
this.options.whereCollection = optcpy.where || null;

return this.QueryInterface.select(this, [this.getTableName(), joinTableName], optcpy, Utils._.defaults({
type: 'SELECT'
type: QueryTypes.SELECT
}, queryOptions, { transaction: (options || {}).transaction }))
}

Expand Down Expand Up @@ -534,7 +535,7 @@ module.exports = (function() {

return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true,
type: 'SELECT',
type: QueryTypes.SELECT,
hasJoin: hasJoin
}, queryOptions, { transaction: (options || {}).transaction }))
}
Expand Down Expand Up @@ -927,6 +928,7 @@ module.exports = (function() {
DAOFactory.prototype.destroy = function(where, options) {
options = options || {}
options.force = options.force === undefined ? false : Boolean(options.force)
options.type = QueryTypes.BULKDELETE

var self = this
, query = null
Expand Down Expand Up @@ -1050,6 +1052,7 @@ module.exports = (function() {
options = options || {}
options.validate = options.validate === undefined ? true : Boolean(options.validate)
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.type = QueryTypes.BULKUPDATE

if (self.options.timestamps) {
var attr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored)
Expand Down
13 changes: 12 additions & 1 deletion lib/dialects/abstract/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Utils = require('../../utils')
, CustomEventEmitter = require("../../emitters/custom-event-emitter")
, Dot = require('dottie')
, _ = require('lodash')
, QueryTypes = require('../../query-types')

module.exports = (function() {
var AbstractQuery = function(database, sequelize, callee, options) {}
Expand Down Expand Up @@ -95,6 +96,8 @@ module.exports = (function() {
}
} else if (isCallQuery.call(this)) {
result = data[0]
} else if (isBulkUpateQuery.call(this) || isBulkDeleteQuery.call(this)) {
result = data.affectedRows
}

return result
Expand Down Expand Up @@ -191,7 +194,15 @@ module.exports = (function() {
}

var isSelectQuery = function() {
return this.options.type === 'SELECT'
return this.options.type === QueryTypes.SELECT
}

var isBulkUpateQuery = function() {
return this.options.type === QueryTypes.BULKUPDATE
}

var isBulkDeleteQuery = function() {
return this.options.type === QueryTypes.BULKDELETE
}

var isUpdateQuery = function() {
Expand Down
9 changes: 6 additions & 3 deletions lib/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Utils = require("../../utils")
, AbstractQuery = require('../abstract/query')
, DataTypes = require('../../data-types')
, hstore = require('./hstore')
, QueryTypes = require('../../query-types')

module.exports = (function() {
var Query = function(client, sequelize, callee, options) {
Expand Down Expand Up @@ -41,14 +42,14 @@ module.exports = (function() {
self.emit('error', err, self.callee)
})

query.on('end', function() {
query.on('end', function(result) {
self.emit('sql', self.sql)

if (receivedError) {
return
}

onSuccess.call(self, rows, sql)
onSuccess.call(self, rows, sql, result)
})

return this
Expand All @@ -58,7 +59,7 @@ module.exports = (function() {
return 'id'
}

var onSuccess = function(rows, sql) {
var onSuccess = function(rows, sql, result) {
var results = rows
, self = this
, isTableNameQuery = (sql.indexOf('SELECT table_name FROM information_schema.tables') === 0)
Expand Down Expand Up @@ -144,6 +145,8 @@ module.exports = (function() {
}
}
this.emit('success', this.callee)
} else if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].indexOf(this.options.type) !== -1) {
this.emit('success', result.rowCount)
} else if (this.send('isUpdateQuery')) {
if(this.callee !== null) { // may happen for bulk updates
for (var key in rows[0]) {
Expand Down
3 changes: 3 additions & 0 deletions lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Utils = require("../../utils")
, AbstractQuery = require('../abstract/query')
, QueryTypes = require('../../query-types')

module.exports = (function() {
var Query = function(database, sequelize, callee, options) {
Expand Down Expand Up @@ -157,6 +158,8 @@ module.exports = (function() {
result = results[0]
} else if (this.sql.indexOf('PRAGMA foreign_keys') !== -1) {
result = results
} else if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].indexOf(this.options.type) !== -1) {
result = metaData.changes
}

this.emit('success', result)
Expand Down
9 changes: 5 additions & 4 deletions lib/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Utils = require(__dirname + '/utils')
, DataTypes = require(__dirname + '/data-types')
, SQLiteQueryInterface = require(__dirname + '/dialects/sqlite/query-interface')
, Transaction = require(__dirname + '/transaction')
, QueryTypes = require('./query-types')

module.exports = (function() {
var QueryInterface = function(sequelize) {
Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports = (function() {
for (i = 0; i < keyLen; i++) {
if (attributes[keys[i]].toString().match(/^ENUM\(/)) {
sql = self.QueryGenerator.pgListEnums(getTableName, keys[i], options)
chainer.add(self.sequelize.query(sql, null, { plain: true, raw: true, type: 'SELECT', logging: options.logging }))
chainer.add(self.sequelize.query(sql, null, { plain: true, raw: true, type: QueryTypes.SELECT, logging: options.logging }))
}
}

Expand Down Expand Up @@ -535,7 +536,7 @@ module.exports = (function() {
.success(function(results){
emitter.query = { sql: sql }
emitter.emit('sql', sql)
emitter.emit('success', results[1])
emitter.emit('success', results[0])
})
.error(function(err) {
emitter.query = { sql: sql }
Expand Down Expand Up @@ -650,7 +651,7 @@ module.exports = (function() {
.success(function(results){
emitter.query = { sql: sql }
emitter.emit('sql', sql)
emitter.emit('success', results[1])
emitter.emit('success', results[0])
})
.error(function(err) {
emitter.query = { sql: sql }
Expand Down Expand Up @@ -699,7 +700,7 @@ module.exports = (function() {

return new Utils.CustomEventEmitter(function(emitter) {
var sql = self.QueryGenerator.selectQuery(tableName, options)
, queryOptions = Utils._.extend({ transaction: options.transaction }, { plain: true, raw: true, type: 'SELECT' })
, queryOptions = Utils._.extend({ transaction: options.transaction }, { plain: true, raw: true, type: QueryTypes.SELECT })
, query = self.sequelize.query(sql, null, queryOptions)

query
Expand Down
5 changes: 4 additions & 1 deletion lib/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var url = require("url")
, QueryInterface = require("./query-interface")
, Transaction = require("./transaction")
, TransactionManager = require('./transaction-manager')
, QueryTypes = require('./query-types')

module.exports = (function() {
/**
Expand Down Expand Up @@ -129,6 +130,8 @@ module.exports = (function() {
*/
Sequelize.Utils = Utils

Sequelize.QueryTypes = QueryTypes

for (var dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType]
}
Expand Down Expand Up @@ -312,7 +315,7 @@ module.exports = (function() {
options = Utils._.extend(Utils._.clone(this.options.query), options)
options = Utils._.defaults(options, {
logging: this.options.hasOwnProperty('logging') ? this.options.logging : console.log,
type: (sql.toLowerCase().indexOf('select') === 0) ? 'SELECT' : false
type: (sql.toLowerCase().indexOf('select') === 0) ? QueryTypes.SELECT : false
})

return this.transactionManager.query(sql, callee, options)
Expand Down
50 changes: 49 additions & 1 deletion test/dao-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {

this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).done(function(err) {
console.log(err)
expect(err).not.to.be.ok
self.User.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3)
Expand All @@ -707,6 +706,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})

it('returns the number of affected rows', function(_done) {
var self = this
, data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Paul', secretValue: '42' },
{ username: 'Bob', secretValue: '43' }]
, done = _.after(2, _done)

this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok
expect(affectedRows).to.equal(2)

done()
})

self.User.update({username: 'Bill'}, {secretValue: '44'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok
expect(affectedRows).to.equal(0)

done()
})
})
})
})

describe('destroy', function() {
Expand Down Expand Up @@ -867,6 +890,31 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})

it('returns the number of affected rows', function(_done) {
var self = this
, data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Paul', secretValue: '42' },
{ username: 'Bob', secretValue: '43' }]
, done = _.after(2, _done)


this.User.bulkCreate(data).success(function() {
self.User.destroy({secretValue: '42'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok
expect(affectedRows).to.equal(2)

done()
})

self.User.destroy({secretValue: '44'}).done(function(err, affectedRows) {
expect(err).not.to.be.ok
expect(affectedRows).to.equal(0)

done()
})
})
})
})

describe('equals', function() {
Expand Down

0 comments on commit b21db19

Please sign in to comment.