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

Commit 25c1e5c7 by Pedro Branco Committed by Sushant

Fix `Instance.decrement` precision problems (#7118)

* Fix `Instance.decrement` precision problems

* [ci skip] Link to issue
1 parent 1b675e20
# Future
- [FIXED] Fix `Instance.decrement` precision problems [#7112](https://github.com/sequelize/sequelize/pull/7112)
- [FIXED] MSSQL tedious debug regression fix when dialectOptions are not passed [#7130](https://github.com/sequelize/sequelize/pull/7130)
- [CHANGED] `setIsolationLevelQuery` to skip under MSSQL dialect, added debug listener for tedious [#7130](https://github.com/sequelize/sequelize/pull/7130)
- [FIXED] `sourceKey` FOR `hasMany` now also works if a `where` was specified in an `include` [#7141](https://github.com/sequelize/sequelize/issues/7141)
......
......@@ -377,6 +377,7 @@ const QueryGenerator = {
/*
Returns an update query.
Parameters:
- operator -> String with the arithmetic operator (e.g. '+' or '-')
- tableName -> Name of the table
- values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'})
......@@ -385,7 +386,7 @@ const QueryGenerator = {
If you use a string, you have to escape it on your own.
@private
*/
incrementQuery(tableName, attrValueHash, where, options) {
arithmeticQuery(operator, tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull);
const values = [];
......@@ -402,7 +403,7 @@ const QueryGenerator = {
for (const key in attrValueHash) {
const value = attrValueHash[key];
values.push(this.quoteIdentifier(key) + '=' + this.quoteIdentifier(key) + ' + ' + this.escape(value));
values.push(this.quoteIdentifier(key) + '=' + this.quoteIdentifier(key) + operator + this.escape(value));
}
options = options || {};
......
......@@ -3777,7 +3777,8 @@ class Model {
options = _.defaults({}, options, {
by: 1,
attributes: {},
where: {}
where: {},
increment: true
});
const where = _.extend({}, options.where, identifier);
......@@ -3808,6 +3809,10 @@ class Model {
}
}
if (!options.increment) {
return this.sequelize.getQueryInterface().decrement(this, this.constructor.getTableName(options), values, where, options).return(this);
}
return this.sequelize.getQueryInterface().increment(this, this.constructor.getTableName(options), values, where, options).return(this);
}
......@@ -3837,18 +3842,10 @@ class Model {
* @return {Promise}
*/
decrement(fields, options) {
options = _.defaults({}, options, {
options = _.defaults({ increment: false }, options, {
by: 1
});
if (!Utils._.isString(fields) && !Utils._.isArray(fields)) { // Assume fields is key-value pairs
Utils._.each(fields, (value, field) => {
fields[field] = -value;
});
}
options.by = 0 - options.by;
return this.increment(fields, options);
}
......
......@@ -638,7 +638,17 @@ class QueryInterface {
}
increment(instance, tableName, values, identifier, options) {
const sql = this.QueryGenerator.incrementQuery(tableName, values, identifier, options.attributes);
const sql = this.QueryGenerator.arithmeticQuery('+', tableName, values, identifier, options.attributes);
options = _.clone(options) || {};
options.type = QueryTypes.UPDATE;
options.instance = instance;
return this.sequelize.query(sql, options);
}
decrement(instance, tableName, values, identifier, options) {
const sql = this.QueryGenerator.arithmeticQuery('-', tableName, values, identifier, options.attributes);
options = _.clone(options) || {};
......
......@@ -171,5 +171,34 @@ if (current.dialect.name === 'mssql') {
mssql: 'ALTER TABLE [myTable] DROP [myColumnKey]'
});
});
test('arithmeticQuery', () => {
[{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}],
expectation: 'UPDATE myTable SET foo=foo+\'bar\' '
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE myTable SET foo=foo+\'bar\' WHERE bar = \'biz\''
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }],
expectation: 'UPDATE myTable SET foo=foo-\'bar\' '
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE myTable SET foo=foo-\'bar\' WHERE bar = \'biz\''
}].forEach(test => {
it(test.title, () => {
expectsql(QueryGenerator.arithmeticQuery.call(QueryGenerator, test.arguments), {
mssql: test.expectation
});
});
});
});
});
}
......@@ -11,6 +11,28 @@ var chai = require('chai')
if (dialect === 'mysql') {
describe('[MYSQL Specific] QueryGenerator', function() {
var suites = {
arithmeticQuery: [
{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' '
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' WHERE `bar` = \'biz\''
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' '
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' WHERE `bar` = \'biz\''
}
],
attributesToSQL: [
{
arguments: [{id: 'INTEGER'}],
......
......@@ -14,6 +14,28 @@ var chai = require('chai')
if (dialect.match(/^postgres/)) {
describe('[POSTGRES Specific] QueryGenerator', function() {
var suites = {
arithmeticQuery: [
{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}],
expectation: 'UPDATE "myTable" SET "foo"="foo"+\'bar\' RETURNING *'
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE "myTable" SET "foo"="foo"+\'bar\' WHERE "bar" = \'biz\' RETURNING *'
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }],
expectation: 'UPDATE "myTable" SET "foo"="foo"-\'bar\' RETURNING *'
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE "myTable" SET "foo"="foo"-\'bar\' WHERE "bar" = \'biz\' RETURNING *'
}
],
attributesToSQL: [
{
arguments: [{id: 'INTEGER'}],
......
......@@ -20,6 +20,28 @@ if (dialect === 'sqlite') {
});
var suites = {
arithmeticQuery: [
{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' '
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' WHERE `bar` = \'biz\''
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' '
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' WHERE `bar` = \'biz\''
}
],
attributesToSQL: [
{
arguments: [{id: 'INTEGER'}],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!