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

Commit 8a6c1a5f by Mick Hansen

Merge pull request #3836 from BridgeAR/fix-types

Fix some data types in mssql and postgres
2 parents a92fb9ce db6531dc
# Next # Next
- [FEATURE] Add support for keyword `after` in options of a field (useful for migrations), only for MySQL. [#3166](https://github.com/sequelize/sequelize/pull/3166) - [FEATURE] Add support for keyword `after` in options of a field (useful for migrations), only for MySQL. [#3166](https://github.com/sequelize/sequelize/pull/3166)
- [FEATURE] There's a new sequelize.truncate function to truncate all tables defined through the sequelize models [#2671](https://github.com/sequelize/sequelize/pull/2671) - [FEATURE] There's a new sequelize.truncate function to truncate all tables defined through the sequelize models [#2671](https://github.com/sequelize/sequelize/pull/2671)
- [FEATURE] Add support for MySQLs TINYTEXT, MEDIUMTEXT and LONGTEXT. [#3836](https://github.com/sequelize/sequelize/pull/3836)
- [FIXED] Fix a case where `type` in `sequelize.query` was not being set to raw. [#3800](https://github.com/sequelize/sequelize/pull/3800) - [FIXED] Fix a case where `type` in `sequelize.query` was not being set to raw. [#3800](https://github.com/sequelize/sequelize/pull/3800)
- [FIXED] Fix an issue where include all was not being properly expanded for self-references [#3804](https://github.com/sequelize/sequelize/issues/3804) - [FIXED] Fix an issue where include all was not being properly expanded for self-references [#3804](https://github.com/sequelize/sequelize/issues/3804)
- [FIXED] Fix instance.changed regression to not return false negatives for not changed null values [#3812](https://github.com/sequelize/sequelize/issues/3812) - [FIXED] Fix instance.changed regression to not return false negatives for not changed null values [#3812](https://github.com/sequelize/sequelize/issues/3812)
......
...@@ -101,18 +101,32 @@ CHAR.prototype.toSql = function() { ...@@ -101,18 +101,32 @@ CHAR.prototype.toSql = function() {
return 'CHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : ''); return 'CHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : '');
}; };
/** /**
* An unlimited length text column * An (un)limited length text column. Available lengths: `tiny`, `medium`, `long`
* @property TEXT * @property TEXT
*/ */
var TEXT = function(options) { var TEXT = function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof TEXT)) return new TEXT(options); if (!(this instanceof TEXT)) return new TEXT(options);
this._length = options.length || '';
}; };
util.inherits(TEXT, ABSTRACT); util.inherits(TEXT, ABSTRACT);
TEXT.prototype.key = TEXT.key = 'TEXT'; TEXT.prototype.key = TEXT.key = 'TEXT';
TEXT.prototype.toSql = function() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYTEXT';
case 'medium':
return 'MEDIUMTEXT';
case 'long':
return 'LONGTEXT';
default:
return this.key;
}
};
var NUMBER = function(options) { var NUMBER = function(options) {
this.options = options; this.options = options;
......
...@@ -18,6 +18,15 @@ STRING.prototype.toSql = function() { ...@@ -18,6 +18,15 @@ STRING.prototype.toSql = function() {
} }
}; };
BaseTypes.TEXT.prototype.toSql = function() {
// TEXT is deprecated in mssql and it would normally be saved as a non-unicode string.
// Using unicode is just future proof
if (this._length.toLowerCase() === 'tiny') {
return 'NVARCHAR(256)'; // tiny text should be 2^8 < 8000
}
return 'NVARCHAR(MAX)';
};
var BOOLEAN = function() { var BOOLEAN = function() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN(); if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments); BaseTypes.BOOLEAN.apply(this, arguments);
...@@ -28,13 +37,10 @@ BOOLEAN.prototype.toSql = function() { ...@@ -28,13 +37,10 @@ BOOLEAN.prototype.toSql = function() {
return 'BIT'; return 'BIT';
}; };
var BLOB = function() { BaseTypes.BLOB.prototype.toSql = function() {
if (!(this instanceof BLOB)) return new BLOB(); if (this._length.toLowerCase() === 'tiny') {
BaseTypes.BLOB.apply(this, arguments); return 'VARBINARY(256)'; // tiny blobs should be 2^8 < 8000
}; }
util.inherits(BLOB, BaseTypes.BLOB);
BLOB.prototype.toSql = function() {
return 'VARBINARY(MAX)'; return 'VARBINARY(MAX)';
}; };
...@@ -68,13 +74,68 @@ DATE.prototype.toSql = function() { ...@@ -68,13 +74,68 @@ DATE.prototype.toSql = function() {
return 'DATETIME2'; return 'DATETIME2';
}; };
var INTEGER = function() {
if (!(this instanceof INTEGER)) return new INTEGER();
BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any parameters for integer
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
};
util.inherits(INTEGER, BaseTypes.INTEGER);
var BIGINT = function() {
if (!(this instanceof BIGINT)) return new BIGINT();
BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any parameters for bigint
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
};
util.inherits(BIGINT, BaseTypes.BIGINT);
var REAL = function() {
if (!(this instanceof REAL)) return new REAL();
BaseTypes.REAL.apply(this, arguments);
// MSSQL does not support any parameters for real
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
};
util.inherits(REAL, BaseTypes.REAL);
var FLOAT = function() {
if (!(this instanceof FLOAT)) return new FLOAT();
BaseTypes.FLOAT.apply(this, arguments);
// MSSQL does only support lengths as parameter.
// Values between 1-24 result in 7 digits precision (4 bytes storage size)
// Values between 25-53 result in 15 digits precision (8 bytes storage size)
if (this._decimals) {
this._length = undefined;
this.options.length = undefined;
}
this._unsigned = undefined;
this._zerofill = undefined;
};
util.inherits(FLOAT, BaseTypes.FLOAT);
module.exports = { module.exports = {
BOOLEAN: BOOLEAN, BOOLEAN: BOOLEAN,
STRING: STRING, STRING: STRING,
BLOB: BLOB,
UUID: UUID, UUID: UUID,
DATE: DATE, DATE: DATE,
NOW: NOW NOW: NOW,
INTEGER: INTEGER,
BIGINT: BIGINT,
REAL: REAL,
FLOAT: FLOAT
}; };
_.forIn(module.exports, function (DataType, key) { _.forIn(module.exports, function (DataType, key) {
......
...@@ -45,7 +45,8 @@ MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.support ...@@ -45,7 +45,8 @@ MssqlDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.support
parser: false, parser: false,
type: true, type: true,
using: false, using: false,
} },
NUMERIC: true
}); });
MssqlDialect.prototype.Query = Query; MssqlDialect.prototype.Query = Query;
......
...@@ -17,6 +17,10 @@ STRING.prototype.toSql = function() { ...@@ -17,6 +17,10 @@ STRING.prototype.toSql = function() {
return BaseTypes.STRING.prototype.toSql.call(this); return BaseTypes.STRING.prototype.toSql.call(this);
}; };
BaseTypes.TEXT.prototype.toSql = function() {
return 'TEXT';
};
var CHAR = function() { var CHAR = function() {
if (!(this instanceof CHAR)) return new CHAR(); if (!(this instanceof CHAR)) return new CHAR();
BaseTypes.CHAR.apply(this, arguments); BaseTypes.CHAR.apply(this, arguments);
...@@ -55,10 +59,10 @@ var INTEGER = function() { ...@@ -55,10 +59,10 @@ var INTEGER = function() {
BaseTypes.INTEGER.apply(this, arguments); BaseTypes.INTEGER.apply(this, arguments);
// POSTGRES does not support any parameters for integer // POSTGRES does not support any parameters for integer
this._length = null; this._length = undefined;
this.options.length = null; this.options.length = undefined;
this._unsigned = null; this._unsigned = undefined;
this._zerofill = null; this._zerofill = undefined;
}; };
util.inherits(INTEGER, BaseTypes.INTEGER); util.inherits(INTEGER, BaseTypes.INTEGER);
...@@ -67,10 +71,10 @@ var BIGINT = function() { ...@@ -67,10 +71,10 @@ var BIGINT = function() {
BaseTypes.BIGINT.apply(this, arguments); BaseTypes.BIGINT.apply(this, arguments);
// POSTGRES does not support any parameters for bigint // POSTGRES does not support any parameters for bigint
this._length = null; this._length = undefined;
this.options.length = null; this.options.length = undefined;
this._unsigned = null; this._unsigned = undefined;
this._zerofill = null; this._zerofill = undefined;
}; };
util.inherits(BIGINT, BaseTypes.BIGINT); util.inherits(BIGINT, BaseTypes.BIGINT);
...@@ -79,10 +83,10 @@ var REAL = function() { ...@@ -79,10 +83,10 @@ var REAL = function() {
BaseTypes.REAL.apply(this, arguments); BaseTypes.REAL.apply(this, arguments);
// POSTGRES does not support any parameters for real // POSTGRES does not support any parameters for real
this._length = null; this._length = undefined;
this.options.length = null; this.options.length = undefined;
this._unsigned = null; this._unsigned = undefined;
this._zerofill = null; this._zerofill = undefined;
}; };
util.inherits(REAL, BaseTypes.REAL); util.inherits(REAL, BaseTypes.REAL);
...@@ -91,10 +95,10 @@ var DOUBLE = function() { ...@@ -91,10 +95,10 @@ var DOUBLE = function() {
BaseTypes.DOUBLE.apply(this, arguments); BaseTypes.DOUBLE.apply(this, arguments);
// POSTGRES does not support any parameters for double // POSTGRES does not support any parameters for double
this._length = null; this._length = undefined;
this.options.length = null; this.options.length = undefined;
this._unsigned = null; this._unsigned = undefined;
this._zerofill = null; this._zerofill = undefined;
}; };
util.inherits(DOUBLE, BaseTypes.DOUBLE); util.inherits(DOUBLE, BaseTypes.DOUBLE);
...@@ -102,25 +106,20 @@ var FLOAT = function() { ...@@ -102,25 +106,20 @@ var FLOAT = function() {
if (!(this instanceof FLOAT)) return new FLOAT(); if (!(this instanceof FLOAT)) return new FLOAT();
BaseTypes.FLOAT.apply(this, arguments); BaseTypes.FLOAT.apply(this, arguments);
// POSTGRES does not support any parameters for float // POSTGRES does only support lengths as parameter.
this._length = null; // Values between 1-24 result in REAL
this.options.length = null; // Values between 25-53 result in DOUBLE PRECISION
this._unsigned = null; if (this._decimals) {
this._zerofill = null; this._length = undefined;
this.options.length = undefined;
this._decimals = undefined;
}
this._unsigned = undefined;
this._zerofill = undefined;
}; };
util.inherits(FLOAT, BaseTypes.FLOAT); util.inherits(FLOAT, BaseTypes.FLOAT);
FLOAT.prototype.toSql = function() { BaseTypes.BLOB.prototype.toSql = function() {
return 'DOUBLE PRECISION';
};
var BLOB = function() {
if (!(this instanceof BLOB)) return new BLOB();
BaseTypes.BLOB.apply(this, arguments);
};
util.inherits(BLOB, BaseTypes.BLOB);
BLOB.prototype.toSql = function() {
return 'BYTEA'; return 'BYTEA';
}; };
...@@ -131,7 +130,6 @@ module.exports = { ...@@ -131,7 +130,6 @@ module.exports = {
DATE: DATE, DATE: DATE,
INTEGER: INTEGER, INTEGER: INTEGER,
BIGINT: BIGINT, BIGINT: BIGINT,
BLOB: BLOB,
REAL: REAL, REAL: REAL,
'DOUBLE PRECISION': DOUBLE, 'DOUBLE PRECISION': DOUBLE,
FLOAT: FLOAT FLOAT: FLOAT
......
...@@ -18,6 +18,10 @@ STRING.prototype.toSql = function() { ...@@ -18,6 +18,10 @@ STRING.prototype.toSql = function() {
} }
}; };
BaseTypes.TEXT.prototype.toSql = function() {
return 'TEXT';
};
var CHAR = function() { var CHAR = function() {
if (!(this instanceof CHAR)) return new CHAR(); if (!(this instanceof CHAR)) return new CHAR();
BaseTypes.CHAR.apply(this, arguments); BaseTypes.CHAR.apply(this, arguments);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!