Skip to content

Commit

Permalink
Add esc(std::string_view).
Browse files Browse the repository at this point in the history
Fixes #295.

I guess I meant to do this before, but forgot!  The overload was there
on `connection` but not on `transaction_base`.
  • Loading branch information
jtv committed Mar 8, 2020
1 parent cde0db9 commit 9567239
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
7.0.5
- Compile fix for g++ 10: include `<limits>` (#292).
- Cleaned up error-checking of PG results. (#280).
- The `esc()` methods now also take `std::string_view` (#295).
7.0.4
- Fix possible crash in `connection::connection_string` (#290).
- Fix filtering of default values in `connection::connection_string` (#288).
Expand Down
11 changes: 8 additions & 3 deletions include/pqxx/transaction_base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,26 @@ public:
* @ingroup escaping-functions
*/
//@{
/// Escape string for use as SQL string literal in this transaction
/// Escape string for use as SQL string literal in this transaction.
[[nodiscard]] std::string esc(char const text[]) const
{
return conn().esc(text);
}
/// Escape string for use as SQL string literal in this transaction
/// Escape string for use as SQL string literal in this transaction.
[[nodiscard]] std::string esc(char const text[], size_t maxlen) const
{
return conn().esc(text, maxlen);
}
/// Escape string for use as SQL string literal in this transaction
/// Escape string for use as SQL string literal in this transaction.
[[nodiscard]] std::string esc(std::string const &text) const
{
return conn().esc(text);
}
/// Escape string for use as SQL string literal in this transaction.
[[nodiscard]] std::string esc(std::string_view text) const
{
return conn().esc(text);
}

/// Escape binary data for use as SQL string literal in this transaction
/** Raw, binary data is treated differently from regular strings. Binary
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test_escape.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void test_esc(pqxx::connection_base &c, pqxx::transaction_base &t)
{
PQXX_CHECK_EQUAL(t.esc("", 0), "", "Empty string doesn't escape properly.");
PQXX_CHECK_EQUAL(t.esc("'", 1), "''", "Single quote escaped incorrectly.");
PQXX_CHECK_EQUAL(
t.esc(std::string_view{"hello"}), "hello", "Trivial escape went wrong.");
char const *const escstrings[]{"x", " ", "", nullptr};
for (size_t i{0}; escstrings[i] != nullptr; ++i)
compare_esc(c, t, escstrings[i]);
Expand Down

0 comments on commit 9567239

Please sign in to comment.