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

Commit 6f8dabc7 by Mick Hansen

fix(sql/where): fixes regression for /: [], now correctly generates (NULL) rathe…

…r than (), closes #3132, closes #3105
1 parent 2a8e5f17
# Next
- [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132)
# 2.0.2 # 2.0.2
- [BUG] Fixed regression with `DataTypes.ARRAY(DataTypes.STRING(length))` [#3106](https://github.com/sequelize/sequelize/issues/3106) - [BUG] Fixed regression with `DataTypes.ARRAY(DataTypes.STRING(length))` [#3106](https://github.com/sequelize/sequelize/issues/3106)
- [BUG] Fixed regression where `.or([{key: value}, {key: value, key2: value}])` would result in 3 `A OR B OR C` rather than `A OR (B AND C)` [#3107](https://github.com/sequelize/sequelize/issues/3107) - [BUG] Fixed regression where `.or([{key: value}, {key: value, key2: value}])` would result in 3 `A OR B OR C` rather than `A OR (B AND C)` [#3107](https://github.com/sequelize/sequelize/issues/3107)
......
...@@ -1643,6 +1643,7 @@ module.exports = (function() { ...@@ -1643,6 +1643,7 @@ module.exports = (function() {
return this.handleSequelizeMethod(value); return this.handleSequelizeMethod(value);
} }
// Convert where: [] to $and if possible, else treat as literal/replacements
if (key === undefined && Array.isArray(value)) { if (key === undefined && Array.isArray(value)) {
if (Utils.canTreatArrayAsAnd(value)) { if (Utils.canTreatArrayAsAnd(value)) {
key = '$and'; key = '$and';
...@@ -1701,15 +1702,32 @@ module.exports = (function() { ...@@ -1701,15 +1702,32 @@ module.exports = (function() {
})(); })();
} }
// Do [] to $in/$notIn normalization
if (value && !fieldType || !fieldType instanceof DataTypes.ARRAY) {
if (Array.isArray(value)) {
value = {
$in: value
};
} else if (value.$not && Array.isArray(value.$not)) {
value.$notIn = value.$not;
delete value.$not;
}
}
// Setup keys and comparators // Setup keys and comparators
if (Array.isArray(value) && fieldType instanceof DataTypes.ARRAY) { if (Array.isArray(value) && fieldType instanceof DataTypes.ARRAY) {
value = this.escape(value, field); value = this.escape(value, field);
} else if (value && (value.$in || Array.isArray(value) || (value.$not && Array.isArray(value.$not)) || value.$notIn)) { } else if (value && (value.$in || value.$notIn)) {
comparator = 'IN'; comparator = 'IN';
if (value.$not || value.$notIn) comparator = 'NOT IN'; if (value.$notIn) comparator = 'NOT IN';
value = '('+(value.$in || value.$not || value.$notIn || value).map(function (item) {
return self.escape(item); if ((value.$in || value.$notIn).length) {
}).join(', ')+')'; value = '('+(value.$in || value.$notIn).map(function (item) {
return self.escape(item);
}).join(', ')+')';
} else {
value = '(NULL)';
}
} else if (value && value.$any) { } else if (value && value.$any) {
comparator = '= ANY'; comparator = '= ANY';
if (value.$any.$values) { if (value.$any.$values) {
......
...@@ -95,6 +95,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -95,6 +95,12 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
default: '[equipment] IN (1, 3)' default: '[equipment] IN (1, 3)'
}); });
testsql('equipment', {
$in: []
}, {
default: '[equipment] IN (NULL)'
});
testsql('muscles', { testsql('muscles', {
in: [2, 4] in: [2, 4]
}, { }, {
...@@ -139,6 +145,14 @@ suite(Support.getTestDialectTeaser('SQL'), function() { ...@@ -139,6 +145,14 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
}); });
}); });
suite('$notIn', function () {
testsql('equipment', {
$notIn: []
}, {
default: '[equipment] NOT IN (NULL)'
});
});
suite('$and/$or', function () { suite('$and/$or', function () {
suite('$or', function () { suite('$or', function () {
testsql('email', { testsql('email', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!