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

Commit fd1ace03 by Jan Aagaard Meier

Changes to raw query handling

1 parent edb18238
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
- [FEATURE] Validations are now enabled by default for upsert. - [FEATURE] Validations are now enabled by default for upsert.
- [FEATURE] Preliminary support for `include.through.where` - [FEATURE] Preliminary support for `include.through.where`
- [SECURITY/BUG] Fixed injection issue in direction param for order - [SECURITY/BUG] Fixed injection issue in direction param for order
- [FEATURE/BUG] Raw queries always return all results (including affected rows etc).
# 2.0.0-rc7 # 2.0.0-rc7
- [FEATURE] Throw an error if no where clause is given to `Model.destroy()`. - [FEATURE] Throw an error if no where clause is given to `Model.destroy()`.
......
...@@ -83,6 +83,10 @@ module.exports = (function() { ...@@ -83,6 +83,10 @@ module.exports = (function() {
} }
}; };
AbstractQuery.prototype.isRawQuery = function () {
return this.options.type === QueryTypes.RAW;
};
AbstractQuery.prototype.isVersionQuery = function () { AbstractQuery.prototype.isVersionQuery = function () {
return this.options.type === QueryTypes.VERSION; return this.options.type === QueryTypes.VERSION;
}; };
...@@ -150,6 +154,10 @@ module.exports = (function() { ...@@ -150,6 +154,10 @@ module.exports = (function() {
}; };
AbstractQuery.prototype.isUpdateQuery = function() { AbstractQuery.prototype.isUpdateQuery = function() {
if (this.options.type === QueryTypes.UPDATE) {
return true;
}
return (this.sql.toLowerCase().indexOf('update') === 0); return (this.sql.toLowerCase().indexOf('update') === 0);
}; };
......
...@@ -98,6 +98,9 @@ module.exports = (function() { ...@@ -98,6 +98,9 @@ module.exports = (function() {
result = data.affectedRows; result = data.affectedRows;
} else if (this.isVersionQuery()) { } else if (this.isVersionQuery()) {
result = data[0].version; result = data[0].version;
} else if (this.isRawQuery()) {
// MySQL returns row data and metadata (affected rows etc) in a single object - let's standarize it, sorta
result = [data, data];
} }
return result; return result;
......
...@@ -243,6 +243,8 @@ module.exports = (function() { ...@@ -243,6 +243,8 @@ module.exports = (function() {
return self.callee || (rows && ((self.options.plain && rows[0]) || rows)) || undefined; return self.callee || (rows && ((self.options.plain && rows[0]) || rows)) || undefined;
} else if (self.isVersionQuery()) { } else if (self.isVersionQuery()) {
return results[0].version; return results[0].version;
} else if (self.isRawQuery()) {
return [rows, result];
} else { } else {
return results; return results;
} }
......
...@@ -139,6 +139,8 @@ module.exports = (function() { ...@@ -139,6 +139,8 @@ module.exports = (function() {
result = undefined; result = undefined;
} else if (self.options.type === QueryTypes.VERSION) { } else if (self.options.type === QueryTypes.VERSION) {
result = results[0].version; result = results[0].version;
} else if (self.options.type === QueryTypes.RAW) {
result = [results, metaData];
} }
resolve(result); resolve(result);
......
...@@ -831,7 +831,7 @@ module.exports = (function() { ...@@ -831,7 +831,7 @@ module.exports = (function() {
* @param {Integer} [options.by=1] The number to increment by * @param {Integer} [options.by=1] The number to increment by
* @param {Transaction} [options.transaction] * @param {Transaction} [options.transaction]
* *
* @return {Promise} * @return {Promise<this>}
*/ */
Instance.prototype.increment = function(fields, countOrOptions) { Instance.prototype.increment = function(fields, countOrOptions) {
Utils.validateParameter(countOrOptions, Object, { Utils.validateParameter(countOrOptions, Object, {
...@@ -895,7 +895,7 @@ module.exports = (function() { ...@@ -895,7 +895,7 @@ module.exports = (function() {
} }
}, this); }, this);
return this.QueryInterface.increment(this, this.Model.getTableName(countOrOptions), values, where, countOrOptions); return this.QueryInterface.increment(this, this.Model.getTableName(countOrOptions), values, where, countOrOptions).return(this);
}; };
/** /**
......
...@@ -725,7 +725,6 @@ module.exports = (function() { ...@@ -725,7 +725,6 @@ module.exports = (function() {
} }
}).then(function() { }).then(function() {
return this.QueryInterface.select(this, this.getTableName(options), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(options), options, Utils._.defaults({
type: QueryTypes.SELECT,
hasJoin: hasJoin, hasJoin: hasJoin,
tableNames: Object.keys(tableNames) tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: options.transaction })); }, queryOptions, { transaction: options.transaction }));
...@@ -743,9 +742,7 @@ module.exports = (function() { ...@@ -743,9 +742,7 @@ module.exports = (function() {
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null; this.options.whereCollection = options.where || null;
return this.QueryInterface.select(this, [[this.getTableName(options), this.name], joinTableName], options, Utils._.defaults({ return this.QueryInterface.select(this, [[this.getTableName(options), this.name], joinTableName], options, Utils._.defaults(queryOptions, { transaction: (options || {}).transaction }));
type: QueryTypes.SELECT
}, queryOptions, { transaction: (options || {}).transaction }));
}; };
/** /**
...@@ -1449,7 +1446,6 @@ module.exports = (function() { ...@@ -1449,7 +1446,6 @@ module.exports = (function() {
options.type = QueryTypes.BULKDELETE; options.type = QueryTypes.BULKDELETE;
mapFieldNames.call(this, options, this); mapFieldNames.call(this, options, this);
return Promise.try(function() { return Promise.try(function() {
......
...@@ -443,7 +443,7 @@ module.exports = (function() { ...@@ -443,7 +443,7 @@ module.exports = (function() {
} }
return Utils.Promise.map(tableNames, function(tableName) { return Utils.Promise.map(tableNames, function(tableName) {
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database)); return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database)).get(0);
}).then(function(results) { }).then(function(results) {
var result = {}; var result = {};
...@@ -556,6 +556,9 @@ module.exports = (function() { ...@@ -556,6 +556,9 @@ module.exports = (function() {
, restrict = false , restrict = false
, sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, dao.Model.rawAttributes); , sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, dao.Model.rawAttributes);
options = options || {};
options.type = QueryTypes.UPDATE;
// Check for a restrict field // Check for a restrict field
if (!!dao.Model && !!dao.Model.associations) { if (!!dao.Model && !!dao.Model.associations) {
var keys = Object.keys(dao.Model.associations) var keys = Object.keys(dao.Model.associations)
...@@ -689,13 +692,14 @@ module.exports = (function() { ...@@ -689,13 +692,14 @@ module.exports = (function() {
var sql = this.QueryGenerator.selectQuery(tableName, options, model); var sql = this.QueryGenerator.selectQuery(tableName, options, model);
queryOptions = Utils._.extend({}, queryOptions, { queryOptions = Utils._.extend({}, queryOptions, {
type: QueryTypes.SELECT,
include: options.include, include: options.include,
includeNames: options.includeNames, includeNames: options.includeNames,
includeMap: options.includeMap, includeMap: options.includeMap,
hasSingleAssociation: options.hasSingleAssociation, hasSingleAssociation: options.hasSingleAssociation,
hasMultiAssociation: options.hasMultiAssociation, hasMultiAssociation: options.hasMultiAssociation,
attributes: options.attributes, attributes: options.attributes,
originalAttributes: options.originalAttributes originalAttributes: options.originalAttributes,
}); });
return this.sequelize.query(sql, model, queryOptions); return this.sequelize.query(sql, model, queryOptions);
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
module.exports = { module.exports = {
SELECT: 'SELECT', SELECT: 'SELECT',
INSERT: 'INSERT', INSERT: 'INSERT',
UPDATE: 'UPDATE',
BULKUPDATE: 'BULKUPDATE', BULKUPDATE: 'BULKUPDATE',
BULKDELETE: 'BULKDELETE', BULKDELETE: 'BULKDELETE',
UPSERT: 'UPSERT', UPSERT: 'UPSERT',
VERSION: 'VERSION', VERSION: 'VERSION',
SHOWTABLES: 'SHOWTABLES', SHOWTABLES: 'SHOWTABLES',
SHOWINDEXES: 'SHOWINDEXES' SHOWINDEXES: 'SHOWINDEXES',
RAW: 'RAW',
}; };
...@@ -660,7 +660,7 @@ module.exports = (function() { ...@@ -660,7 +660,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) ? QueryTypes.SELECT : false type: QueryTypes.RAW
}); });
if (options.transaction === undefined && Sequelize.cls) { if (options.transaction === undefined && Sequelize.cls) {
......
...@@ -1300,7 +1300,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1300,7 +1300,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('sorts the results via a date column', function(done) { it('sorts the results via a date column', function(done) {
var self = this; var self = this;
self.User.create({username: 'user3', data: 'bar', theDate: moment().add('hours', 2).toDate()}).success(function() { self.User.create({username: 'user3', data: 'bar', theDate: moment().add(2, 'hours').toDate()}).success(function() {
self.User.findAll({ order: [['theDate', 'DESC']] }).success(function(users) { self.User.findAll({ order: [['theDate', 'DESC']] }).success(function(users) {
expect(users[0].id).to.be.above(users[2].id); expect(users[0].id).to.be.above(users[2].id);
done(); done();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!