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

You need to sign in or sign up before continuing.
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 = {
}]
}).include,
model: topInclude.through.model,
where: self.sequelize.and(
where: { $and: [
self.sequelize.asIs([
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyAttributes[0]),
self.quoteIdentifier(topInclude.through.model.name) + '.' + self.quoteIdentifier(topInclude.association.identifierField)
].join(' = ')),
topInclude.through.where
),
]},
limit: 1,
includeIgnoreAttributes: false
}, topInclude.through.model);
......@@ -1290,9 +1290,7 @@ var QueryGenerator = {
'IS NOT NULL'
].join(' '));
if (options.where instanceof Utils.and) {
options.where.args.push(subQueryWhere);
} else if (Utils._.isPlainObject(options.where)) {
if (Utils._.isPlainObject(options.where)) {
options.where['__' + as] = subQueryWhere;
} else {
options.where = { $and: [options.where, subQueryWhere] };
......@@ -1700,17 +1698,7 @@ var QueryGenerator = {
var self = this
, result;
if ((smth instanceof Utils.and) || (smth instanceof Utils.or)) {
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) {
if (smth instanceof Utils.where) {
var value = smth.logic
, key;
......@@ -1894,10 +1882,7 @@ var QueryGenerator = {
}
if (key === undefined) {
if (value instanceof Utils.or || value instanceof Utils.and) {
key = value instanceof Utils.or ? '$or' : '$and';
value = value.args;
} else if (typeof value === 'string') {
if (typeof value === 'string') {
return value;
}
}
......@@ -2130,21 +2115,23 @@ var QueryGenerator = {
value = this.escape(value, field);
}
if (key._isSequelizeMethod) {
key = this.handleSequelizeMethod(key);
} else {
key = this.quoteIdentifier(key);
}
if (options.prefix) {
if (options.prefix instanceof Utils.literal) {
key = [this.handleSequelizeMethod(options.prefix), key].join('.');
if (key) {
if (key._isSequelizeMethod) {
key = this.handleSequelizeMethod(key);
} 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 = {
}
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);
} else if (Utils._.isPlainObject(smth)) {
return self.whereItemsQuery(smth, {
......@@ -2207,7 +2188,7 @@ var QueryGenerator = {
} else if (Array.isArray(smth)) {
if (smth.length === 0) return '1=1';
if (Utils.canTreatArrayAsAnd(smth)) {
var _smth = self.sequelize.and.apply(null, smth);
var _smth = { $and: smth };
result = self.getWhereConditions(_smth, tableName, factory, options, prepend);
} else {
result = Utils.format(smth, this.dialect);
......
......@@ -804,6 +804,7 @@ Instance.prototype.updateAttributes = Instance.prototype.update;
*
* @return {Promise<undefined>}
*/
Instance.prototype.destroy = function(options) {
options = Utils._.extend({
hooks: true,
......
......@@ -141,7 +141,7 @@ var paranoidClause = function(model, options) {
if (Utils._.isEmpty(options.where)) {
options.where = deletedAtObject;
} else {
options.where = model.sequelize.and(deletedAtObject, options.where);
options.where = { $and: [deletedAtObject, options.where] };
}
return options;
......@@ -1214,17 +1214,22 @@ Model.prototype.all = function(options) {
* __Queries using OR__
* ```js
* Model.findAll({
* where: Sequelize.and(
* { name: 'a project' },
* Sequelize.or(
* { id: [1,2,3] },
* { id: { gt: 10 } }
* )
* )
* })
* where: {
* name: 'a project',
* $or: [
* {id: [1, 2, 3]},
* {
* $and: [
* {id: {gt: 10}},
* {id: {lt: 100}}
* ]
* }
* ]
* }
* });
* ```
* ```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.
......@@ -1258,6 +1263,7 @@ Model.prototype.all = function(options) {
* @return {Promise<Array<Instance>>}
* @alias all
*/
Model.prototype.findAll = function(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');
......
......@@ -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.raw = true;
......
......@@ -1019,7 +1019,7 @@ Sequelize.literal = Sequelize.asIs = Sequelize.prototype.asIs = Sequelize.protot
* @return {Sequelize.and}
*/
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() {
* @return {Sequelize.or}
*/
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) {
*
* @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|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
* @alias condition
* @since v2.0.0-dev3
......
......@@ -95,15 +95,6 @@ var Utils = module.exports = {
, attribute
, 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) {
for (attribute in attributes) {
rawAttribute = Model.rawAttributes[attribute];
......@@ -168,7 +159,7 @@ var Utils = module.exports = {
if (treatAsAnd) {
return treatAsAnd;
} else {
return !(arg instanceof Date) && ((arg instanceof Utils.and) || (arg instanceof Utils.or) || Utils._.isPlainObject(arg));
return Utils._.isPlainObject(arg);
}
}, false);
},
......@@ -342,14 +333,6 @@ var Utils = module.exports = {
this.val = val;
},
and: function(args) {
this.args = args;
},
or: function(args) {
this.args = args;
},
json: function(conditionsOrPath, value) {
if (Utils._.isObject(conditionsOrPath)) {
this.conditions = conditionsOrPath;
......@@ -386,8 +369,6 @@ var Utils = module.exports = {
}
};
Utils.and.prototype._isSequelizeMethod =
Utils.or.prototype._isSequelizeMethod =
Utils.where.prototype._isSequelizeMethod =
Utils.literal.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!