13#ifndef PQXX_H_CONNECTION
14#define PQXX_H_CONNECTION
16#if !defined(PQXX_HEADER_PRE)
17# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
23#include <initializer_list>
32#if defined(PQXX_HAVE_CONCEPTS) && __has_include(<ranges>)
36#include "pqxx/errorhandler.hxx"
37#include "pqxx/except.hxx"
38#include "pqxx/internal/concat.hxx"
39#include "pqxx/params.hxx"
40#include "pqxx/separated_list.hxx"
41#include "pqxx/strconv.hxx"
42#include "pqxx/types.hxx"
43#include "pqxx/util.hxx"
44#include "pqxx/zview.hxx"
83#if defined(PQXX_HAVE_CONCEPTS)
88 { std::get<0>(*std::cbegin(
t)) } ->
ZString;
89 { std::get<1>(*std::cbegin(
t)) } ->
ZString;
90}
and std::tuple_size_v<typename std::ranges::iterator_t<T>::value_type> == 2;
106namespace pqxx::internal::gate
109class connection_errorhandler;
110class connection_largeobject;
113class connection_sql_cursor;
116class connection_transaction;
117class const_connection_largeobject;
219 pqxx::internal::skip_init_ssl(((1 <<
SKIP) | ...));
278class PQXX_LIBEXPORT connection
281 connection() : connection{
""} {}
284 explicit connection(
char const options[])
291 explicit connection(zview options) : connection{options.c_str()}
302 connection(connection &&rhs);
304#if defined(PQXX_HAVE_CONCEPTS)
321 template<
internal::ZKey_ZValues MAPPING>
322 inline connection(MAPPING
const ¶ms);
331 catch (std::exception
const &)
340 connection &operator=(connection &&rhs);
342 connection(connection
const &) =
delete;
343 connection &operator=(connection
const &) =
delete;
351 [[nodiscard]]
bool PQXX_PURE is_open() const noexcept;
354 void process_notice(
char const[]) noexcept;
359 void process_notice(zview) noexcept;
362 void trace(std::FILE *) noexcept;
376 [[nodiscard]]
char const *dbname() const;
380 [[nodiscard]]
char const *username() const;
386 [[nodiscard]]
char const *hostname() const;
389 [[nodiscard]]
char const *port() const;
392 [[nodiscard]]
int PQXX_PURE backendpid() const & noexcept;
405 [[nodiscard]]
int PQXX_PURE sock() const & noexcept;
411 [[nodiscard]]
int PQXX_PURE protocol_version() const noexcept;
426 [[nodiscard]]
int PQXX_PURE server_version() const noexcept;
451 [[nodiscard]] std::
string get_client_encoding() const;
457 void set_client_encoding(zview encoding) &
459 set_client_encoding(encoding.c_str());
466 void set_client_encoding(
char const encoding[]) &;
469 [[nodiscard]]
int encoding_id()
const;
495 template<
typename TYPE>
496 void set_session_var(std::string_view var, TYPE
const &value) &
498 if constexpr (nullness<TYPE>::has_null)
500 if (nullness<TYPE>::is_null(value))
501 throw variable_set_to_null{
502 internal::concat(
"Attempted to set variable ", var,
" to null.")};
504 exec(internal::concat(
"SET ", quote_name(var),
"=", quote(value)));
514 std::string get_var(std::string_view var);
523 template<
typename TYPE> TYPE get_var_as(std::string_view var)
525 return from_string<TYPE>(get_var(var));
653 int await_notification();
672 int await_notification(std::time_t seconds,
long microseconds = 0);
682 using notification_handler = std::function<void(notification)>;
706 void listen(std::string_view channel, notification_handler handler = {});
741 [[nodiscard]] std::string
748 char const user[],
char const password[],
char const *algorithm =
nullptr);
799 void prepare(zview name, zview definition) &
801 prepare(name.c_str(), definition.c_str());
808 void prepare(
char const name[],
char const definition[]) &;
818 void prepare(
char const definition[]) &;
819 void prepare(zview definition) & {
return prepare(definition.c_str()); }
822 void unprepare(std::string_view name);
831 [[nodiscard]] std::string adorn_name(std::string_view);
839 [[nodiscard]] std::string esc(
char const text[])
const
841 return esc(std::string_view{text});
844#if defined(PQXX_HAVE_SPAN)
857 [[nodiscard]] std::string_view
858 esc(std::string_view text, std::span<char> buffer)
860 auto const size{std::size(text)}, space{std::size(buffer)};
861 auto const needed{2 * size + 1};
863 throw range_error{internal::concat(
864 "Not enough room to escape string of ", size,
" byte(s): need ",
865 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
866 auto const data{buffer.data()};
867 return {data, esc_to_buf(text, data)};
875 [[nodiscard]] std::string esc(std::string_view text)
const;
877#if defined(PQXX_HAVE_CONCEPTS)
880 template<binary DATA> [[nodiscard]] std::string esc(DATA
const &data)
const
882 return esc_raw(data);
886#if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN)
899 template<binary DATA>
900 [[nodiscard]] zview esc(DATA
const &data, std::span<char> buffer)
const
902 auto const size{std::size(data)}, space{std::size(buffer)};
903 auto const needed{internal::size_esc_bin(std::size(data))};
905 throw range_error{internal::concat(
906 "Not enough room to escape binary string of ", size,
" byte(s): need ",
907 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
909 bytes_view view{std::data(data), std::size(data)};
910 auto const out{std::data(buffer)};
913 internal::esc_bin(view, out);
914 return zview{out, needed - 1};
919 [[deprecated(
"Use std::byte for binary data.")]] std::string
920 esc_raw(
unsigned char const bin[], std::size_t len)
const;
924 [[nodiscard]] std::string esc_raw(bytes_view)
const;
926#if defined(PQXX_HAVE_SPAN)
929 [[nodiscard]] std::string esc_raw(bytes_view, std::span<char> buffer)
const;
932#if defined(PQXX_HAVE_CONCEPTS)
935 template<binary DATA>
936 [[nodiscard]] std::string esc_raw(DATA
const &data)
const
938 return esc_raw(bytes_view{std::data(data), std::size(data)});
942#if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN)
944 template<binary DATA>
945 [[nodiscard]] zview esc_raw(DATA
const &data, std::span<char> buffer)
const
969 std::string quote_raw(bytes_view)
const;
971#if defined(PQXX_HAVE_CONCEPTS)
974 template<binary DATA>
975 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
977 return quote_raw(bytes_view{std::data(data), std::size(data)});
983 [[nodiscard]] std::string quote_name(std::string_view identifier)
const;
990 [[nodiscard]] std::string quote_table(std::string_view name)
const;
1002 [[nodiscard]] std::string quote_table(table_path)
const;
1013 template<PQXX_CHAR_STRINGS_ARG STRINGS>
1014 inline std::string quote_columns(STRINGS
const &columns)
const;
1021 template<
typename T>
1022 [[nodiscard]]
inline std::string quote(T
const &t)
const;
1024 [[deprecated(
"Use std::byte for binary data.")]] std::string
1025 quote(binarystring
const &)
const;
1029 [[nodiscard]] std::string quote(bytes_view bytes)
const;
1058 [[nodiscard]] std::string
1059 esc_like(std::string_view text,
char escape_char =
'\\')
const;
1066 [[deprecated(
"Use std::string_view or pqxx:zview.")]] std::string
1067 esc(
char const text[], std::size_t maxlen)
const
1069 return esc(std::string_view{text, maxlen});
1076 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
1077 unesc_raw(zview text)
const
1079#include "pqxx/internal/ignore-deprecated-pre.hxx"
1080 return unesc_raw(text.c_str());
1081#include "pqxx/internal/ignore-deprecated-post.hxx"
1088 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
1089 unesc_raw(
char const text[])
const;
1092 [[deprecated(
"Use quote(bytes_view).")]] std::string
1093 quote_raw(
unsigned char const bin[], std::size_t len)
const;
1101 void cancel_query();
1103#if defined(_WIN32) || __has_include(<fcntl.h>)
1109 void set_blocking(
bool block) &;
1125 void set_verbosity(error_verbosity verbosity) &
noexcept;
1142 void set_notice_handler(std::function<
void(zview)> handler)
1144 m_notice_waiters->notice_handler = std::move(handler);
1153 [[nodiscard, deprecated(
"Use a notice handler instead.")]]
1154 std::vector<errorhandler *> get_errorhandlers()
const;
1163 [[nodiscard]] std::string connection_string()
const;
1183 static connection seize_raw_connection(internal::pq::PGconn *raw_conn)
1185 return connection{raw_conn};
1194 internal::pq::PGconn *release_raw_connection() &&
1196 return std::exchange(m_conn,
nullptr);
1213 [[deprecated(
"To set session variables, use set_session_var.")]]
void
1214 set_variable(std::string_view var, std::string_view value) &;
1220 [[deprecated(
"Use get_var instead.")]] std::string
1221 get_variable(std::string_view);
1224 friend class connecting;
1229 connection(connect_mode, zview connection_string);
1232 explicit connection(internal::pq::PGconn *raw_conn);
1240 std::pair<bool, bool> poll_connect();
1243 void init(
char const options[]);
1245 void init(
char const *params[],
char const *values[]);
1246 void set_up_notice_handlers();
1247 void complete_init();
1250 internal::pq::PGresult *pgr, std::shared_ptr<std::string>
const &query,
1251 std::string_view desc =
""sv);
1253 void PQXX_PRIVATE set_up_state();
1255 int PQXX_PRIVATE PQXX_PURE status() const noexcept;
1262 std::
size_t esc_to_buf(std::string_view text,
char *buf) const;
1264 friend class internal::gate::const_connection_largeobject;
1265 char const *PQXX_PURE err_msg() const noexcept;
1267 result exec_prepared(std::string_view statement, internal::c_params const &);
1270 void check_movable() const;
1272 void check_overwritable() const;
1274 friend class internal::gate::connection_errorhandler;
1275 void PQXX_PRIVATE register_errorhandler(errorhandler *);
1276 void PQXX_PRIVATE unregister_errorhandler(errorhandler *) noexcept;
1278 friend class internal::gate::connection_transaction;
1279 result exec(std::string_view, std::string_view = ""sv);
1281 exec(std::shared_ptr<std::
string> const &, std::string_view = ""sv);
1282 void PQXX_PRIVATE register_transaction(transaction_base *);
1283 void PQXX_PRIVATE unregister_transaction(transaction_base *) noexcept;
1285 friend struct internal::gate::connection_stream_from;
1292 std::pair<std::unique_ptr<
char,
void (*)(
void const *)>, std::
size_t>
1295 friend class internal::gate::connection_stream_to;
1296 void PQXX_PRIVATE write_copy_line(std::string_view);
1297 void PQXX_PRIVATE end_copy_write();
1299 friend class internal::gate::connection_largeobject;
1300 internal::pq::PGconn *raw_connection()
const {
return m_conn; }
1302 friend class internal::gate::connection_notification_receiver;
1303 void add_receiver(notification_receiver *);
1304 void remove_receiver(notification_receiver *)
noexcept;
1306 friend class internal::gate::connection_pipeline;
1307 void PQXX_PRIVATE start_exec(
char const query[]);
1308 bool PQXX_PRIVATE consume_input() noexcept;
1309 bool PQXX_PRIVATE is_busy() const noexcept;
1310 internal::pq::PGresult *get_result();
1312 friend class internal::gate::connection_dbtransaction;
1313 friend class internal::gate::connection_sql_cursor;
1315 result exec_params(std::string_view query, internal::c_params const &args);
1318 internal::pq::PGconn *m_conn =
nullptr;
1328 transaction_base const *m_trans =
nullptr;
1331 std::shared_ptr<
pqxx::internal::notice_waiters> m_notice_waiters;
1335 using receiver_list =
1336 std::multimap<std::
string,
pqxx::notification_receiver *>;
1338 receiver_list m_receivers;
1348 std::map<std::
string, notification_handler> m_notification_handlers;
1351 int m_unique_id = 0;
1403class PQXX_LIBEXPORT connecting
1407 connecting(
zview connection_string =
""_zv);
1409 connecting(connecting
const &) =
delete;
1410 connecting(connecting &&) =
default;
1411 connecting &operator=(connecting
const &) =
delete;
1412 connecting &operator=(connecting &&) =
default;
1415 [[nodiscard]]
int sock()
const &
noexcept {
return m_conn.sock(); }
1418 [[nodiscard]]
constexpr bool wait_to_read()
const &
noexcept
1424 [[nodiscard]]
constexpr bool wait_to_write()
const &
noexcept
1433 [[nodiscard]]
constexpr bool done()
const &
noexcept
1435 return not m_reading and not m_writing;
1447 [[nodiscard]] connection produce() &&;
1451 bool m_reading{
false};
1452 bool m_writing{
true};
1456template<
typename T>
inline std::string connection::quote(T
const &t)
const
1458 if constexpr (nullness<T>::always_null)
1466 auto const text{to_string(t)};
1472 std::string buf{
'\''};
1473 buf.resize(2 + 2 * std::size(text) + 1);
1474 auto const content_bytes{esc_to_buf(text, buf.data() + 1)};
1475 auto const closing_quote{1 + content_bytes};
1476 buf[closing_quote] =
'\'';
1477 auto const end{closing_quote + 1};
1484template<PQXX_CHAR_STRINGS_ARG STRINGS>
1485inline std::string connection::quote_columns(STRINGS
const &columns)
const
1488 ","sv, std::cbegin(columns), std::cend(columns),
1489 [
this](
auto col) {
return this->quote_name(*col); });
1493#if defined(PQXX_HAVE_CONCEPTS)
1494template<
internal::ZKey_ZValues MAPPING>
1495inline connection::connection(MAPPING
const ¶ms)
1499 std::vector<char const *> keys, values;
1500 if constexpr (std::ranges::sized_range<MAPPING>)
1502 auto const size{std::ranges::size(params) + 1};
1504 values.reserve(size);
1506 for (
auto const &[key, value] : params)
1508 keys.push_back(internal::as_c_string(key));
1509 values.push_back(internal::as_c_string(value));
1511 keys.push_back(
nullptr);
1512 values.push_back(
nullptr);
1513 init(std::data(keys), std::data(values));
1520 deprecated(
"Use connection::encrypt_password instead.")]] std::string
1526 deprecated(
"Use connection::encrypt_password instead.")]]
inline std::string
1529#include "pqxx/internal/ignore-deprecated-pre.hxx"
1531#include "pqxx/internal/ignore-deprecated-post.hxx"
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition zview.hxx:96
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
void PQXX_LIBEXPORT unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition util.cxx:166
constexpr 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:525
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
std::string encrypt_password(zview user, zview password)
Encrypt password.
Definition connection.hxx:1527
std::string separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition separated_list.hxx:46
constexpr char array_separator
Element separator between SQL array elements of this type.
Definition strconv.hxx:559
PQXX_PRIVATE void check_version() noexcept
Definition util.hxx:236
std::conditional< has_generic_bytes_char_traits, std::basic_string_view< std::byte >, std::basic_string_view< std::byte, byte_char_traits > >::type bytes_view
Type alias for a view of bytes.
Definition util.hxx:383
std::conditional< has_generic_bytes_char_traits, std::basic_string< std::byte >, std::basic_string< std::byte, byte_char_traits > >::type bytes
Type alias for a container containing bytes.
Definition util.hxx:373
void skip_init_ssl() noexcept
Control initialisation of OpenSSL and libcrypto libraries.
Definition connection.hxx:216
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition util.hxx:409
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition connection.hxx:231
connection & conn
The connection which received the notification.
Definition connection.hxx:143
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition strconv.hxx:515
int backend_pid
Process ID of the backend that sent the notification.
Definition connection.hxx:167
skip_init
Flags for skipping initialisation of SSL-related libraries.
Definition connection.hxx:180
@ crypto
Skip initialisation of libcrypto.
Definition connection.hxx:188
@ openssl
Skip initialisation of OpenSSL library.
Definition connection.hxx:185
@ nothing
A do-nothing flag that does not affect anything.
Definition connection.hxx:182
zview channel
Channel name.
Definition connection.hxx:149
connection connection_base
Definition connection.hxx:1356
zview payload
Optional payload text.
Definition connection.hxx:154
error_verbosity
Error verbosity levels.
Definition connection.hxx:236
An incoming notification.
Definition connection.hxx:136