Skip to content

Commit

Permalink
Fix knex#1733, knex#920, incorrect postgres array bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser authored and Ruben Slabbert committed Nov 9, 2016
1 parent 23570f8 commit 73e23a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@
# Master (Unreleased)
- Add more having* methods / join clause on* methods #1674

# 0.12.4 - 12 Oct, 2016
- Fix #1733, #920, incorrect postgres array bindings

# 0.12.3 - 9 Oct, 2016
- Fix #1703, #1694 - connections should be returned to pool if acquireConnectionTimeout is triggered
- Fix #1710 regression in postgres array escaping
Expand Down
20 changes: 19 additions & 1 deletion src/dialects/postgres/index.js
Expand Up @@ -52,7 +52,7 @@ assign(Client_PG.prototype, {

_escapeBinding: makeEscape({
escapeArray(val, esc) {
return "'{" + val.map(esc).join(',') + "}'"
return esc(arrayString(val, esc))
},
escapeString(str) {
let hasBackslash = false
Expand Down Expand Up @@ -223,4 +223,22 @@ assign(Client_PG.prototype, {

})

function arrayString(arr, esc) {
let result = '{'
for (let i = 0 ; i < arr.length; i++) {
if (i > 0) result += ','
const val = arr[i]
if (val === null || typeof val === 'undefined') {
result += 'NULL'
} else if (Array.isArray(val)) {
result += arrayString(val, esc)
} else if (typeof val === 'number') {
result += val
} else {
result += JSON.stringify(typeof val === 'string' ? val : esc(val))
}
}
return result + '}'
}

export default Client_PG
6 changes: 6 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -3979,6 +3979,12 @@ describe("QueryBuilder", function() {
testquery(qb().select('*').from('sometable').where('array_field', '&&', [7]), {
postgres: "select * from \"sometable\" where \"array_field\" && '{7}'"
});
testquery(qb().select('*').from('sometable').where('array_field', '&&', ['abc', 'def']), {
postgres: "select * from \"sometable\" where \"array_field\" && '{\"abc\",\"def\"}'"
});
testquery(qb().select('*').from('sometable').where('array_field', '&&', ['abc', 'def', ['g', 2]]), {
postgres: "select * from \"sometable\" where \"array_field\" && '{\"abc\",\"def\",{\"g\",2}}'"
});
})

});

0 comments on commit 73e23a7

Please sign in to comment.