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

You need to sign in or sign up before continuing.
Commit 03b32d74 by Mick Hansen

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

1 parent b1f106ca
...@@ -1544,10 +1544,11 @@ module.exports = (function() { ...@@ -1544,10 +1544,11 @@ module.exports = (function() {
var self = this var self = this
, items = []; , items = [];
binding = ' '+ (binding || 'AND') + ' '; binding = binding || 'AND';
if (binding.substr(0, 1) !== ' ') binding = ' '+binding+' ';
if (_.isPlainObject(where)) { if (_.isPlainObject(where)) {
_.forIn(where, function (value, key) { _.forOwn(where, function (value, key) {
items.push(self.whereItemQuery(key, value, options)); items.push(self.whereItemQuery(key, value, options));
}); });
} else { } else {
...@@ -1635,28 +1636,47 @@ module.exports = (function() { ...@@ -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); 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 // OR/AND grouping logic
if (key === '$or' || key === '$and') { if (key === '$or' || key === '$and') {
binding = key === '$or' ? 'OR' : 'AND'; binding = key === '$or' ? ' OR ' : ' AND ';
if (Array.isArray(value)) { if (Array.isArray(value)) {
return '('+value.map(function (item) { value = value.map(function (item) {
return self.whereItemsQuery(item, options, binding); return self.whereItemsQuery(item, options, binding);
}).join(' '+binding+' ')+')'; }).filter(function (item) {
return item && item.length;
});
return value.length ? '('+value.join(binding)+')' : undefined;
} else { } else {
return '('+self.whereItemsQuery(value, options, binding)+')'; value = self.whereItemsQuery(value, options, binding);
return value ? '('+value+')' : undefined;
} }
} }
if (value) { if (value) {
if (value.$or || value.$and) { if (value.$or || value.$and) {
return '('+value.$or.map(function (_value) { binding = value.$or ? ' OR ' : ' AND ';
return self.whereItemQuery(key, _value, options);
}).join(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() { ...@@ -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 && 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); 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, {
...@@ -1776,7 +1802,6 @@ module.exports = (function() { ...@@ -1776,7 +1802,6 @@ module.exports = (function() {
result = this.escape(smth); result = this.escape(smth);
} 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 = self.sequelize.and.apply(null, smth);
result = self.getWhereConditions(_smth, tableName, factory, options, prepend); result = self.getWhereConditions(_smth, tableName, factory, options, prepend);
......
...@@ -1366,9 +1366,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1366,9 +1366,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
.then(function(pet) { return pet.destroy(); }) .then(function(pet) { return pet.destroy(); })
.then(function() { .then(function() {
return [ return [
User.find({ where: user.id, include: Pet }), User.find({ where: {id: user.id}, include: Pet }),
User.find({ User.find({
where: user.id, where: {id: user.id},
include: [{ model: Pet, paranoid: false }] include: [{ model: Pet, paranoid: false }]
}) })
]; ];
......
...@@ -102,6 +102,15 @@ suite('SQL', function() { ...@@ -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 () { suite('$not', function () {
testsql('deleted', { testsql('deleted', {
$not: true $not: true
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!