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

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'; 'use strict';
var _ = require('lodash'); const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) { module.exports = BaseTypes => {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx'); 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.DATE.types.mssql = [42];
BaseTypes.STRING.types.mssql = [231, 173]; BaseTypes.STRING.types.mssql = [231, 173];
...@@ -24,9 +25,13 @@ module.exports = function (BaseTypes) { ...@@ -24,9 +25,13 @@ module.exports = function (BaseTypes) {
// BaseTypes.GEOMETRY.types.mssql = [240]; // not yet supported // BaseTypes.GEOMETRY.types.mssql = [240]; // not yet supported
BaseTypes.GEOMETRY.types.mssql = false; 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) {
if (this._length.toLowerCase() === 'tiny') { // tiny = 2^8 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.'); 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) { ...@@ -37,13 +42,17 @@ module.exports = function (BaseTypes) {
return 'VARBINARY(MAX)'; return 'VARBINARY(MAX)';
}; };
BLOB.prototype.$hexify = function (hex) { BLOB.prototype.$hexify = function $hexify(hex) {
return '0x' + 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) { if (!this._binary) {
return 'NVARCHAR(' + this._length + ')'; return 'NVARCHAR(' + this._length + ')';
} else{ } else{
...@@ -52,7 +61,7 @@ module.exports = function (BaseTypes) { ...@@ -52,7 +61,7 @@ module.exports = function (BaseTypes) {
}; };
STRING.prototype.escape = false; STRING.prototype.escape = false;
STRING.prototype.$stringify = function (value, options) { STRING.prototype.$stringify = function $stringify(value, options) {
if (this._binary) { if (this._binary) {
return BLOB.prototype.$stringify(value); return BLOB.prototype.$stringify(value);
} else { } else {
...@@ -60,9 +69,13 @@ module.exports = function (BaseTypes) { ...@@ -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. // 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) { if (this._length) {
...@@ -75,39 +88,55 @@ module.exports = function (BaseTypes) { ...@@ -75,39 +88,55 @@ module.exports = function (BaseTypes) {
return 'NVARCHAR(MAX)'; 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'; 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)'; 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()'; 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'; return 'DATETIME2';
}; };
DATE.prototype.$stringify = function (date, options) { DATE.prototype.$stringify = function $stringify(date, options) {
date = this.$applyTimezone(date, options); date = this.$applyTimezone(date, options);
// mssql not allow +timezone datetime format // mssql not allow +timezone datetime format
return date.format('YYYY-MM-DD HH:mm:ss.SSS'); return date.format('YYYY-MM-DD HH:mm:ss.SSS');
}; };
var INTEGER = BaseTypes.INTEGER.inherits(function() { function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(); if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments); BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any options for integer // MSSQL does not support any options for integer
...@@ -118,10 +147,11 @@ module.exports = function (BaseTypes) { ...@@ -118,10 +147,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined; this._unsigned = undefined;
this._zerofill = undefined; this._zerofill = undefined;
} }
}); }
inherits(INTEGER, BaseTypes.INTEGER);
var BIGINT = BaseTypes.BIGINT.inherits(function() { function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(); if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments); BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any options for bigint // MSSQL does not support any options for bigint
...@@ -132,10 +162,11 @@ module.exports = function (BaseTypes) { ...@@ -132,10 +162,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined; this._unsigned = undefined;
this._zerofill = undefined; this._zerofill = undefined;
} }
}); }
inherits(BIGINT, BaseTypes.BIGINT);
var REAL = BaseTypes.REAL.inherits(function() { function REAL(length, decimals) {
if (!(this instanceof REAL)) return new REAL(); if (!(this instanceof REAL)) return new REAL(length, decimals);
BaseTypes.REAL.apply(this, arguments); BaseTypes.REAL.apply(this, arguments);
// MSSQL does not support any options for real // MSSQL does not support any options for real
...@@ -146,10 +177,11 @@ module.exports = function (BaseTypes) { ...@@ -146,10 +177,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined; this._unsigned = undefined;
this._zerofill = undefined; this._zerofill = undefined;
} }
}); }
inherits(REAL, BaseTypes.REAL);
var FLOAT = BaseTypes.FLOAT.inherits(function() { function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(); if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments); BaseTypes.FLOAT.apply(this, arguments);
// MSSQL does only support lengths as option. // MSSQL does only support lengths as option.
...@@ -169,32 +201,42 @@ module.exports = function (BaseTypes) { ...@@ -169,32 +201,42 @@ module.exports = function (BaseTypes) {
warn('MSSQL does not support Float zerofill. `ZEROFILL` was removed.'); warn('MSSQL does not support Float zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined; 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 toSql() {
ENUM.prototype.toSql = function() {
return 'VARCHAR(255)'; return 'VARCHAR(255)';
}; };
var exports = { const exports = {
BLOB: BLOB, BLOB,
BOOLEAN: BOOLEAN, BOOLEAN,
ENUM: ENUM, ENUM,
STRING: STRING, STRING,
UUID: UUID, UUID,
DATE: DATE, DATE,
NOW: NOW, NOW,
INTEGER: INTEGER, INTEGER,
BIGINT: BIGINT, BIGINT,
REAL: REAL, REAL,
FLOAT: FLOAT, FLOAT,
TEXT: TEXT TEXT
}; };
_.forIn(exports, function (DataType, key) { _.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key; if (!DataType.key) DataType.key = key;
if (!DataType.extend) { if (!DataType.extend) {
DataType.extend = function(oldType) { DataType.extend = function extend(oldType) {
return new DataType(oldType.options); return new DataType(oldType.options);
}; };
} }
......
'use strict'; 'use strict';
var wkx = require('wkx') const wkx = require('wkx');
, _ = require('lodash') const _ = require('lodash');
, moment = require('moment-timezone'); 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.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
BaseTypes.DATE.types.mysql = ['DATETIME']; BaseTypes.DATE.types.mysql = ['DATETIME'];
...@@ -24,13 +25,17 @@ module.exports = function (BaseTypes) { ...@@ -24,13 +25,17 @@ module.exports = function (BaseTypes) {
BaseTypes.REAL.types.mysql = ['DOUBLE']; BaseTypes.REAL.types.mysql = ['DOUBLE'];
BaseTypes.DOUBLE.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 + ')' : ''); 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); date = BaseTypes.DATE.prototype.$applyTimezone(date, options);
// Fractional DATETIMEs only supported on MySQL 5.6.4+ // Fractional DATETIMEs only supported on MySQL 5.6.4+
if (this._length) { if (this._length) {
...@@ -40,7 +45,7 @@ module.exports = function (BaseTypes) { ...@@ -40,7 +45,7 @@ module.exports = function (BaseTypes) {
return date.format('YYYY-MM-DD HH:mm:ss'); return date.format('YYYY-MM-DD HH:mm:ss');
}; };
DATE.parse = function (value, options) { DATE.parse = function parse(value, options) {
value = value.string(); value = value.string();
if (value === null) { if (value === null) {
...@@ -56,15 +61,20 @@ module.exports = function (BaseTypes) { ...@@ -56,15 +61,20 @@ module.exports = function (BaseTypes) {
return value; 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'; return 'CHAR(36) BINARY';
}; };
var SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON']; const SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
var GEOMETRY = BaseTypes.GEOMETRY.inherits(function() {
if (!(this instanceof GEOMETRY)) return new GEOMETRY(); function GEOMETRY(type, srid) {
if (!(this instanceof GEOMETRY)) return new GEOMETRY(type, srid);
BaseTypes.GEOMETRY.apply(this, arguments); BaseTypes.GEOMETRY.apply(this, arguments);
if (_.isEmpty(this.type)) { if (_.isEmpty(this.type)) {
...@@ -74,9 +84,10 @@ module.exports = function (BaseTypes) { ...@@ -74,9 +84,10 @@ module.exports = function (BaseTypes) {
} else { } else {
throw new Error('Supported geometry types are: ' + SUPPORTED_GEOMETRY_TYPES.join(', ')); 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(); value = value.buffer();
//MySQL doesn't support POINT EMPTY, https://dev.mysql.com/worklog/task/?id=2381 //MySQL doesn't support POINT EMPTY, https://dev.mysql.com/worklog/task/?id=2381
...@@ -90,31 +101,37 @@ module.exports = function (BaseTypes) { ...@@ -90,31 +101,37 @@ module.exports = function (BaseTypes) {
return wkx.Geometry.parse(value).toGeoJSON(); return wkx.Geometry.parse(value).toGeoJSON();
}; };
GEOMETRY.prototype.toSql = function() { GEOMETRY.prototype.toSql = function toSql() {
return this.sqlType; 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) { ENUM.prototype.toSql = function toSql(options) {
return 'ENUM(' + _.map(this.values, function(value) { return 'ENUM(' + _.map(this.values, value => options.escape(value)).join(', ') + ')';
return options.escape(value);
}).join(', ') + ')';
}; };
BaseTypes.GEOMETRY.types.mysql = ['GEOMETRY']; BaseTypes.GEOMETRY.types.mysql = ['GEOMETRY'];
var exports = { const exports = {
ENUM: ENUM, ENUM,
DATE: DATE, DATE,
UUID: UUID, UUID,
GEOMETRY: GEOMETRY GEOMETRY
}; };
_.forIn(exports, function (DataType, key) { _.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key; if (!DataType.key) DataType.key = key;
if (!DataType.extend) { if (!DataType.extend) {
DataType.extend = function(oldType) { DataType.extend = function extend(oldType) {
return new DataType(oldType.options); return new DataType(oldType.options);
}; };
} }
......
'use strict'; 'use strict';
var _ = require('lodash'); const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) { module.exports = BaseTypes => {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html'); const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
BaseTypes.DATE.types.sqlite = ['DATETIME']; BaseTypes.DATE.types.sqlite = ['DATETIME'];
BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY']; BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY'];
...@@ -23,8 +24,13 @@ module.exports = function (BaseTypes) { ...@@ -23,8 +24,13 @@ module.exports = function (BaseTypes) {
BaseTypes.DOUBLE.types.sqlite = ['DOUBLE PRECISION']; BaseTypes.DOUBLE.types.sqlite = ['DOUBLE PRECISION'];
BaseTypes.GEOMETRY.types.sqlite = false; BaseTypes.GEOMETRY.types.sqlite = false;
var DATE = BaseTypes.DATE.inherits(); function DATE(length) {
DATE.parse = function (date, options) { 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) { if (date.indexOf('+') === -1) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set // For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
return new Date(date + options.timezone); return new Date(date + options.timezone);
...@@ -33,8 +39,13 @@ module.exports = function (BaseTypes) { ...@@ -33,8 +39,13 @@ module.exports = function (BaseTypes) {
} }
}; };
var STRING = BaseTypes.STRING.inherits(); function STRING(length, binary) {
STRING.prototype.toSql = function() { 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) { if (this._binary) {
return 'VARCHAR BINARY(' + this._length + ')'; return 'VARCHAR BINARY(' + this._length + ')';
} else { } else {
...@@ -42,8 +53,13 @@ module.exports = function (BaseTypes) { ...@@ -42,8 +53,13 @@ module.exports = function (BaseTypes) {
} }
}; };
var TEXT = BaseTypes.TEXT.inherits(); function TEXT(length) {
TEXT.prototype.toSql = function() { 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) { if (this._length) {
warn('SQLite does not support TEXT with options. Plain `TEXT` will be used instead.'); warn('SQLite does not support TEXT with options. Plain `TEXT` will be used instead.');
this._length = undefined; this._length = undefined;
...@@ -51,8 +67,13 @@ module.exports = function (BaseTypes) { ...@@ -51,8 +67,13 @@ module.exports = function (BaseTypes) {
return 'TEXT'; return 'TEXT';
}; };
var CHAR = BaseTypes.CHAR.inherits(); function CHAR(length, binary) {
CHAR.prototype.toSql = function() { 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) { if (this._binary) {
return 'CHAR BINARY(' + this._length + ')'; return 'CHAR BINARY(' + this._length + ')';
} else { } else {
...@@ -60,9 +81,14 @@ module.exports = function (BaseTypes) { ...@@ -60,9 +81,14 @@ module.exports = function (BaseTypes) {
} }
}; };
var NUMBER = BaseTypes.NUMBER.inherits(); function NUMBER(options) {
NUMBER.prototype.toSql = function() { if (!(this instanceof NUMBER)) return new NUMBER(options);
var result = this.key; BaseTypes.NUMBER.apply(this, arguments);
}
inherits(NUMBER, BaseTypes.NUMBER);
NUMBER.prototype.toSql = function toSql() {
let result = this.key;
if (this._unsigned) { if (this._unsigned) {
result += ' UNSIGNED'; result += ' UNSIGNED';
...@@ -81,71 +107,55 @@ module.exports = function (BaseTypes) { ...@@ -81,71 +107,55 @@ module.exports = function (BaseTypes) {
return result; return result;
}; };
var INTEGER = BaseTypes.INTEGER.inherits(function(length) { function INTEGER(length) {
var options = typeof length === 'object' && length || { if (!(this instanceof INTEGER)) return new INTEGER(length);
length: length BaseTypes.INTEGER.apply(this, arguments);
}; }
if (!(this instanceof INTEGER)) return new INTEGER(options); inherits(INTEGER, BaseTypes.INTEGER);
BaseTypes.INTEGER.call(this, options);
}); INTEGER.prototype.toSql = function toSql() {
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this); return NUMBER.prototype.toSql.call(this);
}; };
var BIGINT = BaseTypes.BIGINT.inherits(function(length) { function BIGINT(length) {
var options = typeof length === 'object' && length || { if (!(this instanceof BIGINT)) return new BIGINT(length);
length: length BaseTypes.BIGINT.apply(this, arguments);
}; }
if (!(this instanceof BIGINT)) return new BIGINT(options); inherits(BIGINT, BaseTypes.BIGINT);
BaseTypes.BIGINT.call(this, options);
}); BIGINT.prototype.toSql = function toSql() {
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this); return NUMBER.prototype.toSql.call(this);
}; };
var FLOAT = BaseTypes.FLOAT.inherits(function(length, decimals) { function FLOAT(length, decimals) {
var options = typeof length === 'object' && length || { if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
length: length, BaseTypes.FLOAT.apply(this, arguments);
decimals: decimals }
}; inherits(FLOAT, BaseTypes.FLOAT);
if (!(this instanceof FLOAT)) return new FLOAT(options); FLOAT.prototype.toSql = function toSql() {
BaseTypes.FLOAT.call(this, options);
});
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this); return NUMBER.prototype.toSql.call(this);
}; };
var DOUBLE = BaseTypes.DOUBLE.inherits(function(length, decimals) { function DOUBLE(length, decimals) {
var options = typeof length === 'object' && length || { if (!(this instanceof DOUBLE)) return new DOUBLE(length, decimals);
length: length, BaseTypes.DOUBLE.apply(this, arguments);
decimals: decimals }
}; inherits(DOUBLE, BaseTypes.DOUBLE);
if (!(this instanceof DOUBLE)) return new DOUBLE(options); DOUBLE.prototype.toSql = function toSql() {
BaseTypes.DOUBLE.call(this, options);
});
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
DOUBLE.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this); return NUMBER.prototype.toSql.call(this);
}; };
var REAL = BaseTypes.REAL.inherits(function(length, decimals) { function REAL(length, decimals) {
var options = typeof length === 'object' && length || { if (!(this instanceof REAL)) return new REAL(length, decimals);
length: length, BaseTypes.REAL.apply(this, arguments);
decimals: decimals }
}; inherits(REAL, BaseTypes.REAL);
if (!(this instanceof REAL)) return new REAL(options); REAL.prototype.toSql = function toSql() {
BaseTypes.REAL.call(this, options);
});
REAL.prototype.key = REAL.key = 'REAL';
REAL.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this); return NUMBER.prototype.toSql.call(this);
}; };
[FLOAT, DOUBLE, REAL].forEach(function (floating) { [FLOAT, DOUBLE, REAL].forEach(floating => {
floating.parse = function (value) { floating.parse = function parse(value) {
if (_.isString(value)) { if (_.isString(value)) {
if (value === 'NaN') { if (value === 'NaN') {
return NaN; return NaN;
...@@ -159,30 +169,38 @@ module.exports = function (BaseTypes) { ...@@ -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'; return 'TEXT';
}; };
var exports = { const exports = {
DATE: DATE, DATE,
STRING: STRING, STRING,
CHAR: CHAR, CHAR,
NUMBER: NUMBER, NUMBER,
FLOAT: FLOAT, FLOAT,
REAL: REAL, REAL,
'DOUBLE PRECISION': DOUBLE, 'DOUBLE PRECISION': DOUBLE,
INTEGER: INTEGER, INTEGER,
BIGINT: BIGINT, BIGINT,
TEXT: TEXT, TEXT,
ENUM: ENUM ENUM
}; };
_.forIn(exports, function (DataType, key) { _.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key; if (!DataType.key) DataType.key = key;
if (!DataType.extend) { if (!DataType.extend) {
DataType.extend = function(oldType) { DataType.extend = oldType => {
return new DataType(oldType.options); 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!