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) && __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" 81 #if defined(PQXX_HAVE_CONCEPTS) 84 concept ZKey_ZValues = std::ranges::input_range<T> and requires(T t)
88 std::get<0>(*std::cbegin(t))
91 std::get<1>(*std::cbegin(t))
93 } and std::tuple_size_v<typename std::ranges::iterator_t<T>::value_type>
95 #endif // PQXX_HAVE_CONCEPTS 101 class connection_dbtransaction;
102 class connection_errorhandler;
103 class connection_largeobject;
104 class connection_notification_receiver;
105 class connection_pipeline;
106 class connection_sql_cursor;
107 class connection_stream_from;
108 class connection_stream_to;
109 class connection_transaction;
110 class const_connection_largeobject;
129 deprecated(
"Use connection::encrypt_password instead.")]] std::string
135 deprecated(
"Use connection::encrypt_password instead.")]]
inline std::string
138 #include "pqxx/internal/ignore-deprecated-pre.hxx" 140 #include "pqxx/internal/ignore-deprecated-post.hxx" 211 #if defined(PQXX_HAVE_CONCEPTS) 228 template<
internal::ZKey_ZValues MAPPING>
230 #endif // PQXX_HAVE_CONCEPTS 238 catch (std::exception
const &)
246 connection &operator=(connection &&rhs);
248 connection(connection
const &) =
delete;
249 connection &operator=(connection
const &) =
delete;
257 [[nodiscard]]
bool PQXX_PURE is_open()
const noexcept;
260 void process_notice(
char const[]) noexcept;
265 void process_notice(
zview) noexcept;
268 void trace(std::FILE *) noexcept;
280 [[nodiscard]]
char const *dbname()
const;
284 [[nodiscard]]
char const *username()
const;
287 [[nodiscard]]
char const *hostname()
const;
290 [[nodiscard]]
char const *port()
const;
293 [[nodiscard]]
int PQXX_PURE backendpid()
const &noexcept;
306 [[nodiscard]]
int PQXX_PURE sock()
const &noexcept;
312 [[nodiscard]]
int PQXX_PURE protocol_version()
const noexcept;
327 [[nodiscard]]
int PQXX_PURE server_version()
const noexcept;
351 [[nodiscard]] std::string get_client_encoding()
const;
360 set_client_encoding(encoding.c_str());
367 void set_client_encoding(
char const encoding[]) &;
370 [[nodiscard]]
int PQXX_PRIVATE encoding_id()
const;
388 [[deprecated(
"To set session variables, use set_session_var.")]]
void 389 set_variable(std::string_view var, std::string_view value) &;
413 template<
typename TYPE>
420 internal::concat(
"Attempted to set variable ", var,
" to null.")};
422 exec(internal::concat(
"SET ", quote_name(var),
"=", quote(value)));
429 [[deprecated(
"Use get_var instead.")]] std::string
430 get_variable(std::string_view);
439 std::string get_var(std::string_view var);
448 template<
typename TYPE> TYPE
get_var_as(std::string_view var)
450 return from_string<TYPE>(get_var(var));
490 int await_notification();
505 int await_notification(std::time_t seconds,
long microseconds);
538 [[nodiscard]] std::string
546 char const user[],
char const password[],
char const *algorithm =
nullptr);
599 prepare(name.c_str(), definition.c_str());
606 void prepare(
char const name[],
char const definition[]) &;
616 void prepare(
char const definition[]) &;
620 void unprepare(std::string_view name);
629 [[nodiscard]] std::string adorn_name(std::string_view);
641 [[deprecated(
"Use std::string_view or pqxx:zview.")]] std::string
642 esc(
char const text[], std::size_t maxlen)
const 644 return esc(std::string_view{text, maxlen});
648 [[nodiscard]] std::string
esc(
char const text[])
const 650 return esc(std::string_view{text});
653 #if defined(PQXX_HAVE_SPAN) 666 [[nodiscard]] std::string_view
667 esc(std::string_view
text, std::span<char> buffer)
669 auto const size{std::size(text)}, space{std::size(buffer)};
670 auto const needed{2 * size + 1};
673 "Not enough room to escape string of ", size,
" byte(s): need ",
674 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
675 auto const data{buffer.data()};
676 return {data, esc_to_buf(text, data)};
684 [[nodiscard]] std::string esc(std::string_view text)
const;
686 #if defined(PQXX_HAVE_CONCEPTS) 689 template<binary DATA> [[nodiscard]] std::string esc(DATA
const &data)
const 691 return esc_raw(data);
695 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN) 708 template<binary DATA>
709 [[nodiscard]]
zview esc(DATA
const &data, std::span<char> buffer)
const 711 auto const size{std::size(data)}, space{std::size(buffer)};
715 "Not enough room to escape binary string of ", size,
" byte(s): need ",
716 needed,
" bytes of buffer space, but buffer size is ", space,
".")};
718 std::basic_string_view<std::byte> view{std::data(data), std::size(data)};
719 auto const out{std::data(buffer)};
723 return zview{out, needed - 1};
728 [[deprecated(
"Use std::byte for binary data.")]] std::string
729 esc_raw(
unsigned char const bin[], std::size_t len)
const;
733 [[nodiscard]] std::string esc_raw(std::basic_string_view<std::byte>)
const;
735 #if defined(PQXX_HAVE_SPAN) 738 [[nodiscard]] std::string
739 esc_raw(std::basic_string_view<std::byte>, std::span<char> buffer)
const;
742 #if defined(PQXX_HAVE_CONCEPTS) 745 template<binary DATA>
746 [[nodiscard]] std::string esc_raw(DATA
const &data)
const 749 std::basic_string_view<std::byte>{std::data(data), std::size(data)});
753 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_SPAN) 754 template<binary DATA>
756 [[nodiscard]]
zview esc_raw(DATA
const &data, std::span<char> buffer)
const 766 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
769 #include "pqxx/internal/ignore-deprecated-pre.hxx" 770 return unesc_raw(text.
c_str());
771 #include "pqxx/internal/ignore-deprecated-post.hxx" 778 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
779 unesc_raw(
char const text[])
const;
790 [[nodiscard]] std::basic_string<std::byte>
793 std::basic_string<std::byte> buf;
800 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
801 quote_raw(
unsigned char const bin[], std::size_t len)
const;
804 std::string quote_raw(std::basic_string_view<std::byte>)
const;
806 #if defined(PQXX_HAVE_CONCEPTS) 809 template<binary DATA>
810 [[nodiscard]] std::string quote_raw(DATA
const &data)
const 813 std::basic_string_view<std::byte>{std::data(data), std::size(data)});
819 [[nodiscard]] std::string quote_name(std::string_view identifier)
const;
826 [[nodiscard]] std::string quote_table(std::string_view name)
const;
838 [[nodiscard]] std::string quote_table(
table_path)
const;
849 template<PQXX_CHAR_STRINGS_ARG STRINGS>
850 inline std::string quote_columns(STRINGS
const &columns)
const;
858 [[nodiscard]]
inline std::string quote(T
const &t)
const;
860 [[deprecated(
"Use std::byte for binary data.")]] std::string
865 [[nodiscard]] std::string
866 quote(std::basic_string_view<std::byte> bytes)
const;
895 [[nodiscard]] std::string
896 esc_like(std::string_view text,
char escape_char =
'\\')
const;
906 #if defined(_WIN32) || __has_include(<fcntl.h>) 907 void set_blocking(
bool block) &;
909 #endif // defined(_WIN32) || __has_include(<fcntl.h>) 936 [[nodiscard]] std::vector<errorhandler *> get_errorhandlers()
const;
945 [[nodiscard]] std::string connection_string()
const;
967 return connection{raw_conn};
978 return std::exchange(m_conn,
nullptr);
987 connection(connect_mode,
zview connection_string);
990 explicit connection(internal::pq::PGconn *raw_conn) : m_conn{raw_conn} {}
998 std::pair<bool, bool> poll_connect();
1001 void init(
char const options[]);
1003 void init(
char const *params[],
char const *values[]);
1004 void complete_init();
1007 internal::pq::PGresult *pgr, std::shared_ptr<std::string>
const &query,
1008 std::string_view desc =
""sv);
1010 void PQXX_PRIVATE set_up_state();
1012 int PQXX_PRIVATE PQXX_PURE status() const noexcept;
1019 std::
size_t esc_to_buf(
std::string_view text,
char *buf) const;
1021 friend class internal::gate::const_connection_largeobject;
1022 char const *PQXX_PURE err_msg() const noexcept;
1024 void PQXX_PRIVATE process_notice_raw(
char const msg[]) noexcept;
1026 result exec_prepared(
std::string_view statement, internal::c_params const &);
1029 void check_movable() const;
1031 void check_overwritable() const;
1033 friend class internal::gate::connection_errorhandler;
1034 void PQXX_PRIVATE register_errorhandler(
errorhandler *);
1035 void PQXX_PRIVATE unregister_errorhandler(errorhandler *) noexcept;
1037 friend class internal::gate::connection_transaction;
1038 result exec(
std::string_view,
std::string_view = ""sv);
1040 PQXX_PRIVATE exec(
std::shared_ptr<
std::
string>,
std::string_view = ""sv);
1042 void PQXX_PRIVATE unregister_transaction(transaction_base *) noexcept;
1044 friend class internal::gate::connection_stream_from;
1045 std::pair<
std::unique_ptr<
char,
std::function<
void(
char *)>>,
std::
size_t>
1046 PQXX_PRIVATE read_copy_line();
1048 friend class internal::gate::connection_stream_to;
1049 void PQXX_PRIVATE write_copy_line(
std::string_view);
1050 void PQXX_PRIVATE end_copy_write();
1052 friend class internal::gate::connection_largeobject;
1053 internal::pq::PGconn *raw_connection()
const {
return m_conn; }
1055 friend class internal::gate::connection_notification_receiver;
1059 friend class internal::gate::connection_pipeline;
1060 void PQXX_PRIVATE start_exec(
char const query[]);
1061 bool PQXX_PRIVATE consume_input() noexcept;
1062 bool PQXX_PRIVATE is_busy() const noexcept;
1063 internal::pq::PGresult *get_result();
1065 friend class internal::gate::connection_dbtransaction;
1066 friend class internal::gate::connection_sql_cursor;
1068 result exec_params(
std::string_view query, internal::c_params const &args);
1071 internal::pq::PGconn *m_conn =
nullptr;
1085 using receiver_list =
1088 receiver_list m_receivers;
1091 int m_unique_id = 0;
1147 connecting(
zview connection_string =
""_zv);
1149 connecting(connecting
const &) =
delete;
1150 connecting(connecting &&) =
default;
1151 connecting &operator=(connecting
const &) =
delete;
1152 connecting &operator=(connecting &&) =
default;
1155 [[nodiscard]]
int sock() const &noexcept {
return m_conn.sock(); }
1173 [[nodiscard]] constexpr
bool done() const &noexcept
1175 return not m_reading and not m_writing;
1187 [[nodiscard]] connection produce() &&;
1191 bool m_reading{
false};
1192 bool m_writing{
true};
1212 std::string buf{
'\''};
1213 buf.resize(2 + 2 * std::size(text) + 1);
1214 auto const content_bytes{esc_to_buf(text, buf.data() + 1)};
1215 auto const closing_quote{1 + content_bytes};
1216 buf[closing_quote] =
'\'';
1217 auto const end{closing_quote + 1};
1224 template<PQXX_CHAR_STRINGS_ARG STRINGS>
1228 ","sv, std::cbegin(columns), std::cend(columns),
1229 [
this](
auto col) {
return this->quote_name(*col); });
1233 #if defined(PQXX_HAVE_CONCEPTS) 1234 template<
internal::ZKey_ZValues MAPPING>
1239 std::vector<char const *> keys, values;
1240 if constexpr (std::ranges::sized_range<MAPPING>)
1242 auto const size{std::ranges::size(params) + 1};
1244 values.reserve(size);
1246 for (
auto const &[key, value] : params)
1251 keys.push_back(
nullptr);
1252 values.push_back(
nullptr);
1253 init(std::data(keys), std::data(values));
1255 #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:404
std::string esc(char const text[]) const
Escape string for use as SQL string literal on this connection.
Definition: connection.hxx:648
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:1225
TYPE get_var_as(std::string_view var)
Read currently applicable value of a variable.
Definition: connection.hxx:448
static connection seize_raw_connection(internal::pq::PGconn *raw_conn)
Seize control of a raw libpq connection.
Definition: connection.hxx:965
Connection to a database.
Definition: connection.hxx:185
void prepare(zview name, zview definition) &
Define a prepared statement.
Definition: connection.hxx:597
constexpr bool wait_to_read() const &noexcept
Should we currently wait to be able to read from the socket?
Definition: connection.hxx:1158
std::string encrypt_password(char const user[], char const password[])
Encrypt a password.
Definition: connection.cxx:81
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:198
std::string encrypt_password(zview user, zview password, zview algorithm)
Encrypt a password for a given user.
Definition: connection.hxx:540
~connection()
Definition: connection.hxx:232
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:414
void check_version() noexcept
Definition: util.hxx:228
error_verbosity
Error verbosity levels.
Definition: connection.hxx:145
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:976
Definition: notification.hxx:56
constexpr bool done() const &noexcept
Is our connection finished?
Definition: connection.hxx:1173
Definition: connection.hxx:99
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:1164
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:76
connection()
Definition: connection.hxx:188
Result set containing data returned by a query or command.
Definition: result.hxx:72
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:1196
void set_client_encoding(zview encoding) &
Set client-side character encoding, by name.
Definition: connection.hxx:358
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:791
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:413
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:767
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:617
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:642
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:297
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:191
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:124
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:367
An ongoing, non-blocking stepping stone to a connection.
Definition: connection.hxx:1143
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:1155