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

Commit 465aa4cb by Jan Aagaard Meier

internals(/) Use plain and internally, instead of sequlize.or and sequelize.and

1 parent 8eb2cf19
...@@ -1225,13 +1225,13 @@ var QueryGenerator = { ...@@ -1225,13 +1225,13 @@ var QueryGenerator = {
}] }]
}).include, }).include,
model: topInclude.through.model, model: topInclude.through.model,
where: self.sequelize.and( where: { $and: [
self.sequelize.asIs([ self.sequelize.asIs([
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyAttributes[0]), self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyAttributes[0]),
self.quoteIdentifier(topInclude.through.model.name) + '.' + self.quoteIdentifier(topInclude.association.identifierField) self.quoteIdentifier(topInclude.through.model.name) + '.' + self.quoteIdentifier(topInclude.association.identifierField)
].join(' = ')), ].join(' = ')),
topInclude.through.where topInclude.through.where
), ]},
limit: 1, limit: 1,
includeIgnoreAttributes: false includeIgnoreAttributes: false
}, topInclude.through.model); }, topInclude.through.model);
...@@ -1290,9 +1290,7 @@ var QueryGenerator = { ...@@ -1290,9 +1290,7 @@ var QueryGenerator = {
'IS NOT NULL' 'IS NOT NULL'
].join(' ')); ].join(' '));
if (options.where instanceof Utils.and) { if (Utils._.isPlainObject(options.where)) {
options.where.args.push(subQueryWhere);
} else if (Utils._.isPlainObject(options.where)) {
options.where['__' + as] = subQueryWhere; options.where['__' + as] = subQueryWhere;
} else { } else {
options.where = { $and: [options.where, subQueryWhere] }; options.where = { $and: [options.where, subQueryWhere] };
...@@ -1700,17 +1698,7 @@ var QueryGenerator = { ...@@ -1700,17 +1698,7 @@ var QueryGenerator = {
var self = this var self = this
, result; , result;
if ((smth instanceof Utils.and) || (smth instanceof Utils.or)) { if (smth instanceof Utils.where) {
var connector = (smth instanceof Utils.and) ? ' AND ' : ' OR ';
result = smth.args.filter(function(arg) {
return arg !== undefined;
}).map(function(arg) {
return self.getWhereConditions(arg, tableName, factory, options, prepend);
}).join(connector);
result = result.length && '(' + result + ')' || undefined;
} else if (smth instanceof Utils.where) {
var value = smth.logic var value = smth.logic
, key; , key;
...@@ -1894,10 +1882,7 @@ var QueryGenerator = { ...@@ -1894,10 +1882,7 @@ var QueryGenerator = {
} }
if (key === undefined) { if (key === undefined) {
if (value instanceof Utils.or || value instanceof Utils.and) { if (typeof value === 'string') {
key = value instanceof Utils.or ? '$or' : '$and';
value = value.args;
} else if (typeof value === 'string') {
return value; return value;
} }
} }
...@@ -2130,21 +2115,23 @@ var QueryGenerator = { ...@@ -2130,21 +2115,23 @@ var QueryGenerator = {
value = this.escape(value, field); value = this.escape(value, field);
} }
if (key._isSequelizeMethod) { if (key) {
key = this.handleSequelizeMethod(key); if (key._isSequelizeMethod) {
} else { key = this.handleSequelizeMethod(key);
key = this.quoteIdentifier(key);
}
if (options.prefix) {
if (options.prefix instanceof Utils.literal) {
key = [this.handleSequelizeMethod(options.prefix), key].join('.');
} else { } else {
key = [this.quoteTable(options.prefix), key].join('.'); key = this.quoteIdentifier(key);
} }
}
return [key, value].join(' '+comparator+' '); if (options.prefix) {
if (options.prefix instanceof Utils.literal) {
key = [this.handleSequelizeMethod(options.prefix), key].join('.');
} else {
key = [this.quoteTable(options.prefix), key].join('.');
}
}
return [key, value].join(' '+comparator+' ');
}
return value;
}, },
/* /*
...@@ -2169,12 +2156,6 @@ var QueryGenerator = { ...@@ -2169,12 +2156,6 @@ var QueryGenerator = {
} }
if (smth && smth._isSequelizeMethod === true) { // Checking a property is cheaper than a lot of instanceof calls if (smth && smth._isSequelizeMethod === true) { // Checking a property is cheaper than a lot of instanceof calls
if (smth instanceof Utils.and || smth instanceof Utils.or) {
return self.whereItemsQuery(smth, {
model: factory,
prefix: prepend && tableName
});
}
result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend); result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
} else if (Utils._.isPlainObject(smth)) { } else if (Utils._.isPlainObject(smth)) {
return self.whereItemsQuery(smth, { return self.whereItemsQuery(smth, {
...@@ -2207,7 +2188,7 @@ var QueryGenerator = { ...@@ -2207,7 +2188,7 @@ var QueryGenerator = {
} else if (Array.isArray(smth)) { } else if (Array.isArray(smth)) {
if (smth.length === 0) return '1=1'; if (smth.length === 0) return '1=1';
if (Utils.canTreatArrayAsAnd(smth)) { if (Utils.canTreatArrayAsAnd(smth)) {
var _smth = self.sequelize.and.apply(null, smth); var _smth = { $and: smth };
result = self.getWhereConditions(_smth, tableName, factory, options, prepend); result = self.getWhereConditions(_smth, tableName, factory, options, prepend);
} else { } else {
result = Utils.format(smth, this.dialect); result = Utils.format(smth, this.dialect);
......
...@@ -804,6 +804,7 @@ Instance.prototype.updateAttributes = Instance.prototype.update; ...@@ -804,6 +804,7 @@ Instance.prototype.updateAttributes = Instance.prototype.update;
* *
* @return {Promise<undefined>} * @return {Promise<undefined>}
*/ */
Instance.prototype.destroy = function(options) { Instance.prototype.destroy = function(options) {
options = Utils._.extend({ options = Utils._.extend({
hooks: true, hooks: true,
......
...@@ -141,7 +141,7 @@ var paranoidClause = function(model, options) { ...@@ -141,7 +141,7 @@ var paranoidClause = function(model, options) {
if (Utils._.isEmpty(options.where)) { if (Utils._.isEmpty(options.where)) {
options.where = deletedAtObject; options.where = deletedAtObject;
} else { } else {
options.where = model.sequelize.and(deletedAtObject, options.where); options.where = { $and: [deletedAtObject, options.where] };
} }
return options; return options;
...@@ -1214,17 +1214,22 @@ Model.prototype.all = function(options) { ...@@ -1214,17 +1214,22 @@ Model.prototype.all = function(options) {
* __Queries using OR__ * __Queries using OR__
* ```js * ```js
* Model.findAll({ * Model.findAll({
* where: Sequelize.and( * where: {
* { name: 'a project' }, * name: 'a project',
* Sequelize.or( * $or: [
* { id: [1,2,3] }, * {id: [1, 2, 3]},
* { id: { gt: 10 } } * {
* ) * $and: [
* ) * {id: {gt: 10}},
* }) * {id: {lt: 100}}
* ]
* }
* ]
* }
* });
* ``` * ```
* ```sql * ```sql
* WHERE name = 'a project' AND (id` IN (1,2,3) OR id > 10) * WHERE `Model`.`name` = 'a project' AND (`Model`.`id` IN (1, 2, 3) OR (`Model`.`id` > 10 AND `Model`.`id` < 100));
* ``` * ```
* *
* The success listener is called with an array of instances if the query succeeds. * The success listener is called with an array of instances if the query succeeds.
...@@ -1258,6 +1263,7 @@ Model.prototype.all = function(options) { ...@@ -1258,6 +1263,7 @@ Model.prototype.all = function(options) {
* @return {Promise<Array<Instance>>} * @return {Promise<Array<Instance>>}
* @alias all * @alias all
*/ */
Model.prototype.findAll = function(options) { Model.prototype.findAll = function(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value'); throw new Error('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value');
......
...@@ -553,7 +553,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode ...@@ -553,7 +553,7 @@ QueryInterface.prototype.upsert = function(tableName, values, updateValues, mode
} }
}); });
where = this.sequelize.or.apply(this.sequelize, wheres); where = { $or: wheres };
options.type = QueryTypes.UPSERT; options.type = QueryTypes.UPSERT;
options.raw = true; options.raw = true;
......
...@@ -1019,7 +1019,7 @@ Sequelize.literal = Sequelize.asIs = Sequelize.prototype.asIs = Sequelize.protot ...@@ -1019,7 +1019,7 @@ Sequelize.literal = Sequelize.asIs = Sequelize.prototype.asIs = Sequelize.protot
* @return {Sequelize.and} * @return {Sequelize.and}
*/ */
Sequelize.and = Sequelize.prototype.and = function() { Sequelize.and = Sequelize.prototype.and = function() {
return new Utils.and(Utils.sliceArgs(arguments)); return { $and: Utils.sliceArgs(arguments) };
}; };
/** /**
...@@ -1032,7 +1032,7 @@ Sequelize.and = Sequelize.prototype.and = function() { ...@@ -1032,7 +1032,7 @@ Sequelize.and = Sequelize.prototype.and = function() {
* @return {Sequelize.or} * @return {Sequelize.or}
*/ */
Sequelize.or = Sequelize.prototype.or = function() { Sequelize.or = Sequelize.prototype.or = function() {
return new Utils.or(Utils.sliceArgs(arguments)); return { $or: Utils.sliceArgs(arguments) };
}; };
/** /**
...@@ -1060,7 +1060,7 @@ Sequelize.json = Sequelize.prototype.json = function (conditionsOrPath, value) { ...@@ -1060,7 +1060,7 @@ Sequelize.json = Sequelize.prototype.json = function (conditionsOrPath, value) {
* *
* @param {Object} attr The attribute, which can be either an attribute object from `Model.rawAttributes` or a sequelize object, for example an instance of `sequelize.fn`. For simple string attributes, use the POJO syntax * @param {Object} attr The attribute, which can be either an attribute object from `Model.rawAttributes` or a sequelize object, for example an instance of `sequelize.fn`. For simple string attributes, use the POJO syntax
* @param {string} [comparator='='] * @param {string} [comparator='=']
* @param {String|Object} logic The condition. Can be both a simply type, or a further condition (`.or`, `.and`, `.literal` etc.) * @param {String|Object} logic The condition. Can be both a simply type, or a further condition (`$or`, `$and`, `.literal` etc.)
* @method where * @method where
* @alias condition * @alias condition
* @since v2.0.0-dev3 * @since v2.0.0-dev3
......
...@@ -95,15 +95,6 @@ var Utils = module.exports = { ...@@ -95,15 +95,6 @@ var Utils = module.exports = {
, attribute , attribute
, rawAttribute; , rawAttribute;
if (options.where instanceof Utils.and || options.where instanceof Utils.or) {
attributes = undefined;
options.where.args = options.where.args.map(function (where) {
return Utils.mapOptionFieldNames({
where: where
}, Model).where;
});
}
if (attributes) { if (attributes) {
for (attribute in attributes) { for (attribute in attributes) {
rawAttribute = Model.rawAttributes[attribute]; rawAttribute = Model.rawAttributes[attribute];
...@@ -168,7 +159,7 @@ var Utils = module.exports = { ...@@ -168,7 +159,7 @@ var Utils = module.exports = {
if (treatAsAnd) { if (treatAsAnd) {
return treatAsAnd; return treatAsAnd;
} else { } else {
return !(arg instanceof Date) && ((arg instanceof Utils.and) || (arg instanceof Utils.or) || Utils._.isPlainObject(arg)); return Utils._.isPlainObject(arg);
} }
}, false); }, false);
}, },
...@@ -342,14 +333,6 @@ var Utils = module.exports = { ...@@ -342,14 +333,6 @@ var Utils = module.exports = {
this.val = val; this.val = val;
}, },
and: function(args) {
this.args = args;
},
or: function(args) {
this.args = args;
},
json: function(conditionsOrPath, value) { json: function(conditionsOrPath, value) {
if (Utils._.isObject(conditionsOrPath)) { if (Utils._.isObject(conditionsOrPath)) {
this.conditions = conditionsOrPath; this.conditions = conditionsOrPath;
...@@ -386,8 +369,6 @@ var Utils = module.exports = { ...@@ -386,8 +369,6 @@ var Utils = module.exports = {
} }
}; };
Utils.and.prototype._isSequelizeMethod =
Utils.or.prototype._isSequelizeMethod =
Utils.where.prototype._isSequelizeMethod = Utils.where.prototype._isSequelizeMethod =
Utils.literal.prototype._isSequelizeMethod = Utils.literal.prototype._isSequelizeMethod =
Utils.cast.prototype._isSequelizeMethod = Utils.cast.prototype._isSequelizeMethod =
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!