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

Commit b21db19f by Jan Aagaard Meier

Affected rows

1 parent 4ad706aa
...@@ -5,6 +5,7 @@ var Utils = require("./utils") ...@@ -5,6 +5,7 @@ var Utils = require("./utils")
, sql = require('sql') , sql = require('sql')
, SqlString = require('./sql-string') , SqlString = require('./sql-string')
, Transaction = require('./transaction') , Transaction = require('./transaction')
, QueryTypes = require('./query-types')
module.exports = (function() { module.exports = (function() {
var DAOFactory = function(name, attributes, options) { var DAOFactory = function(name, attributes, options) {
...@@ -101,7 +102,7 @@ module.exports = (function() { ...@@ -101,7 +102,7 @@ module.exports = (function() {
result.exec = function(options) { result.exec = function(options) {
options = Utils._.extend({ options = Utils._.extend({
transaction: null, transaction: null,
type: 'SELECT' type: QueryTypes.SELECT
}, options || {}) }, options || {})
return self.QueryInterface.queryAndEmit([result.toSql(), self, options], 'snafu') return self.QueryInterface.queryAndEmit([result.toSql(), self, options], 'snafu')
...@@ -439,7 +440,7 @@ module.exports = (function() { ...@@ -439,7 +440,7 @@ module.exports = (function() {
options = paranoidClause.call(this, options) options = paranoidClause.call(this, options)
return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({ return this.QueryInterface.select(this, this.tableName, options, Utils._.defaults({
type: 'SELECT', type: QueryTypes.SELECT,
hasJoin: hasJoin hasJoin: hasJoin
}, queryOptions, { transaction: (options || {}).transaction })) }, queryOptions, { transaction: (options || {}).transaction }))
} }
...@@ -453,7 +454,7 @@ module.exports = (function() { ...@@ -453,7 +454,7 @@ module.exports = (function() {
this.options.whereCollection = optcpy.where || null; this.options.whereCollection = optcpy.where || null;
return this.QueryInterface.select(this, [this.getTableName(), joinTableName], optcpy, Utils._.defaults({ return this.QueryInterface.select(this, [this.getTableName(), joinTableName], optcpy, Utils._.defaults({
type: 'SELECT' type: QueryTypes.SELECT
}, queryOptions, { transaction: (options || {}).transaction })) }, queryOptions, { transaction: (options || {}).transaction }))
} }
...@@ -534,7 +535,7 @@ module.exports = (function() { ...@@ -534,7 +535,7 @@ module.exports = (function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true, plain: true,
type: 'SELECT', type: QueryTypes.SELECT,
hasJoin: hasJoin hasJoin: hasJoin
}, queryOptions, { transaction: (options || {}).transaction })) }, queryOptions, { transaction: (options || {}).transaction }))
} }
...@@ -927,6 +928,7 @@ module.exports = (function() { ...@@ -927,6 +928,7 @@ module.exports = (function() {
DAOFactory.prototype.destroy = function(where, options) { DAOFactory.prototype.destroy = function(where, options) {
options = options || {} options = options || {}
options.force = options.force === undefined ? false : Boolean(options.force) options.force = options.force === undefined ? false : Boolean(options.force)
options.type = QueryTypes.BULKDELETE
var self = this var self = this
, query = null , query = null
...@@ -1050,6 +1052,7 @@ module.exports = (function() { ...@@ -1050,6 +1052,7 @@ module.exports = (function() {
options = options || {} options = options || {}
options.validate = options.validate === undefined ? true : Boolean(options.validate) options.validate = options.validate === undefined ? true : Boolean(options.validate)
options.hooks = options.hooks === undefined ? false : Boolean(options.hooks) options.hooks = options.hooks === undefined ? false : Boolean(options.hooks)
options.type = QueryTypes.BULKUPDATE
if (self.options.timestamps) { if (self.options.timestamps) {
var attr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored) var attr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored)
......
...@@ -2,6 +2,7 @@ var Utils = require('../../utils') ...@@ -2,6 +2,7 @@ var Utils = require('../../utils')
, CustomEventEmitter = require("../../emitters/custom-event-emitter") , CustomEventEmitter = require("../../emitters/custom-event-emitter")
, Dot = require('dottie') , Dot = require('dottie')
, _ = require('lodash') , _ = require('lodash')
, QueryTypes = require('../../query-types')
module.exports = (function() { module.exports = (function() {
var AbstractQuery = function(database, sequelize, callee, options) {} var AbstractQuery = function(database, sequelize, callee, options) {}
...@@ -95,6 +96,8 @@ module.exports = (function() { ...@@ -95,6 +96,8 @@ module.exports = (function() {
} }
} else if (isCallQuery.call(this)) { } else if (isCallQuery.call(this)) {
result = data[0] result = data[0]
} else if (isBulkUpateQuery.call(this) || isBulkDeleteQuery.call(this)) {
result = data.affectedRows
} }
return result return result
...@@ -191,7 +194,15 @@ module.exports = (function() { ...@@ -191,7 +194,15 @@ module.exports = (function() {
} }
var isSelectQuery = 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() { var isUpdateQuery = function() {
......
...@@ -2,6 +2,7 @@ var Utils = require("../../utils") ...@@ -2,6 +2,7 @@ var Utils = require("../../utils")
, AbstractQuery = require('../abstract/query') , AbstractQuery = require('../abstract/query')
, DataTypes = require('../../data-types') , DataTypes = require('../../data-types')
, hstore = require('./hstore') , hstore = require('./hstore')
, QueryTypes = require('../../query-types')
module.exports = (function() { module.exports = (function() {
var Query = function(client, sequelize, callee, options) { var Query = function(client, sequelize, callee, options) {
...@@ -41,14 +42,14 @@ module.exports = (function() { ...@@ -41,14 +42,14 @@ module.exports = (function() {
self.emit('error', err, self.callee) self.emit('error', err, self.callee)
}) })
query.on('end', function() { query.on('end', function(result) {
self.emit('sql', self.sql) self.emit('sql', self.sql)
if (receivedError) { if (receivedError) {
return return
} }
onSuccess.call(self, rows, sql) onSuccess.call(self, rows, sql, result)
}) })
return this return this
...@@ -58,7 +59,7 @@ module.exports = (function() { ...@@ -58,7 +59,7 @@ module.exports = (function() {
return 'id' return 'id'
} }
var onSuccess = function(rows, sql) { var onSuccess = function(rows, sql, result) {
var results = rows var results = rows
, self = this , self = this
, isTableNameQuery = (sql.indexOf('SELECT table_name FROM information_schema.tables') === 0) , isTableNameQuery = (sql.indexOf('SELECT table_name FROM information_schema.tables') === 0)
...@@ -144,6 +145,8 @@ module.exports = (function() { ...@@ -144,6 +145,8 @@ module.exports = (function() {
} }
} }
this.emit('success', this.callee) 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')) { } else if (this.send('isUpdateQuery')) {
if(this.callee !== null) { // may happen for bulk updates if(this.callee !== null) { // may happen for bulk updates
for (var key in rows[0]) { for (var key in rows[0]) {
......
var Utils = require("../../utils") var Utils = require("../../utils")
, AbstractQuery = require('../abstract/query') , AbstractQuery = require('../abstract/query')
, QueryTypes = require('../../query-types')
module.exports = (function() { module.exports = (function() {
var Query = function(database, sequelize, callee, options) { var Query = function(database, sequelize, callee, options) {
...@@ -157,6 +158,8 @@ module.exports = (function() { ...@@ -157,6 +158,8 @@ module.exports = (function() {
result = results[0] result = results[0]
} else if (this.sql.indexOf('PRAGMA foreign_keys') !== -1) { } else if (this.sql.indexOf('PRAGMA foreign_keys') !== -1) {
result = results result = results
} else if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].indexOf(this.options.type) !== -1) {
result = metaData.changes
} }
this.emit('success', result) this.emit('success', result)
......
...@@ -2,6 +2,7 @@ var Utils = require(__dirname + '/utils') ...@@ -2,6 +2,7 @@ var Utils = require(__dirname + '/utils')
, DataTypes = require(__dirname + '/data-types') , DataTypes = require(__dirname + '/data-types')
, SQLiteQueryInterface = require(__dirname + '/dialects/sqlite/query-interface') , SQLiteQueryInterface = require(__dirname + '/dialects/sqlite/query-interface')
, Transaction = require(__dirname + '/transaction') , Transaction = require(__dirname + '/transaction')
, QueryTypes = require('./query-types')
module.exports = (function() { module.exports = (function() {
var QueryInterface = function(sequelize) { var QueryInterface = function(sequelize) {
...@@ -102,7 +103,7 @@ module.exports = (function() { ...@@ -102,7 +103,7 @@ module.exports = (function() {
for (i = 0; i < keyLen; i++) { for (i = 0; i < keyLen; i++) {
if (attributes[keys[i]].toString().match(/^ENUM\(/)) { if (attributes[keys[i]].toString().match(/^ENUM\(/)) {
sql = self.QueryGenerator.pgListEnums(getTableName, keys[i], options) 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 }))
} }
} }
...@@ -535,7 +536,7 @@ module.exports = (function() { ...@@ -535,7 +536,7 @@ module.exports = (function() {
.success(function(results){ .success(function(results){
emitter.query = { sql: sql } emitter.query = { sql: sql }
emitter.emit('sql', sql) emitter.emit('sql', sql)
emitter.emit('success', results[1]) emitter.emit('success', results[0])
}) })
.error(function(err) { .error(function(err) {
emitter.query = { sql: sql } emitter.query = { sql: sql }
...@@ -650,7 +651,7 @@ module.exports = (function() { ...@@ -650,7 +651,7 @@ module.exports = (function() {
.success(function(results){ .success(function(results){
emitter.query = { sql: sql } emitter.query = { sql: sql }
emitter.emit('sql', sql) emitter.emit('sql', sql)
emitter.emit('success', results[1]) emitter.emit('success', results[0])
}) })
.error(function(err) { .error(function(err) {
emitter.query = { sql: sql } emitter.query = { sql: sql }
...@@ -699,7 +700,7 @@ module.exports = (function() { ...@@ -699,7 +700,7 @@ module.exports = (function() {
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)
, 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 = self.sequelize.query(sql, null, queryOptions)
query query
......
...@@ -7,6 +7,7 @@ var url = require("url") ...@@ -7,6 +7,7 @@ var url = require("url")
, QueryInterface = require("./query-interface") , QueryInterface = require("./query-interface")
, Transaction = require("./transaction") , Transaction = require("./transaction")
, TransactionManager = require('./transaction-manager') , TransactionManager = require('./transaction-manager')
, QueryTypes = require('./query-types')
module.exports = (function() { module.exports = (function() {
/** /**
...@@ -129,6 +130,8 @@ module.exports = (function() { ...@@ -129,6 +130,8 @@ module.exports = (function() {
*/ */
Sequelize.Utils = Utils Sequelize.Utils = Utils
Sequelize.QueryTypes = QueryTypes
for (var dataType in DataTypes) { for (var dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType] Sequelize[dataType] = DataTypes[dataType]
} }
...@@ -312,7 +315,7 @@ module.exports = (function() { ...@@ -312,7 +315,7 @@ module.exports = (function() {
options = Utils._.extend(Utils._.clone(this.options.query), options) options = Utils._.extend(Utils._.clone(this.options.query), options)
options = Utils._.defaults(options, { options = Utils._.defaults(options, {
logging: this.options.hasOwnProperty('logging') ? this.options.logging : console.log, 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) return this.transactionManager.query(sql, callee, options)
......
...@@ -690,7 +690,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -690,7 +690,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.bulkCreate(data).success(function() { this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).done(function(err) { self.User.update({username: 'Bill'}, {secretValue: '42'}).done(function(err) {
console.log(err)
expect(err).not.to.be.ok expect(err).not.to.be.ok
self.User.findAll({order: 'id'}).success(function(users) { self.User.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3) expect(users.length).to.equal(3)
...@@ -707,6 +706,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -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() { describe('destroy', function() {
...@@ -867,6 +890,31 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -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() { describe('equals', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!