Traced statement interpolation fixes #381
Merged
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.
I've maintained a project that performs similar behavior to what occurs in the
TracedStatement(generating a string from the parameterized query with the bound arguments put in place into it), so I've seen some of the things to beware of when offering this functionality. Here's a synopsis of the issues and how I work around them:Back reference syntax in the bound parameter value
If a value to be replaced back into the string contains a substring that looks like a regex back reference (e.g.
$1), thepreg_replacefunction looks to facilitate that:i.e. password_hash function may produce something like this:
but the interpolated query value ends up like this:
I work around this by appending a
%to any$%or\(the back reference characters) present in the bound argument value, then change them all back again afterward.Placeholder substring presence in replaced value
If a replaced value within the query string contains the placeholder character for a future replacement, the replacement may be incorrectly performed against the value within that replacement:
Performing the translation as it was would have resulted in this:
I work around this by ensuring the replacement doesn't occur within a set of the
$quotationChar.Optional leading colon in parameter binding
Named placeholders are required to have a leading colon, but the colon is optional when calling
bindParam:I work around this by checking for the presence of a leading colon on the bound parameter and adding it if it's not present.
I added tests for these scenarios.