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

Commit cc4ee8b8 by Felix Becker Committed by Mick Hansen

ES6 refactor of data types (#6072)

- Use let, const and property shorthands.
 - The data types are not changed to ES6 classes because it should be possible
   to call them without `new`.
 - Use function declarations so the functions (which are classes) have a name
 - Simplify `inherits` function and move it to utils.js
   The manual style may be a bit more verbose but it comes closer to true ES6
   classes because the functions are named and not dynamically created
 - Name methods
 - Fix bugs in dialect data types where constructor arguments were not applied
   when called without `new`
 - Remove unneeded code
1 parent b9bdc1d4
'use strict';
var _ = require('lodash');
const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
BaseTypes.DATE.types.mssql = [42];
BaseTypes.STRING.types.mssql = [231, 173];
......@@ -24,9 +25,13 @@ module.exports = function (BaseTypes) {
// BaseTypes.GEOMETRY.types.mssql = [240]; // not yet supported
BaseTypes.GEOMETRY.types.mssql = false;
var BLOB = BaseTypes.BLOB.inherits();
function BLOB(length) {
if (!(this instanceof BLOB)) return new BLOB(length);
BaseTypes.BLOB.apply(this, arguments);
}
inherits(BLOB, BaseTypes.BLOB);
BLOB.prototype.toSql = function() {
BLOB.prototype.toSql = function toSql() {
if (this._length) {
if (this._length.toLowerCase() === 'tiny') { // tiny = 2^8
warn('MSSQL does not support BLOB with the `length` = `tiny` option. `VARBINARY(256)` will be used instead.');
......@@ -37,13 +42,17 @@ module.exports = function (BaseTypes) {
return 'VARBINARY(MAX)';
};
BLOB.prototype.$hexify = function (hex) {
BLOB.prototype.$hexify = function $hexify(hex) {
return '0x' + hex;
};
var STRING = BaseTypes.STRING.inherits();
function STRING(length, binary) {
if (!(this instanceof STRING)) return new STRING(length, binary);
BaseTypes.STRING.apply(this, arguments);
}
inherits(STRING, BaseTypes.STRING);
STRING.prototype.toSql = function() {
STRING.prototype.toSql = function toSql() {
if (!this._binary) {
return 'NVARCHAR(' + this._length + ')';
} else{
......@@ -52,7 +61,7 @@ module.exports = function (BaseTypes) {
};
STRING.prototype.escape = false;
STRING.prototype.$stringify = function (value, options) {
STRING.prototype.$stringify = function $stringify(value, options) {
if (this._binary) {
return BLOB.prototype.$stringify(value);
} else {
......@@ -60,9 +69,13 @@ module.exports = function (BaseTypes) {
}
};
var TEXT = BaseTypes.TEXT.inherits();
function TEXT(length) {
if (!(this instanceof TEXT)) return new TEXT(length);
BaseTypes.TEXT.apply(this, arguments);
}
inherits(TEXT, BaseTypes.TEXT);
TEXT.prototype.toSql = function() {
TEXT.prototype.toSql = function toSql() {
// 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) {
......@@ -75,39 +88,55 @@ module.exports = function (BaseTypes) {
return 'NVARCHAR(MAX)';
};
var BOOLEAN = BaseTypes.BOOLEAN.inherits();
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
}
inherits(BOOLEAN, BaseTypes.BOOLEAN);
BOOLEAN.prototype.toSql = function() {
BOOLEAN.prototype.toSql = function toSql() {
return 'BIT';
};
var UUID = BaseTypes.UUID.inherits();
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
}
inherits(UUID, BaseTypes.UUID);
UUID.prototype.toSql = function() {
UUID.prototype.toSql = function toSql() {
return 'CHAR(36)';
};
var NOW = BaseTypes.NOW.inherits();
function NOW() {
if (!(this instanceof NOW)) return new NOW();
BaseTypes.NOW.apply(this, arguments);
}
inherits(NOW, BaseTypes.NOW);
NOW.prototype.toSql = function() {
NOW.prototype.toSql = function toSql() {
return 'GETDATE()';
};
var DATE = BaseTypes.DATE.inherits();
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function() {
DATE.prototype.toSql = function toSql() {
return 'DATETIME2';
};
DATE.prototype.$stringify = function (date, options) {
DATE.prototype.$stringify = function $stringify(date, options) {
date = this.$applyTimezone(date, options);
// mssql not allow +timezone datetime format
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
};
var INTEGER = BaseTypes.INTEGER.inherits(function() {
if (!(this instanceof INTEGER)) return new INTEGER();
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any options for integer
......@@ -118,10 +147,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(INTEGER, BaseTypes.INTEGER);
var BIGINT = BaseTypes.BIGINT.inherits(function() {
if (!(this instanceof BIGINT)) return new BIGINT();
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any options for bigint
......@@ -132,10 +162,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(BIGINT, BaseTypes.BIGINT);
var REAL = BaseTypes.REAL.inherits(function() {
if (!(this instanceof REAL)) return new REAL();
function REAL(length, decimals) {
if (!(this instanceof REAL)) return new REAL(length, decimals);
BaseTypes.REAL.apply(this, arguments);
// MSSQL does not support any options for real
......@@ -146,10 +177,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(REAL, BaseTypes.REAL);
var FLOAT = BaseTypes.FLOAT.inherits(function() {
if (!(this instanceof FLOAT)) return new FLOAT();
function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments);
// MSSQL does only support lengths as option.
......@@ -169,32 +201,42 @@ module.exports = function (BaseTypes) {
warn('MSSQL does not support Float zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined;
}
});
}
inherits(FLOAT, BaseTypes.FLOAT);
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
var ENUM = BaseTypes.ENUM.inherits();
ENUM.prototype.toSql = function() {
ENUM.prototype.toSql = function toSql() {
return 'VARCHAR(255)';
};
var exports = {
BLOB: BLOB,
BOOLEAN: BOOLEAN,
ENUM: ENUM,
STRING: STRING,
UUID: UUID,
DATE: DATE,
NOW: NOW,
INTEGER: INTEGER,
BIGINT: BIGINT,
REAL: REAL,
FLOAT: FLOAT,
TEXT: TEXT
const exports = {
BLOB,
BOOLEAN,
ENUM,
STRING,
UUID,
DATE,
NOW,
INTEGER,
BIGINT,
REAL,
FLOAT,
TEXT
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
DataType.extend = function extend(oldType) {
return new DataType(oldType.options);
};
}
......
'use strict';
var wkx = require('wkx')
, _ = require('lodash')
, moment = require('moment-timezone');
const wkx = require('wkx');
const _ = require('lodash');
const moment = require('moment-timezone');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
module.exports = BaseTypes => {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
BaseTypes.DATE.types.mysql = ['DATETIME'];
......@@ -24,13 +25,17 @@ module.exports = function (BaseTypes) {
BaseTypes.REAL.types.mysql = ['DOUBLE'];
BaseTypes.DOUBLE.types.mysql = ['DOUBLE'];
var DATE = BaseTypes.DATE.inherits();
function DATE(length) {
if (!(this instanceof DATE)) return new Date(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function () {
DATE.prototype.toSql = function toSql() {
return 'DATETIME' + (this._length ? '(' + this._length + ')' : '');
};
DATE.prototype.$stringify = function (date, options) {
DATE.prototype.$stringify = function $stringify(date, options) {
date = BaseTypes.DATE.prototype.$applyTimezone(date, options);
// Fractional DATETIMEs only supported on MySQL 5.6.4+
if (this._length) {
......@@ -40,7 +45,7 @@ module.exports = function (BaseTypes) {
return date.format('YYYY-MM-DD HH:mm:ss');
};
DATE.parse = function (value, options) {
DATE.parse = function parse(value, options) {
value = value.string();
if (value === null) {
......@@ -56,15 +61,20 @@ module.exports = function (BaseTypes) {
return value;
};
var UUID = BaseTypes.UUID.inherits();
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
}
inherits(UUID, BaseTypes.UUID);
UUID.prototype.toSql = function() {
UUID.prototype.toSql = function toSql() {
return 'CHAR(36) BINARY';
};
var SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
var GEOMETRY = BaseTypes.GEOMETRY.inherits(function() {
if (!(this instanceof GEOMETRY)) return new GEOMETRY();
const SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
function GEOMETRY(type, srid) {
if (!(this instanceof GEOMETRY)) return new GEOMETRY(type, srid);
BaseTypes.GEOMETRY.apply(this, arguments);
if (_.isEmpty(this.type)) {
......@@ -74,9 +84,10 @@ module.exports = function (BaseTypes) {
} else {
throw new Error('Supported geometry types are: ' + SUPPORTED_GEOMETRY_TYPES.join(', '));
}
});
}
inherits(GEOMETRY, BaseTypes.GEOMETRY);
GEOMETRY.parse = GEOMETRY.prototype.parse = function(value) {
GEOMETRY.parse = GEOMETRY.prototype.parse = function parse(value) {
value = value.buffer();
//MySQL doesn't support POINT EMPTY, https://dev.mysql.com/worklog/task/?id=2381
......@@ -90,31 +101,37 @@ module.exports = function (BaseTypes) {
return wkx.Geometry.parse(value).toGeoJSON();
};
GEOMETRY.prototype.toSql = function() {
GEOMETRY.prototype.toSql = function toSql() {
return this.sqlType;
};
var ENUM = BaseTypes.ENUM.inherits();
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function (options) {
return 'ENUM(' + _.map(this.values, function(value) {
return options.escape(value);
}).join(', ') + ')';
ENUM.prototype.toSql = function toSql(options) {
return 'ENUM(' + _.map(this.values, value => options.escape(value)).join(', ') + ')';
};
BaseTypes.GEOMETRY.types.mysql = ['GEOMETRY'];
var exports = {
ENUM: ENUM,
DATE: DATE,
UUID: UUID,
GEOMETRY: GEOMETRY
const exports = {
ENUM,
DATE,
UUID,
GEOMETRY
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
DataType.extend = function extend(oldType) {
return new DataType(oldType.options);
};
}
......
'use strict';
var _ = require('lodash');
const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
BaseTypes.DATE.types.sqlite = ['DATETIME'];
BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY'];
......@@ -23,8 +24,13 @@ module.exports = function (BaseTypes) {
BaseTypes.DOUBLE.types.sqlite = ['DOUBLE PRECISION'];
BaseTypes.GEOMETRY.types.sqlite = false;
var DATE = BaseTypes.DATE.inherits();
DATE.parse = function (date, options) {
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.parse = function parse(date, options) {
if (date.indexOf('+') === -1) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
return new Date(date + options.timezone);
......@@ -33,8 +39,13 @@ module.exports = function (BaseTypes) {
}
};
var STRING = BaseTypes.STRING.inherits();
STRING.prototype.toSql = function() {
function STRING(length, binary) {
if (!(this instanceof STRING)) return new STRING(length, binary);
BaseTypes.STRING.apply(this, arguments);
}
inherits(STRING, BaseTypes.STRING);
STRING.prototype.toSql = function toSql() {
if (this._binary) {
return 'VARCHAR BINARY(' + this._length + ')';
} else {
......@@ -42,8 +53,13 @@ module.exports = function (BaseTypes) {
}
};
var TEXT = BaseTypes.TEXT.inherits();
TEXT.prototype.toSql = function() {
function TEXT(length) {
if (!(this instanceof TEXT)) return new TEXT(length);
BaseTypes.TEXT.apply(this, arguments);
}
inherits(TEXT, BaseTypes.TEXT);
TEXT.prototype.toSql = function toSql() {
if (this._length) {
warn('SQLite does not support TEXT with options. Plain `TEXT` will be used instead.');
this._length = undefined;
......@@ -51,8 +67,13 @@ module.exports = function (BaseTypes) {
return 'TEXT';
};
var CHAR = BaseTypes.CHAR.inherits();
CHAR.prototype.toSql = function() {
function CHAR(length, binary) {
if (!(this instanceof CHAR)) return new CHAR(length, binary);
BaseTypes.CHAR.apply(this, arguments);
}
inherits(CHAR, BaseTypes.CHAR);
CHAR.prototype.toSql = function toSql() {
if (this._binary) {
return 'CHAR BINARY(' + this._length + ')';
} else {
......@@ -60,9 +81,14 @@ module.exports = function (BaseTypes) {
}
};
var NUMBER = BaseTypes.NUMBER.inherits();
NUMBER.prototype.toSql = function() {
var result = this.key;
function NUMBER(options) {
if (!(this instanceof NUMBER)) return new NUMBER(options);
BaseTypes.NUMBER.apply(this, arguments);
}
inherits(NUMBER, BaseTypes.NUMBER);
NUMBER.prototype.toSql = function toSql() {
let result = this.key;
if (this._unsigned) {
result += ' UNSIGNED';
......@@ -81,71 +107,55 @@ module.exports = function (BaseTypes) {
return result;
};
var INTEGER = BaseTypes.INTEGER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof INTEGER)) return new INTEGER(options);
BaseTypes.INTEGER.call(this, options);
});
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.toSql = function() {
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
}
inherits(INTEGER, BaseTypes.INTEGER);
INTEGER.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var BIGINT = BaseTypes.BIGINT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof BIGINT)) return new BIGINT(options);
BaseTypes.BIGINT.call(this, options);
});
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.toSql = function() {
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
}
inherits(BIGINT, BaseTypes.BIGINT);
BIGINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var FLOAT = BaseTypes.FLOAT.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof FLOAT)) return new FLOAT(options);
BaseTypes.FLOAT.call(this, options);
});
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.toSql = function() {
function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments);
}
inherits(FLOAT, BaseTypes.FLOAT);
FLOAT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var DOUBLE = BaseTypes.DOUBLE.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof DOUBLE)) return new DOUBLE(options);
BaseTypes.DOUBLE.call(this, options);
});
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
DOUBLE.prototype.toSql = function() {
function DOUBLE(length, decimals) {
if (!(this instanceof DOUBLE)) return new DOUBLE(length, decimals);
BaseTypes.DOUBLE.apply(this, arguments);
}
inherits(DOUBLE, BaseTypes.DOUBLE);
DOUBLE.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var REAL = BaseTypes.REAL.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof REAL)) return new REAL(options);
BaseTypes.REAL.call(this, options);
});
REAL.prototype.key = REAL.key = 'REAL';
REAL.prototype.toSql = function() {
function REAL(length, decimals) {
if (!(this instanceof REAL)) return new REAL(length, decimals);
BaseTypes.REAL.apply(this, arguments);
}
inherits(REAL, BaseTypes.REAL);
REAL.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
[FLOAT, DOUBLE, REAL].forEach(function (floating) {
floating.parse = function (value) {
[FLOAT, DOUBLE, REAL].forEach(floating => {
floating.parse = function parse(value) {
if (_.isString(value)) {
if (value === 'NaN') {
return NaN;
......@@ -159,30 +169,38 @@ module.exports = function (BaseTypes) {
};
});
var ENUM = BaseTypes.ENUM.inherits();
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function () {
ENUM.prototype.toSql = function toSql() {
return 'TEXT';
};
var exports = {
DATE: DATE,
STRING: STRING,
CHAR: CHAR,
NUMBER: NUMBER,
FLOAT: FLOAT,
REAL: REAL,
const exports = {
DATE,
STRING,
CHAR,
NUMBER,
FLOAT,
REAL,
'DOUBLE PRECISION': DOUBLE,
INTEGER: INTEGER,
BIGINT: BIGINT,
TEXT: TEXT,
ENUM: ENUM
INTEGER,
BIGINT,
TEXT,
ENUM
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
DataType.extend = oldType => {
return new DataType(oldType.options);
};
}
......
'use strict';
const util = require('util');
const _ = require('lodash');
/** like util.inherits, but also copies over static properties */
function inherits(constructor, superConstructor) {
util.inherits(constructor, superConstructor); // Instance (prototype) methods
_.extend(constructor, superConstructor); // Static methods
}
module.exports = inherits;
module.exports.inherits = inherits;
module.exports.default = inherits;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!