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> 31 #if defined(PQXX_HAVE_CONCEPTS) && __has_include(<ranges>) 35 #include "pqxx/errorhandler.hxx" 36 #include "pqxx/except.hxx" 37 #include "pqxx/internal/concat.hxx" 38 #include "pqxx/params.hxx" 39 #include "pqxx/separated_list.hxx" 40 #include "pqxx/strconv.hxx" 41 #include "pqxx/types.hxx" 42 #include "pqxx/util.hxx" 43 #include "pqxx/zview.hxx" 80 #if defined(PQXX_HAVE_CONCEPTS) 83 concept ZKey_ZValues = std::ranges::input_range<T> and requires(T t)
87 std::get<0>(*std::cbegin(t))
90 std::get<1>(*std::cbegin(t))
92 } and std::tuple_size_v<typename std::ranges::iterator_t<T>::value_type>
94 #endif // PQXX_HAVE_CONCEPTS 100 class connection_dbtransaction;
101 class connection_errorhandler;
102 class connection_largeobject;
103 class connection_notification_receiver;
104 class connection_pipeline;
105 class connection_sql_cursor;
106 class connection_stream_from;
107 class connection_stream_to;
108 class connection_transaction;
109 class const_connection_largeobject;
128 deprecated(
"Use connection::encrypt_password instead.")]] std::string
134 deprecated(
"Use connection::encrypt_password instead.")]]
inline std::string
137 #include "pqxx/internal/ignore-deprecated-pre.hxx" 139 #include "pqxx/internal/ignore-deprecated-post.hxx" 210 #if defined(PQXX_HAVE_CONCEPTS) 227 template<
internal::ZKey_ZValues MAPPING>
229 #endif // PQXX_HAVE_CONCEPTS 237 catch (std::exception
const &)
245 connection &operator=(connection &&rhs);
247 connection(connection
const &) =
delete;
248 connection &operator=(connection
const &) =
delete;
256 [[nodiscard]]
bool PQXX_PURE is_open()
const noexcept;
259 void process_notice(
char const[]) noexcept;
264 void process_notice(
zview) noexcept;
267 void trace(std::FILE *) noexcept;
279 [[nodiscard]]
char const *dbname()
const;
283 [[nodiscard]]
char const *username()
const;
286 [[nodiscard]]
char const *hostname()
const;
289 [[nodiscard]]
char const *port()
const;
292 [[nodiscard]]
int PQXX_PURE backendpid()
const &noexcept;
305 [[nodiscard]]
int PQXX_PURE sock()
const &noexcept;
311 [[nodiscard]]
int PQXX_PURE protocol_version()
const noexcept;
326 [[nodiscard]]
int PQXX_PURE server_version()
const noexcept;
350 [[nodiscard]] std::string get_client_encoding()
const;
359 set_client_encoding(encoding.c_str());
366 void set_client_encoding(
char const encoding[]) &;
369 [[nodiscard]]
int PQXX_PRIVATE encoding_id()
const;
387 [[deprecated(
"To set session variables, use set_session_var.")]]
void 388 set_variable(std::string_view var, std::string_view value) &;
412 template<
typename TYPE>
419 internal::concat(
"Attempted to set variable ", var,
" to null.")};
421 exec(internal::concat(
"SET ", quote_name(var),
"=", quote(value)));
428 [[deprecated(
"Use get_var instead.")]] std::string
429 get_variable(std::string_view);
438 std::string get_var(std::string_view var);
447 template<
typename TYPE> TYPE
get_var_as(std::string_view var)
449 return from_string<TYPE>(get_var(var));
489 int await_notification();
504 int await_notification(std::time_t seconds,
long microseconds);
537 [[nodiscard]] std::string
545 char const user[],
char const password[],
char const *algorithm =
nullptr);
598 prepare(name.c_str(), definition.c_str());
605 void prepare(
char const name[],
char const definition[]) &;
615 void prepare(
char const definition[]) &;
618 return prepare(definition.c_str());
622 void unprepare(std::string_view name);
631 [[nodiscard]] std::string adorn_name(std::string_view);
643 [[deprecated(
"Use std::string_view or pqxx:zview.")]] std::string
644 esc(
char const text[], std::size_t maxlen)
const 646 return esc(std::string_view{text, maxlen});
650 [[nodiscard]] std::string
esc(
char const text[])
const 652 return esc(std::string_view{text});
655 #if defined(PQXX_HAVE_SPAN) 668 [[nodiscard]] std::string_view
669 esc(std::string_view
text, std::span<char> buffer)
671 auto const size{std::size(text)}, space{std::size(buffer)};
672 auto const needed{2 * size + 1};
675 "Not enough room to escape string of ", size,
" byte(s): need ",
676 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
677 auto const data{buffer.data()};
678 return {data, esc_to_buf(text, data)};
686 [[nodiscard]] std::string esc(std::string_view text)
const;
688 #if defined(PQXX_HAVE_CONCEPTS) 691 template<binary DATA> [[nodiscard]] std::string esc(DATA
const &data)
const 693 return esc_raw(data);
697 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN) 710 template<binary DATA>
711 [[nodiscard]]
zview esc(DATA
const &data, std::span<char> buffer)
const 713 auto const size{std::size(data)}, space{std::size(buffer)};
717 "Not enough room to escape binary string of ", size,
" byte(s): need ",
718 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
720 std::basic_string_view<std::byte> view{std::data(data), std::size(data)};
721 auto const out{std::data(buffer)};
725 return zview{out, needed - 1};
730 [[deprecated(
"Use std::byte for binary data.")]] std::string
731 esc_raw(
unsigned char const bin[], std::size_t len)
const;
735 [[nodiscard]] std::string esc_raw(std::basic_string_view<std::byte>)
const;
737 #if defined(PQXX_HAVE_SPAN) 740 [[nodiscard]] std::string
741 esc_raw(std::basic_string_view<std::byte>, std::span<char> buffer)
const;
744 #if defined(PQXX_HAVE_CONCEPTS) 747 template<binary DATA>
748 [[nodiscard]] std::string esc_raw(DATA
const &data)
const 751 std::basic_string_view<std::byte>{std::data(data), std::size(data)});
755 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN) 756 template<binary DATA>
758 [[nodiscard]]
zview esc_raw(DATA
const &data, std::span<char> buffer)
const 768 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
771 #include "pqxx/internal/ignore-deprecated-pre.hxx" 772 return unesc_raw(text.
c_str());
773 #include "pqxx/internal/ignore-deprecated-post.hxx" 780 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
781 unesc_raw(
char const text[])
const;
792 [[nodiscard]] std::basic_string<std::byte>
795 std::basic_string<std::byte> buf;
802 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
803 quote_raw(
unsigned char const bin[], std::size_t len)
const;
806 std::string quote_raw(std::basic_string_view<std::byte>)
const;
808 #if defined(PQXX_HAVE_CONCEPTS) 811 template<binary DATA>
812 [[nodiscard]] std::string quote_raw(DATA
const &data)
const 815 std::basic_string_view<std::byte>{std::data(data), std::size(data)});
821 [[nodiscard]] std::string quote_name(std::string_view identifier)
const;
828 [[nodiscard]] std::string quote_table(std::string_view name)
const;
840 [[nodiscard]] std::string quote_table(
table_path)
const;
851 template<PQXX_CHAR_STRINGS_ARG STRINGS>
852 inline std::string quote_columns(STRINGS
const &columns)
const;
860 [[nodiscard]]
inline std::string quote(T
const &t)
const;
862 [[deprecated(
"Use std::byte for binary data.")]] std::string
867 [[nodiscard]] std::string
868 quote(std::basic_string_view<std::byte> bytes)
const;
897 [[nodiscard]] std::string
898 esc_like(std::string_view text,
char escape_char =
'\\')
const;
908 #if defined(_WIN32) || __has_include(<fcntl.h>) 914 void set_blocking(
bool block) &;
915 #endif // defined(_WIN32) || __has_include(<fcntl.h>) 942 [[nodiscard]] std::vector<errorhandler *> get_errorhandlers()
const;
951 [[nodiscard]] std::string connection_string()
const;
973 return connection{raw_conn};
984 return std::exchange(m_conn,
nullptr);
993 connection(connect_mode,
zview connection_string);
996 explicit connection(internal::pq::PGconn *raw_conn) : m_conn{raw_conn} {}
1004 std::pair<bool, bool> poll_connect();
1007 void init(
char const options[]);
1009 void init(
char const *params[],
char const *values[]);
1010 void complete_init();
1013 internal::pq::PGresult *pgr, std::shared_ptr<std::string>
const &query,
1014 std::string_view desc =
""sv);
1016 void PQXX_PRIVATE set_up_state();
1018 int PQXX_PRIVATE PQXX_PURE status() const noexcept;
1025 std::
size_t esc_to_buf(
std::string_view text,
char *buf) const;
1027 friend class internal::gate::const_connection_largeobject;
1028 char const *PQXX_PURE err_msg() const noexcept;
1030 void PQXX_PRIVATE process_notice_raw(
char const msg[]) noexcept;
1032 result exec_prepared(
std::string_view statement, internal::c_params const &);
1035 void check_movable() const;
1037 void check_overwritable() const;
1039 friend class internal::gate::connection_errorhandler;
1040 void PQXX_PRIVATE register_errorhandler(
errorhandler *);
1041 void PQXX_PRIVATE unregister_errorhandler(errorhandler *) noexcept;
1043 friend class internal::gate::connection_transaction;
1044 result exec(
std::string_view,
std::string_view = ""sv);
1046 PQXX_PRIVATE exec(
std::shared_ptr<
std::
string>,
std::string_view = ""sv);
1048 void PQXX_PRIVATE unregister_transaction(transaction_base *) noexcept;
1050 friend class internal::gate::connection_stream_from;
1051 std::pair<
std::unique_ptr<
char,
std::function<
void(
char *)>>,
std::
size_t>
1052 PQXX_PRIVATE read_copy_line();
1054 friend class internal::gate::connection_stream_to;
1055 void PQXX_PRIVATE write_copy_line(
std::string_view);
1056 void PQXX_PRIVATE end_copy_write();
1058 friend class internal::gate::connection_largeobject;
1059 internal::pq::PGconn *raw_connection()
const 1064 friend class internal::gate::connection_notification_receiver;
1068 friend class internal::gate::connection_pipeline;
1069 void PQXX_PRIVATE start_exec(
char const query[]);
1070 bool PQXX_PRIVATE consume_input() noexcept;
1071 bool PQXX_PRIVATE is_busy() const noexcept;
1072 internal::pq::PGresult *get_result();
1074 friend class internal::gate::connection_dbtransaction;
1075 friend class internal::gate::connection_sql_cursor;
1077 result exec_params(
std::string_view query, internal::c_params const &args);
1080 internal::pq::PGconn *m_conn =
nullptr;
1094 using receiver_list =
1097 receiver_list m_receivers;
1100 int m_unique_id = 0;
1156 connecting(
zview connection_string =
""_zv);
1158 connecting(connecting
const &) =
delete;
1159 connecting(connecting &&) =
default;
1160 connecting &operator=(connecting
const &) =
delete;
1161 connecting &operator=(connecting &&) =
default;
1164 [[nodiscard]]
int sock() const &noexcept {
return m_conn.sock(); }
1182 [[nodiscard]] constexpr
bool done() const &noexcept
1184 return not m_reading and not m_writing;
1196 [[nodiscard]] connection produce() &&;
1200 bool m_reading{
false};
1201 bool m_writing{
true};
1221 std::string buf{
'\''};
1222 buf.resize(2 + 2 * std::size(text) + 1);
1223 auto const content_bytes{esc_to_buf(text, buf.data() + 1)};
1224 auto const closing_quote{1 + content_bytes};
1225 buf[closing_quote] =
'\'';
1226 auto const end{closing_quote + 1};
1233 template<PQXX_CHAR_STRINGS_ARG STRINGS>
1237 ","sv, std::cbegin(columns), std::cend(columns),
1238 [
this](
auto col) {
return this->quote_name(*col); });
1242 #if defined(PQXX_HAVE_CONCEPTS) 1243 template<
internal::ZKey_ZValues MAPPING>
1248 std::vector<char const *> keys, values;
1249 if constexpr (std::ranges::sized_range<MAPPING>)
1251 auto const size{std::ranges::size(params) + 1};
1253 values.reserve(size);
1255 for (
auto const &[key, value] : params)
1260 keys.push_back(
nullptr);
1261 values.push_back(
nullptr);
1262 init(std::data(keys), std::data(values));
1264 #endif // PQXX_HAVE_CONCEPTS Something is out of range, similar to std::out_of_range.
Definition: except.hxx:201
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:413
std::string esc(char const text[]) const
Escape string for use as SQL string literal on this connection.
Definition: connection.hxx:650
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
std::string quote_columns(STRINGS const &columns) const
Quote and comma-separate a series of column names.
Definition: connection.hxx:1234
TYPE get_var_as(std::string_view var)
Read currently applicable value of a variable.
Definition: connection.hxx:447
static connection seize_raw_connection(internal::pq::PGconn *raw_conn)
Seize control of a raw libpq connection.
Definition: connection.hxx:971
Connection to a database.
Definition: connection.hxx:184
void prepare(zview name, zview definition) &
Define a prepared statement.
Definition: connection.hxx:596
constexpr bool wait_to_read() const &noexcept
Should we currently wait to be able to read from the socket?
Definition: connection.hxx:1167
std::string encrypt_password(char const user[], char const password[])
Encrypt a password.
Definition: connection.cxx:80
The caller attempted to set a variable to null, which is not allowed.
Definition: except.hxx:76
connection(zview options)
Connect to a database, using options string.
Definition: connection.hxx:197
std::string encrypt_password(zview user, zview password, zview algorithm)
Encrypt a password for a given user.
Definition: connection.hxx:539
~connection()
Definition: connection.hxx:231
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:533
void set_session_var(std::string_view var, TYPE const &value) &
Set one of the session variables to a new value.
Definition: connection.hxx:413
void check_version() noexcept
Definition: util.hxx:237
error_verbosity
Error verbosity levels.
Definition: connection.hxx:144
void esc_bin(std::basic_string_view< std::byte > binary_data, char buffer[]) noexcept
Hex-escape binary data into a buffer.
Definition: util.cxx:126
internal::pq::PGconn * release_raw_connection() &&
Release the raw connection without closing it.
Definition: connection.hxx:982
Definition: notification.hxx:56
constexpr bool done() const &noexcept
Is our connection finished?
Definition: connection.hxx:1182
Definition: connection.hxx:98
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:141
constexpr bool wait_to_write() const &noexcept
Should we currently wait to be able to write to the socket?
Definition: connection.hxx:1173
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:76
connection()
Definition: connection.hxx:187
Result set containing data returned by a query or command.
Definition: result.hxx:73
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:82
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: connection.hxx:1205
void set_client_encoding(zview encoding) &
Set client-side character encoding, by name.
Definition: connection.hxx:357
Base class for error-handler callbacks.
Definition: errorhandler.hxx:53
std::basic_string< std::byte > unesc_bin(std::string_view text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: connection.hxx:793
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:422
constexpr char const * c_str() const &noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:92
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: connection.hxx:769
void unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition: util.cxx:158
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:58
void prepare(zview definition) &
Definition: connection.hxx:616
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:644
Traits describing a type's "null value," if any.
Definition: strconv.hxx:92
std::basic_string_view< std::byte > binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:306
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:43
connection(char const options[])
Connect to a database, using options string.
Definition: connection.hxx:190
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:123
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:370
An ongoing, non-blocking stepping stone to a connection.
Definition: connection.hxx:1152
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:219
int sock() const &noexcept
Get the socket. The socket may change during the connection process.
Definition: connection.hxx:1164