libpqxx
The C++ client library for PostgreSQL
connection.hxx
Go to the documentation of this file.
1 /* Definition of the connection class.
2  *
3  * pqxx::connection encapsulates a connection to a database.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/connection instead.
6  *
7  * Copyright (c) 2000-2026, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_CONNECTION_HXX
14 #define PQXX_CONNECTION_HXX
15 
16 #if !defined(PQXX_HEADER_PRE)
17 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
18 #endif
19 
20 #include <cstddef>
21 #include <ctime>
22 #include <format>
23 #include <functional>
24 #include <initializer_list>
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <ranges>
29 #include <string_view>
30 #include <tuple>
31 #include <utility>
32 
33 #include "pqxx/errorhandler.hxx"
34 #include "pqxx/except.hxx"
36 #include "pqxx/params.hxx"
37 #include "pqxx/result.hxx"
38 #include "pqxx/separated_list.hxx"
39 #include "pqxx/strconv.hxx"
40 #include "pqxx/types.hxx"
41 #include "pqxx/util.hxx"
42 #include "pqxx/zview.hxx"
43 
44 
76 namespace pqxx::internal
77 {
78 class sql_cursor;
79 
80 
82 
89 PQXX_COLD PQXX_LIBEXPORT void skip_init_ssl(int skips) noexcept;
90 } // namespace pqxx::internal
91 
92 
94 {
95 class connection_dbtransaction;
105 } // namespace pqxx::internal::gate
106 
107 
108 namespace pqxx
109 {
111 
119 template<typename T>
120 concept ZKey_ZValues = std::ranges::input_range<T> and requires(T t) {
121  { std::get<0>(*std::cbegin(t)) } -> ZString;
122  { std::get<1>(*std::cbegin(t)) } -> ZString;
123  requires(
124  std::tuple_size_v<std::remove_cvref_t<decltype(*std::cbegin(t))>> == 2u);
125 };
126 
127 
129 
140 struct notification final
141 {
142  // NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)
143 
145 
151 
152  // NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
153 
155 
159 
161 
164 
166 
176  int backend_pid = 0;
177 };
178 
179 
181 
188 enum skip_init : int
189 {
192 
195 
198 };
199 
200 
202 
225 template<skip_init... SKIP> inline void skip_init_ssl() noexcept
226 {
227  // (Normalise skip flags to one per.)
228  pqxx::internal::skip_init_ssl(((1 << SKIP) | ...));
229 }
230 
231 
233 
240 using table_path = std::initializer_list<std::string_view>;
241 
242 
244 enum class error_verbosity : int
245 {
246  // These values must match those in libpq's PGVerbosity enum.
247  terse = 0,
248  normal = 1,
249  verbose = 2
250 };
251 
252 
254 
273 {
274 public:
331  explicit connection(sl loc = sl::current()) :
332  connection("", empty_params_t(), loc)
333  {}
334 
336 
342  template<pqxx::ZString STRING, ZKey_ZValues MAPPING>
343  explicit inline connection(
344  STRING const &connection_string, MAPPING &&params = empty_params_t(),
345  sl = sl::current());
346 
348 
354  template<pqxx::ZString STRING>
355  explicit connection(
356  STRING const &connection_string, sl loc = sl::current()) :
357  connection{connection_string, empty_params_t(), loc}
358  {}
359 
361  template<ZKey_ZValues MAPPING>
362  explicit connection(MAPPING &&params, sl loc = sl::current()) :
363  connection{"", params, loc}
364  {}
366 
368 
373  connection(connection &&rhs, sl = sl::current());
374 
376  {
377  try
378  {
379  close(m_created_loc);
380  }
381  catch (std::exception const &)
382  {
383  // TODO: Try to report the error.
384  }
385  }
386 
387  // TODO: Once we drop notification_receiver/errorhandler, move is easier.
389 
398  connection &operator=(connection &&rhs);
399 
400  connection(connection const &) = delete;
401  connection &operator=(connection const &) = delete;
402 
404 
409  [[nodiscard]] bool is_open() const noexcept;
410 
412  PQXX_ZARGS void process_notice(char const[]) noexcept;
414 
417  void process_notice(zview) noexcept;
418 
420  void trace(std::FILE *) noexcept;
421 
433 
434  [[nodiscard]] char const *dbname() const noexcept;
435 
437 
438  [[nodiscard]] char const *username() const noexcept;
439 
441 
444  [[nodiscard]] PQXX_PURE char const *hostname() const noexcept;
445 
447  [[nodiscard, deprecated("Use port_number().")]] PQXX_PURE char const *
448  port() const noexcept;
449 
451  [[nodiscard]] PQXX_PURE std::optional<int>
452  port_number(sl loc = sl::current()) const;
453 
455  [[nodiscard]] PQXX_PURE int backendpid() const & noexcept;
456 
458 
468  [[nodiscard]] PQXX_PURE int sock() const & noexcept;
469 
471 
474  [[nodiscard]] PQXX_PURE int protocol_version() const noexcept;
475 
477 
489  [[nodiscard]] PQXX_PURE int server_version() const noexcept;
491 
493 
514  [[nodiscard]] std::string get_client_encoding(sl loc = sl::current()) const;
515 
517 
520  void set_client_encoding(zview encoding, sl loc = sl::current()) &
521  {
522  set_client_encoding(encoding.c_str(), loc);
523  }
524 
526 
529  PQXX_ZARGS void
530  set_client_encoding(char const encoding[], sl = sl::current()) &;
531 
533  [[nodiscard]] int encoding_id(sl = sl::current()) const;
534 
536  [[nodiscard]] encoding_group get_encoding_group(sl loc = sl::current()) const
537  {
538  return pqxx::internal::enc_group(this->encoding_id(loc), loc);
539  }
540 
542 
544 
565  template<typename TYPE>
567  std::string_view var, TYPE const &value, sl loc = sl::current()) &
568  {
569  if constexpr (has_null<TYPE>())
570  {
571  if (is_null(value))
572  throw variable_set_to_null{
573  std::format("Attempted to set variable {} to null.", var), loc};
574  }
575  exec(std::format("SET {}={}", quote_name(var), quote(value, loc)), loc);
576  }
577 
579 
585  std::string get_var(std::string_view var, sl loc = sl::current());
586 
588 
598  template<not_borrowed TYPE>
599  TYPE get_var_as(std::string_view var, sl loc = sl::current())
600  {
601  conversion_context const c{get_encoding_group(), loc};
602  return from_string<TYPE>(get_var(var, loc), c);
603  }
604 
696 
713  int get_notifs(sl = sl::current());
714 
715  // TODO: Unify with the other overload.
717 
740  int await_notification(sl = sl::current());
741 
743 
765  int await_notification(
766  std::time_t seconds = 10, long microseconds = 0, sl = sl::current());
767 
769 
776  using notification_handler = std::function<void(notification)>;
777 
779 
806  void listen(
807  std::string_view channel, notification_handler handler = {},
808  sl = sl::current());
809 
811 
843  [[nodiscard]] std::string
844  encrypt_password(zview user, zview password, zview algorithm)
845  {
846  return encrypt_password(user.c_str(), password.c_str(), algorithm.c_str());
847  }
849  [[nodiscard]] PQXX_ZARGS std::string encrypt_password(
850  char const user[], char const password[], char const *algorithm = nullptr);
852 
895 
897 
901  void prepare(zview name, zview definition, sl loc = sl::current()) &
902  {
903  prepare(name.c_str(), definition.c_str(), loc);
904  }
905 
907 
913  void prepare(
914  std::string const &name, std::string const &definition,
915  sl loc = sl::current()) &
916  {
917  prepare(name.c_str(), definition.c_str(), loc);
918  }
919 
921 
925  PQXX_ZARGS void prepare(
926  char const name[], char const definition[], sl loc = sl::current()) &;
927 
929 
941  void prepare(
942  std::string_view name, std::string_view definition,
943  sl loc = sl::current()) &
944  {
945  std::string const n{name}, d{definition};
946  prepare(n.c_str(), d.c_str(), loc);
947  }
948 
950  [[deprecated("Either name your statement, or just parameterise it.")]]
951  PQXX_ZARGS void prepare(char const definition[], sl loc = sl::current()) &;
952  [[deprecated("Either name your statement, or just parameterise it.")]]
953  void prepare(zview definition, sl loc = sl::current()) &
954  {
956  prepare(definition.c_str(), loc);
958  }
959 
961  void unprepare(std::string_view name, sl loc = sl::current());
962 
964 
966 
969  [[nodiscard]] std::string adorn_name(std::string_view);
970 
975 
977  [[nodiscard]] PQXX_ZARGS std::string
978  esc(char const text[], sl loc = sl::current()) const
979  {
980  return esc(std::string_view{text}, loc);
981  }
982 
984 
995  [[nodiscard]] std::string_view
996  esc(std::string_view text, std::span<char> buffer, sl loc = sl::current())
997  {
998  auto const size{std::size(text)}, space{std::size(buffer)};
999  auto const needed{2 * size + 1};
1000  if (space < needed)
1001  throw range_error{
1002  std::format(
1003  "Not enough room to escape string of {} byte(s): need {} bytes of "
1004  "buffer space, but buffer size is {}.",
1005  size, needed, size),
1006  loc};
1007  return {std::data(buffer), esc_to_buf(text, buffer, loc)};
1008  }
1009 
1011 
1014  [[nodiscard]] std::string
1015  esc(std::string_view text, sl loc = sl::current()) const;
1016 
1018 
1019  template<binary DATA> [[nodiscard]] std::string esc(DATA const &data) const
1020  {
1021  return esc_raw(data);
1022  }
1023 
1025 
1036  template<binary DATA>
1037  [[nodiscard]] zview
1038  esc(DATA const &data, std::span<char> buffer, sl loc = sl::current()) const
1039  {
1040  auto const size{std::size(data)}, space{std::size(buffer)};
1041  auto const needed{internal::size_esc_bin(std::size(data))};
1042  if (space < needed)
1043  throw range_error{
1044  std::format(
1045  "Not enough room to escape binary string of {} byte(s): need {} ",
1046  " bytes of buffer space, but buffer size is {}.", size, needed,
1047  space),
1048  loc};
1049 
1050  bytes_view const view{std::data(data), std::size(data)};
1051  // Actually, in the modern format, we know beforehand exactly how many
1052  // bytes we're going to fill. Just leave out the trailing zero.
1053  internal::esc_bin(view, buffer);
1054  return zview{std::data(buffer), needed - 1};
1055  }
1056 
1058 
1059  [[nodiscard]] std::string esc_raw(bytes_view) const;
1060 
1062 
1063  template<binary DATA>
1064  [[nodiscard]] std::string esc_raw(DATA const &data) const
1065  {
1066  return esc_raw(bytes_view{std::data(data), std::size(data)});
1067  }
1068 
1070  template<binary DATA>
1071  [[nodiscard]] zview esc_raw(DATA const &data, std::span<char> buffer) const
1072  {
1073  return this->esc(binary_cast(data), buffer);
1074  }
1075 
1076  // TODO: Make "into buffer" variant to eliminate a string allocation.
1078 
1085  [[nodiscard]] bytes
1086  unesc_bin(std::string_view text, sl loc = sl::current()) const
1087  {
1088  bytes buf{pqxx::internal::size_unesc_bin(std::size(text))};
1089  pqxx::internal::unesc_bin(text, buf, loc);
1090  return buf;
1091  }
1092 
1094 
1095  [[nodiscard]] std::string quote_raw(bytes_view) const;
1096 
1098 
1099  template<binary DATA>
1100  [[nodiscard]] std::string quote_raw(DATA const &data) const
1101  {
1102  return quote_raw(bytes_view{std::data(data), std::size(data)});
1103  }
1104 
1105  // TODO: Make "into buffer" variant to eliminate a string allocation.
1107  [[nodiscard]] std::string quote_name(std::string_view identifier) const;
1108 
1109  // TODO: Make "into buffer" variant to eliminate a string allocation.
1111 
1114  [[nodiscard]] std::string quote_table(std::string_view name) const;
1115 
1116  // TODO: Make "into buffer" variant to eliminate a string allocation.
1118 
1126  [[nodiscard]] std::string quote_table(table_path) const;
1127 
1128  // TODO: Make "into buffer" variant to eliminate a string allocation.
1130 
1137  template<pqxx::char_strings STRINGS>
1138  inline std::string
1139  quote_columns(STRINGS const &columns, sl = sl::current()) const;
1140 
1141  // TODO: Make "into buffer" variant to eliminate a string allocation.
1143 
1146  template<typename T>
1147  [[nodiscard]] inline std::string quote(T const &t, sl = sl::current()) const;
1148 
1149  // TODO: Make "into buffer" variant to eliminate a string allocation.
1151 
1176  [[nodiscard]] std::string esc_like(
1177  std::string_view text, char escape_char = '\\',
1178  sl loc = sl::current()) const;
1180 
1182 
1186  void cancel_query(sl = sl::current());
1187 
1188 #if defined(_WIN32) || __has_include(<fcntl.h>)
1190 
1194  void set_blocking(bool block, sl = sl::current()) &;
1195 #endif // defined(_WIN32) || __has_include(<fcntl.h>)
1196 
1198 
1210  void set_verbosity(error_verbosity verbosity) & noexcept;
1211 
1213 
1225  void set_notice_handler(std::function<void(zview)> handler)
1226  {
1227  m_notice_waiters->notice_handler = std::move(handler);
1228  }
1229 
1231 
1236  [[nodiscard, deprecated("Use a notice handler instead.")]]
1237  std::vector<errorhandler *> get_errorhandlers() const;
1238 
1240 
1246  [[nodiscard]] std::string connection_string() const;
1247 
1249 
1257  void close(sl = sl::current());
1258 
1260 
1267  static connection
1268  seize_raw_connection(internal::pq::PGconn *raw_conn, sl loc = sl::current())
1269  {
1270  return connection{raw_conn, loc};
1271  }
1272 
1274 
1283  {
1284  return std::exchange(m_conn, nullptr);
1285  }
1286 
1288 
1301  [[deprecated("To set session variables, use set_session_var.")]] void
1302  set_variable(
1303  std::string_view var, std::string_view value, sl loc = sl::current()) &;
1304 
1306 
1309  [[deprecated("Use get_var instead.")]] std::string
1310  get_variable(std::string_view, sl loc = sl::current());
1311 
1312 private:
1314  using empty_params_t =
1315  std::initializer_list<std::pair<char const *, char const *>>;
1316 
1317  friend class connecting;
1318  enum connect_mode
1319  {
1320  connect_nonblocking
1321  };
1323  connection(connect_mode, zview connection_string, sl);
1324 
1326  explicit connection(internal::pq::PGconn *raw_conn, sl);
1327 
1329 
1334  std::pair<bool, bool> poll_connect(sl);
1335 
1336  // Initialise based on connection string and key/value parameter pairs.
1337  void init(
1338  std::vector<const char *> const &override_keys,
1339  std::vector<const char *> const &override_values, sl);
1340 
1341  void set_up_notice_handlers();
1342 
1344  void complete_connection(sl);
1345 
1346  result make_result(
1347  internal::pq::PGresult *pgr, std::shared_ptr<std::string> const &query,
1348  std::string_view desc, sl = sl::current());
1349 
1350  result make_result(
1351  internal::pq::PGresult *pgr, std::shared_ptr<std::string> const &query,
1352  sl loc = sl::current())
1353  {
1354  return make_result(pgr, query, "", loc);
1355  }
1356 
1357  PQXX_PRIVATE [[nodiscard]] int status() const noexcept;
1358 
1360 
1364  [[nodiscard]] std::size_t
1365  esc_to_buf(std::string_view text, std::span<char> buf, sl loc) const;
1366 
1367  friend class internal::gate::const_connection_largeobject;
1368  [[nodiscard]] char const *err_msg() const noexcept;
1369 
1370  result exec_prepared(
1371  std::string_view statement, internal::c_params const &,
1372  sl loc = sl::current());
1373 
1375  void check_movable(sl) const;
1377  void check_overwritable(sl) const;
1378 
1379  friend class internal::gate::connection_errorhandler;
1380  PQXX_PRIVATE void register_errorhandler(errorhandler *);
1381  PQXX_PRIVATE void unregister_errorhandler(errorhandler *) noexcept;
1382 
1383  friend class internal::gate::connection_transaction;
1384  result exec(std::string_view query, sl loc) { return exec(query, "", loc); }
1385  result exec(std::string_view, std::string_view, sl);
1387  exec(std::shared_ptr<std::string> const &, std::string_view, sl);
1388  PQXX_PRIVATE result exec(std::shared_ptr<std::string> const &query, sl loc)
1389  {
1390  return exec(query, "", loc);
1391  }
1392 
1393  PQXX_PRIVATE void register_transaction(transaction_base *);
1394  PQXX_PRIVATE void unregister_transaction(transaction_base *) noexcept;
1395 
1398 
1403  std::pair<std::unique_ptr<char[], void (*)(void const *)>, std::size_t>
1404  read_copy_line(sl);
1405 
1407  PQXX_PRIVATE void write_copy_line(std::string_view, sl);
1408  PQXX_PRIVATE void end_copy_write(sl);
1409 
1411  [[nodiscard]] constexpr internal::pq::PGconn *raw_connection() const noexcept
1412  {
1413  return m_conn;
1414  }
1415 
1417  void add_receiver(notification_receiver *, sl);
1418  void remove_receiver(notification_receiver *, sl) noexcept;
1419 
1421  PQXX_PRIVATE PQXX_ZARGS void start_exec(char const query[]);
1422  PQXX_PRIVATE bool consume_input() noexcept;
1423  PQXX_PRIVATE [[nodiscard]] bool is_busy() const noexcept;
1424  internal::pq::PGresult *get_result();
1425 
1426  friend class internal::gate::connection_dbtransaction;
1427  friend class internal::gate::connection_sql_cursor;
1428 
1429  result
1430  exec_params(std::string_view query, internal::c_params const &args, sl);
1431 
1433  internal::pq::PGconn *m_conn = nullptr;
1434 
1436 
1443  transaction_base const *m_trans = nullptr;
1444 
1446  std::shared_ptr<pqxx::internal::notice_waiters> m_notice_waiters;
1447 
1448  // TODO: Remove these when we retire notification_receiver.
1449  // TODO: Can we make these movable?
1450  using receiver_list =
1451  std::multimap<std::string, pqxx::notification_receiver *>;
1453  receiver_list m_receivers;
1454 
1456 
1463  std::map<std::string, notification_handler> m_notification_handlers;
1464 
1466  sl m_created_loc;
1467 
1469  int m_unique_id = 0;
1470 };
1471 
1472 
1474 
1518 {
1519 public:
1521  explicit connecting(zview connection_string = ""_zv, sl = sl::current());
1522 
1523  connecting(connecting const &) = delete;
1524  connecting(connecting &&) = default;
1525  ~connecting() = default;
1526  connecting &operator=(connecting const &) = delete;
1528 
1530  [[nodiscard]] int sock() const & noexcept { return m_conn.sock(); }
1531 
1533  [[nodiscard]] constexpr bool wait_to_read() const & noexcept
1534  {
1535  return m_reading;
1536  }
1537 
1539  [[nodiscard]] constexpr bool wait_to_write() const & noexcept
1540  {
1541  return m_writing;
1542  }
1543 
1545  void process(sl loc = sl::current()) &;
1546 
1548  [[nodiscard]] constexpr bool done() const & noexcept
1549  {
1550  return not m_reading and not m_writing;
1551  }
1552 
1554 
1562  [[nodiscard]] connection produce(sl = sl::current()) &&;
1563 
1564 private:
1565  connection m_conn;
1566  bool m_reading{false};
1567  bool m_writing{true};
1568 };
1569 
1570 
1571 template<typename T>
1572 inline std::string connection::quote(T const &t, sl loc) const
1573 {
1574  if (is_null(t))
1575  {
1576  // It's easy to forget, but we can't support nulls in string conversion
1577  // itself, because the "NULL" may end up inside quotes or something.
1578  // We can only handle nulls at this slightly higher level in the call tree,
1579  // where there is awareness of the quoting.
1580  return "NULL";
1581  }
1582  else if constexpr (binary<T>)
1583  {
1584  return quote_raw(t);
1585  }
1586  else
1587  {
1588  // TODO: Can we leave the quotes out if unquoted_safe?
1589  auto const text{to_string(t)};
1590 
1591  // Okay, there's an easy way to do this and there's a hard way. The easy
1592  // way was "quote, esc(to_string(t)), quote". I'm going with the hard way
1593  // because it's going to save some string manipulation that will probably
1594  // incur some unnecessary memory allocations and deallocations.
1595  std::string buf{'\''};
1596  buf.resize(2 + 2 * std::size(text) + 1);
1597  auto const content_bytes{
1598  esc_to_buf(text, {std::begin(buf) + 1, std::end(buf)}, loc)};
1599  auto const closing_quote{1 + content_bytes};
1600  buf[closing_quote] = '\'';
1601  auto const end{closing_quote + 1};
1602  buf.resize(end);
1603  return buf;
1604  }
1605 }
1606 
1607 
1608 template<pqxx::char_strings STRINGS>
1609 inline std::string
1610 connection::quote_columns(STRINGS const &columns, sl loc) const
1611 {
1612  conversion_context const c{get_encoding_group(), loc};
1613  return separated_list(
1614  ","sv, std::cbegin(columns), std::cend(columns),
1615  [this](auto col) { return this->quote_name(*col); }, c);
1616 }
1617 
1618 
1619 template<pqxx::ZString STRING, ZKey_ZValues MAPPING>
1621  STRING const &connection_string, MAPPING &&params, sl loc) :
1622  m_created_loc{loc}
1623 {
1624  // Check that the libpqxx binary library version is compatible with the
1625  // version against which the application was compiled. We're in an inline
1626  // function, so this _call_ ends up in the application binary. On the other
1627  // hand, `check_libpqxx_version()` is compiled into the libpqx binary.
1628  // That's how the function is in a position to compare the two versions.
1629  //
1630  // There is no particular reason to do this here in @ref connection, except
1631  // to ensure that every meaningful libpqxx client will execute it, while
1632  // minimising overhead. The ideal would be to pay a small price exactly once
1633  // per application run.
1634  //
1635  // A local static variable is initialised only on the definition's first
1636  // execution. Compilers will be well optimised for this, so there's a
1637  // minimal one-time cost.
1638  [[maybe_unused]] static auto const version_check{
1641 
1642  // clang-tidy rule bug:
1643  // NOLINTBEGIN(
1644  // cppcoreguidelines-pro-bounds-array-to-pointer-decay,
1645  // hicpp-no-array-decay
1646  // )
1647  pqxx::internal::connection_string_parser const parsed_string{
1649  // NOLINTEND(
1650  // cppcoreguidelines-pro-bounds-array-to-pointer-decay,
1651  // hicpp-no-array-decay
1652  // )
1653  auto [keys, values]{parsed_string.parse()};
1654 
1655  // Merge key/value pairs into the pairs we got from the connection string.
1656  for (auto const &[org_key, org_value] : params)
1657  {
1658  auto const key{pqxx::internal::as_c_string(org_key)},
1659  value{pqxx::internal::as_c_string(org_value)};
1660 
1661  // Did we have a value for key already? This is a horrible O(n^2) search,
1662  // in principle, but there's only so many possible options. Trying to
1663  // optimise this might make it worse in common practical cases.
1664  //
1665  // (Landau's "large O" notation really only applies to inputs growing
1666  // towards infinity. If there's a known constant bound b such that n < b,
1667  // then O(n^2) is at worst O(b^2). Since b is a constant, O(b^2) boils
1668  // down to O(1).)
1669  auto const it{std::ranges::find_if(keys, [key](char const *existing) {
1670  return std::strcmp(existing, key) == 0;
1671  })};
1672 
1673  if (it == keys.end())
1674  {
1675  // New key. Append.
1676  keys.push_back(key);
1677  values.push_back(value);
1678  }
1679  else
1680  {
1681  // A key we've already seen. Override.
1682  auto const idx{
1683  static_cast<std::size_t>(std::distance(keys.begin(), it))};
1684  values[idx] = value;
1685  }
1686  }
1687 
1688  // Null-terminate both arrays.
1689  keys.push_back(nullptr);
1690  values.push_back(nullptr);
1691 
1692  init(keys, values, loc);
1693 }
1694 } // namespace pqxx
1695 #endif
An ongoing, non-blocking stepping stone to a connection.
Definition: connection.hxx:1518
connecting(connecting &&)=default
~connecting()=default
connection produce(sl=sl::current()) &&
Produce the completed connection object.
int sock() const &noexcept
Get the socket. The socket may change during the connection process.
Definition: connection.hxx:1530
constexpr bool done() const &noexcept
Is our connection finished?
Definition: connection.hxx:1548
connecting(connecting const &)=delete
constexpr bool wait_to_write() const &noexcept
Should we currently wait to be able to write to the socket?
Definition: connection.hxx:1539
connecting & operator=(connecting &&)=default
connecting & operator=(connecting const &)=delete
constexpr bool wait_to_read() const &noexcept
Should we currently wait to be able to read from the socket?
Definition: connection.hxx:1533
connecting(zview connection_string=""_zv, sl=sl::current())
Start connecting.
void process(sl loc=sl::current()) &
Progress towards completion (but don't block).
Connection to a database.
Definition: connection.hxx:273
std::string encrypt_password(zview user, zview password, zview algorithm)
Encrypt a password for a given user.
Definition: connection.hxx:844
void set_session_var(std::string_view var, TYPE const &value, sl loc=sl::current()) &
Set one of the session variables to a new value.
Definition: connection.hxx:566
encoding_group get_encoding_group(sl loc=sl::current()) const
Read the curent client encoding's pqxx::encoding_group.
Definition: connection.hxx:536
std::string quote(T const &t, sl=sl::current()) const
Represent object as SQL string, including quoting & escaping.
Definition: connection.hxx:1572
std::string_view esc(std::string_view text, std::span< char > buffer, sl loc=sl::current())
Escape string for use as SQL string literal, into buffer.
Definition: connection.hxx:996
std::string esc(DATA const &data) const
Escape binary string for use as SQL string literal on this connection.
Definition: connection.hxx:1019
internal::pq::PGconn * release_raw_connection() &&
Release the raw connection without closing it.
Definition: connection.hxx:1282
zview esc_raw(DATA const &data, std::span< char > buffer) const
Escape binary string for use as SQL string literal, into buffer.
Definition: connection.hxx:1071
void set_notice_handler(std::function< void(zview)> handler)
Set a notice handler to the connection.
Definition: connection.hxx:1225
std::string quote_raw(DATA const &data) const
Escape and quote a string of binary data.
Definition: connection.hxx:1100
~connection()
Definition: connection.hxx:375
void prepare(zview definition, sl loc=sl::current()) &
Definition: connection.hxx:953
void prepare(zview name, zview definition, sl loc=sl::current()) &
Define a prepared statement.
Definition: connection.hxx:901
bytes unesc_bin(std::string_view text, sl loc=sl::current()) const
Unescape binary data, e.g. from a bytea field.
Definition: connection.hxx:1086
std::function< void(notification)> notification_handler
A handler callback for incoming notifications on a given channel.
Definition: connection.hxx:776
zview esc(DATA const &data, std::span< char > buffer, sl loc=sl::current()) const
Escape binary string for use as SQL string literal, into buffer.
Definition: connection.hxx:1038
connection(sl loc=sl::current())
Definition: connection.hxx:331
connection(STRING const &connection_string, sl loc=sl::current())
Connect to a database, passing a connection string.
Definition: connection.hxx:355
std::string connection_string() const
Return a connection string encapsulating this connection's options.
Definition: connection.cxx:1430
TYPE get_var_as(std::string_view var, sl loc=sl::current())
Read currently applicable value of a configuration variable.
Definition: connection.hxx:599
connection & operator=(connection const &)=delete
connection(MAPPING &&params, sl loc=sl::current())
Connect to a database, passing connection parameters.
Definition: connection.hxx:362
std::string esc_raw(DATA const &data) const
Escape binary string for use as SQL string literal on this connection.
Definition: connection.hxx:1064
static connection seize_raw_connection(internal::pq::PGconn *raw_conn, sl loc=sl::current())
Seize control of a raw libpq connection.
Definition: connection.hxx:1268
void prepare(std::string const &name, std::string const &definition, sl loc=sl::current()) &
Define a prepared statement.
Definition: connection.hxx:913
connection(connection const &)=delete
PQXX_ZARGS std::string esc(char const text[], sl loc=sl::current()) const
Escape string for use as SQL string literal on this connection.
Definition: connection.hxx:978
void prepare(std::string_view name, std::string_view definition, sl loc=sl::current()) &
Define a prepared statement.
Definition: connection.hxx:941
std::string quote_columns(STRINGS const &columns, sl=sl::current()) const
Quote and comma-separate a series of column names.
Definition: connection.hxx:1610
Definition: errorhandler.hxx:46
Parse a connection string into option keys and their values.
Definition: connection-string.hxx:11
Definition: connection-errorhandler.hxx:15
Definition: connection-largeobject.hxx:18
Definition: connection-notification_receiver.hxx:19
Definition: connection-pipeline.hxx:11
Definition: connection-sql_cursor.hxx:15
Definition: connection-stream_from.hxx:17
Definition: connection-stream_to.hxx:12
Definition: connection-transaction.hxx:14
Definition: connection-largeobject.hxx:33
Definition: notification.hxx:60
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:65
Result set containing data returned by a query or command.
Definition: result.hxx:101
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
constexpr char const * c_str() const &noexcept
Return as C string.
Definition: zview.hxx:150
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:651
The caller attempted to set a variable to null, which is not allowed.
Definition: except.hxx:358
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:151
#define PQXX_ZARGS
Definition: header-pre.hxx:136
#define PQXX_COLD
Definition: header-pre.hxx:72
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
#define PQXX_PURE
Definition: header-pre.hxx:64
#define PQXX_PRIVATE
Definition: header-pre.hxx:207
Definition: connection.hxx:94
void PGconn
Placeholder for libpq's connection type.
Definition: types.hxx:429
void PGresult
Placeholder for libpq's result type.
Definition: types.hxx:431
Private namespace for libpqxx's internal use; do not access.
Definition: connection.cxx:333
PQXX_COLD PQXX_LIBEXPORT void skip_init_ssl(int skips) noexcept
Control OpenSSL/crypto library initialisation.
Definition: connection.cxx:185
constexpr PQXX_ZARGS char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: util.hxx:406
constexpr PQXX_PURE pqxx::encoding_group get_encoding_group(encoding_group const &enc, sl=sl::current()) noexcept
Identity function for encoding_group, for regularity.
Definition: params.hxx:29
void unesc_bin(std::string_view escaped_data, std::span< std::byte > buffer, sl loc)
Reconstitute binary data from its escaped version.
Definition: util.cxx:194
int check_libpqxx_version(int apps_major, int apps_minor, int apps_patch, std::string_view apps_version)
Check library binary version against application's expectations.
Definition: util.cxx:258
constexpr PQXX_PURE std::size_t size_esc_bin(std::size_t binary_bytes) noexcept
Compute buffer size needed to escape binary data for use as a BYTEA.
Definition: util.hxx:493
constexpr encoding_group enc_group(std::string_view encoding_name, sl loc)
Look up encoding group for an encoding by name.
Definition: encodings.cxx:56
constexpr PQXX_PURE std::size_t size_unesc_bin(std::size_t escaped_bytes) noexcept
Compute binary size from the size of its escaped version.
Definition: util.hxx:505
void esc_bin(bytes_view binary_data, std::span< char > buffer) noexcept
Hex-escape binary data into a buffer.
Definition: util.cxx:159
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
std::span< std::byte const > bytes_view
Type alias for a view of bytes.
Definition: types.hxx:188
constexpr int const version_patch
Libpqxx patch version number. (E.g. for libpqxx 9.3.1, this will be 1.)
Definition: version.hxx:49
constexpr int const version_minor
Minor libpqxx version number. (E.g. for libpqxx 9.3.1, this will be 3.)
Definition: version.hxx:42
concept ZKey_ZValues
Concept: T is a range of pairs of zero-terminated strings.
Definition: connection.hxx:120
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
PQXX_LIBEXPORT std::string to_string(field_ref const &value, ctx)
Convert a field_ref to a string.
Definition: field.hxx:891
void skip_init_ssl() noexcept
Control initialisation of OpenSSL and libcrypto libraries.
Definition: connection.hxx:225
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:260
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:240
encoding_group
Definition: encoding_group.hxx:40
concept ZString
Concept: T is a known zero-terminated string type.
Definition: zview.hxx:33
std::string separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access, ctx c={})
Represent sequence of values as a string, joined by a given separator.
Definition: separated_list.hxx:41
connection & conn
The connection which received the notification.
Definition: connection.hxx:150
constexpr bool is_null(TYPE const &value) noexcept
Is value a null?
Definition: strconv.hxx:764
requires(pqxx::internal::to_buf_7< TYPE > or pqxx::internal::to_buf_8< TYPE >) const eval bool supports_to_buf_8()
Is the libpqxx 8 version of to_buf() supported for TYPE?
Definition: strconv.hxx:417
int backend_pid
Process ID of the backend that sent the notification.
Definition: connection.hxx:176
constexpr int const version_major
Major libpqxx version number. (E.g. for libpqxx 9.3.1, this will be 9.)
Definition: version.hxx:38
skip_init
Flags for skipping initialisation of SSL-related libraries.
Definition: connection.hxx:189
@ crypto
Skip initialisation of libcrypto.
Definition: connection.hxx:197
@ openssl
Skip initialisation of OpenSSL library.
Definition: connection.hxx:194
@ nothing
A do-nothing flag that does not affect anything.
Definition: connection.hxx:191
zview channel
Channel name.
Definition: connection.hxx:158
constexpr std::string_view const version
Full libpqxx version string.
Definition: version.hxx:24
std::vector< std::byte > bytes
Type alias for a container containing bytes.
Definition: util.hxx:240
zview payload
Optional payload text.
Definition: connection.hxx:163
error_verbosity
Error verbosity levels.
Definition: connection.hxx:245
format
Format code: is data text or binary?
Definition: types.hxx:121
An incoming notification.
Definition: connection.hxx:141
Contextual parameters for string conversions implementations.
Definition: strconv.hxx:163