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

Commit 03b32d74 by Mick Hansen

refactor(sql/where): further integration of new where generator

1 parent b1f106ca
......@@ -1544,10 +1544,11 @@ module.exports = (function() {
var self = this
, items = [];
binding = ' '+ (binding || 'AND') + ' ';
binding = binding || 'AND';
if (binding.substr(0, 1) !== ' ') binding = ' '+binding+' ';
if (_.isPlainObject(where)) {
_.forIn(where, function (value, key) {
_.forOwn(where, function (value, key) {
items.push(self.whereItemQuery(key, value, options));
});
} else {
......@@ -1635,28 +1636,47 @@ module.exports = (function() {
}
}
if (value instanceof Utils.literal) {
if (value instanceof Utils.literal || value instanceof Utils.where) {
return this.handleSequelizeMethod(value);
}
if (key === undefined && Array.isArray(value)) {
if (Utils.canTreatArrayAsAnd(value)) {
key = '$and';
} else {
return Utils.format(value, this.dialect);
}
}
// OR/AND grouping logic
if (key === '$or' || key === '$and') {
binding = key === '$or' ? 'OR' : 'AND';
binding = key === '$or' ? ' OR ' : ' AND ';
if (Array.isArray(value)) {
return '('+value.map(function (item) {
value = value.map(function (item) {
return self.whereItemsQuery(item, options, binding);
}).join(' '+binding+' ')+')';
}).filter(function (item) {
return item && item.length;
});
return value.length ? '('+value.join(binding)+')' : undefined;
} else {
return '('+self.whereItemsQuery(value, options, binding)+')';
value = self.whereItemsQuery(value, options, binding);
return value ? '('+value+')' : undefined;
}
}
if (value) {
if (value.$or || value.$and) {
return '('+value.$or.map(function (_value) {
return self.whereItemQuery(key, _value, options);
}).join(value.$or ? ' OR ' : ' AND ')+')';
binding = value.$or ? ' OR ' : ' AND ';
value = value.$or.map(function (_value) {
return self.whereItemQuery(key, _value, options);
}).filter(function (item) {
return item && item.length;
});
return value.length ? '('+value.join(binding)+')' : undefined;
}
}
......@@ -1745,6 +1765,12 @@ module.exports = (function() {
}
if (smth && smth._isSequelizeMethod === true) { // Checking a property is cheaper than a lot of instanceof calls
if (smth instanceof Utils.and || smth instanceof Utils.and) {
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, {
......@@ -1776,7 +1802,6 @@ module.exports = (function() {
result = this.escape(smth);
} else if (Array.isArray(smth)) {
if (smth.length === 0) return '1=1';
if (Utils.canTreatArrayAsAnd(smth)) {
var _smth = self.sequelize.and.apply(null, smth);
result = self.getWhereConditions(_smth, tableName, factory, options, prepend);
......
......@@ -1366,9 +1366,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
.then(function(pet) { return pet.destroy(); })
.then(function() {
return [
User.find({ where: user.id, include: Pet }),
User.find({ where: {id: user.id}, include: Pet }),
User.find({
where: user.id,
where: {id: user.id},
include: [{ model: Pet, paranoid: false }]
})
];
......
......@@ -102,6 +102,15 @@ suite('SQL', function() {
});
});
suite('Buffer', function () {
testsql('field', new Buffer('Sequelize'), {
postgres: '"field" = E\'\\\\x53657175656c697a65\'',
sqlite: "`field` = X'53657175656c697a65'",
mysql: "`field` = X'53657175656c697a65'",
mssql: "[field] = 0x'53657175656c697a65'"
});
});
suite('$not', function () {
testsql('deleted', {
$not: true
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!