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

Commit cec519bf by Mick Hansen

Merge pull request #1241 from yuri0/master

Fixed hashToWhereConditions and arrayValue in QueryGenerator to correctly handle in: and not: in where: conditions (fixes #1079)
2 parents 6b5472cb b0c3e46f
...@@ -1066,12 +1066,12 @@ module.exports = (function() { ...@@ -1066,12 +1066,12 @@ module.exports = (function() {
return joins; return joins;
}, },
arrayValue: function(value, key, _key){ arrayValue: function(value, key, _key, factory, logicResult){
var _value = null; var _value = null;
if (value.length === 0) { value = [null] } if (value.length === 0) { value = [null] }
_value = "(" + value.map(function(v) { return this.escape(v) }.bind(this)).join(',') + ")" _value = "(" + value.map(function(v) { return this.escape(v) }.bind(this)).join(',') + ")"
return [_key, _value].join(" IN ") return [_key, _value].join(" " + logicResult + " ")
}, },
/* /*
...@@ -1104,7 +1104,7 @@ module.exports = (function() { ...@@ -1104,7 +1104,7 @@ module.exports = (function() {
} }
if (Array.isArray(value)) { if (Array.isArray(value)) {
result.push(this.arrayValue(value, key, _key, dao)) result.push(this.arrayValue(value, key, _key, dao, "IN"))
} else if ((value) && (typeof value == 'object') && !(value instanceof Date) && !Buffer.isBuffer(value)) { } else if ((value) && (typeof value == 'object') && !(value instanceof Date) && !Buffer.isBuffer(value)) {
if (!!value.join) { if (!!value.join) {
//using as sentinel for join column => value //using as sentinel for join column => value
...@@ -1113,10 +1113,9 @@ module.exports = (function() { ...@@ -1113,10 +1113,9 @@ module.exports = (function() {
} else { } else {
for (var logic in value) { for (var logic in value) {
var logicResult = Utils.getWhereLogic(logic, hash[key][logic]); var logicResult = Utils.getWhereLogic(logic, hash[key][logic]);
if (logic === "IN" || logic === "NOT IN") { if (logicResult === "IN" || logicResult === "NOT IN") {
var values = Array.isArray(where[i][ii]) ? where[i][ii] : [where[i][ii]] var values = Array.isArray(value[logic]) ? value[logic] : [value[logic]]
_where[_where.length] = i + ' ' + logic + ' (' + values.map(function(){ return '?' }).join(',') + ')' result.push(this.arrayValue(values, key, _key, dao, logicResult))
_whereArgs = _whereArgs.concat(values)
} }
else if (logicResult === "BETWEEN" || logicResult === "NOT BETWEEN") { else if (logicResult === "BETWEEN" || logicResult === "NOT BETWEEN") {
_value = this.escape(value[logic][0]) _value = this.escape(value[logic][0])
......
...@@ -156,7 +156,7 @@ module.exports = (function() { ...@@ -156,7 +156,7 @@ module.exports = (function() {
attributes: attrString.join(', ') }) attributes: attrString.join(', ') })
}, },
arrayValue: function(value, key, _key, factory){ arrayValue: function(value, key, _key, factory, logicResult){
var col = null var col = null
, coltype = null , coltype = null
, _realKey = key.split('.').pop() , _realKey = key.split('.').pop()
...@@ -177,7 +177,7 @@ module.exports = (function() { ...@@ -177,7 +177,7 @@ module.exports = (function() {
return [_key, _value].join(" && ") return [_key, _value].join(" && ")
} else { } else {
_value = "(" + value.map(this.escape).join(',') + ")" _value = "(" + value.map(this.escape).join(',') + ")"
return [_key, _value].join(" IN ") return [_key, _value].join(" " + logicResult + " ")
} }
}, },
......
...@@ -906,6 +906,14 @@ if (dialect.match(/^postgres/)) { ...@@ -906,6 +906,14 @@ if (dialect.match(/^postgres/)) {
arguments: [{ id: [] }], arguments: [{ id: [] }],
expectation: "\"id\" IN (NULL)" expectation: "\"id\" IN (NULL)"
}, },
{
arguments: [{id: {not: [1, 2, 3] }}],
expectation: "\"id\" NOT IN (1,2,3)"
},
{
arguments: [{id: {not: [] }}],
expectation: "\"id\" NOT IN (NULL)"
},
// Variants when quoteIdentifiers is false // Variants when quoteIdentifiers is false
{ {
...@@ -918,6 +926,16 @@ if (dialect.match(/^postgres/)) { ...@@ -918,6 +926,16 @@ if (dialect.match(/^postgres/)) {
expectation: "id IN (NULL)", expectation: "id IN (NULL)",
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, },
{
arguments: [{ id: {not: [1,2,3] }}],
expectation: "id NOT IN (1,2,3)",
context: {options: {quoteIdentifiers: false}}
},
{
arguments: [{ id: {not: [] }}],
expectation: "id NOT IN (NULL)",
context: {options: {quoteIdentifiers: false}}
}
] ]
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!