Don't treat parameters with an explicit index as named #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Unlike most of the other libraries we use,
better-sqlite3
supports binding parameters by name, extracting values from an object. The way the current implementation works is that named parameters in fact have to be given by name and can't be given as an array. For instance, the querySELECT :name
can't be executed withexecute(['Simon'])
, one has to useexecute({name: 'Simon'})
.Unfortunately, this requirement is also applied to variables that aren't named at all! The implementation just used to check for
sqlite3_bind_parameter_name
returningNULL
, but that's not the case for parameters with an explicit index (e.g.SELECT ?1
).As a consequence,
better-sqlite3
behaves quite weirdly here and wouldn't allow that statement to be executed withexecute(['Simon'])
, one would have to use{[1]: 'Simon'}
I suppose. This behavior is different from our other SDKs, which is obviously quite bad.This PR fixes the internal check to treat
?NNN
variables as non-named. This should mostly keep backwards compatibility while also ensuring that we can use these parameters withbetter-sqlite3
. There's still a problem if users other named variables (SELECT :name
) because our Node SDK will pass an array tobetter-sqlite3
which won't work.One could argue that this approach of binding named parameters is fundamentally flawed to begin with (because in SQLite, named parameters also have an associated index). So passing an array to bind named parameters should be allowed, but it breaks
better-sqlite3
. I didn't want to do that change right now because I'd then have to also fix a lot of tests. But it might be worth ripping out all special checks for named parameters entirely to restore