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

Commit b9eaf806 by Sushant Committed by Mick Hansen

Fixes #2038, Added missing UNSIGNED / ZEROFILL support for DECIMAL (#6281)

* added unsigned zerofilled decimal support for MySQL

* changelog for missing decimal unsigned support

* simplify code and statements
1 parent 1b4af001
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
- [FIXED] `sync` don't handle global `options.logging` properly [#5788](https://github.com/sequelize/sequelize/issues/5788) - [FIXED] `sync` don't handle global `options.logging` properly [#5788](https://github.com/sequelize/sequelize/issues/5788)
- [FIXED] `attribute:[]` throw errors with `include` or `through` [#5078](https://github.com/sequelize/sequelize/issues/5078) [#4222](https://github.com/sequelize/sequelize/issues/4222) [#5958](https://github.com/sequelize/sequelize/issues/5958) [#5590](https://github.com/sequelize/sequelize/issues/5590) [#6139](https://github.com/sequelize/sequelize/issues/6139) [#4866](https://github.com/sequelize/sequelize/issues/4866) [#6242](https://github.com/sequelize/sequelize/issues/6242) - [FIXED] `attribute:[]` throw errors with `include` or `through` [#5078](https://github.com/sequelize/sequelize/issues/5078) [#4222](https://github.com/sequelize/sequelize/issues/4222) [#5958](https://github.com/sequelize/sequelize/issues/5958) [#5590](https://github.com/sequelize/sequelize/issues/5590) [#6139](https://github.com/sequelize/sequelize/issues/6139) [#4866](https://github.com/sequelize/sequelize/issues/4866) [#6242](https://github.com/sequelize/sequelize/issues/6242)
- [SECURITY] `GEOMETRY` and `GEOGRAPHY` SQL injection attacks [#6194](https://github.com/sequelize/sequelize/issues/6194) - [SECURITY] `GEOMETRY` and `GEOGRAPHY` SQL injection attacks [#6194](https://github.com/sequelize/sequelize/issues/6194)
- [FIXED] `DECIMAL` now supports `UNSIGNED` / `ZEROFILL` (MySQL) [#2038](https://github.com/sequelize/sequelize/issues/2038)
## BC breaks: ## BC breaks:
- Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive), previously was `()` (exclusive, exclusive) - Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive), previously was `()` (exclusive, exclusive)
......
...@@ -345,8 +345,9 @@ inherits(DECIMAL, NUMBER); ...@@ -345,8 +345,9 @@ inherits(DECIMAL, NUMBER);
DECIMAL.prototype.key = DECIMAL.key = 'DECIMAL'; DECIMAL.prototype.key = DECIMAL.key = 'DECIMAL';
DECIMAL.prototype.toSql = function toSql() { DECIMAL.prototype.toSql = function toSql() {
if (this._precision || this._scale) { if (this._precision || this._scale) {
return 'DECIMAL(' + [this._precision, this._scale].filter(_.identity).join(',') + ')'; return 'DECIMAL(' + [this._precision, this._scale].filter(_.identity).join(',') + ')';
} }
return 'DECIMAL'; return 'DECIMAL';
...@@ -811,8 +812,8 @@ ARRAY.is = function is(obj, type) { ...@@ -811,8 +812,8 @@ ARRAY.is = function is(obj, type) {
const helpers = { const helpers = {
BINARY: [STRING, CHAR], BINARY: [STRING, CHAR],
UNSIGNED: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL], UNSIGNED: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
ZEROFILL: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL], ZEROFILL: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
PRECISION: [DECIMAL], PRECISION: [DECIMAL],
SCALE: [DECIMAL] SCALE: [DECIMAL]
}; };
......
...@@ -25,6 +25,26 @@ module.exports = BaseTypes => { ...@@ -25,6 +25,26 @@ module.exports = BaseTypes => {
BaseTypes.REAL.types.mysql = ['DOUBLE']; BaseTypes.REAL.types.mysql = ['DOUBLE'];
BaseTypes.DOUBLE.types.mysql = ['DOUBLE']; BaseTypes.DOUBLE.types.mysql = ['DOUBLE'];
function DECIMAL(precision, scale) {
if (!(this instanceof DECIMAL)) return new DECIMAL(precision, scale);
BaseTypes.DECIMAL.apply(this, arguments);
}
inherits(DECIMAL, BaseTypes.DECIMAL);
DECIMAL.prototype.toSql = function toSql() {
let definition = BaseTypes.DECIMAL.prototype.toSql.apply(this);
if (this._unsigned) {
definition += ' UNSIGNED';
}
if (this._zerofill) {
definition += ' ZEROFILL';
}
return definition;
};
function DATE(length) { function DATE(length) {
if (!(this instanceof DATE)) return new Date(length); if (!(this instanceof DATE)) return new Date(length);
BaseTypes.DATE.apply(this, arguments); BaseTypes.DATE.apply(this, arguments);
...@@ -125,7 +145,8 @@ module.exports = BaseTypes => { ...@@ -125,7 +145,8 @@ module.exports = BaseTypes => {
ENUM, ENUM,
DATE, DATE,
UUID, UUID,
GEOMETRY GEOMETRY,
DECIMAL
}; };
_.forIn(exports, (DataType, key) => { _.forIn(exports, (DataType, key) => {
......
...@@ -804,6 +804,21 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -804,6 +804,21 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
default: 'DECIMAL(10)' default: 'DECIMAL(10)'
}); });
testsql('DECIMAL.UNSIGNED', DataTypes.DECIMAL.UNSIGNED, {
mysql: 'DECIMAL UNSIGNED',
default: 'DECIMAL'
});
testsql('DECIMAL.UNSIGNED.ZEROFILL', DataTypes.DECIMAL.UNSIGNED.ZEROFILL, {
mysql: 'DECIMAL UNSIGNED ZEROFILL',
default: 'DECIMAL'
});
testsql('DECIMAL({ precision: 10, scale: 2 }).UNSIGNED', DataTypes.DECIMAL({ precision: 10, scale: 2 }).UNSIGNED, {
mysql: 'DECIMAL(10,2) UNSIGNED',
default: 'DECIMAL(10,2)'
});
suite('validate', function () { suite('validate', function () {
test('should throw an error if `value` is invalid', function() { test('should throw an error if `value` is invalid', function() {
var type = DataTypes.DECIMAL(10); var type = DataTypes.DECIMAL(10);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!