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

Commit 6e13324f by Mick Hansen

Merge pull request #4186 from sequelize/datatypes

WIP: Datatypes system refactor
2 parents 1b8d2ade 8879669a
'use strict';
/*jshint -W110 */
var util = require('util')
, _ = require('lodash')
, Wkt = require('wellknown')
, sequelizeErrors = require('./errors')
, warnings = {}
, Validator = require('validator');
, Validator = require('validator')
, moment = require('moment-timezone')
, hstore = require('./dialects/postgres/hstore');
/**
* A convenience class holding commonly used data types. The datatypes are used when defining a new model using `Sequelize.define`, like this:
......@@ -39,24 +42,50 @@ var util = require('util')
*/
var ABSTRACT = function(options) {
};
ABSTRACT.prototype.dialectTypes = '';
ABSTRACT.prototype.toString = function() {
return this.toSql();
ABSTRACT.prototype.toString = function(options) {
return this.toSql(options);
};
ABSTRACT.prototype.toSql = function() {
return this.key;
};
ABSTRACT.prototype.warn = function(text) {
ABSTRACT.warn = function(link, text) {
if (!warnings[text]) {
warnings[text] = true;
console.warn('>> WARNING:', text, '\n>> Check:', this.dialectTypes);
console.warn('>> WARNING:', text, '\n>> Check:', link);
}
};
ABSTRACT.prototype.stringify = function (value, options) {
if (this.$stringify) {
return this.$stringify(value, options);
}
return value;
};
ABSTRACT.inherits = function (Constructor) {
var baseType = this;
if (!Constructor) {
Constructor = function () {
if (!(this instanceof Constructor)) {
var args = [null].concat(arguments);
var FactoryFunction = Constructor.bind.apply(Constructor, args);
return new FactoryFunction();
}
baseType.apply(this, arguments);
};
}
util.inherits(Constructor, baseType); // Instance (prototype) methods
_.extend(Constructor, this); // Static methods
return Constructor;
};
/**
* A variable length string. Default length 255
*
......@@ -64,7 +93,7 @@ ABSTRACT.prototype.warn = function(text) {
*
* @property STRING
*/
var STRING = function(length, binary) {
var STRING = ABSTRACT.inherits(function(length, binary) {
var options = typeof length === 'object' && length || {
length: length,
binary: binary
......@@ -75,8 +104,7 @@ var STRING = function(length, binary) {
this.options = options;
this._binary = options.binary;
this._length = options.length || 255;
};
util.inherits(STRING, ABSTRACT);
});
STRING.prototype.key = STRING.key = 'STRING';
STRING.prototype.toSql = function() {
......@@ -107,7 +135,7 @@ Object.defineProperty(STRING.prototype, 'BINARY', {
*
* @property CHAR
*/
var CHAR = function(length, binary) {
var CHAR = STRING.inherits(function(length, binary) {
var options = typeof length === 'object' && length || {
length: length,
binary: binary
......@@ -115,8 +143,7 @@ var CHAR = function(length, binary) {
if (!(this instanceof CHAR)) return new CHAR(options);
STRING.apply(this, arguments);
};
util.inherits(CHAR, STRING);
});
CHAR.prototype.key = CHAR.key = 'CHAR';
CHAR.prototype.toSql = function() {
......@@ -127,14 +154,14 @@ CHAR.prototype.toSql = function() {
* An (un)limited length text column. Available lengths: `tiny`, `medium`, `long`
* @property TEXT
*/
var TEXT = function(length) {
var TEXT = ABSTRACT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof TEXT)) return new TEXT(options);
this.options = options;
this._length = options.length || '';
};
util.inherits(TEXT, ABSTRACT);
});
TEXT.prototype.key = TEXT.key = 'TEXT';
TEXT.prototype.toSql = function() {
......@@ -157,7 +184,7 @@ TEXT.prototype.validate = function(value) {
return true;
};
var NUMBER = function(options) {
var NUMBER = ABSTRACT.inherits(function(options) {
this.options = options;
this._length = options.length;
this._zerofill = options.zerofill;
......@@ -165,8 +192,7 @@ var NUMBER = function(options) {
this._precision = options.precision;
this._scale = options.scale;
this._unsigned = options.unsigned;
};
util.inherits(NUMBER, ABSTRACT);
});
NUMBER.prototype.key = NUMBER.key = 'NUMBER';
NUMBER.prototype.toSql = function() {
......@@ -216,14 +242,13 @@ Object.defineProperty(NUMBER.prototype, 'ZEROFILL', {
*
* @property INTEGER
*/
var INTEGER = function(length) {
var INTEGER = NUMBER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof INTEGER)) return new INTEGER(options);
NUMBER.call(this, options);
};
util.inherits(INTEGER, NUMBER);
});
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.validate = function(value) {
......@@ -242,14 +267,13 @@ INTEGER.prototype.validate = function(value) {
* @property BIGINT
*/
var BIGINT = function(length) {
var BIGINT = NUMBER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof BIGINT)) return new BIGINT(options);
NUMBER.call(this, options);
};
util.inherits(BIGINT, NUMBER);
});
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.validate = function(value) {
......@@ -267,15 +291,14 @@ BIGINT.prototype.validate = function(value) {
*
* @property FLOAT
*/
var FLOAT = function(length, decimals) {
var FLOAT = NUMBER.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof FLOAT)) return new FLOAT(options);
NUMBER.call(this, options);
};
util.inherits(FLOAT, NUMBER);
});
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.validate = function(value) {
......@@ -293,15 +316,14 @@ FLOAT.prototype.validate = function(value) {
*
* @property REAL
*/
var REAL = function(length, decimals) {
var REAL = NUMBER.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof REAL)) return new REAL(options);
NUMBER.call(this, options);
};
util.inherits(REAL, NUMBER);
});
REAL.prototype.key = REAL.key = 'REAL';
......@@ -312,15 +334,14 @@ REAL.prototype.key = REAL.key = 'REAL';
*
* @property DOUBLE
*/
var DOUBLE = function(length, decimals) {
var DOUBLE = NUMBER.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof DOUBLE)) return new DOUBLE(options);
NUMBER.call(this, options);
};
util.inherits(DOUBLE, NUMBER);
});
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
......@@ -331,15 +352,14 @@ DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
*
* @property DECIMAL
*/
var DECIMAL = function(precision, scale) {
var DECIMAL = NUMBER.inherits(function(precision, scale) {
var options = typeof precision === 'object' && precision || {
precision: precision,
scale: scale
};
if (!(this instanceof DECIMAL)) return new DECIMAL(options);
NUMBER.call(this, options);
};
util.inherits(DECIMAL, NUMBER);
});
DECIMAL.prototype.key = DECIMAL.key = 'DECIMAL';
DECIMAL.prototype.toSql = function() {
......@@ -361,11 +381,7 @@ DECIMAL.prototype.validate = function(value) {
* A boolean / tinyint column, depending on dialect
* @property BOOLEAN
*/
var BOOLEAN = function() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
ABSTRACT.apply(this, arguments);
};
util.inherits(BOOLEAN, ABSTRACT);
var BOOLEAN = ABSTRACT.inherits();
BOOLEAN.prototype.key = BOOLEAN.key = 'BOOLEAN';
BOOLEAN.prototype.toSql = function() {
......@@ -384,11 +400,7 @@ BOOLEAN.prototype.validate = function(value) {
* @property TIME
*/
var TIME = function() {
if (!(this instanceof TIME)) return new TIME();
ABSTRACT.apply(this, arguments);
};
util.inherits(TIME, ABSTRACT);
var TIME = ABSTRACT.inherits();
TIME.prototype.key = TIME.key = 'TIME';
TIME.prototype.toSql = function() {
......@@ -399,11 +411,7 @@ TIME.prototype.toSql = function() {
* A datetime column
* @property DATE
*/
var DATE = function() {
if (!(this instanceof DATE)) return new DATE();
ABSTRACT.apply(this, arguments);
};
util.inherits(DATE, ABSTRACT);
var DATE = ABSTRACT.inherits();
DATE.prototype.key = DATE.key = 'DATE';
DATE.prototype.toSql = function() {
......@@ -416,6 +424,28 @@ DATE.prototype.validate = function(value) {
return true;
};
DATE.prototype.$applyTimezone = function (date, options) {
date = moment(date);
if (options.timezone) {
if (moment.tz.zone(options.timezone)) {
date = date.tz(options.timezone);
} else {
date = date.utcOffset(options.timezone);
}
}
return date;
};
DATE.prototype.$stringify = function (date, options) {
date = this.$applyTimezone(date, options);
// Z here means current timezone, _not_ UTC
return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
};
/**
* A date only column
* @property DATEONLY
......@@ -437,11 +467,7 @@ DATEONLY.prototype.toSql = function() {
* @property HSTORE
*/
var HSTORE = function() {
if (!(this instanceof HSTORE)) return new HSTORE();
ABSTRACT.apply(this, arguments);
};
util.inherits(HSTORE, ABSTRACT);
var HSTORE = ABSTRACT.inherits();
HSTORE.prototype.key = HSTORE.key = 'HSTORE';
HSTORE.prototype.validate = function(value) {
......@@ -452,6 +478,11 @@ HSTORE.prototype.validate = function(value) {
return true;
};
HSTORE.prototype.escape = false;
HSTORE.prototype.$stringify = function (value) {
return '\'' + hstore.stringify(value) + '\'';
};
/**
* A JSON string column. Only available in postgres.
* @property JSON
......@@ -467,6 +498,10 @@ JSONTYPE.prototype.validate = function(value) {
return true;
};
JSONTYPE.prototype.$stringify = function (value, options) {
return JSON.stringify(value);
};
/**
* A pre-processed JSON data column. Only available in postgres.
* @property JSONB
......@@ -483,11 +518,7 @@ JSONB.prototype.key = JSONB.key = 'JSONB';
* A default value of the current timestamp
* @property NOW
*/
var NOW = function() {
if (!(this instanceof NOW)) return new NOW();
ABSTRACT.apply(this, arguments);
};
util.inherits(NOW, ABSTRACT);
var NOW = ABSTRACT.inherits();
NOW.prototype.key = NOW.key = 'NOW';
......@@ -497,14 +528,14 @@ NOW.prototype.key = NOW.key = 'NOW';
* @property BLOB
*/
var BLOB = function(length) {
var BLOB = ABSTRACT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof BLOB)) return new BLOB(options);
this.options = options;
this._length = options.length || '';
};
util.inherits(BLOB, ABSTRACT);
});
BLOB.prototype.key = BLOB.key = 'BLOB';
BLOB.prototype.toSql = function() {
......@@ -527,6 +558,20 @@ BLOB.prototype.validate = function(value) {
return true;
};
BLOB.prototype.escape = false;
BLOB.prototype.$stringify = function (value) {
if (!Buffer.isBuffer(value)) {
value = new Buffer(value);
}
var hex = value.toString('hex');
return this.$hexify(hex);
};
BLOB.prototype.$hexify = function (hex) {
return "X'" + hex + "'";
};
/**
* Range types are data types representing a range of values of some element type (called the range's subtype).
* Only available in postgres.
......@@ -534,17 +579,21 @@ BLOB.prototype.validate = function(value) {
* @property RANGE
*/
var RANGE = function (subtype) {
var RANGE = ABSTRACT.inherits(function (subtype) {
var options = _.isPlainObject(subtype) ? subtype : { subtype: subtype };
if (!options.subtype) options.subtype = INTEGER;
if (!options.subtype) options.subtype = new INTEGER();
if (_.isFunction(options.subtype)) {
options.subtype = new options.subtype();
}
if (!(this instanceof RANGE)) return new RANGE(options);
ABSTRACT.apply(this, arguments);
this._subtype = options.subtype.key;
};
util.inherits(RANGE, ABSTRACT);
this.options = options;
});
var pgRangeSubtypes = {
integer: 'int4range',
......@@ -574,15 +623,12 @@ RANGE.prototype.validate = function(value) {
return true;
};
/**
* A column storing a unique univeral identifier. Use with `UUIDV1` or `UUIDV4` for default values.
* @property UUID
*/
var UUID = function() {
if (!(this instanceof UUID)) return new UUID();
ABSTRACT.apply(this, arguments);
};
util.inherits(UUID, ABSTRACT);
var UUID = ABSTRACT.inherits();
UUID.prototype.key = UUID.key = 'UUID';
UUID.prototype.validate = function(value) {
......@@ -691,7 +737,7 @@ VIRTUAL.prototype.key = VIRTUAL.key = 'VIRTUAL';
*
* @property ENUM
*/
var ENUM = function(value) {
var ENUM = ABSTRACT.inherits(function(value) {
var options = typeof value === 'object' && !Array.isArray(value) && value || {
values: Array.prototype.slice.call(arguments).reduce(function(result, element) {
return result.concat(Array.isArray(element) ? element : [element]);
......@@ -699,8 +745,8 @@ var ENUM = function(value) {
};
if (!(this instanceof ENUM)) return new ENUM(options);
this.values = options.values;
};
util.inherits(ENUM, ABSTRACT);
this.options = options;
});
ENUM.prototype.key = ENUM.key = 'ENUM';
ENUM.prototype.validate = function(value) {
......@@ -747,7 +793,7 @@ var helpers = {
SCALE: [DECIMAL]
};
var GEOMETRY = function(type, srid) {
var GEOMETRY = ABSTRACT.inherits(function(type, srid) {
var options = _.isPlainObject(type) ? type : {
type: type,
srid: srid
......@@ -758,13 +804,13 @@ var GEOMETRY = function(type, srid) {
this.options = options;
this.type = options.type;
this.srid = options.srid;
};
util.inherits(GEOMETRY, ABSTRACT);
});
GEOMETRY.prototype.key = GEOMETRY.key = 'GEOMETRY';
GEOMETRY.prototype.parse = function(value) {
return Wkt.parse(value);
GEOMETRY.prototype.escape = false;
GEOMETRY.prototype.$stringify = function (value) {
return 'GeomFromText(\'' + Wkt.stringify(value) + '\')';
};
Object.keys(helpers).forEach(function (helper) {
......@@ -783,7 +829,7 @@ Object.keys(helpers).forEach(function (helper) {
});
});
module.exports = {
var dataTypes = {
ABSTRACT: ABSTRACT,
STRING: STRING,
CHAR: CHAR,
......@@ -816,3 +862,14 @@ module.exports = {
'DOUBLE PRECISION': DOUBLE,
GEOMETRY: GEOMETRY
};
_.each(dataTypes, function (dataType) {
dataType.types = {};
});
dataTypes.postgres = require('./dialects/postgres/data-types')(dataTypes);
dataTypes.mysql = require('./dialects/mysql/data-types')(dataTypes);
dataTypes.sqlite = require('./dialects/sqlite/data-types')(dataTypes);
dataTypes.mssql = require('./dialects/mssql/data-types')(dataTypes);
module.exports = dataTypes;
......@@ -19,6 +19,7 @@ ConnectionManager = function(dialect, sequelize) {
this.config = config;
this.dialect = dialect;
this.versionPromise = null;
this.dialectName = this.sequelize.options.dialect;
if (config.pool) {
config.pool = _.clone(config.pool); // Make sure we don't modify the existing config object (user might re-use it)
......@@ -44,6 +45,23 @@ ConnectionManager = function(dialect, sequelize) {
process.on('exit', this.onProcessExit);
};
ConnectionManager.prototype.refreshTypeParser = function(dataTypes) {
_.each(dataTypes, function (dataType, key) {
if (dataType.hasOwnProperty('parse')) {
var dialectName = this.dialectName;
if (dialectName === 'mariadb') {
dialectName = 'mysql';
}
if (dataType.types[dialectName]) {
this.$refreshTypeParser(dataType);
} else {
throw new Error('Parse function not supported for type ' + dataType.key + ' in dialect ' + this.dialectName);
}
}
}, this);
};
ConnectionManager.prototype.onProcessExit = function() {
var self = this;
......
......@@ -943,16 +943,34 @@ var QueryGenerator = {
*/
escape: function(value, field, options) {
options = options || {};
if (value && value._isSequelizeMethod) {
if (value) {
if (value._isSequelizeMethod) {
return this.handleSequelizeMethod(value);
} else {
if (field && field.type) {
if (['INSERT', 'UPDATE'].indexOf(options.context) !== -1 && this.typeValidation && field && field.type && value) {
if (field.type.validate) {
field.type.validate(value);
}
}
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
if (field.type.stringify) {
// Users shouldn't have to worry about these args - just give them a function that takes a single arg
var simpleEscape = _.partialRight(SqlString.escape, this.options.timezone, this.dialect);
value = field.type.stringify(value, { escape: simpleEscape, field: field, timezone: this.options.timezone });
if (field.type.escape === false) {
// The data-type already did the required escaping
return value;
}
}
}
}
}
return SqlString.escape(value, this.options.timezone, this.dialect);
},
/**
......@@ -1048,10 +1066,6 @@ var QueryGenerator = {
return self.handleSequelizeMethod(attr);
}
if (mainModel && mainModel._isGeometryAttribute(attr)) {
attr = self.geometrySelect(attr);
}
if (Array.isArray(attr) && attr.length === 2) {
attr = attr.slice();
......@@ -1107,7 +1121,6 @@ var QueryGenerator = {
// includeIgnoreAttributes is used by aggregate functions
if (options.includeIgnoreAttributes !== false) {
attributes = include.attributes.map(function(attr) {
var attrAs = attr,
verbatim = false;
......@@ -2291,10 +2304,6 @@ var QueryGenerator = {
booleanValue: function(value) {
return value;
},
geometrySelect: function(column) {
return this.quoteIdentifiers(column);
}
};
......
......@@ -4,7 +4,8 @@ var AbstractConnectionManager = require('../abstract/connection-manager')
, ConnectionManager
, Utils = require('../../utils')
, Promise = require('../../promise')
, sequelizeErrors = require('../../errors');
, sequelizeErrors = require('../../errors')
, parserStore = require('../parserStore')('mssql');
ConnectionManager = function(dialect, sequelize) {
AbstractConnectionManager.call(this, dialect, sequelize);
......@@ -20,6 +21,16 @@ ConnectionManager = function(dialect, sequelize) {
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
ConnectionManager.prototype.$refreshTypeParser = function (dataType) {
parserStore.refresh(dataType);
};
ConnectionManager.prototype.$clearTypeParser = function () {
parserStore.clear();
};
ConnectionManager.prototype.connect = function(config) {
var self = this;
return new Promise(function (resolve, reject) {
......
'use strict';
var BaseTypes = require('../../data-types')
, util = require('util')
, _ = require('lodash');
var _ = require('lodash');
module.exports = function (BaseTypes) {
var 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];
BaseTypes.CHAR.types.mssql = [175];
BaseTypes.TEXT.types.mssql = false;
BaseTypes.INTEGER.types.mssql = [38];
BaseTypes.BIGINT.types.mssql = false;
BaseTypes.FLOAT.types.mssql = [109];
BaseTypes.TIME.types.mssql = [41];
BaseTypes.DATEONLY.types.mssql = [40];
BaseTypes.BOOLEAN.types.mssql = [104];
BaseTypes.BLOB.types.mssql = [165];
BaseTypes.DECIMAL.types.mssql = [106];
BaseTypes.UUID.types.mssql = false;
BaseTypes.ENUM.types.mssql = false;
BaseTypes.REAL.types.mssql = [109];
BaseTypes.DOUBLE.types.mssql = [109];
// BaseTypes.GEOMETRY.types.mssql = [240]; // not yet supported
BaseTypes.GEOMETRY.types.mssql = false;
var BLOB = BaseTypes.BLOB.inherits();
BLOB.prototype.toSql = function() {
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.');
return 'VARBINARY(256)';
}
warn('MSSQL does not support BLOB with the `length` option. `VARBINARY(MAX)` will be used instead.');
}
return 'VARBINARY(MAX)';
};
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx';
BLOB.prototype.$hexify = function (hex) {
return '0x' + hex;
};
var STRING = function() {
if (!(this instanceof STRING)) return new STRING();
BaseTypes.STRING.apply(this, arguments);
};
util.inherits(STRING, BaseTypes.STRING);
var STRING = BaseTypes.STRING.inherits();
STRING.prototype.toSql = function() {
STRING.prototype.toSql = function() {
if (!this._binary) {
return 'NVARCHAR(' + this._length + ')';
} else{
return 'BINARY(' + this._length + ')';
}
};
};
STRING.prototype.escape = false;
STRING.prototype.$stringify = function (value, options) {
if (this._binary) {
return BLOB.prototype.$stringify(value);
} else {
return options.escape(value);
}
};
var TEXT = BaseTypes.TEXT.inherits();
BaseTypes.TEXT.prototype.toSql = function() {
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) {
if (this._length.toLowerCase() === 'tiny') { // tiny = 2^8
this.warn('MSSQL does not support TEXT with the `length` = `tiny` option. `NVARCHAR(256)` will be used instead.');
warn('MSSQL does not support TEXT with the `length` = `tiny` option. `NVARCHAR(256)` will be used instead.');
return 'NVARCHAR(256)';
}
this.warn('MSSQL does not support TEXT with the `length` option. `NVARCHAR(MAX)` will be used instead.');
warn('MSSQL does not support TEXT with the `length` option. `NVARCHAR(MAX)` will be used instead.');
}
return 'NVARCHAR(MAX)';
};
};
var BOOLEAN = function() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
};
util.inherits(BOOLEAN, BaseTypes.BOOLEAN);
var BOOLEAN = BaseTypes.BOOLEAN.inherits();
BOOLEAN.prototype.toSql = function() {
BOOLEAN.prototype.toSql = function() {
return 'BIT';
};
BaseTypes.BLOB.prototype.toSql = function() {
if (this._length) {
if (this._length.toLowerCase() === 'tiny') { // tiny = 2^8
this.warn('MSSQL does not support BLOB with the `length` = `tiny` option. `VARBINARY(256)` will be used instead.');
return 'VARBINARY(256)';
}
this.warn('MSSQL does not support BLOB with the `length` option. `VARBINARY(MAX)` will be used instead.');
}
return 'VARBINARY(MAX)';
};
};
var UUID = function() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
};
util.inherits(UUID, BaseTypes.UUID);
var UUID = BaseTypes.UUID.inherits();
UUID.prototype.toSql = function() {
UUID.prototype.toSql = function() {
return 'CHAR(36)';
};
};
var NOW = function() {
if (!(this instanceof NOW)) return new NOW();
BaseTypes.NOW.apply(this, arguments);
};
util.inherits(NOW, BaseTypes.NOW);
var NOW = BaseTypes.NOW.inherits();
NOW.prototype.toSql = function() {
NOW.prototype.toSql = function() {
return 'GETDATE()';
};
};
var DATE = function() {
if (!(this instanceof DATE)) return new DATE();
BaseTypes.DATE.apply(this, arguments);
};
util.inherits(DATE, BaseTypes.DATE);
var DATE = BaseTypes.DATE.inherits();
DATE.prototype.toSql = function() {
DATE.prototype.toSql = function() {
return 'DATETIME2';
};
};
var INTEGER = function() {
var INTEGER = BaseTypes.INTEGER.inherits(function() {
if (!(this instanceof INTEGER)) return new INTEGER();
BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any options for integer
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('MSSQL does not support INTEGER with options. Plain `INTEGER` will be used instead.');
warn('MSSQL does not support INTEGER with options. Plain `INTEGER` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(INTEGER, BaseTypes.INTEGER);
});
var BIGINT = function() {
var BIGINT = BaseTypes.BIGINT.inherits(function() {
if (!(this instanceof BIGINT)) return new BIGINT();
BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any options for bigint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('MSSQL does not support BIGINT with options. Plain `BIGINT` will be used instead.');
warn('MSSQL does not support BIGINT with options. Plain `BIGINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(BIGINT, BaseTypes.BIGINT);
});
var REAL = function() {
var REAL = BaseTypes.REAL.inherits(function() {
if (!(this instanceof REAL)) return new REAL();
BaseTypes.REAL.apply(this, arguments);
// MSSQL does not support any options for real
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('MSSQL does not support REAL with options. Plain `REAL` will be used instead.');
warn('MSSQL does not support REAL with options. Plain `REAL` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(REAL, BaseTypes.REAL);
});
var FLOAT = function() {
var FLOAT = BaseTypes.FLOAT.inherits(function() {
if (!(this instanceof FLOAT)) return new FLOAT();
BaseTypes.FLOAT.apply(this, arguments);
......@@ -138,27 +150,29 @@ var FLOAT = function() {
// Values between 25-53 result in 15 digits precision (8 bytes storage size)
// If decimals are provided remove these and print a warning
if (this._decimals) {
this.warn('MSSQL does not support Float with decimals. Plain `FLOAT` will be used instead.');
warn('MSSQL does not support Float with decimals. Plain `FLOAT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
}
if (this._unsigned) {
this.warn('MSSQL does not support Float unsigned. `UNSIGNED` was removed.');
warn('MSSQL does not support Float unsigned. `UNSIGNED` was removed.');
this._unsigned = undefined;
}
if (this._zerofill) {
this.warn('MSSQL does not support Float zerofill. `ZEROFILL` was removed.');
warn('MSSQL does not support Float zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined;
}
};
util.inherits(FLOAT, BaseTypes.FLOAT);
});
BaseTypes.ENUM.prototype.toSql = function() {
var ENUM = BaseTypes.ENUM.inherits();
ENUM.prototype.toSql = function() {
return 'VARCHAR(255)';
};
};
module.exports = {
var exports = {
BLOB: BLOB,
BOOLEAN: BOOLEAN,
ENUM: ENUM,
STRING: STRING,
UUID: UUID,
DATE: DATE,
......@@ -166,14 +180,18 @@ module.exports = {
INTEGER: INTEGER,
BIGINT: BIGINT,
REAL: REAL,
FLOAT: FLOAT
};
FLOAT: FLOAT,
TEXT: TEXT
};
_.forIn(module.exports, function (DataType, key) {
_.forIn(exports, function (DataType, key) {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
return new DataType(oldType.options);
};
}
});
});
return exports;
};
......@@ -5,7 +5,7 @@ var _ = require('lodash')
, ConnectionManager = require('./connection-manager')
, Query = require('./query')
, QueryGenerator = require('./query-generator')
, DataTypes = require('./data-types');
, DataTypes = require('../../data-types').mssql;
var MssqlDialect = function(sequelize) {
this.sequelize = sequelize;
......
......@@ -2,7 +2,8 @@
var Utils = require('../../utils')
, AbstractQuery = require('../abstract/query')
, sequelizeErrors = require('../../errors.js');
, sequelizeErrors = require('../../errors.js')
, parserStore = require('../parserStore')('mssql');
var Query = function(connection, sequelize, options) {
this.connection = connection;
......@@ -73,7 +74,14 @@ Query.prototype.run = function(sql, parameters) {
request.on('row', function(columns) {
var row = {};
columns.forEach(function(column) {
row[column.metadata.colName] = column.value;
var typeid = column.metadata.type.id
, value = column.value
, parse = parserStore.get(typeid);
if (value !== null & !!parse) {
value = parse(value);
}
row[column.metadata.colName] = value;
});
results.push(row);
......
......@@ -4,7 +4,9 @@ var AbstractConnectionManager = require('../abstract/connection-manager')
, ConnectionManager
, Utils = require('../../utils')
, Promise = require('../../promise')
, sequelizeErrors = require('../../errors');
, sequelizeErrors = require('../../errors')
, dataTypes = require('../../data-types').mysql
, parserMap = {};
ConnectionManager = function(dialect, sequelize) {
AbstractConnectionManager.call(this, dialect, sequelize);
......@@ -16,10 +18,31 @@ ConnectionManager = function(dialect, sequelize) {
} catch (err) {
throw new Error('Please install mysql package manually');
}
this.refreshTypeParser(dataTypes);
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
ConnectionManager.prototype.$refreshTypeParser = function (dataType) {
dataType.types.mysql.forEach(function (type) {
parserMap[type] = dataType.parse;
});
};
ConnectionManager.prototype.$clearTypeParser = function () {
parserMap = {};
};
ConnectionManager.$typecast = function (field, next) {
if (parserMap[field.type]) {
return parserMap[field.type](field);
}
return next();
};
ConnectionManager.prototype.connect = function(config) {
var self = this;
return new Promise(function (resolve, reject) {
......@@ -29,7 +52,8 @@ ConnectionManager.prototype.connect = function(config) {
user: config.username,
password: config.password,
database: config.database,
timezone: self.sequelize.options.timezone
timezone: self.sequelize.options.timezone,
typeCast: ConnectionManager.$typecast
};
if (config.dialectOptions) {
......
'use strict';
var BaseTypes = require('../../data-types')
, util = require('util')
var wkx = require('wkx')
, _ = require('lodash');
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
module.exports = function (BaseTypes) {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
var UUID = function() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
};
util.inherits(UUID, BaseTypes.UUID);
BaseTypes.DATE.types.mysql = ['DATETIME'];
BaseTypes.STRING.types.mysql = ['VAR_STRING'];
BaseTypes.CHAR.types.mysql = ['STRING'];
BaseTypes.TEXT.types.mysql = ['BLOB'];
BaseTypes.INTEGER.types.mysql = ['LONG'];
BaseTypes.BIGINT.types.mysql = ['LONGLONG'];
BaseTypes.FLOAT.types.mysql = ['FLOAT'];
BaseTypes.TIME.types.mysql = ['TIME'];
BaseTypes.DATEONLY.types.mysql = ['DATE'];
BaseTypes.BOOLEAN.types.mysql = ['TINY'];
BaseTypes.BLOB.types.mysql = ['TINYBLOB', 'BLOB', 'LONGBLOB'];
BaseTypes.DECIMAL.types.mysql = ['NEWDECIMAL'];
BaseTypes.UUID.types.mysql = false;
BaseTypes.ENUM.types.mysql = false;
BaseTypes.REAL.types.mysql = ['DOUBLE'];
BaseTypes.DOUBLE.types.mysql = ['DOUBLE'];
var DATE = BaseTypes.DATE.inherits();
DATE.prototype.$stringify = function (date, options) {
date = BaseTypes.DATE.prototype.$applyTimezone(date, options);
return date.format('YYYY-MM-DD HH:mm:ss');
};
var UUID = BaseTypes.UUID.inherits();
UUID.prototype.toSql = function() {
UUID.prototype.toSql = function() {
return 'CHAR(36) BINARY';
};
};
var SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
var GEOMETRY = function() {
var SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
var GEOMETRY = BaseTypes.GEOMETRY.inherits(function() {
if (!(this instanceof GEOMETRY)) return new GEOMETRY();
BaseTypes.GEOMETRY.apply(this, arguments);
......@@ -28,23 +49,44 @@ var GEOMETRY = function() {
} else {
throw new Error('Supported geometry types are: ' + SUPPORTED_GEOMETRY_TYPES.join(', '));
}
};
util.inherits(GEOMETRY, BaseTypes.GEOMETRY);
});
GEOMETRY.parse = GEOMETRY.prototype.parse = function(value) {
// For some reason, discard the first 4 bytes
value = value.buffer().slice(4);
return wkx.Geometry.parse(value).toGeoJSON();
};
GEOMETRY.prototype.toSql = function() {
GEOMETRY.prototype.toSql = function() {
return this.sqlType;
};
};
var ENUM = BaseTypes.ENUM.inherits();
ENUM.prototype.toSql = function (options) {
return 'ENUM(' + _.map(this.values, function(value) {
return options.escape(value);
}).join(', ') + ')';
};
module.exports = {
BaseTypes.GEOMETRY.types.mysql = ['GEOMETRY'];
var exports = {
ENUM: ENUM,
DATE: DATE,
UUID: UUID,
GEOMETRY: GEOMETRY
};
};
_.forIn(module.exports, function (DataType, key) {
_.forIn(exports, function (DataType, key) {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
return new DataType(oldType.options);
};
}
});
});
return exports;
};
......@@ -5,7 +5,7 @@ var _ = require('lodash')
, ConnectionManager = require('./connection-manager')
, Query = require('./query')
, QueryGenerator = require('./query-generator')
, DataTypes = require('./data-types');
, DataTypes = require('../../data-types').mysql;
var MysqlDialect = function(sequelize) {
this.sequelize = sequelize;
......
'use strict';
var Utils = require('../../utils')
, DataTypes = require('../../data-types')
, SqlString = require('../../sql-string')
, Wkt = require('wellknown');
var Utils = require('../../utils');
var QueryGenerator = {
dialect: 'mysql',
......@@ -220,16 +217,7 @@ var QueryGenerator = {
};
}
var template;
if (attribute.type instanceof DataTypes.ENUM) {
if (attribute.type.values && !attribute.values) attribute.values = attribute.type.values;
template = 'ENUM(' + Utils._.map(attribute.values, function(value) {
return this.escape(value);
}.bind(this)).join(', ') + ')';
} else {
template = attribute.type.toString();
}
var template = attribute.type.toString({ escape: this.escape.bind(this) });
if (attribute.allowNull === false) {
template += ' NOT NULL';
......@@ -312,23 +300,6 @@ var QueryGenerator = {
return Utils.addTicks(identifier, '`');
},
escape: function(value, field, options) {
options = options || {};
if (value && value._isSequelizeMethod) {
return this.handleSequelizeMethod(value);
} else if (value && field && field.type instanceof DataTypes.GEOMETRY) {
return 'GeomFromText(\'' + Wkt.stringify(value) + '\')';
} else {
if (['INSERT', 'UPDATE'].indexOf(options.context) !== -1 && this.typeValidation && field && field.type && value) {
if (field.type.validate) {
field.type.validate(value);
}
}
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
}
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
......@@ -350,10 +321,6 @@ var QueryGenerator = {
*/
dropForeignKeyQuery: function(tableName, foreignKey) {
return 'ALTER TABLE ' + this.quoteTable(tableName) + ' DROP FOREIGN KEY ' + this.quoteIdentifier(foreignKey) + ';';
},
geometrySelect: function(column) {
return 'AsText(' + this.quoteIdentifiers(column) + ') AS ' + column;
}
};
......
'use strict';
var stores = {};
module.exports = function (dialect) {
stores[dialect] = stores[dialect] || {};
return {
clear: function () {
stores[dialect] = {};
},
refresh: function (dataType) {
dataType.types[dialect].forEach(function (type) {
stores[dialect][type] = dataType.parse;
});
},
get: function (type) {
return stores[dialect][type];
}
};
};
......@@ -5,7 +5,8 @@ var AbstractConnectionManager = require('../abstract/connection-manager')
, Utils = require('../../utils')
, Promise = require('../../promise')
, sequelizeErrors = require('../../errors')
, semver = require('semver');
, semver = require('semver')
, dataTypes = require('../../data-types');
ConnectionManager = function(dialect, sequelize) {
AbstractConnectionManager.call(this, dialect, sequelize);
......@@ -17,10 +18,35 @@ ConnectionManager = function(dialect, sequelize) {
} catch (err) {
throw new Error('Please install \'' + (sequelize.config.dialectModulePath || 'pg') + '\' module manually');
}
this.refreshTypeParser(dataTypes.postgres);
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
ConnectionManager.prototype.$refreshTypeParser = function (dataType) {
var self = this;
if (dataType.types.postgres.oids) {
dataType.types.postgres.oids.forEach(function (oid) {
self.lib.types.setTypeParser(oid, function (value) {
return dataType.parse(value, oid, self.lib.types.getTypeParser);
});
});
}
if (dataType.types.postgres.array_oids) {
dataType.types.postgres.array_oids.forEach(function (oid) {
self.lib.types.setTypeParser(oid, function (value) {
return self.lib.types.arrayParser.create(value, function (value) {
return dataType.parse(value, oid, self.lib.types.getTypeParser);
}).parse();
});
});
}
};
ConnectionManager.prototype.connect = function(config) {
var self = this
, connectionConfig = {};
......@@ -103,12 +129,29 @@ ConnectionManager.prototype.connect = function(config) {
}
if (!self.sequelize.config.keepDefaultTimezone) {
query += 'SET client_min_messages TO warning; SET TIME ZONE INTERVAL \'' + self.sequelize.options.timezone + '\' HOUR TO MINUTE';
query += 'SET client_min_messages TO warning; SET TIME ZONE INTERVAL \'' + self.sequelize.options.timezone + '\' HOUR TO MINUTE;';
}
// oids for hstore and geometry are dynamic - so select them at connection time
if (dataTypes.HSTORE.types.postgres.oids.length === 0) {
query += 'SELECT typname, oid, typarray FROM pg_type WHERE typtype = \'b\' AND typname IN (\'hstore\', \'geometry\')';
}
return new Promise(function (resolve, reject) {
connection.query(query).on('error', function (err) {
reject(err);
}).on('row', function (row) {
var type;
if (row.typname === 'geometry') {
type = dataTypes.postgres.GEOMETRY;
} else if (row.typname === 'hstore') {
type = dataTypes.postgres.HSTORE;
}
type.types.postgres.oids.push(row.oid);
type.types.postgres.array_oids.push(row.typarray);
self.$refreshTypeParser(type);
}).on('end', function () {
resolve();
});
......
'use strict';
var BaseTypes = require('../../data-types')
, util = require('util')
, _ = require('lodash')
/*jshint -W110 */
var _ = require('lodash')
, wkx = require('wkx')
, wkt = require('wellknown');
, hstore = require('./hstore');
BaseTypes.ABSTRACT.prototype.dialectTypes = 'http://www.postgresql.org/docs/9.4/static/datatype.html';
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'http://www.postgresql.org/docs/9.4/static/datatype.html');
var STRING = function() {
if (!(this instanceof STRING)) return new STRING();
BaseTypes.STRING.apply(this, arguments);
};
util.inherits(STRING, BaseTypes.STRING);
BaseTypes.UUID.types.postgres = {
oids: [2950],
array_oids: [2951]
};
BaseTypes.JSON.types.postgres = {
oids: [114],
array_oids: [199]
};
BaseTypes.JSONB.types.postgres = {
oids: [3802],
array_oids: [3807]
};
BaseTypes.DATEONLY.types.postgres = {
oids: [1082],
array_oids: [1182]
};
BaseTypes.TIME.types.postgres = {
oids: [1083],
array_oids: [1183]
};
var DECIMAL = BaseTypes.DECIMAL.inherits();
DECIMAL.parse = function (value) {
return parseFloat(value);
};
// numeric
BaseTypes.DECIMAL.types.postgres = {
oids: [1700],
array_oids: [1231]
};
STRING.prototype.toSql = function() {
var STRING = BaseTypes.STRING.inherits();
STRING.prototype.toSql = function () {
if (this._binary) {
return 'BYTEA';
}
return BaseTypes.STRING.prototype.toSql.call(this);
};
};
BaseTypes.STRING.types.postgres = {
oids: [1043],
array_oids: [1015]
};
BaseTypes.TEXT.prototype.toSql = function() {
var TEXT = BaseTypes.TEXT.inherits();
TEXT.prototype.toSql = function() {
if (this._length) {
this.warn('PostgreSQL does not support TEXT with options. Plain `TEXT` will be used instead.');
warn('PostgreSQL does not support TEXT with options. Plain `TEXT` will be used instead.');
this._length = undefined;
}
return 'TEXT';
};
};
var CHAR = function() {
if (!(this instanceof CHAR)) return new CHAR();
BaseTypes.CHAR.apply(this, arguments);
};
util.inherits(CHAR, BaseTypes.CHAR);
BaseTypes.TEXT.types.postgres = {
oids: [25],
array_oids: [1009]
};
var CHAR = BaseTypes.CHAR.inherits();
CHAR.prototype.toSql = function() {
CHAR.prototype.toSql = function() {
if (this._binary) {
return 'BYTEA';
}
return BaseTypes.CHAR.prototype.toSql.call(this);
};
};
var BOOLEAN = function() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
};
util.inherits(BOOLEAN, BaseTypes.BOOLEAN);
BaseTypes.CHAR.types.postgres = {
oids: [18, 1042],
array_oids: [1002, 1014]
};
BOOLEAN.prototype.toSql = function() {
var BOOLEAN = BaseTypes.BOOLEAN.inherits();
BOOLEAN.prototype.toSql = function() {
return 'BOOLEAN';
};
};
var DATE = function() {
if (!(this instanceof DATE)) return new DATE();
BaseTypes.DATE.apply(this, arguments);
};
util.inherits(DATE, BaseTypes.DATE);
BaseTypes.BOOLEAN.types.postgres = {
oids: [16],
array_oids: [1000]
};
var DATE = BaseTypes.DATE.inherits();
DATE.prototype.toSql = function() {
DATE.prototype.toSql = function() {
return 'TIMESTAMP WITH TIME ZONE';
};
};
BaseTypes.DATE.types.postgres = {
oids: [1184],
array_oids: [1185]
};
var INTEGER = function() {
var INTEGER = BaseTypes.INTEGER.inherits(function() {
if (!(this instanceof INTEGER)) return new INTEGER();
BaseTypes.INTEGER.apply(this, arguments);
// POSTGRES does not support any parameters for integer
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('PostgreSQL does not support INTEGER with options. Plain `INTEGER` will be used instead.');
warn('PostgreSQL does not support INTEGER with options. Plain `INTEGER` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(INTEGER, BaseTypes.INTEGER);
});
INTEGER.parse = function (value) {
return parseInt(value, 10);
};
var BIGINT = function() {
// int4
BaseTypes.INTEGER.types.postgres = {
oids: [23],
array_oids: [1007]
};
var BIGINT = BaseTypes.BIGINT.inherits(function() {
if (!(this instanceof BIGINT)) return new BIGINT();
BaseTypes.BIGINT.apply(this, arguments);
// POSTGRES does not support any parameters for bigint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('PostgreSQL does not support BIGINT with options. Plain `BIGINT` will be used instead.');
warn('PostgreSQL does not support BIGINT with options. Plain `BIGINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(BIGINT, BaseTypes.BIGINT);
});
var REAL = function() {
// int8
BaseTypes.BIGINT.types.postgres = {
oids: [20],
array_oids: [1016]
};
var REAL = BaseTypes.REAL.inherits(function() {
if (!(this instanceof REAL)) return new REAL();
BaseTypes.REAL.apply(this, arguments);
// POSTGRES does not support any parameters for real
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('PostgreSQL does not support REAL with options. Plain `REAL` will be used instead.');
warn('PostgreSQL does not support REAL with options. Plain `REAL` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(REAL, BaseTypes.REAL);
});
var DOUBLE = function() {
// float4
BaseTypes.REAL.types.postgres = {
oids: [700],
array_oids: [1021]
};
var DOUBLE = BaseTypes.DOUBLE.inherits(function() {
if (!(this instanceof DOUBLE)) return new DOUBLE();
BaseTypes.DOUBLE.apply(this, arguments);
// POSTGRES does not support any parameters for double
if (this._length || this.options.length || this._unsigned || this._zerofill) {
this.warn('PostgreSQL does not support DOUBLE with options. Plain `DOUBLE` will be used instead.');
warn('PostgreSQL does not support DOUBLE with options. Plain `DOUBLE` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
};
util.inherits(DOUBLE, BaseTypes.DOUBLE);
});
var FLOAT = function() {
// float8
BaseTypes.DOUBLE.types.postgres = {
oids: [701],
array_oids: [1022]
};
var FLOAT = BaseTypes.FLOAT.inherits(function() {
if (!(this instanceof FLOAT)) return new FLOAT();
BaseTypes.FLOAT.apply(this, arguments);
......@@ -131,37 +203,44 @@ var FLOAT = function() {
// Values between 25-53 result in DOUBLE PRECISION
// If decimals are provided remove these and print a warning
if (this._decimals) {
this.warn('PostgreSQL does not support FLOAT with decimals. Plain `FLOAT` will be used instead.');
warn('PostgreSQL does not support FLOAT with decimals. Plain `FLOAT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._decimals = undefined;
}
if (this._unsigned) {
this.warn('PostgreSQL does not support FLOAT unsigned. `UNSIGNED` was removed.');
warn('PostgreSQL does not support FLOAT unsigned. `UNSIGNED` was removed.');
this._unsigned = undefined;
}
if (this._zerofill) {
this.warn('PostgreSQL does not support FLOAT zerofill. `ZEROFILL` was removed.');
warn('PostgreSQL does not support FLOAT zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined;
}
};
util.inherits(FLOAT, BaseTypes.FLOAT);
});
var BLOB = BaseTypes.BLOB.inherits();
BaseTypes.BLOB.prototype.toSql = function() {
BLOB.prototype.toSql = function() {
if (this._length) {
this.warn('PostgreSQL does not support BLOB (BYTEA) with options. Plain `BYTEA` will be used instead.');
warn('PostgreSQL does not support BLOB (BYTEA) with options. Plain `BYTEA` will be used instead.');
this._length = undefined;
}
return 'BYTEA';
};
};
var GEOMETRY = function() {
if (!(this instanceof GEOMETRY)) return new GEOMETRY();
BaseTypes.GEOMETRY.apply(this, arguments);
};
util.inherits(GEOMETRY, BaseTypes.GEOMETRY);
BLOB.prototype.$hexify = function (hex) {
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
return "E'\\\\x" + hex + "'";
};
BaseTypes.BLOB.types.postgres = {
oids: [17],
array_oids: [1001]
};
GEOMETRY.prototype.toSql = function() {
var GEOMETRY = BaseTypes.GEOMETRY.inherits();
GEOMETRY.prototype.toSql = function() {
var result = this.key;
if (this.type){
......@@ -175,31 +254,124 @@ GEOMETRY.prototype.toSql = function() {
}
return result;
};
};
GEOMETRY.prototype.parse = function(value) {
BaseTypes.GEOMETRY.types.postgres = {
oids: [],
array_oids: []
};
GEOMETRY.parse = GEOMETRY.prototype.parse = function(value) {
var b = new Buffer(value, 'hex');
return wkt.parse(wkx.Geometry.parse(b).toWkt());
};
return wkx.Geometry.parse(b).toGeoJSON();
};
GEOMETRY.prototype.$stringify = function (value) {
return 'ST_GeomFromGeoJSON(\'' + JSON.stringify(value) + '\')';
};
var HSTORE = BaseTypes.HSTORE.inherits();
module.exports = {
HSTORE.parse = function (value) {
return hstore.parse(value);
};
BaseTypes.HSTORE.types.postgres = {
oids: [],
array_oids: []
};
var RANGE = BaseTypes.RANGE.inherits();
RANGE.oid_map = {
3904: 1007, // int4
3905: 1007,
3906: 1700, // Numeric
3907: 1700,
3908: 1114, // timestamp
3909: 1114,
3910: 1184, // timestamptz
3911: 1184,
3912: 1082, // date
3913: 1082,
3926: 20, // int8
3927: 20,
};
var range = require('./range');
RANGE.parse = function (value, oid, getTypeParser) {
var parser = getTypeParser(RANGE.oid_map[oid]);
return range.parse(value, parser);
};
RANGE.prototype.escape = false;
RANGE.prototype.$stringify = function (values, options) {
var valuesStringified = values.map(function (value) {
if (this.options.subtype.stringify) {
return this.options.subtype.stringify(value, options);
} else {
return options.escape(value);
}
}, this);
// Array.map does not preserve extra array properties
valuesStringified.inclusive = values.inclusive;
return '\'' + range.stringify(valuesStringified) + '\'';
};
BaseTypes.RANGE.types.postgres = {
oids: [3904, 3906, 3908, 3910, 3912, 3926],
array_oids: [3905, 3907, 3909, 3911, 3913, 3927]
};
BaseTypes.ARRAY.prototype.escape = false;
BaseTypes.ARRAY.prototype.$stringify = function (values, options) {
var str = 'ARRAY[' + values.map(function (value) {
if (this.type && this.type.stringify) {
value = this.type.stringify(value, options);
if (this.type.escape === false) {
return value;
}
}
return options.escape(value);
}, this).join(',') + ']';
if (this.type) {
str += '::' + this.toSql();
}
return str;
};
var exports = {
DECIMAL: DECIMAL,
BLOB: BLOB,
STRING: STRING,
CHAR: CHAR,
TEXT: TEXT,
INTEGER: INTEGER,
BOOLEAN: BOOLEAN,
DATE: DATE,
INTEGER: INTEGER,
BIGINT: BIGINT,
REAL: REAL,
'DOUBLE PRECISION': DOUBLE,
FLOAT: FLOAT,
GEOMETRY: GEOMETRY
};
GEOMETRY: GEOMETRY,
HSTORE: HSTORE,
RANGE: RANGE
};
_.forIn(module.exports, function (DataType, key) {
_.forIn(exports, function (DataType, key) {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
return new DataType(oldType.options);
};
}
});
});
return exports;
};
......@@ -5,16 +5,13 @@ var _ = require('lodash')
, ConnectionManager = require('./connection-manager')
, Query = require('./query')
, QueryGenerator = require('./query-generator')
, DataTypes = require('./data-types');
, DataTypes = require('../../data-types').postgres;
var PostgresDialect = function(sequelize) {
this.sequelize = sequelize;
this.connectionManager = new ConnectionManager(this, sequelize);
this.connectionManager.initPools();
// parseDialectSpecificFields needs access to the pg lib in order to use its array parser. We cannot simply require pg in query.js since the user can specify another library path (such as pg-native etc)
Query.prototype.parseDialectSpecificFields.lib = this.connectionManager.lib;
this.QueryGenerator = _.extend({}, QueryGenerator, {
options: sequelize.options,
_dialect: this,
......
......@@ -2,11 +2,8 @@
/* jshint -W110 */
var Utils = require('../../utils')
, hstore = require('./hstore')
, range = require('./range')
, util = require('util')
, DataTypes = require('../../data-types')
, SqlString = require('../../sql-string')
, AbstractQueryGenerator = require('../abstract/query-generator')
, primaryKeys = {}
, _ = require('lodash');
......@@ -845,48 +842,6 @@ var QueryGenerator = {
},
/*
Escape a value (e.g. a string, number or date)
*/
escape: function(value, field, options) {
options = options || {};
if (value && value._isSequelizeMethod) {
return this.handleSequelizeMethod(value);
}
if (['INSERT', 'UPDATE'].indexOf(options.context) !== -1 && this.typeValidation && field && field.type && value) {
if (field.type.validate) {
field.type.validate(value);
}
}
if (Utils._.isObject(value) && field && (field.type instanceof DataTypes.HSTORE || DataTypes.ARRAY.is(field.type, DataTypes.HSTORE))) {
if (field.type instanceof DataTypes.HSTORE){
return "'" + hstore.stringify(value) + "'";
} else if (DataTypes.ARRAY.is(field.type, DataTypes.HSTORE)) {
return 'ARRAY[' + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(',') + ']::HSTORE[]';
}
} else if(Utils._.isArray(value) && field && (field.type instanceof DataTypes.RANGE || DataTypes.ARRAY.is(field.type, DataTypes.RANGE))) {
if(field.type instanceof DataTypes.RANGE) { // escape single value
return "'" + range.stringify(value) + "'";
}
else if (DataTypes.ARRAY.is(field.type, DataTypes.RANGE)) { // escape array of ranges
return 'ARRAY[' + Utils._.map(value, function(v){return "'" + range.stringify(v) + "'";}).join(',') + ']::' + field.type.toString();
}
} else if (value!==null && field && field.type instanceof DataTypes.JSON) {
value = JSON.stringify(value);
} else if (Array.isArray(value) && field && DataTypes.ARRAY.is(field.type, DataTypes.JSON)) {
var jsonType = field.type.type; // type may be JSON or JSONB
return 'ARRAY[' + value.map(function (v) {
return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
}, this).join(',') + ']::' + jsonType.key + '[]';
} else if (value && field && field.type instanceof DataTypes.GEOMETRY) {
return "ST_GeomFromGeoJSON('" + JSON.stringify(value) + "')";
}
return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
},
/**
* Generates an SQL query that returns all foreign keys of a table.
*
......@@ -922,10 +877,6 @@ var QueryGenerator = {
}
return AbstractQueryGenerator.setAutocommitQuery.call(this, value, options);
},
geometrySelect: function(column) {
return this.quoteIdentifiers(column);
}
};
......
......@@ -2,80 +2,17 @@
var Utils = require('../../utils')
, AbstractQuery = require('../abstract/query')
, DataTypes = require('../../data-types')
, hstore = require('./hstore')
, range = require('./range')
, QueryTypes = require('../../query-types')
, Promise = require('../../promise')
, sequelizeErrors = require('../../errors.js');
var parseDialectSpecificFields,
dialectSpecificTypes;
parseDialectSpecificFields = {
// Parses hstore fields if the model has any hstore fields.
// This cannot be done in the 'pg' lib because hstore is a UDT.
hstore: function (value, options) {
if (value === null) return null;
return DataTypes.ARRAY.is(options.dataType, DataTypes.HSTORE) ?
this.lib.types.arrayParser.create(value, hstore.parse).parse() : hstore.parse(value);
},
range: function (value, options) {
if (value === null) return null;
return DataTypes.ARRAY.is(options.dataType, DataTypes.RANGE) ?
this.lib.types.arrayParser.create(value, function (v) {
return range.parse(v, options.dataType.type);
}).parse() : range.parse(value, options.dataType);
},
geometry: function (value, options) {
if (value === null) return null;
return options.dataType.parse(value);
}
};
dialectSpecificTypes = Utils._.keys(parseDialectSpecificFields);
function dialectSpecificFieldDatatypeMap (options, prefix) {
if (!Utils._.isArray(options.types))
return [];
prefix = prefix || '';
var fields = {};
if (options.callee) {
if (options.as)
prefix += options.as + '.';
Utils._.each(options.types, function (type) {
Utils._.each(options.callee['_' + type + 'Attributes'], function (attrName) {
fields[prefix + attrName] = {
dataType: options.callee.attributes[attrName].type || null,
type: type
};
});
});
}
Utils._.each(options.include, function (include) {
fields = Utils._.merge(fields, dialectSpecificFieldDatatypeMap({
callee: include.model,
as: include.as,
include: include.include,
types: options.types
}, prefix));
});
return fields;
}
, sequelizeErrors = require('../../errors.js')
, _ = require('lodash');
var Query = function(client, sequelize, options) {
this.client = client;
this.sequelize = sequelize;
this.instance = options.instance;
this.model = options.model;
this.options = Utils._.extend({
this.options = _.extend({
logging: console.log,
plain: false,
raw: false
......@@ -85,8 +22,6 @@ var Query = function(client, sequelize, options) {
};
Utils.inherit(Query, AbstractQuery);
Query.prototype.parseDialectSpecificFields = parseDialectSpecificFields;
/**
* rewrite query with parameters
*/
......@@ -156,22 +91,17 @@ Query.prototype.run = function(sql, parameters) {
}).spread(function(rows, sql, result) {
var results = rows
, isTableNameQuery = (sql.indexOf('SELECT table_name FROM information_schema.tables') === 0)
, isRelNameQuery = (sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0)
, dialectSpecificFields
, isDialectSpecificField = Utils._.memoize(function (key) { return dialectSpecificFields.hasOwnProperty(key); });
, isRelNameQuery = (sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') === 0);
if (isTableNameQuery || isRelNameQuery) {
if (isRelNameQuery) {
results = rows.map(function(row) {
return rows.map(function(row) {
return {
name: row.relname,
tableName: row.relname.split('_')[0]
};
});
} else {
results = rows.map(function(row) { return Utils._.values(row); });
}
return results;
} else if (isTableNameQuery) {
return rows.map(function(row) { return _.values(row); });
}
if (rows[0] && rows[0].sequelize_caught_exception !== undefined) {
......@@ -193,12 +123,11 @@ Query.prototype.run = function(sql, parameters) {
results.forEach(function (result) {
var attributes = /ON .*? (?:USING .*?\s)?\((.*)\)/gi.exec(result.definition)[1].split(',')
, field
, attribute
, columns;
// Map column index in table to column name
columns = Utils._.zipObject(
columns = _.zipObject(
result.column_indexes,
self.sequelize.queryInterface.QueryGenerator.fromArray(result.column_names)
);
......@@ -246,9 +175,9 @@ Query.prototype.run = function(sql, parameters) {
// Postgres will treat tables as case-insensitive, so fix the case
// of the returned values to match attributes
if (self.options.raw === false && self.sequelize.options.quoteIdentifiers === false) {
var attrsMap = Utils._.reduce(self.model.attributes, function(m, v, k) { m[k.toLowerCase()] = k; return m; }, {});
var attrsMap = _.reduce(self.model.attributes, function(m, v, k) { m[k.toLowerCase()] = k; return m; }, {});
rows.forEach(function(row) {
Utils._.keys(row).forEach(function(key) {
_.keys(row).forEach(function(key) {
var targetAttr = attrsMap[key];
if (typeof targetAttr === 'string' && targetAttr !== key) {
row[targetAttr] = row[key];
......@@ -258,25 +187,6 @@ Query.prototype.run = function(sql, parameters) {
});
}
if (!!self.model && rows.length > 0) {
dialectSpecificFields = dialectSpecificFieldDatatypeMap({
callee: self.model,
include: self.options.include,
types: dialectSpecificTypes
});
// check whether custom fields exist in responses model
if (!Utils._.isEmpty(dialectSpecificFields)) {
rows.forEach(function (row, rowId, rows) {
Utils._.each(row, function (value, key) {
if (isDialectSpecificField(key))
rows[rowId][key] =
parseDialectSpecificFields[dialectSpecificFields[key].type](value, { dataType: dialectSpecificFields[key].dataType });
});
});
}
}
return self.handleSelectQuery(rows);
} else if (QueryTypes.DESCRIBE === self.options.type) {
result = {};
......@@ -320,24 +230,6 @@ Query.prototype.run = function(sql, parameters) {
return parseInt(result.rowCount, 10);
}
if (!!self.model && rows.length > 0) {
dialectSpecificFields = dialectSpecificFieldDatatypeMap({
callee: self.model,
types: dialectSpecificTypes
});
// check whether custom fields exist in responses model
if (!Utils._.isEmpty(dialectSpecificFields)) {
rows.forEach(function (row, rowId, rows) {
Utils._.each(row, function (value, key) {
if (isDialectSpecificField(key))
rows[rowId][key] =
parseDialectSpecificFields[dialectSpecificFields[key].type](value, { dataType: dialectSpecificFields[key].dataType });
});
});
}
}
return self.handleSelectQuery(rows);
} else if (QueryTypes.BULKDELETE === self.options.type) {
return parseInt(result.rowCount, 10);
......@@ -345,27 +237,11 @@ Query.prototype.run = function(sql, parameters) {
return rows[0].sequelize_upsert;
} else if (self.isInsertQuery() || self.isUpdateQuery()) {
if (self.instance && self.instance.dataValues) {
if (self.model) {
dialectSpecificFields = dialectSpecificFieldDatatypeMap({
callee: self.model,
types: dialectSpecificTypes
});
// check whether custom fields exist in responses model
if (!Utils._.isEmpty(dialectSpecificFields)) {
Utils._.each(rows[0], function (value, key) {
if (isDialectSpecificField(key))
rows[0][key] =
parseDialectSpecificFields[dialectSpecificFields[key].type](value, { dataType: dialectSpecificFields[key].dataType });
});
}
}
for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key];
var attr = Utils._.find(self.model.rawAttributes, function (attribute) {
var attr = _.find(self.model.rawAttributes, function (attribute) {
return attribute.fieldName === key || attribute.field === key;
});
......@@ -418,19 +294,19 @@ Query.prototype.formatError = function (err) {
match = errDetail.replace(/"/g, '').match(/Key \((.*?)\)=\((.*?)\)/);
if (match) {
fields = Utils._.zipObject(match[1].split(', '), match[2].split(', '));
fields = _.zipObject(match[1].split(', '), match[2].split(', '));
errors = [];
message = 'Validation error';
Utils._.forOwn(fields, function(value, field) {
_.forOwn(fields, function(value, field) {
errors.push(new sequelizeErrors.ValidationErrorItem(
self.getUniqueConstraintErrorMessage(field),
'unique violation', field, value));
});
if (this.model && this.model.uniqueKeys) {
Utils._.forOwn(this.model.uniqueKeys, function(constraint) {
if (Utils._.isEqual(constraint.fields, Object.keys(fields)) && !!constraint.msg) {
_.forOwn(this.model.uniqueKeys, function(constraint) {
if (_.isEqual(constraint.fields, Object.keys(fields)) && !!constraint.msg) {
message = constraint.msg;
return false;
}
......@@ -455,7 +331,7 @@ Query.prototype.formatError = function (err) {
match = errDetail.match(/Key \((.*?)\)=\((.*?)\)/);
if (match) {
fields = Utils._.zipObject(match[1].split(', '), match[2].split(', '));
fields = _.zipObject(match[1].split(', '), match[2].split(', '));
}
message = 'Exclusion constraint error';
......
'use strict';
var Utils = require('../../utils'),
moment = require('moment');
var _ = require('lodash');
function stringify (data) {
if (data === null) return null;
if (!Utils._.isArray(data) || data.length !== 2) return '';
if (!_.isArray(data) || data.length !== 2) return '';
if (Utils._.any(data, Utils._.isNull)) return '';
if (_.any(data, _.isNull)) return '';
if (data.hasOwnProperty('inclusive')) {
if (!data.inclusive) data.inclusive = [false, false];
......@@ -17,8 +16,8 @@ function stringify (data) {
data.inclusive = [false, false];
}
Utils._.each(data, function (value, index) {
if (Utils._.isObject(value)) {
_.each(data, function (value, index) {
if (_.isObject(value)) {
if (value.hasOwnProperty('inclusive')) data.inclusive[index] = !!value.inclusive;
if (value.hasOwnProperty('value')) data[index] = value.value;
}
......@@ -27,12 +26,9 @@ function stringify (data) {
return (data.inclusive[0] ? '[' : '(') + JSON.stringify(data[0]) + ',' + JSON.stringify(data[1]) + (data.inclusive[1] ? ']' : ')');
}
function parse (value, AttributeType) {
function parse (value, parser) {
if (value === null) return null;
if(typeof AttributeType === 'function') AttributeType = new AttributeType();
AttributeType = AttributeType || ''; // if attribute is not defined, assign empty string in order to prevent
// AttributeType.toString() to fail with uncaught exception later in the code
var result = value
.substring(1, value.length - 1)
.split(',', 2);
......@@ -41,18 +37,7 @@ function parse (value, AttributeType) {
result = result
.map(function (value) {
switch (AttributeType.toString()) {
case 'int4range':
return parseInt(value, 10);
case 'numrange':
return parseFloat(value);
case 'daterange':
case 'tsrange':
case 'tstzrange':
return moment(value).toDate();
}
return value;
return parser(value);
});
result.inclusive = [(value[0] === '['), (value[value.length - 1] === ']')];
......
......@@ -4,12 +4,15 @@ var AbstractConnectionManager = require('../abstract/connection-manager')
, ConnectionManager
, Utils = require('../../utils')
, Promise = require('../../promise')
, sequelizeErrors = require('../../errors');
, dataTypes = require('../../data-types').sqlite
, sequelizeErrors = require('../../errors')
, parserStore = require('../parserStore')('sqlite');
ConnectionManager = function(dialect, sequelize) {
this.sequelize = sequelize;
this.config = sequelize.config;
this.dialect = dialect;
this.dialectName = this.sequelize.options.dialect;
this.connections = {};
// We attempt to parse file location from a connection uri but we shouldn't match sequelize default host.
......@@ -20,10 +23,21 @@ ConnectionManager = function(dialect, sequelize) {
} catch (err) {
throw new Error('Please install sqlite3 package manually');
}
this.refreshTypeParser(dataTypes);
};
Utils._.extend(ConnectionManager.prototype, AbstractConnectionManager.prototype);
// Expose this as a method so that the parsing may be updated when the user has added additional, custom types
ConnectionManager.prototype.$refreshTypeParser = function (dataType) {
parserStore.refresh(dataType);
};
ConnectionManager.prototype.$clearTypeParser = function () {
parserStore.clear();
};
ConnectionManager.prototype.getConnection = function(options) {
var self = this;
options = options || {};
......
'use strict';
var BaseTypes = require('../../data-types')
, util = require('util')
, _ = require('lodash');
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://www.sqlite.org/datatype3.html';
var STRING = function() {
if (!(this instanceof STRING)) return new STRING();
BaseTypes.STRING.apply(this, arguments);
};
util.inherits(STRING, BaseTypes.STRING);
var _ = require('lodash');
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.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.INTEGER.types.sqlite = ['INTEGER'];
BaseTypes.BIGINT.types.sqlite = ['BIGINT'];
BaseTypes.FLOAT.types.sqlite = ['FLOAT'];
BaseTypes.TIME.types.sqlite = ['TIME'];
BaseTypes.DATEONLY.types.sqlite = ['DATE'];
BaseTypes.BOOLEAN.types.sqlite = ['TINYINT'];
BaseTypes.BLOB.types.sqlite = ['TINYBLOB', 'BLOB', 'LONGBLOB'];
BaseTypes.DECIMAL.types.sqlite = ['DECIMAL'];
BaseTypes.UUID.types.sqlite = ['UUID'];
BaseTypes.ENUM.types.sqlite = false;
BaseTypes.REAL.types.sqlite = ['REAL'];
BaseTypes.DOUBLE.types.sqlite = ['DOUBLE PRECISION'];
BaseTypes.GEOMETRY.types.sqlite = false;
var DATE = BaseTypes.DATE.inherits();
DATE.parse = function (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);
} else {
return new Date(date); // We already have a timezone stored in the string
}
};
STRING.prototype.toSql = function() {
var STRING = BaseTypes.STRING.inherits();
STRING.prototype.toSql = function() {
if (this._binary) {
return 'VARCHAR BINARY(' + this._length + ')';
} else {
return BaseTypes.STRING.prototype.toSql.call(this);
}
};
};
BaseTypes.TEXT.prototype.toSql = function() {
var TEXT = BaseTypes.TEXT.inherits();
TEXT.prototype.toSql = function() {
if (this._length) {
this.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;
}
return 'TEXT';
};
var CHAR = function() {
if (!(this instanceof CHAR)) return new CHAR();
BaseTypes.CHAR.apply(this, arguments);
};
util.inherits(CHAR, BaseTypes.CHAR);
};
CHAR.prototype.toSql = function() {
var CHAR = BaseTypes.CHAR.inherits();
CHAR.prototype.toSql = function() {
if (this._binary) {
return 'CHAR BINARY(' + this._length + ')';
} else {
return BaseTypes.CHAR.prototype.toSql.call(this);
}
};
var NUMBER = function() {
BaseTypes.NUMBER.apply(this, arguments);
};
util.inherits(NUMBER, BaseTypes.NUMBER);
};
NUMBER.prototype.toSql = function() {
var NUMBER = BaseTypes.NUMBER.inherits();
NUMBER.prototype.toSql = function() {
var result = this.key;
if (this._unsigned) {
......@@ -65,77 +79,79 @@ NUMBER.prototype.toSql = function() {
result += ')';
}
return result;
};
};
var INTEGER = function(length) {
var INTEGER = BaseTypes.INTEGER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof INTEGER)) return new INTEGER(options);
NUMBER.call(this, options);
};
util.inherits(INTEGER, BaseTypes.INTEGER);
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.toSql = function() {
BaseTypes.INTEGER.call(this, options);
});
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this);
};
};
var BIGINT = function(length) {
var BIGINT = BaseTypes.BIGINT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof BIGINT)) return new BIGINT(options);
NUMBER.call(this, options);
};
util.inherits(BIGINT, BaseTypes.BIGINT);
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.toSql = function() {
BaseTypes.BIGINT.call(this, options);
});
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this);
};
};
var FLOAT = function(length, decimals) {
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);
NUMBER.call(this, options);
};
util.inherits(FLOAT, BaseTypes.FLOAT);
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.toSql = function() {
BaseTypes.FLOAT.call(this, options);
});
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this);
};
};
var DOUBLE = function(length, decimals) {
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);
NUMBER.call(this, options);
};
util.inherits(DOUBLE, BaseTypes.DOUBLE);
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
DOUBLE.prototype.toSql = function() {
BaseTypes.DOUBLE.call(this, options);
});
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
DOUBLE.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this);
};
};
var REAL = function(length, decimals) {
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);
NUMBER.call(this, options);
};
util.inherits(REAL, BaseTypes.REAL);
REAL.prototype.key = REAL.key = 'REAL';
REAL.prototype.toSql = function() {
BaseTypes.REAL.call(this, options);
});
REAL.prototype.key = REAL.key = 'REAL';
REAL.prototype.toSql = function() {
return NUMBER.prototype.toSql.call(this);
};
};
var ENUM = BaseTypes.ENUM.inherits();
ENUM.prototype.toSql = function () {
return 'TEXT';
};
module.exports = {
var exports = {
DATE: DATE,
STRING: STRING,
CHAR: CHAR,
NUMBER: NUMBER,
......@@ -143,14 +159,20 @@ module.exports = {
REAL: REAL,
'DOUBLE PRECISION': DOUBLE,
INTEGER: INTEGER,
BIGINT: BIGINT
};
BIGINT: BIGINT,
TEXT: TEXT,
ENUM: ENUM
};
_.forIn(module.exports, function (DataType, key) {
_.forIn(exports, function (DataType, key) {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
return new DataType(oldType.options);
};
}
});
\ No newline at end of file
});
return exports;
};
......@@ -5,7 +5,7 @@ var _ = require('lodash')
, ConnectionManager = require('./connection-manager')
, Query = require('./query')
, QueryGenerator = require('./query-generator')
, DataTypes = require('./data-types');
, DataTypes = require('../../data-types').sqlite;
var SqliteDialect = function(sequelize) {
this.sequelize = sequelize;
......
......@@ -2,7 +2,6 @@
/* jshint -W110 */
var Utils = require('../../utils')
, DataTypes = require('../../data-types')
, Transaction = require('../../transaction')
, _ = require('lodash');
......@@ -195,15 +194,7 @@ var QueryGenerator = {
if (Utils._.isObject(dataType)) {
var template = '<%= type %>'
, replacements = { type: dataType.type };
if (dataType.type instanceof DataTypes.ENUM) {
replacements.type = 'TEXT';
if (!(Array.isArray(dataType.values) && (dataType.values.length > 0))) {
throw new Error("Values for ENUM haven't been defined.");
}
}
, replacements = { type: dataType.type.toString() };
if (dataType.hasOwnProperty('allowNull') && !dataType.allowNull) {
template += ' NOT NULL';
......
'use strict';
var Utils = require('../../utils')
, _ = require('lodash')
, Promise = require('../../promise')
, AbstractQuery = require('../abstract/query')
, QueryTypes = require('../../query-types')
, sequelizeErrors = require('../../errors.js');
, sequelizeErrors = require('../../errors.js')
, parserStore = require('../parserStore')('sqlite');
var Query = function(database, sequelize, options) {
this.database = database;
this.sequelize = sequelize;
this.instance = options.instance;
this.model = options.model;
this.options = Utils._.extend({
this.options = _.extend({
logging: console.log,
plain: false,
raw: false
......@@ -47,6 +50,28 @@ Query.formatBindParameters = function(sql, values, dialect) {
return [sql, bindParam];
};
Query.prototype.$collectModels = function(include, prefix) {
var ret = {};
if (include) {
include.forEach(function (include) {
var key;
if (!prefix) {
key = include.as;
} else {
key = prefix + '.' + include.as;
}
ret[key] = include.model;
if (include.include) {
_.merge(ret, this.$collectModels(include.include, key));
}
}, this);
}
return ret;
};
Query.prototype.run = function(sql, parameters) {
var self = this
, promise;
......@@ -61,24 +86,21 @@ Query.prototype.run = function(sql, parameters) {
this.sequelize.log('Executing (' + (this.database.uuid || 'default') + '): ' + this.sql, this.options);
promise = new Utils.Promise(function(resolve) {
promise = new Promise(function(resolve) {
var columnTypes = {};
self.database.serialize(function() {
var executeSql = function() {
if (self.sql.indexOf('-- ') === 0) {
return resolve();
} else {
resolve(new Utils.Promise(function(resolve, reject) {
resolve(new Promise(function(resolve, reject) {
var afterExecute = function(err, results) {
if (err) {
err.sql = self.sql;
reject(self.formatError(err));
} else {
var metaData = this;
metaData.columnTypes = columnTypes;
var result = self.instance;
var metaData = this
, result = self.instance;
// add the inserted row id to the instance
if (self.isInsertQuery(results, metaData)) {
......@@ -93,29 +115,51 @@ Query.prototype.run = function(sql, parameters) {
result = results.map(function(resultSet) { return resultSet.name; });
} else if (self.isSelectQuery()) {
if (!self.options.raw) {
// This is a map of prefix strings to models, e.g. user.projects -> Project model
var prefixes = self.$collectModels(self.options.include);
results = results.map(function(result) {
for (var name in result) {
if (result.hasOwnProperty(name) && metaData.columnTypes[name]) {
if (metaData.columnTypes[name] === 'DATETIME') {
// we need to convert the timestamps into actual date objects
var val = result[name];
if (val !== null) {
if (val.indexOf('+') === -1) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
result[name] = new Date(val + self.sequelize.options.timezone);
return _.mapValues(result, function (value, name) {
var model;
if (name.indexOf('.') !== -1) {
var lastind = name.lastIndexOf('.');
model = prefixes[name.substr(0, lastind)];
name = name.substr(lastind + 1);
} else {
result[name] = new Date(val); // We already have a timezone stored in the string
model = self.options.model;
}
var tableName = model.getTableName().toString().replace(/`/g, '')
, tableTypes = columnTypes[tableName];
if (tableTypes && !(name in tableTypes)) {
// The column is aliased
_.forOwn(model.rawAttributes, function (attribute, key) {
if (name === key && attribute.field) {
name = attribute.field;
return false;
}
} else if (metaData.columnTypes[name].lastIndexOf('BLOB') !== -1) {
if (result[name]) {
result[name] = new Buffer(result[name]);
});
}
var type = tableTypes[name];
if (type) {
if (type.indexOf('(') !== -1) {
// Remove the lenght part
type = type.substr(0, type.indexOf('('));
}
type = type.replace('UNSIGNED', '').replace('ZEROFILL', '');
type = type.trim();
var parse = parserStore.get(type);
if (value !== null && parse) {
return parse(value, { timezone: self.sequelize.options.timezone});
}
}
return result;
return value;
});
});
}
......@@ -189,23 +233,28 @@ Query.prototype.run = function(sql, parameters) {
tableNames.push(/FROM `(.*?)`/i.exec(self.sql)[1]);
}
// If we already have the metadata for the table, there's no need to ask for it again
tableNames = _.filter(tableNames, function (tableName) {
return !(tableName in columnTypes) && tableName !== 'sqlite_master';
});
if (!tableNames.length) {
return executeSql();
} else {
return Utils.Promise.map(tableNames, function(tableName) {
if (tableName !== 'sqlite_master') {
return new Utils.Promise(function(resolve) {
// get the column types
return Promise.map(tableNames, function(tableName) {
return new Promise(function(resolve) {
tableName = tableName.replace(/`/g, '');
columnTypes[tableName] = {};
self.database.all('PRAGMA table_info(`' + tableName + '`)', function(err, results) {
if (!err) {
for (var i = 0, l = results.length; i < l; i++) {
columnTypes[tableName + '.' + results[i].name] = columnTypes[results[i].name] = results[i].type;
}
results.forEach(function (result) {
columnTypes[tableName][result.name] = result.type;
});
}
resolve();
});
});
}
}).then(executeSql);
}
} else {
......@@ -257,8 +306,8 @@ Query.prototype.formatError = function (err) {
});
if (this.model) {
Utils._.forOwn(this.model.uniqueKeys, function(constraint) {
if (Utils._.isEqual(constraint.fields, fields) && !!constraint.msg) {
_.forOwn(this.model.uniqueKeys, function(constraint) {
if (_.isEqual(constraint.fields, fields) && !!constraint.msg) {
message = constraint.msg;
return false;
}
......
......@@ -368,10 +368,6 @@ Instance.prototype.set = function(key, value, options) { // testhint options:non
}
}
if (this.Model._hasGeometryAttributes && this.Model._isGeometryAttribute(key) && _.isString(value)) {
value = this.Model.attributes[key].type.parse(value);
}
if (!options.raw && ((!Utils.isPrimitive(value) && value !== null) || value !== originalValue)) {
this._previousDataValues[key] = originalValue;
this.changed(key, true);
......
......@@ -93,15 +93,6 @@ var Model = function(name, attributes, options) {
throw new Error('Unrecognized data type for field ' + name);
}
if (attribute.type instanceof DataTypes.ENUM) {
if (!attribute.values.length) {
throw new Error('Values for ENUM have not been defined.');
}
// BC compatible.
attribute.type.values = attribute.values;
}
return attribute;
}, this);
};
......@@ -837,14 +828,6 @@ Model.prototype.refreshAttributes = function() {
}
if (definition.hasOwnProperty('defaultValue')) {
if (typeof definition.defaultValue === 'function' && (
definition.defaultValue === DataTypes.NOW ||
definition.defaultValue === DataTypes.UUIDV1 ||
definition.defaultValue === DataTypes.UUIDV4
)) {
definition.defaultValue = new definition.defaultValue();
}
self._defaultValues[name] = Utils._.partial(Utils.toDefaultValue, definition.defaultValue);
}
......
......@@ -82,26 +82,7 @@ QueryInterface.prototype.createTable = function(tableName, attributes, options,
attribute = { type: attribute, allowNull: true };
}
attribute.type = self.sequelize.normalizeDataType(attribute.type);
if (attribute.hasOwnProperty('defaultValue')) {
if (typeof attribute.defaultValue === 'function' && (
attribute.defaultValue === DataTypes.NOW ||
attribute.defaultValue === DataTypes.UUIDV1 ||
attribute.defaultValue === DataTypes.UUIDV4
)) {
attribute.defaultValue = new attribute.defaultValue();
}
}
if (attribute.type instanceof DataTypes.ENUM) {
// The ENUM is a special case where the type is an object containing the values
attribute.values = attribute.values || attribute.type.values || [];
if (!attribute.values.length) {
throw new Error('Values for ENUM haven\'t been defined.');
}
}
attribute = self.sequelize.normalizeAttribute(attribute);
return attribute;
});
......
......@@ -428,6 +428,10 @@ Sequelize.prototype.InvalidConnectionError = Sequelize.InvalidConnectionError =
Sequelize.prototype.ConnectionTimedOutError = Sequelize.ConnectionTimedOutError =
sequelizeErrors.ConnectionTimedOutError;
Sequelize.prototype.refreshTypes = function () {
this.connectionManager.refreshTypeParser(DataTypes);
};
/**
* Returns the specified dialect.
*
......@@ -1304,9 +1308,27 @@ Sequelize.prototype.normalizeAttribute = function(attribute) {
attribute.type = this.normalizeDataType(attribute.type);
if (attribute.hasOwnProperty('defaultValue')) {
if (typeof attribute.defaultValue === 'function' && (
attribute.defaultValue === DataTypes.NOW ||
attribute.defaultValue === DataTypes.UUIDV1 ||
attribute.defaultValue === DataTypes.UUIDV4
)) {
attribute.defaultValue = new attribute.defaultValue();
}
}
if (attribute.type instanceof DataTypes.ENUM) {
// The ENUM is a special case where the type is an object containing the values
attribute.values = attribute.values || attribute.type.values || [];
if (attribute.values) {
attribute.type.values = attribute.type.options.values = attribute.values;
} else {
attribute.values = attribute.type.values;
}
if (!attribute.values.length) {
throw new Error('Values for ENUM have not been defined.');
}
}
return attribute;
......
'use strict';
/* jshint -W110 */
var moment = require('moment-timezone')
, isArrayBufferView
var dataTypes = require('./data-types')
, _ = require('lodash')
, SqlString = exports;
if (typeof ArrayBufferView === 'function') {
isArrayBufferView = function(object) { return object && (object instanceof ArrayBufferView); };
} else {
var arrayBufferViews = [
Int8Array, Uint8Array, Int16Array, Uint16Array,
Int32Array, Uint32Array, Float32Array, Float64Array
];
isArrayBufferView = function(object) {
for (var i = 0; i < 8; i++) {
if (object instanceof arrayBufferViews[i]) {
return true;
}
}
return false;
};
}
SqlString.escapeId = function(val, forbidQualified) {
if (forbidQualified) {
return '`' + val.replace(/`/g, '``') + '`';
......@@ -30,19 +12,7 @@ SqlString.escapeId = function(val, forbidQualified) {
return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`';
};
SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
if (arguments.length === 1 && typeof val === 'object' && val !== null) {
val = val.val || val.value || null;
stringifyObjects = val.stringifyObjects || val.objects || undefined;
timeZone = val.timeZone || val.zone || null;
dialect = val.dialect || null;
field = val.field || null;
}
else if (arguments.length === 2 && typeof stringifyObjects === 'object' && stringifyObjects !== null) {
timeZone = stringifyObjects.timeZone || stringifyObjects.zone || null;
dialect = stringifyObjects.dialect || null;
field = stringifyObjects.field || null;
}
SqlString.escape = function(val, timeZone, dialect) {
if (val === undefined || val === null) {
return 'NULL';
}
......@@ -60,21 +30,23 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
}
if (val instanceof Date) {
val = SqlString.dateToString(val, timeZone || 'Z', dialect);
val = dataTypes[dialect].DATE.prototype.stringify(val, { timezone: timeZone });
}
if (Buffer.isBuffer(val)) {
return SqlString.bufferToString(val, dialect);
if (dataTypes[dialect].BLOB) {
return dataTypes[dialect].BLOB.prototype.stringify(val);
}
if (Array.isArray(val) || isArrayBufferView(val)) {
return SqlString.arrayToList(val, timeZone, dialect, field);
return dataTypes.BLOB.prototype.stringify(val);
}
if (typeof val === 'object' && val !== null) {
if (stringifyObjects) {
val = val.toString();
if (Array.isArray(val)) {
var escape = _.partialRight(SqlString.escape, timeZone, dialect);
if (dialect === 'postgres') {
return dataTypes.ARRAY.prototype.stringify(val, {escape: escape});
} else {
return SqlString.objectToValues(val, timeZone);
return '[' + val.map(escape) + ']';
}
}
......@@ -98,43 +70,6 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
return "'" + val + "'";
};
SqlString.arrayToList = function(array, timeZone, dialect, field) {
var valstr, i;
if (dialect === 'postgres') {
valstr = '';
if (array.map) {
valstr = array.map(function(v) {
return SqlString.escape(v, true, timeZone, dialect, field);
}).join(',');
} else {
for (i = 0; i < array.length; i++) {
valstr += SqlString.escape(array[i], true, timeZone, dialect, field) + ',';
}
valstr = valstr.slice(0, -1);
}
var ret = 'ARRAY[' + valstr + ']';
if (!!field && !!field.type) {
ret += '::' + field.type.toSql().replace(/\(\d+\)/g, '');
}
return ret;
} else {
if (array.map) {
return array.map(function(v) {
if (Array.isArray(v)) {
return '(' + SqlString.arrayToList(v, timeZone, dialect) + ')';
}
return SqlString.escape(v, true, timeZone, dialect);
}).join(', ');
} else {
valstr = '';
for (i = 0; i < array.length; i++) {
valstr += SqlString.escape(array[i], true, timeZone, dialect) + ', ';
}
return valstr.slice(0, -2);
}
}
};
SqlString.format = function(sql, values, timeZone, dialect) {
values = [].concat(values);
......@@ -143,7 +78,7 @@ SqlString.format = function(sql, values, timeZone, dialect) {
return match;
}
return SqlString.escape(values.shift(), false, timeZone, dialect);
return SqlString.escape(values.shift(), timeZone, dialect);
});
};
......@@ -154,51 +89,9 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
}
if (values[key] !== undefined) {
return SqlString.escape(values[key], false, timeZone, dialect);
return SqlString.escape(values[key], timeZone, dialect);
} else {
throw new Error('Named parameter "' + value + '" has no value in the given object.');
}
});
};
SqlString.dateToString = function(date, timeZone, dialect) {
if (moment.tz.zone(timeZone)) {
date = moment(date).tz(timeZone);
} else {
date = moment(date).utcOffset(timeZone);
}
if (dialect === 'mysql' || dialect === 'mariadb') {
return date.format('YYYY-MM-DD HH:mm:ss');
} else {
// ZZ here means current timezone, _not_ UTC
return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
}
};
SqlString.bufferToString = function(buffer, dialect) {
var hex = buffer.toString('hex');
if (dialect === 'postgres') {
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
return "E'\\\\x" + hex + "'";
} else if (dialect === 'mssql') {
return '0x' + hex;
}
return "X'" + hex + "'";
};
SqlString.objectToValues = function(object, timeZone) {
var values = [];
for (var key in object) {
var value = object[key];
if (typeof value === 'function') {
continue;
}
values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone));
}
return values.join(', ');
};
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../index')
, expect = chai.expect
, Support = require(__dirname + '/support')
, sinon = require('sinon')
, _ = require('lodash')
, moment = require('moment')
, current = Support.sequelize
, uuid = require('node-uuid')
, DataTypes = require('../../lib/data-types')
, dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('DataTypes'), function() {
afterEach(function () {
// Restore some sanity by resetting all parsers
switch (dialect) {
case 'postgres':
var types = require('../../node_modules/pg/node_modules/pg-types');
_.each(DataTypes, function (dataType) {
if (dataType.types && dataType.types.postgres) {
dataType.types.postgres.oids.forEach(function (oid) {
types.setTypeParser(oid, _.identity);
});
}
});
require('../../node_modules/pg/node_modules/pg-types/lib/binaryParsers').init(function (oid, converter) {
types.setTypeParser(oid, 'binary', converter);
});
require('../../node_modules/pg/node_modules/pg-types/lib/textParsers').init(function (oid, converter) {
types.setTypeParser(oid, 'text', converter);
});
break;
default:
this.sequelize.connectionManager.$clearTypeParser();
}
this.sequelize.connectionManager.refreshTypeParser(DataTypes[dialect]); // Reload custom parsers
});
it('allows me to return values from a custom parse function', function () {
var parse = Sequelize.DATE.parse = sinon.spy(function (value) {
return moment(value, 'YYYY-MM-DD HH:mm:ss Z');
});
var stringify = Sequelize.DATE.prototype.stringify = sinon.spy(function (value, options) {
if (!moment.isMoment(value)) {
value = this.$applyTimezone(value, options);
}
return value.format('YYYY-MM-DD HH:mm:ss Z');
});
current.refreshTypes();
var User = current.define('user', {
dateField: Sequelize.DATE
}, {
timestamps: false
});
return current.sync({ force: true }).then(function () {
return User.create({
dateField: moment("2011 10 31", 'YYYY MM DD')
});
}).then(function () {
return User.findAll().get(0);
}).then(function (user) {
expect(parse).to.have.been.called;
expect(stringify).to.have.been.called;
expect(moment.isMoment(user.dateField)).to.be.ok;
delete Sequelize.DATE.parse;
});
});
var testSuccess = function (Type, value) {
var parse = Type.constructor.parse = sinon.spy(function (value) {
return value;
});
var stringify = Type.constructor.prototype.stringify = sinon.spy(function (value) {
return Sequelize.ABSTRACT.prototype.stringify.apply(this, arguments);
});
current.refreshTypes();
var User = current.define('user', {
field: Type
}, {
timestamps: false
});
return current.sync({ force: true }).then(function () {
return User.create({
field: value
});
}).then(function () {
return User.findAll().get(0);
}).then(function (user) {
expect(parse).to.have.been.called;
expect(stringify).to.have.been.called;
delete Type.constructor.parse;
delete Type.constructor.prototype.stringify;
});
};
var testFailure = function (Type, value) {
Type.constructor.parse = _.noop();
expect(function () {
current.refreshTypes();
}).to.throw('Parse function not supported for type ' + Type.key + ' in dialect ' + dialect);
delete Type.constructor.parse;
};
if (dialect === 'postgres') {
it('calls parse and stringify for JSON', function () {
var Type = new Sequelize.JSON();
return testSuccess(Type, { test: 42, nested: { foo: 'bar' }});
});
it('calls parse and stringify for JSONB', function () {
var Type = new Sequelize.JSONB();
return testSuccess(Type, { test: 42, nested: { foo: 'bar' }});
});
it('calls parse and stringify for HSTORE', function () {
var Type = new Sequelize.HSTORE();
return testSuccess(Type, { test: 42, nested: false });
});
it('calls parse and stringify for RANGE', function () {
var Type = new Sequelize.RANGE(new Sequelize.INTEGER());
return testSuccess(Type, [1, 2]);
});
}
it('calls parse and stringify for DATE', function () {
var Type = new Sequelize.DATE();
return testSuccess(Type, new Date());
});
it('calls parse and stringify for DATEONLY', function () {
var Type = new Sequelize.DATEONLY();
return testSuccess(Type, new Date());
});
it('calls parse and stringify for TIME', function () {
var Type = new Sequelize.TIME();
return testSuccess(Type, new Date());
});
it('calls parse and stringify for BLOB', function () {
var Type = new Sequelize.BLOB();
return testSuccess(Type, 'foobar');
});
it('calls parse and stringify for CHAR', function () {
var Type = new Sequelize.CHAR();
return testSuccess(Type, 'foobar');
});
it('calls parse and stringify for STRING', function () {
var Type = new Sequelize.STRING();
return testSuccess(Type, 'foobar');
});
it('calls parse and stringify for TEXT', function () {
var Type = new Sequelize.TEXT();
if (dialect === 'mssql') {
// Text uses nvarchar, same type as string
testFailure(Type);
} else {
return testSuccess(Type, 'foobar');
}
});
it('calls parse and stringify for BOOLEAN', function () {
var Type = new Sequelize.BOOLEAN();
return testSuccess(Type, true);
});
it('calls parse and stringify for INTEGER', function () {
var Type = new Sequelize.INTEGER();
return testSuccess(Type, 1);
});
it('calls parse and stringify for DECIMAL', function () {
var Type = new Sequelize.DECIMAL();
return testSuccess(Type, 1.5);
});
it('calls parse and stringify for BIGINT', function () {
var Type = new Sequelize.BIGINT();
if (dialect === 'mssql') {
// Same type as integer
testFailure(Type);
} else {
return testSuccess(Type, 1);
}
});
it('calls parse and stringify for DOUBLE', function () {
var Type = new Sequelize.DOUBLE();
return testSuccess(Type, 1.5);
});
it('calls parse and stringify for FLOAT', function () {
var Type = new Sequelize.FLOAT();
if (dialect === 'postgres') {
// Postgres doesn't have float, maps to either decimal or double
testFailure(Type);
} else {
return testSuccess(Type, 1.5);
}
});
it('calls parse and stringify for REAL', function () {
var Type = new Sequelize.REAL();
return testSuccess(Type, 1.5);
});
it('calls parse and stringify for GEOMETRY', function () {
var Type = new Sequelize.GEOMETRY();
if (['postgres', 'mysql', 'mariadb'].indexOf(dialect) !== -1) {
return testSuccess(Type, { type: "Point", coordinates: [125.6, 10.1] });
} else {
// Not implemented yet
testFailure(Type);
}
});
it('calls parse and stringify for UUID', function () {
var Type = new Sequelize.UUID();
if (['postgres', 'sqlite'].indexOf(dialect) !== -1) {
return testSuccess(Type, uuid.v4());
} else {
// No native uuid type
testFailure(Type);
}
});
it('calls parse and stringify for ENUM', function () {
var Type = new Sequelize.ENUM('hat', 'cat');
// No dialects actually allow us to identify that we get an enum back..
testFailure(Type);
});
});
......@@ -566,7 +566,7 @@ if (Support.dialectIsMySQL()) {
if (_.isFunction(test.arguments[1])) test.arguments[1] = test.arguments[1](this.sequelize);
if (_.isFunction(test.arguments[2])) test.arguments[2] = test.arguments[2](this.sequelize);
}
QueryGenerator.options = context.options;
QueryGenerator.options = _.assign(context.options, { timezone: '+00:00' });
QueryGenerator._dialect = this.sequelize.dialect;
var conditions = QueryGenerator[suiteTitle].apply(QueryGenerator, test.arguments);
expect(conditions).to.deep.equal(test.expectation);
......
......@@ -658,6 +658,7 @@ if (dialect.match(/^postgres/)) {
var period = [new Date(2015, 0, 1), new Date(2015, 11, 31)];
return this.User.create({ username: 'user', email: ['foo@bar.com'], course_period: period}).then(function(newUser) {
// Check to see if the default value for a range field works
expect(newUser.acceptable_marks.length).to.equal(2);
expect(newUser.acceptable_marks[0]).to.equal(0.65); // lower bound
expect(newUser.acceptable_marks[1]).to.equal(1); // upper bound
......
......@@ -20,6 +20,7 @@ if (dialect.match(/^postgres/)) {
numbers: { type: DataTypes.ARRAY(DataTypes.FLOAT) },
document: { type: DataTypes.HSTORE, defaultValue: { default: '"value"' } }
});
return this.User.sync({ force: true });
});
......@@ -548,7 +549,7 @@ if (dialect.match(/^postgres/)) {
arguments: ['myTable', {data: new Buffer('Sequelize') }],
expectation: "INSERT INTO \"myTable\" (\"data\") VALUES (E'\\\\x53657175656c697a65');"
}, {
arguments: ['myTable', {name: 'foo', numbers: new Uint8Array([1, 2, 3])}],
arguments: ['myTable', {name: 'foo', numbers: [1, 2, 3]}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"numbers\") VALUES ('foo',ARRAY[1,2,3]);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1}],
......@@ -601,7 +602,7 @@ if (dialect.match(/^postgres/)) {
expectation: "INSERT INTO myTable (name,birthday) VALUES ('foo','2011-03-27 10:01:55.000 +00:00');",
context: {options: {quoteIdentifiers: false}}
}, {
arguments: ['myTable', {name: 'foo', numbers: new Uint8Array([1, 2, 3])}],
arguments: ['myTable', {name: 'foo', numbers: [1, 2, 3]}],
expectation: "INSERT INTO myTable (name,numbers) VALUES ('foo',ARRAY[1,2,3]);",
context: {options: {quoteIdentifiers: false}}
}, {
......@@ -744,7 +745,7 @@ if (dialect.match(/^postgres/)) {
arguments: ['myTable', {bar: 2}, {name: 'foo'}, { returning: true }],
expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\" = 'foo' RETURNING *"
}, {
arguments: ['myTable', {numbers: new Uint8Array([1, 2, 3])}, {name: 'foo'}],
arguments: ['myTable', {numbers: [1, 2, 3]}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"numbers\"=ARRAY[1,2,3] WHERE \"name\" = 'foo'"
}, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}],
......@@ -802,7 +803,7 @@ if (dialect.match(/^postgres/)) {
expectation: "UPDATE myTable SET bar=2 WHERE name = 'foo'",
context: {options: {quoteIdentifiers: false}}
}, {
arguments: ['myTable', {numbers: new Uint8Array([1, 2, 3])}, {name: 'foo'}],
arguments: ['myTable', {numbers: [1, 2, 3]}, {name: 'foo'}],
expectation: "UPDATE myTable SET numbers=ARRAY[1,2,3] WHERE name = 'foo'",
context: {options: {quoteIdentifiers: false}}
}, {
......@@ -942,11 +943,12 @@ if (dialect.match(/^postgres/)) {
it(title, function() {
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var context = test.context || {options: {}};
if (test.needsSequelize) {
if (_.isFunction(test.arguments[1])) test.arguments[1] = test.arguments[1](this.sequelize);
if (_.isFunction(test.arguments[2])) test.arguments[2] = test.arguments[2](this.sequelize);
}
QueryGenerator.options = context.options;
QueryGenerator.options = _.assign(context.options, { timezone: '+00:00' });
QueryGenerator._dialect = this.sequelize.dialect;
var conditions = QueryGenerator[suiteTitle].apply(QueryGenerator, test.arguments);
expect(conditions).to.deep.equal(test.expectation);
......
......@@ -57,8 +57,9 @@ if (dialect.match(/^postgres/)) {
});
it('should handle date values', function () {
expect(range.stringify([new Date(Date.UTC(2000, 1, 1)),
new Date(Date.UTC(2000, 1, 2))])).to.equal('("2000-02-01T00:00:00.000Z","2000-02-02T00:00:00.000Z")');
var Range = new DataTypes.postgres.RANGE(DataTypes.DATE);
expect(Range.stringify([new Date(Date.UTC(2000, 1, 1)),
new Date(Date.UTC(2000, 1, 2))], { timezone: '+02:00' })).to.equal('\'("2000-02-01 02:00:00.000 +02:00","2000-02-02 02:00:00.000 +02:00")\'');
});
});
......@@ -80,8 +81,12 @@ if (dialect.match(/^postgres/)) {
var testRange = [5,10];
testRange.inclusive = [true, true];
expect(range.parse(range.stringify(testRange), DataTypes.RANGE(DataTypes.INTEGER))).to.deep.equal(testRange);
expect(range.parse(range.stringify(range.parse(range.stringify(testRange), DataTypes.RANGE(DataTypes.INTEGER))), DataTypes.RANGE(DataTypes.INTEGER))).to.deep.equal(testRange);
var Range = new DataTypes.postgres.RANGE(DataTypes.INTEGER);
var stringified = Range.stringify(testRange, {});
stringified = stringified.substr(1, stringified.length - 2); // Remove the escaping ticks
expect(DataTypes.postgres.RANGE.parse(stringified, 3904, function () { return DataTypes.postgres.INTEGER.parse; })).to.deep.equal(testRange);
});
});
});
......
......@@ -10,9 +10,21 @@ if (dialect === 'sqlite') {
describe('[SQLITE Specific] DAO', function() {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING
username: DataTypes.STRING,
dateField: {
type: DataTypes.DATE,
field: 'date_field'
}
});
return this.User.sync({ force: true });
this.Project = this.sequelize.define('project', {
dateField: {
type: DataTypes.DATE,
field: 'date_field'
}
});
this.User.hasMany(this.Project);
return this.sequelize.sync({ force: true });
});
describe('findAll', function() {
......@@ -25,13 +37,39 @@ if (dialect === 'sqlite') {
return user.save().then(function() {
return self.User.create({ username: 'new user' }).then(function() {
return self.User.findAll({
where: ['createdAt > ?', new Date(2012, 1, 1)]
where: { createdAt: { $gt: new Date(2012, 1, 1) }}
}).then(function(users) {
expect(users).to.have.length(1);
});
});
});
});
it('handles dates with aliasses correctly #3611', function() {
return this.User.create({
dateField: new Date(2010, 10, 10)
}).bind(this).then(function () {
return this.User.findAll().get(0);
}).then(function (user) {
expect(user.get('dateField')).to.be.an.instanceof(Date);
expect(user.get('dateField')).to.equalTime(new Date(2010, 10, 10));
});
});
it('handles dates in includes correctly #2644', function() {
return this.User.create({
projects: [
{ dateField: new Date(1990, 5, 5) }
]
}, { include: [this.Project]}).bind(this).then(function () {
return this.User.findAll({
include: [this.Project]
}).get(0);
}).then(function (user) {
expect(user.projects[0].get('dateField')).to.be.an.instanceof(Date);
expect(user.projects[0].get('dateField')).to.equalTime(new Date(1990, 5, 5));
});
});
});
describe('regression tests', function() {
......
......@@ -529,7 +529,7 @@ if (dialect === 'sqlite') {
if (_.isFunction(test.arguments[1])) test.arguments[1] = test.arguments[1](this.sequelize);
if (_.isFunction(test.arguments[2])) test.arguments[2] = test.arguments[2](this.sequelize);
}
QueryGenerator.options = context.options;
QueryGenerator.options = _.assign(context.options, { timezone: '+00:00' });
QueryGenerator._dialect = this.sequelize.dialect;
var conditions = QueryGenerator[suiteTitle].apply(QueryGenerator, test.arguments);
expect(conditions).to.deep.equal(test.expectation);
......
......@@ -846,7 +846,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
logging: function(sql) {
test = true;
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1);
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1);
expect(sql.indexOf('ARRAY[]::VARCHAR(255)[]')).to.be.above(-1);
}
});
}).then(function() {
......@@ -874,7 +874,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
logging: function(sql) {
test = true;
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1);
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1);
expect(sql.indexOf('ARRAY[]::VARCHAR(255)[]')).to.be.above(-1);
}
});
});
......
......@@ -23,6 +23,20 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});
it('works with aliases fields', function () {
var Pub = this.sequelize.define('Pub', {
location: {field: 'coordinates', type: DataTypes.GEOMETRY}
})
, point = {type: 'Point', coordinates: [39.807222, -76.984722]};
return Pub.sync({ force: true }).then(function () {
return Pub.create({location: point});
}).then(function (pub) {
expect(pub).not.to.be.null;
expect(pub.location).to.be.deep.eql(point);
});
});
it('should create a geometry object', function() {
var User = this.User;
var point = { type: 'Point', coordinates: [39.807222,-76.984722]};
......
......@@ -18,9 +18,9 @@ var chai = require('chai')
var qq = function(str) {
if (dialect === 'postgres' || dialect === 'sqlite' || dialect === 'mssql') {
if (dialect === 'postgres' || dialect === 'mssql') {
return '"' + str + '"';
} else if (Support.dialectIsMySQL()) {
} else if (Support.dialectIsMySQL() || dialect === 'sqlite') {
return '`' + str + '`';
} else {
return str;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!