Escaping of bindings in v0.11 with PostgreSQL (or how to get v0.10 behavior?) #1602
Comments
Could you add an example query of your use case? |
Sure:
That used to be sent directly to Postresql library's |
@moll that doesn't seem like complete example? how pg driver should have interpreted passing empty instance? Did you use overriding val.toPostgres or something like that? Anyways I don't know any way to skip knex's prepare value... I don't have strong opinion if there should be way to do it... I don't know the motivation why knex has separate prepare values implementation. Maybe to have consistent functionality with knexQuery.toString() and with sending query through pg driver. |
In the example above interpreting value or identity objects is Pg's value preparation's responsibility. You could think of it as using As far as I can tell now, and correct me if I'm wrong, any custom object serialization, be it of value or identity objects, is not possible in Knex v0.11 because even if Knex is made extensible, Pg will still do its own serialization, thereby escaping serialized strings twice ( Let me know if I need to further elaborate. |
Can you show an example where escaping goes wrong? I was looking into the code and saw that ... but when I try it out there doesn't seem to do double escaping: knex = require('knex')({client:'pg'})
knex("foo").where('name', `'"foo"'`).toSQL()
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ '\'"foo"\'' ], // <--- no sql escaping what so ever (only the javascript escaping on ' inside string)
__knexQueryUid: '0ee379bb-0c66-47b8-8a00-f20ceb74dda4',
sql: 'select * from "foo" where "name" = ?' }
knex("foo").where('name', `'"foo"'`).toString()
'select * from "foo" where "name" = \'\'\'"foo"\'\'\'' // <-- here ' is escaped to '' as it should be ... |
A clarification: When I refer to |
The piece of code I linked to in my original post has The output of |
Result of It is a bit strange why Maybe |
Yeah this escaping behavior here is incorrect - just ran into this issue while updating some tests for the 1.0 refactor branch. The |
- We shouldn’t be testing the “default” client class. Replace any usages with postgresql - Update changelog
Closing in favor of #1661 |
* Modify test suite to test #1602 We shouldn’t be testing the “default” client class. Replace any usages with postgresql * Simplify knex.raw, deprecate global Knex.raw usage * Remove unused bluebird shim * Remove old / unused test comments * Don't capture Pool2 error event * Fix pg string escaping & parameterization #1602, #1548
Hey,
I'm upgrading from v0.10 to v0.11 and noticed non-plain object bindings given to
knex.raw
now get escaped by Knex before being passed to the Postgres driver (node-postgres
) and itsrequire("pg/lib/utils").prepareValue
. I was depending on them being passed as-is to serialize various types in different ways, thereby expanding what the Pg driver supports independent of any layers above it.I presume it's something to do with https://github.com/tgriesser/knex/blob/dfca38da339922f8ad8f52087ca5e6c0dcdab081/src/dialects/postgres/utils.js. Off the top of your head, would there be any risk if I [locally] scrapped that entirely and had it pass everything as-is to the Pg module's
prepareValue
? It's used only for bindings and not used for interpolating inside a SQL string, right?Thanks!
The text was updated successfully, but these errors were encountered: