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

Commit db6531dc by Ruben Bridgewater

Add support for TEXT('length')

1 parent b8056412
# 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,15 +18,12 @@ STRING.prototype.toSql = function() { ...@@ -18,15 +18,12 @@ STRING.prototype.toSql = function() {
} }
}; };
var TEXT = function() { BaseTypes.TEXT.prototype.toSql = function() {
if (!(this instanceof TEXT)) return new TEXT();
BaseTypes.TEXT.apply(this, arguments);
};
util.inherits(TEXT, BaseTypes.TEXT);
TEXT.prototype.toSql = function() {
// TEXT is deprecated in mssql and it would normally be saved as a non-unicode string. // TEXT is deprecated in mssql and it would normally be saved as a non-unicode string.
// Using unicode is just future proof // Using unicode is just future proof
if (this._length.toLowerCase() === 'tiny') {
return 'NVARCHAR(256)'; // tiny text should be 2^8 < 8000
}
return 'NVARCHAR(MAX)'; return 'NVARCHAR(MAX)';
}; };
...@@ -40,13 +37,10 @@ BOOLEAN.prototype.toSql = function() { ...@@ -40,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)';
}; };
...@@ -135,8 +129,6 @@ util.inherits(FLOAT, BaseTypes.FLOAT); ...@@ -135,8 +129,6 @@ util.inherits(FLOAT, BaseTypes.FLOAT);
module.exports = { module.exports = {
BOOLEAN: BOOLEAN, BOOLEAN: BOOLEAN,
STRING: STRING, STRING: STRING,
TEXT: TEXT,
BLOB: BLOB,
UUID: UUID, UUID: UUID,
DATE: DATE, DATE: DATE,
NOW: NOW, NOW: NOW,
......
...@@ -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);
...@@ -108,19 +112,14 @@ var FLOAT = function() { ...@@ -108,19 +112,14 @@ var FLOAT = function() {
if (this._decimals) { if (this._decimals) {
this._length = undefined; this._length = undefined;
this.options.length = undefined; this.options.length = undefined;
this._decimals = undefined;
} }
this._unsigned = undefined; this._unsigned = undefined;
this._zerofill = undefined; this._zerofill = undefined;
}; };
util.inherits(FLOAT, BaseTypes.FLOAT); util.inherits(FLOAT, BaseTypes.FLOAT);
var BLOB = function() { BaseTypes.BLOB.prototype.toSql = 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);
......
...@@ -26,6 +26,11 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -26,6 +26,11 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'NVARCHAR(1234)' mssql: 'NVARCHAR(1234)'
}); });
testsql('STRING({ length: 1234 })', DataTypes.STRING({ length: 1234 }), {
default: 'VARCHAR(1234)',
mssql: 'NVARCHAR(1234)'
});
testsql('STRING(1234).BINARY', DataTypes.STRING(1234).BINARY, { testsql('STRING(1234).BINARY', DataTypes.STRING(1234).BINARY, {
default: 'VARCHAR(1234) BINARY', default: 'VARCHAR(1234) BINARY',
sqlite: 'VARCHAR BINARY(1234)', sqlite: 'VARCHAR BINARY(1234)',
...@@ -46,6 +51,34 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -46,6 +51,34 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
default: 'TEXT', default: 'TEXT',
mssql: 'NVARCHAR(MAX)' // in mssql text is actually representing a non unicode text field mssql: 'NVARCHAR(MAX)' // in mssql text is actually representing a non unicode text field
}); });
testsql('TEXT("tiny")', DataTypes.TEXT('tiny'), {
default: 'TEXT',
mssql: 'NVARCHAR(256)',
mysql: 'TINYTEXT',
mariadb: 'TINYTEXT'
});
testsql('TEXT({ length: "tiny" })', DataTypes.TEXT({ length: 'tiny' }), {
default: 'TEXT',
mssql: 'NVARCHAR(256)',
mysql: 'TINYTEXT',
mariadb: 'TINYTEXT'
});
testsql('TEXT("medium")', DataTypes.TEXT('medium'), {
default: 'TEXT',
mssql: 'NVARCHAR(MAX)',
mysql: 'MEDIUMTEXT',
mariadb: 'MEDIUMTEXT'
});
testsql('TEXT("long")', DataTypes.TEXT('long'), {
default: 'TEXT',
mssql: 'NVARCHAR(MAX)',
mysql: 'LONGTEXT',
mariadb: 'LONGTEXT'
});
}); });
suite('CHAR', function () { suite('CHAR', function () {
...@@ -57,6 +90,10 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -57,6 +90,10 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
default: 'CHAR(12)' default: 'CHAR(12)'
}); });
testsql('CHAR({ length: 12 })', DataTypes.CHAR({ length: 12 }), {
default: 'CHAR(12)'
});
testsql('CHAR(12).BINARY', DataTypes.CHAR(12).BINARY, { testsql('CHAR(12).BINARY', DataTypes.CHAR(12).BINARY, {
default: 'CHAR(12) BINARY', default: 'CHAR(12) BINARY',
sqlite: 'CHAR BINARY(12)', sqlite: 'CHAR BINARY(12)',
...@@ -135,6 +172,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -135,6 +172,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'INTEGER' mssql: 'INTEGER'
}); });
testsql('INTEGER({ length: 11 })', DataTypes.INTEGER({ length: 11 }), {
default: 'INTEGER(11)',
postgres: 'INTEGER',
mssql: 'INTEGER'
});
testsql('INTEGER(11).UNSIGNED', DataTypes.INTEGER(11).UNSIGNED, { testsql('INTEGER(11).UNSIGNED', DataTypes.INTEGER(11).UNSIGNED, {
default: 'INTEGER(11) UNSIGNED', default: 'INTEGER(11) UNSIGNED',
sqlite: 'INTEGER UNSIGNED(11)', sqlite: 'INTEGER UNSIGNED(11)',
...@@ -187,6 +230,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -187,6 +230,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'BIGINT' mssql: 'BIGINT'
}); });
testsql('BIGINT({ length: 11 })', DataTypes.BIGINT({ length: 11 }), {
default: 'BIGINT(11)',
postgres: 'BIGINT',
mssql: 'BIGINT'
});
testsql('BIGINT(11).UNSIGNED', DataTypes.BIGINT(11).UNSIGNED, { testsql('BIGINT(11).UNSIGNED', DataTypes.BIGINT(11).UNSIGNED, {
default: 'BIGINT(11) UNSIGNED', default: 'BIGINT(11) UNSIGNED',
sqlite: 'BIGINT UNSIGNED(11)', sqlite: 'BIGINT UNSIGNED(11)',
...@@ -233,6 +282,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -233,6 +282,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'REAL' mssql: 'REAL'
}); });
testsql('REAL({ length: 11 })', DataTypes.REAL({ length: 11 }), {
default: 'REAL(11)',
postgres: 'REAL',
mssql: 'REAL'
});
testsql('REAL(11).UNSIGNED', DataTypes.REAL(11).UNSIGNED, { testsql('REAL(11).UNSIGNED', DataTypes.REAL(11).UNSIGNED, {
default: 'REAL(11) UNSIGNED', default: 'REAL(11) UNSIGNED',
sqlite: 'REAL UNSIGNED(11)', sqlite: 'REAL UNSIGNED(11)',
...@@ -274,6 +329,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -274,6 +329,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'REAL' mssql: 'REAL'
}); });
testsql('REAL({ length: 11, decimals: 12 }).UNSIGNED', DataTypes.REAL({ length: 11, decimals: 12 }).UNSIGNED, {
default: 'REAL(11,12) UNSIGNED',
sqlite: 'REAL UNSIGNED(11,12)',
postgres: 'REAL',
mssql: 'REAL'
});
testsql('REAL(11, 12).UNSIGNED.ZEROFILL', DataTypes.REAL(11, 12).UNSIGNED.ZEROFILL, { testsql('REAL(11, 12).UNSIGNED.ZEROFILL', DataTypes.REAL(11, 12).UNSIGNED.ZEROFILL, {
default: 'REAL(11,12) UNSIGNED ZEROFILL', default: 'REAL(11,12) UNSIGNED ZEROFILL',
sqlite: 'REAL UNSIGNED ZEROFILL(11,12)', sqlite: 'REAL UNSIGNED ZEROFILL(11,12)',
...@@ -317,6 +379,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -317,6 +379,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
postgres: 'DOUBLE PRECISION' postgres: 'DOUBLE PRECISION'
}); });
testsql('DOUBLE({ length: 11 }).UNSIGNED', DataTypes.DOUBLE({ length: 11 }).UNSIGNED, {
default: 'DOUBLE PRECISION(11) UNSIGNED',
sqlite: 'DOUBLE PRECISION UNSIGNED(11)',
postgres: 'DOUBLE PRECISION'
});
testsql('DOUBLE(11).UNSIGNED.ZEROFILL', DataTypes.DOUBLE(11).UNSIGNED.ZEROFILL, { testsql('DOUBLE(11).UNSIGNED.ZEROFILL', DataTypes.DOUBLE(11).UNSIGNED.ZEROFILL, {
default: 'DOUBLE PRECISION(11) UNSIGNED ZEROFILL', default: 'DOUBLE PRECISION(11) UNSIGNED ZEROFILL',
sqlite: 'DOUBLE PRECISION UNSIGNED ZEROFILL(11)', sqlite: 'DOUBLE PRECISION UNSIGNED ZEROFILL(11)',
...@@ -404,6 +472,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -404,6 +472,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'FLOAT(11)' mssql: 'FLOAT(11)'
}); });
testsql('FLOAT({ length: 11 }).ZEROFILL', DataTypes.FLOAT({ length: 11 }).ZEROFILL, {
default: 'FLOAT(11) ZEROFILL',
sqlite: 'FLOAT ZEROFILL(11)',
postgres: 'FLOAT(11)',
mssql: 'FLOAT(11)'
});
testsql('FLOAT(11).ZEROFILL.UNSIGNED', DataTypes.FLOAT(11).ZEROFILL.UNSIGNED, { testsql('FLOAT(11).ZEROFILL.UNSIGNED', DataTypes.FLOAT(11).ZEROFILL.UNSIGNED, {
default: 'FLOAT(11) UNSIGNED ZEROFILL', default: 'FLOAT(11) UNSIGNED ZEROFILL',
sqlite: 'FLOAT UNSIGNED ZEROFILL(11)', sqlite: 'FLOAT UNSIGNED ZEROFILL(11)',
...@@ -424,6 +499,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -424,6 +499,13 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
mssql: 'FLOAT' mssql: 'FLOAT'
}); });
testsql('FLOAT({ length: 11, decimals: 12 }).UNSIGNED', DataTypes.FLOAT({ length: 11, decimals: 12 }).UNSIGNED, {
default: 'FLOAT(11,12) UNSIGNED',
sqlite: 'FLOAT UNSIGNED(11,12)',
postgres: 'FLOAT',
mssql: 'FLOAT'
});
testsql('FLOAT(11, 12).UNSIGNED.ZEROFILL', DataTypes.FLOAT(11, 12).UNSIGNED.ZEROFILL, { testsql('FLOAT(11, 12).UNSIGNED.ZEROFILL', DataTypes.FLOAT(11, 12).UNSIGNED.ZEROFILL, {
default: 'FLOAT(11,12) UNSIGNED ZEROFILL', default: 'FLOAT(11,12) UNSIGNED ZEROFILL',
sqlite: 'FLOAT UNSIGNED ZEROFILL(11,12)', sqlite: 'FLOAT UNSIGNED ZEROFILL(11,12)',
...@@ -465,9 +547,17 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -465,9 +547,17 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
default: 'DECIMAL(10,2)' default: 'DECIMAL(10,2)'
}); });
testsql('DECIMAL({ precision: 10, scale: 2 })', DataTypes.DECIMAL({ precision: 10, scale: 2 }), {
default: 'DECIMAL(10,2)'
});
testsql('DECIMAL(10)', DataTypes.DECIMAL(10), { testsql('DECIMAL(10)', DataTypes.DECIMAL(10), {
default: 'DECIMAL(10)' default: 'DECIMAL(10)'
}); });
testsql('DECIMAL({ precision: 10 })', DataTypes.DECIMAL({ precision: 10 }), {
default: 'DECIMAL(10)'
});
}); });
suite('BLOB', function () { suite('BLOB', function () {
...@@ -477,19 +567,25 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -477,19 +567,25 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
postgres: 'BYTEA' postgres: 'BYTEA'
}); });
testsql('BLOB(\'tiny\')', DataTypes.BLOB('tiny'), { testsql('BLOB("tiny")', DataTypes.BLOB('tiny'), {
default: 'TINYBLOB', default: 'TINYBLOB',
mssql: 'VARBINARY(256)',
postgres: 'BYTEA'
});
testsql('BLOB("medium")', DataTypes.BLOB('medium'), {
default: 'MEDIUMBLOB',
mssql: 'VARBINARY(MAX)', mssql: 'VARBINARY(MAX)',
postgres: 'BYTEA' postgres: 'BYTEA'
}); });
testsql('BLOB(\'medium\')', DataTypes.BLOB('medium'), { testsql('BLOB({ length: "medium" })', DataTypes.BLOB({ length: 'medium' }), {
default: 'MEDIUMBLOB', default: 'MEDIUMBLOB',
mssql: 'VARBINARY(MAX)', mssql: 'VARBINARY(MAX)',
postgres: 'BYTEA' postgres: 'BYTEA'
}); });
testsql('BLOB(\'long\')', DataTypes.BLOB('long'), { testsql('BLOB("long")', DataTypes.BLOB('long'), {
default: 'LONGBLOB', default: 'LONGBLOB',
mssql: 'VARBINARY(MAX)', mssql: 'VARBINARY(MAX)',
postgres: 'BYTEA' postgres: 'BYTEA'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!