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

Commit 3c19e974 by up9cloud Committed by Sushant

feat(data-types): add TINYINT, SMALLINT, MEDIUMINT (#8515)

1 parent 17b19d9c
......@@ -141,7 +141,7 @@ NUMBER.prototype.toSql = function toSql() {
NUMBER.prototype.validate = function(value) {
if (!Validator.isFloat(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid number', value));
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid ' + _.toLower(this.key), value));
}
return true;
......@@ -172,27 +172,47 @@ inherits(INTEGER, NUMBER);
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid integer', value));
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid ' + _.toLower(this.key), value));
}
return true;
};
function TINYINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof TINYINT)) return new TINYINT(options);
NUMBER.call(this, options);
}
inherits(TINYINT, INTEGER);
TINYINT.prototype.key = TINYINT.key = 'TINYINT';
function SMALLINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof SMALLINT)) return new SMALLINT(options);
NUMBER.call(this, options);
}
inherits(SMALLINT, INTEGER);
SMALLINT.prototype.key = SMALLINT.key = 'SMALLINT';
function MEDIUMINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof MEDIUMINT)) return new MEDIUMINT(options);
NUMBER.call(this, options);
}
inherits(MEDIUMINT, INTEGER);
MEDIUMINT.prototype.key = MEDIUMINT.key = 'MEDIUMINT';
function BIGINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof BIGINT)) return new BIGINT(options);
NUMBER.call(this, options);
}
inherits(BIGINT, NUMBER);
inherits(BIGINT, INTEGER);
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid bigint', value));
}
return true;
};
function FLOAT(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
......@@ -664,8 +684,8 @@ ARRAY.is = function is(obj, type) {
const helpers = {
BINARY: [STRING, CHAR],
UNSIGNED: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
ZEROFILL: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
UNSIGNED: [NUMBER, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
ZEROFILL: [NUMBER, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
PRECISION: [DECIMAL],
SCALE: [DECIMAL]
};
......@@ -771,6 +791,9 @@ for (const helper of Object.keys(helpers)) {
* @property {function(length=255: integer)} STRING A variable length string
* @property {function(length=255: integer)} CHAR A fixed length string.
* @property {function([length]: string)} TEXT An unlimited length text column. Available lengths: `tiny`, `medium`, `long`
* @property {function(length: integer)} TINYINT A 8 bit integer.
* @property {function(length: integer)} SMALLINT A 16 bit integer.
* @property {function(length: integer)} MEDIUMINT A 24 bit integer.
* @property {function(length=255: integer)} INTEGER A 32 bit integer.
* @property {function(length: integer)} BIGINT A 64 bit integer. Note: an attribute defined as `BIGINT` will be treated like a `string` due this [feature from node-postgres](https://github.com/brianc/node-postgres/pull/353) to prevent precision loss. To have this attribute as a `number`, this is a possible [workaround](https://github.com/sequelize/sequelize/issues/2383#issuecomment-58006083).
* @property {function(length: integer, decimals: integer)} FLOAT Floating point number (4-byte precision).
......@@ -875,6 +898,9 @@ const DataTypes = module.exports = {
CHAR,
TEXT,
NUMBER,
TINYINT,
SMALLINT,
MEDIUMINT,
INTEGER,
BIGINT,
FLOAT,
......
......@@ -7,10 +7,19 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
/**
* types: [hex, ...]
* @see hex here https://github.com/tediousjs/tedious/blob/master/src/data-type.js
*/
BaseTypes.DATE.types.mssql = [43];
BaseTypes.STRING.types.mssql = [231, 173];
BaseTypes.CHAR.types.mssql = [175];
BaseTypes.TEXT.types.mssql = false;
// https://msdn.microsoft.com/en-us/library/ms187745(v=sql.110).aspx
BaseTypes.TINYINT.types.mssql = [30];
BaseTypes.SMALLINT.types.mssql = [34];
BaseTypes.MEDIUMINT.types.mssql = false;
BaseTypes.INTEGER.types.mssql = [38];
BaseTypes.BIGINT.types.mssql = false;
BaseTypes.FLOAT.types.mssql = [109];
......@@ -154,6 +163,36 @@ module.exports = BaseTypes => {
}
inherits(INTEGER, BaseTypes.INTEGER);
function TINYINT(length) {
if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments);
// MSSQL does not support any options for tinyint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support TINYINT with options. Plain `TINYINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
inherits(TINYINT, BaseTypes.TINYINT);
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
// MSSQL does not support any options for smallint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support SMALLINT with options. Plain `SMALLINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
inherits(SMALLINT, BaseTypes.SMALLINT);
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
......@@ -231,6 +270,8 @@ module.exports = BaseTypes => {
DATE,
DATEONLY,
NOW,
TINYINT,
SMALLINT,
INTEGER,
BIGINT,
REAL,
......
......@@ -8,10 +8,19 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
/**
* types: [buffer_type, ...]
* @see buffer_type here https://dev.mysql.com/doc/refman/5.7/en/c-api-prepared-statement-type-codes.html
* @see hex here https://github.com/sidorares/node-mysql2/blob/master/lib/constants/types.js
*/
BaseTypes.DATE.types.mysql = ['DATETIME'];
BaseTypes.STRING.types.mysql = ['VAR_STRING'];
BaseTypes.CHAR.types.mysql = ['STRING'];
BaseTypes.TEXT.types.mysql = ['BLOB'];
BaseTypes.TINYINT.types.mysql = ['TINY'];
BaseTypes.SMALLINT.types.mysql = ['SHORT'];
BaseTypes.MEDIUMINT.types.mysql = ['INT24'];
BaseTypes.INTEGER.types.mysql = ['LONG'];
BaseTypes.BIGINT.types.mysql = ['LONGLONG'];
BaseTypes.FLOAT.types.mysql = ['FLOAT'];
......
......@@ -7,6 +7,15 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'http://www.postgresql.org/docs/9.4/static/datatype.html');
/**
* types:
* {
* oids: [oid],
* array_oids: [oid]
* }
* @see oid here https://github.com/lib/pq/blob/master/oid/types.go
*/
BaseTypes.UUID.types.postgres = {
oids: [2950],
array_oids: [2951]
......@@ -230,6 +239,27 @@ module.exports = BaseTypes => {
array_oids: [1185]
};
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
// POSTGRES does not support any parameters for bigint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('PostgreSQL does not support SMALLINT with options. Plain `SMALLINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
inherits(SMALLINT, BaseTypes.SMALLINT);
// int2
BaseTypes.SMALLINT.types.postgres = {
oids: [21],
array_oids: [1005]
};
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
......@@ -556,11 +586,12 @@ module.exports = BaseTypes => {
STRING,
CHAR,
TEXT,
SMALLINT,
INTEGER,
BIGINT,
BOOLEAN,
DATE,
DATEONLY,
BIGINT,
REAL,
'DOUBLE PRECISION': DOUBLE,
FLOAT,
......
......@@ -6,10 +6,17 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
/**
* @see https://sqlite.org/datatype3.html
*/
BaseTypes.DATE.types.sqlite = ['DATETIME'];
BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY'];
BaseTypes.CHAR.types.sqlite = ['CHAR', 'CHAR BINARY'];
BaseTypes.TEXT.types.sqlite = ['TEXT'];
BaseTypes.TINYINT.types.sqlite = ['TINYINT'];
BaseTypes.SMALLINT.types.sqlite = ['SMALLINT'];
BaseTypes.MEDIUMINT.types.sqlite = ['MEDIUMINT'];
BaseTypes.INTEGER.types.sqlite = ['INTEGER'];
BaseTypes.BIGINT.types.sqlite = ['BIGINT'];
BaseTypes.FLOAT.types.sqlite = ['FLOAT'];
......@@ -128,6 +135,36 @@ module.exports = BaseTypes => {
return result;
};
function TINYINT(length) {
if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments);
}
inherits(TINYINT, BaseTypes.TINYINT);
TINYINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
}
inherits(SMALLINT, BaseTypes.SMALLINT);
SMALLINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function MEDIUMINT(length) {
if (!(this instanceof MEDIUMINT)) return new MEDIUMINT(length);
BaseTypes.MEDIUMINT.apply(this, arguments);
}
inherits(MEDIUMINT, BaseTypes.MEDIUMINT);
MEDIUMINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
......@@ -213,6 +250,9 @@ module.exports = BaseTypes => {
FLOAT,
REAL,
'DOUBLE PRECISION': DOUBLE,
TINYINT,
SMALLINT,
MEDIUMINT,
INTEGER,
BIGINT,
TEXT,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!