Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/associations.md
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ Comment.prototype.getItem = function(options) {
return this[
'get' +
this.get('commentable')
.substr(0, 1)
[0]
.toUpperCase() +
this.get('commentable').substr(1)
](options);
Expand Down
32 changes: 19 additions & 13 deletions lib/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ NUMBER.prototype.toSql = function toSql() {
};
NUMBER.prototype.validate = function(value) {
if (!Validator.isFloat(String(value))) {
throw new sequelizeErrors.ValidationError(util.format(`%j is not a valid ${_.toLower(this.key)}`, value));
throw new sequelizeErrors.ValidationError(util.format(`%j is not a valid ${this.key.toLowerCase()}`, value));
}

return true;
Expand Down Expand Up @@ -246,7 +246,7 @@ inherits(INTEGER, NUMBER);
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format(`%j is not a valid ${_.toLower(this.key)}`, value));
throw new sequelizeErrors.ValidationError(util.format(`%j is not a valid ${this.key.toLowerCase()}`, value));
}

return true;
Expand Down Expand Up @@ -406,24 +406,29 @@ DECIMAL.prototype.validate = function validate(value) {
return true;
};

for (const floating of [FLOAT, DOUBLE, REAL]) {
floating.prototype.escape = false;
floating.prototype._value = function _value(value) {
const protoExtensions = {
Comment thread
SimonSchick marked this conversation as resolved.
Resolved
escape: false,
_value(value) {
if (isNaN(value)) {
return 'NaN';
} else if (!isFinite(value)) {
}
if (!isFinite(value)) {
const sign = value < 0 ? '-' : '';
return `${sign}Infinity`;
}

return value;
};
floating.prototype._stringify = function _stringify(value) {
},
_stringify(value) {
return `'${this._value(value)}'`;
};
floating.prototype._bindParam = function _bindParam(value, options) {
},
_bindParam(value, options) {
return options.bindParam(this._value(value));
};
}
};

for (const floating of [FLOAT, DOUBLE, REAL]) {
Object.assign(floating.prototype, protoExtensions);
}

/**
Expand Down Expand Up @@ -896,9 +901,10 @@ VIRTUAL.prototype.key = VIRTUAL.key = 'VIRTUAL';
* @namespace DataTypes.ENUM
*
*/
function ENUM(value) {
function ENUM(...args) {
const value = args[0];
const options = typeof value === 'object' && !Array.isArray(value) && value || {
values: Array.prototype.slice.call(arguments).reduce((result, element) => {
values: args.reduce((result, element) => {
return result.concat(Array.isArray(element) ? element : [element]);
}, [])
};
Expand Down
2 changes: 1 addition & 1 deletion lib/deferrable.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ const Deferrable = module.exports = { // eslint-disable-line
NOT: classToInvokable(NOT),
SET_DEFERRED: classToInvokable(SET_DEFERRED),
SET_IMMEDIATE: classToInvokable(SET_IMMEDIATE)
};
};
10 changes: 5 additions & 5 deletions lib/dialects/abstract/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ class ConnectionManager {
try {
if (this.sequelize.config.dialectModulePath) {
return require(this.sequelize.config.dialectModulePath);
} else if (this.sequelize.config.dialectModule) {
}
if (this.sequelize.config.dialectModule) {
return this.sequelize.config.dialectModule;
} else {
return require(moduleName);
}
return require(moduleName);

} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
if (this.sequelize.config.dialectModulePath) {
throw new Error(`Unable to find dialect at ${this.sequelize.config.dialectModulePath}`);
} else {
throw new Error(`Please install ${moduleName} package manually`);
}
throw new Error(`Please install ${moduleName} package manually`);
}

throw err;
Expand Down
149 changes: 79 additions & 70 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,35 +498,35 @@ class QueryGenerator {
const fieldsSql = options.fields.map(field => {
if (typeof field === 'string') {
return this.quoteIdentifier(field);
} else if (field instanceof Utils.SequelizeMethod) {
}
if (field instanceof Utils.SequelizeMethod) {
return this.handleSequelizeMethod(field);
} else {
let result = '';

if (field.attribute) {
field.name = field.attribute;
}
}
let result = '';

if (!field.name) {
throw new Error(`The following index field has no name: ${util.inspect(field)}`);
}
if (field.attribute) {
field.name = field.attribute;
}

result += this.quoteIdentifier(field.name);
if (!field.name) {
throw new Error(`The following index field has no name: ${util.inspect(field)}`);
}

if (this._dialect.supports.index.collate && field.collate) {
result += ` COLLATE ${this.quoteIdentifier(field.collate)}`;
}
result += this.quoteIdentifier(field.name);

if (this._dialect.supports.index.length && field.length) {
result += `(${field.length})`;
}
if (this._dialect.supports.index.collate && field.collate) {
result += ` COLLATE ${this.quoteIdentifier(field.collate)}`;
}

if (field.order) {
result += ` ${field.order}`;
}
if (this._dialect.supports.index.length && field.length) {
result += `(${field.length})`;
}

return result;
if (field.order) {
result += ` ${field.order}`;
}

return result;
});

if (!options.name) {
Expand Down Expand Up @@ -840,16 +840,18 @@ class QueryGenerator {
}, this);

return sql;
} else if (collection._modelAttribute) {
}
if (collection._modelAttribute) {
return `${this.quoteTable(collection.Model.name)}.${this.quoteIdentifier(collection.fieldName)}`;
} else if (collection instanceof Utils.SequelizeMethod) {
}
if (collection instanceof Utils.SequelizeMethod) {
return this.handleSequelizeMethod(collection);
} else if (_.isPlainObject(collection) && collection.raw) {
}
if (_.isPlainObject(collection) && collection.raw) {
// simple objects with raw is no longer supported
throw new Error('The `{raw: "..."}` syntax is no longer supported. Use `sequelize.literal` instead.');
} else {
throw new Error(`Unknown structure passed to order / group: ${util.inspect(collection)}`);
}
throw new Error(`Unknown structure passed to order / group: ${util.inspect(collection)}`);
}

/**
Expand Down Expand Up @@ -1442,9 +1444,11 @@ class QueryGenerator {

attrAs = attr[1];
attr = attr[0];
} else if (attr instanceof Utils.Literal) {
}
if (attr instanceof Utils.Literal) {
return attr.val; // We trust the user to rename the field correctly
} else if (attr instanceof Utils.Cast || attr instanceof Utils.Fn) {
}
if (attr instanceof Utils.Cast || attr instanceof Utils.Fn) {
throw new Error(
'Tried to select attributes using Sequelize.cast or Sequelize.fn without specifying an alias for the result, during eager loading. ' +
'This means the attribute will not be added to the returned instance'
Expand Down Expand Up @@ -1950,9 +1954,10 @@ class QueryGenerator {
if (value && value instanceof Utils.SequelizeMethod) {
value = this.getWhereConditions(value, tableName, factory, options, prepend);

result = value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
} else if (_.isPlainObject(value)) {
result = this.whereItemQuery(smth.attribute, value, {
return value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
}
if (_.isPlainObject(value)) {
return this.whereItemQuery(smth.attribute, value, {
model: factory
});
} else {
Expand All @@ -1962,11 +1967,13 @@ class QueryGenerator {
value = this.escape(value);
}

result = value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
return value === 'NULL' ? `${key} IS NULL` : [key, value].join(` ${smth.comparator} `);
}
} else if (smth instanceof Utils.Literal) {
result = smth.val;
} else if (smth instanceof Utils.Cast) {
}
if (smth instanceof Utils.Literal) {
return smth.val;
}
if (smth instanceof Utils.Cast) {
if (smth.val instanceof Utils.SequelizeMethod) {
result = this.handleSequelizeMethod(smth.val, tableName, factory, options, prepend);
} else if (_.isPlainObject(smth.val)) {
Expand All @@ -1975,31 +1982,29 @@ class QueryGenerator {
result = this.escape(smth.val);
}

result = `CAST(${result} AS ${smth.type.toUpperCase()})`;
} else if (smth instanceof Utils.Fn) {
result = `${smth.fn}(${smth.args.map(arg => {
return `CAST(${result} AS ${smth.type.toUpperCase()})`;
}
if (smth instanceof Utils.Fn) {
return `${smth.fn}(${smth.args.map(arg => {
if (arg instanceof Utils.SequelizeMethod) {
return this.handleSequelizeMethod(arg, tableName, factory, options, prepend);
} else if (_.isPlainObject(arg)) {
}
if (_.isPlainObject(arg)) {
return this.whereItemsQuery(arg);
} else {
return this.escape(arg);
}
return this.escape(arg);
}).join(', ') })`;
} else if (smth instanceof Utils.Col) {
if (Array.isArray(smth.col)) {
if (!factory) {
throw new Error('Cannot call Sequelize.col() with array outside of order / group clause');
}
} else if (smth.col.startsWith('*')) {
}
if (smth instanceof Utils.Col) {
if (Array.isArray(smth.col) && !factory) {
throw new Error('Cannot call Sequelize.col() with array outside of order / group clause');
}
if (smth.col.startsWith('*')) {
return '*';
}
return this.quote(smth.col, factory);
} else {
result = smth.toString(this, factory);
}

return result;
return smth.toString(this, factory);
}

whereQuery(where, options) {
Expand Down Expand Up @@ -2027,7 +2032,7 @@ class QueryGenerator {
const items = [];

binding = binding || 'AND';
if (binding.substr(0, 1) !== ' ') binding = ` ${binding} `;
if (binding[0] !== ' ') binding = ` ${binding} `;

if (_.isPlainObject(where)) {
Utils.getComplexKeys(where).forEach(prop => {
Expand Down Expand Up @@ -2330,7 +2335,7 @@ class QueryGenerator {
if (prop === Op.not) {
if (Array.isArray(value)) {
prop = Op.notIn;
} else if (![null, true, false].includes(value)) {
} else if (value !== null && value !== true && value !== false) {
prop = Op.ne;
}
}
Expand Down Expand Up @@ -2410,7 +2415,8 @@ class QueryGenerator {

if (value === null && comparator === this.OperatorMap[Op.eq]) {
return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.is], options.prefix);
} else if (value === null && comparator === this.OperatorMap[Op.ne]) {
}
if (value === null && comparator === this.OperatorMap[Op.ne]) {
return this._joinKeyValue(key, this.escape(value, field, escapeOptions), this.OperatorMap[Op.not], options.prefix);
}

Expand All @@ -2422,7 +2428,6 @@ class QueryGenerator {
@private
*/
getWhereConditions(smth, tableName, factory, options, prepend) {
let result = null;
const where = {};

if (Array.isArray(tableName)) {
Expand All @@ -2439,14 +2444,16 @@ class QueryGenerator {
}

if (smth && smth instanceof Utils.SequelizeMethod) { // Checking a property is cheaper than a lot of instanceof calls
result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
} else if (_.isPlainObject(smth)) {
return this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
}
if (_.isPlainObject(smth)) {
return this.whereItemsQuery(smth, {
model: factory,
prefix: prepend && tableName,
type: options.type
});
} else if (typeof smth === 'number') {
}
if (typeof smth === 'number') {
let primaryKeys = factory ? Object.keys(factory.primaryKeys) : [];

if (primaryKeys.length > 0) {
Expand All @@ -2462,40 +2469,42 @@ class QueryGenerator {
model: factory,
prefix: prepend && tableName
});
} else if (typeof smth === 'string') {
}
if (typeof smth === 'string') {
return this.whereItemsQuery(smth, {
model: factory,
prefix: prepend && tableName
});
} else if (Buffer.isBuffer(smth)) {
result = this.escape(smth);
} else if (Array.isArray(smth)) {
}
if (Buffer.isBuffer(smth)) {
return this.escape(smth);
}
if (Array.isArray(smth)) {
if (smth.length === 0 || smth.length > 0 && smth[0].length === 0) return '1=1';
if (Utils.canTreatArrayAsAnd(smth)) {
const _smth = { [Op.and]: smth };
result = this.getWhereConditions(_smth, tableName, factory, options, prepend);
} else {
throw new Error('Support for literal replacements in the `where` object has been removed.');
return this.getWhereConditions(_smth, tableName, factory, options, prepend);
}
} else if (smth === null) {
throw new Error('Support for literal replacements in the `where` object has been removed.');
}
if (smth === null) {
return this.whereItemsQuery(smth, {
model: factory,
prefix: prepend && tableName
});
}

return result ? result : '1=1';
return '1=1';
}

// A recursive parser for nested where conditions
parseConditionObject(conditions, path) {
path = path || [];
return _.reduce(conditions, (result, value, key) => {
if (_.isObject(value)) {
result = result.concat(this.parseConditionObject(value, path.concat(key))); // Recursively parse objects
} else {
result.push({ path: path.concat(key), value });
return result.concat(this.parseConditionObject(value, path.concat(key))); // Recursively parse objects
}
result.push({ path: path.concat(key), value });
return result;
}, []);
}
Expand Down
Loading