Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add escape_identifier() and escape_literal(). #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2020

Conversation

jeff-davis
Copy link
Contributor

@jeff-davis jeff-davis commented Nov 29, 2020

Fix #575

These functions are generally useful, but also useful specifically for PR #696 which needs to include arbitrary identifiers in a SQL string.

/// If input contains backslashes, result will be of the form `
/// E'...'` so it is safe to use regardless of the setting of
/// standard_conforming_strings.
pub fn escape_literal(input: &str) -> String {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have these return a Cow<str> so the no escaping needed case doesn't need to reallocate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escape_literal() follows the semantics of PQescapeLiteral(), which always allocates because the result must be surrounded by quote marks. This is different from PQescapeStringConn() which has some messy history that I don't think we need to follow because we should be using parameters most of the time anyway. In those cases where we still need it (like utility commands or replication commands), either form will suffice.

We could potentially optimize escape_identifier(), but it would deviate from libpq. I don't see this as likely to be a hot path, so the trade-off doesn't seem great to me.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah I missed that quotes were always added. The behavior here makes sense in that case.

@sfackler sfackler merged commit c5ae79a into sfackler:master Nov 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide function to escape strings provided to Postgres?
2 participants