Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- New helper functions to query a type's nullness properties.
- More SQL statements can now be `std::string_view` instead of `pqxx::zview`.
- Work around broken multidim operator[] support in MSVC 2022.
- Nicer replacement for `pqxx::connection::port()`: `port_number()`.
7.10.4
- Logic for controlling non-blocking mode was inverted (non-Windows). (#1057)
- Pass SQL error code when throwing `pqxx::insufficient_privilege`. (#1077)
Expand Down
9 changes: 5 additions & 4 deletions include/pqxx/connection.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,18 @@ public:
/** Returns nullptr when not connected. */
[[nodiscard]] char const *username() const noexcept;

// XXX: Can this return null?
/// Database server address, if given.
/** This may be an IP address, or a hostname, or (for a Unix domain socket)
* a socket path. Returns nullptr when not connected.
*/
[[nodiscard]] PQXX_PURE char const *hostname() const noexcept;

// XXX: Can this return null?
// XXX: Why a string and not a number?
/// Server port number on which we are connected to the database.
[[nodiscard]] PQXX_PURE char const *port() const noexcept;
[[nodiscard, deprecated("Use port_number().")]] PQXX_PURE char const *
port() const noexcept;

/// Server port number on which we are connected to the database, if any.
PQXX_PURE std::optional<int> port_number(sl loc = sl::current()) const;

/// Process ID for backend process, or 0 if inactive.
[[nodiscard]] PQXX_PURE int backendpid() const & noexcept;
Expand Down
11 changes: 11 additions & 0 deletions src/connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,17 @@ PQXX_COLD char const *pqxx::connection::port() const noexcept
}


PQXX_COLD std::optional<int> pqxx::connection::port_number(sl loc) const
{
char const *const ptr{PQport(m_conn)};
if ((ptr == nullptr) or (*ptr == '\0'))
return {};
else
return from_string<int>(
ptr, conversion_context{encoding_group::ascii_safe, loc});
}


char const *pqxx::connection::err_msg() const noexcept
{
return (m_conn == nullptr) ? "No connection to database" :
Expand Down
7 changes: 7 additions & 0 deletions test/test21.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void test_021()

std::string const host{
((cx.hostname() == nullptr) ? "<local>" : cx.hostname())};
#include <pqxx/internal/ignore-deprecated-pre.hxx>
cx.process_notice(
std::string{} + "database=" + cx.dbname() +
", "
Expand All @@ -28,11 +29,13 @@ void test_021()
", "
"backendpid=" +
pqxx::to_string(cx.backendpid()) + "\n");
#include <pqxx/internal/ignore-deprecated-post.hxx>

pqxx::work tx{cx, "test_021"};

// By now our connection should really have been created
cx.process_notice("Printing details on actual connection\n");
#include <pqxx/internal/ignore-deprecated-pre.hxx>
cx.process_notice(
std::string{} + "database=" + cx.dbname() +
", "
Expand All @@ -47,10 +50,14 @@ void test_021()
", "
"backendpid=" +
pqxx::to_string(cx.backendpid()) + "\n");
#include <pqxx/internal/ignore-deprecated-post.hxx>

std::string p;

#include <pqxx/internal/ignore-deprecated-pre.hxx>
pqxx::from_string(cx.port(), p);
PQXX_CHECK_EQUAL(p, pqxx::to_string(cx.port()));
#include <pqxx/internal/ignore-deprecated-post.hxx>
PQXX_CHECK_EQUAL(pqxx::to_string(p), p);

pqxx::result const R(tx.exec("SELECT * FROM pg_tables"));
Expand Down
3 changes: 3 additions & 0 deletions test/test_connection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ void test_closed_connection()
PQXX_CHECK(not cx.dbname());
PQXX_CHECK(not cx.username());
PQXX_CHECK(not cx.hostname());
#include <pqxx/internal/ignore-deprecated-pre.hxx>
PQXX_CHECK(not cx.port());
#include <pqxx/internal/ignore-deprecated-post.hxx>
PQXX_CHECK_EQUAL(cx.port_number(), (std::optional<int>{}));
}


Expand Down