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>
27 #include <string_view>
32 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
83 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
86 concept ZKey_ZValues = std::ranges::input_range<T> and requires(T t) {
88 { std::get<0>(*std::cbegin(t)) } -> ZString;
89 { std::get<1>(*std::cbegin(t)) } -> ZString;
91 #endif // PQXX_HAVE_CONCEPTS
108 class connection_dbtransaction;
302 connection(connection &&rhs);
304 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
321 template<
internal::ZKey_ZValues MAPPING>
322 inline connection(MAPPING
const ¶ms);
323 #endif // PQXX_HAVE_CONCEPTS
331 catch (std::exception
const &)
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;
450 [[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>
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);
746 [[nodiscard]] std::string
754 char const user[],
char const password[],
char const *algorithm =
nullptr);
807 prepare(name.c_str(), definition.c_str());
814 void prepare(
char const name[],
char const definition[]) &;
817 [[deprecated(
"Either name your statement, or just parameterise it.")]]
void
818 prepare(
char const definition[]) &;
819 [[deprecated(
"Either name your statement, or just parameterise it.")]]
void
823 prepare(definition.c_str());
828 void unprepare(std::string_view name);
837 [[nodiscard]] std::string adorn_name(std::string_view);
845 [[nodiscard]] std::string
esc(
char const text[])
const
847 return esc(std::string_view{text});
850 #if defined(PQXX_HAVE_SPAN)
863 [[nodiscard]] std::string_view
864 esc(std::string_view text, std::span<char> buffer)
866 auto const size{std::size(text)}, space{std::size(buffer)};
867 auto const needed{2 * size + 1};
870 "Not enough room to escape string of ", size,
" byte(s): need ",
871 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
872 auto const data{buffer.data()};
873 return {data, esc_to_buf(text, data)};
881 [[nodiscard]] std::string esc(std::string_view text)
const;
883 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
886 template<binary DATA> [[nodiscard]] std::string esc(DATA
const &data)
const
888 return esc_raw(data);
892 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN)
905 template<binary DATA>
906 [[nodiscard]] zview esc(DATA
const &data, std::span<char> buffer)
const
908 auto const size{std::size(data)}, space{std::size(buffer)};
912 "Not enough room to escape binary string of ", size,
" byte(s): need ",
913 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
915 bytes_view view{std::data(data), std::size(data)};
916 auto const out{std::data(buffer)};
920 return zview{out, needed - 1};
925 [[deprecated(
"Use std::byte for binary data.")]] std::string
926 esc_raw(
unsigned char const bin[], std::size_t len)
const;
930 [[nodiscard]] std::string esc_raw(
bytes_view)
const;
932 #if defined(PQXX_HAVE_SPAN)
935 [[nodiscard]] std::string esc_raw(
bytes_view, std::span<char> buffer)
const;
938 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
941 template<binary DATA>
942 [[nodiscard]] std::string esc_raw(DATA
const &data)
const
944 return esc_raw(
bytes_view{std::data(data), std::size(data)});
948 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN)
949 template<binary DATA>
951 [[nodiscard]] zview esc_raw(DATA
const &data, std::span<char> buffer)
const
977 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
980 template<binary DATA>
981 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
983 return quote_raw(
bytes_view{std::data(data), std::size(data)});
989 [[nodiscard]] std::string quote_name(std::string_view identifier)
const;
996 [[nodiscard]] std::string quote_table(std::string_view name)
const;
1008 [[nodiscard]] std::string quote_table(
table_path)
const;
1019 template<PQXX_CHAR_STRINGS_ARG STRINGS>
1020 inline std::string quote_columns(STRINGS
const &columns)
const;
1027 template<
typename T>
1028 [[nodiscard]]
inline std::string quote(T
const &t)
const;
1030 [[deprecated(
"Use std::byte for binary data.")]] std::string
1031 quote(binarystring
const &)
const;
1064 [[nodiscard]] std::string
1065 esc_like(std::string_view text,
char escape_char =
'\\')
const;
1072 [[deprecated(
"Use std::string_view or pqxx:zview.")]] std::string
1073 esc(
char const text[], std::size_t maxlen)
const
1075 return esc(std::string_view{text, maxlen});
1082 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
1086 return unesc_raw(text.
c_str());
1094 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
1095 unesc_raw(
char const text[])
const;
1098 [[deprecated(
"Use quote(bytes_view).")]] std::string
1099 quote_raw(
unsigned char const bin[], std::size_t len)
const;
1107 void cancel_query();
1109 #if defined(_WIN32) || __has_include(<fcntl.h>)
1115 void set_blocking(
bool block) &;
1116 #endif // defined(_WIN32) || __has_include(<fcntl.h>)
1150 m_notice_waiters->notice_handler = std::move(handler);
1159 [[nodiscard, deprecated(
"Use a notice handler instead.")]]
1160 std::vector<errorhandler *> get_errorhandlers()
const;
1169 [[nodiscard]] std::string connection_string()
const;
1202 return std::exchange(m_conn,
nullptr);
1219 [[deprecated(
"To set session variables, use set_session_var.")]]
void
1220 set_variable(std::string_view var, std::string_view value) &;
1226 [[deprecated(
"Use get_var instead.")]] std::string
1227 get_variable(std::string_view);
1246 std::pair<bool, bool> poll_connect();
1249 void init(
char const options[]);
1251 void init(
char const *
params[],
char const *values[]);
1252 void set_up_notice_handlers();
1253 void complete_init();
1257 std::string_view desc =
""sv);
1268 std::
size_t esc_to_buf(std::string_view text,
char *buf) const;
1270 friend class internal::gate::const_connection_largeobject;
1271 char const *
PQXX_PURE err_msg() const noexcept;
1273 result exec_prepared(std::string_view statement, internal::c_params const &);
1276 void check_movable() const;
1278 void check_overwritable() const;
1280 friend class internal::gate::connection_errorhandler;
1282 void PQXX_PRIVATE unregister_errorhandler(errorhandler *) noexcept;
1284 friend class internal::gate::connection_transaction;
1285 result exec(std::string_view, std::string_view = ""sv);
1287 exec(std::shared_ptr<std::
string> const &, std::string_view = ""sv);
1289 void PQXX_PRIVATE unregister_transaction(transaction_base *) noexcept;
1291 friend struct internal::gate::connection_stream_from;
1298 std::pair<std::unique_ptr<
char,
void (*)(
void const *)>, std::
size_t>
1301 friend class internal::gate::connection_stream_to;
1305 friend class internal::gate::connection_largeobject;
1306 internal::pq::
PGconn *raw_connection()
const {
return m_conn; }
1312 friend class internal::gate::connection_pipeline;
1316 internal::pq::
PGresult *get_result();
1318 friend class internal::gate::connection_dbtransaction;
1319 friend class internal::gate::connection_sql_cursor;
1321 result exec_params(std::string_view query, internal::c_params const &args);
1324 internal::pq::
PGconn *m_conn =
nullptr;
1337 std::shared_ptr<
pqxx::internal::notice_waiters> m_notice_waiters;
1341 using receiver_list =
1344 receiver_list m_receivers;
1357 int m_unique_id = 0;
1413 connecting(
zview connection_string =
""_zv);
1415 connecting(connecting
const &) =
delete;
1416 connecting(connecting &&) =
default;
1417 connecting &operator=(connecting
const &) =
delete;
1418 connecting &operator=(connecting &&) =
default;
1421 [[nodiscard]]
int sock() const & noexcept {
return m_conn.sock(); }
1439 [[nodiscard]] constexpr
bool done() const & noexcept
1441 return not m_reading and not m_writing;
1453 [[nodiscard]] connection produce() &&;
1457 bool m_reading{
false};
1458 bool m_writing{
true};
1478 std::string buf{
'\''};
1479 buf.resize(2 + 2 * std::size(text) + 1);
1480 auto const content_bytes{esc_to_buf(text, buf.data() + 1)};
1481 auto const closing_quote{1 + content_bytes};
1482 buf[closing_quote] =
'\'';
1483 auto const end{closing_quote + 1};
1490 template<PQXX_CHAR_STRINGS_ARG STRINGS>
1494 ","sv, std::cbegin(columns), std::cend(columns),
1495 [
this](
auto col) {
return this->quote_name(*col); });
1499 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
1500 template<
internal::ZKey_ZValues MAPPING>
1505 std::vector<char const *> keys, values;
1506 if constexpr (std::ranges::sized_range<MAPPING>)
1508 auto const size{std::ranges::size(params) + 1};
1510 values.reserve(size);
1512 for (
auto const &[key, value] : params)
1517 keys.push_back(
nullptr);
1518 values.push_back(
nullptr);
1519 init(std::data(keys), std::data(values));
1521 #endif // PQXX_HAVE_CONCEPTS
1526 deprecated(
"Use connection::encrypt_password instead.")]] std::string
1532 deprecated(
"Use connection::encrypt_password instead.")]]
inline std::string
connection(zview options)
Connect to a database, using options string.
Definition: connection.hxx:291
std::string concat(TYPE...item)
Efficiently combine a bunch of items into one big string.
Definition: concat.hxx:31
bytes_view binary_cast(TYPE const &data)
End a code block started by "ignore-deprecated-pre.hxx".
Definition: util.hxx:409
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
void set_notice_handler(std::function< void(zview)> handler)
Set a notice handler to the connection.
Definition: connection.hxx:1148
std::string encrypt_password(zview user, zview password, zview algorithm)
Encrypt a password for a given user.
Definition: connection.hxx:748
connection()
Definition: connection.hxx:281
Definition: connection-errorhandler.hxx:11
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
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:96
Definition: connection-stream_to.hxx:8
~connection()
Definition: connection.hxx:325
void PQXX_LIBEXPORT esc_bin(bytes_view binary_data, char buffer[]) noexcept
Hex-escape binary data into a buffer.
Definition: util.cxx:133
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
Definition: connection-pipeline.hxx:8
Internal items for libpqxx' own use. Do not use these yourself.
Definition: encodings.cxx:32
Definition: connection-stream_from.hxx:12
connection(char const options[])
Connect to a database, using options string.
Definition: connection.hxx:284
void prepare(zview name, zview definition)&
Define a prepared statement.
Definition: connection.hxx:805
constexpr bool done() const &noexcept
Is our connection finished?
Definition: connection.hxx:1439
std::string quote_columns(STRINGS const &columns) const
Quote and comma-separate a series of column names.
Definition: connection.hxx:1491
error_verbosity
Error verbosity levels.
Definition: connection.hxx:235
Skip initialisation of libcrypto.
Definition: connection.hxx:188
Result set containing data returned by a query or command.
Definition: result.hxx:91
PQXX_LIBEXPORT std::string to_string(field const &value)
Convert a field to a string.
Definition: connection-largeobject.hxx:26
Definition: connection.hxx:106
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:499
Definition: connection-sql_cursor.hxx:11
void set_session_var(std::string_view var, TYPE const &value)&
Set one of the session variables to a new value.
Definition: connection.hxx:496
static connection seize_raw_connection(internal::pq::PGconn *raw_conn)
Seize control of a raw libpq connection.
Definition: connection.hxx:1189
skip_init
Flags for skipping initialisation of SSL-related libraries.
Definition: connection.hxx:179
void PQXX_COLD PQXX_LIBEXPORT skip_init_ssl(int skips) noexcept
Control OpenSSL/crypto library initialisation.
Definition: connection-notification_receiver.hxx:14
zview payload
Optional payload text.
Definition: connection.hxx:154
An incoming notification.
Definition: connection.hxx:135
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:32
Definition: notification.hxx:56
void PQXX_LIBEXPORT unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition: util.cxx:165
constexpr 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:516
constexpr bool wait_to_read() const &noexcept
Should we currently wait to be able to read from the socket?
Definition: connection.hxx:1424
connection & conn
The connection which received the notification.
Definition: connection.hxx:143
Definition: errorhandler.hxx:45
std::function< void(notification)> notification_handler
A handler callback for incoming notifications on a given channel.
Definition: connection.hxx:682
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:231
zview channel
Channel name.
Definition: connection.hxx:149
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
TYPE get_var_as(std::string_view var)
Read currently applicable value of a variable.
Definition: connection.hxx:523
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:375
A do-nothing flag that does not affect anything.
Definition: connection.hxx:182
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a bytea field.
Definition: connection.hxx:1083
pg_conn PGconn
Definition: libpq-forward.hxx:24
Definition: connection-transaction.hxx:10
int backend_pid
Process ID of the backend that sent the notification.
Definition: connection.hxx:167
The caller attempted to set a variable to null, which is not allowed.
Definition: except.hxx:115
strip_t< decltype(*std::begin(std::declval< CONTAINER >()))> value_type
The type of a container's elements.
Definition: types.hxx:108
PQXX_PRIVATE void check_version() noexcept
Definition: util.hxx:236
std::string esc(char const text[], std::size_t maxlen) const
Escape string for use as SQL string literal on this connection.
Definition: connection.hxx:1073
bytes unesc_bin(std::string_view text) const
Unescape binary data, e.g. from a bytea field.
Definition: connection.hxx:966
Traits describing a type's "null value," if any.
Definition: strconv.hxx:69
void prepare(zview definition)&
Definition: connection.hxx:820
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:385
Connection to a database.
Definition: connection.hxx:278
pg_result PGresult
Definition: libpq-forward.hxx:25
Definition: connection-largeobject.hxx:15
Skip initialisation of OpenSSL library.
Definition: connection.hxx:185
std::string PQXX_LIBEXPORT encrypt_password(char const user[], char const password[])
Encrypt a password.
int sock() const &noexcept
Get the socket. The socket may change during the connection process.
Definition: connection.hxx:1421
std::string esc(char const text[]) const
Escape string for use as SQL string literal on this connection.
Definition: connection.hxx:845
constexpr bool wait_to_write() const &noexcept
Should we currently wait to be able to write to the socket?
Definition: connection.hxx:1430
An ongoing, non-blocking stepping stone to a connection.
Definition: connection.hxx:1409
internal::pq::PGconn * release_raw_connection()&&
Release the raw connection without closing it.
Definition: connection.hxx:1200
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:152
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:150
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: connection.hxx:1462