13 #ifndef PQXX_H_CONNECTION
14 #define PQXX_H_CONNECTION
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
22 #include <initializer_list>
26 #include <string_view>
30 #if defined(PQXX_HAVE_CONCEPTS) && __has_include(<ranges>)
34 #include "pqxx/errorhandler.hxx"
35 #include "pqxx/except.hxx"
36 #include "pqxx/prepared_statement.hxx"
37 #include "pqxx/separated_list.hxx"
38 #include "pqxx/strconv.hxx"
39 #include "pqxx/util.hxx"
40 #include "pqxx/zview.hxx"
74 #if defined(PQXX_HAVE_CONCEPTS)
77 concept ZKey_ZValues = std::ranges::input_range<T> and requires()
79 {std::tuple_size<typename std::ranges::iterator_t<T>::value_type>::value};
81 and std::tuple_size_v<typename std::ranges::iterator_t<T>::value_type> == 2 and
85 std::get<0>(*std::cbegin(t))
89 std::get<1>(*std::cbegin(t))
99 class connection_dbtransaction;
100 class connection_errorhandler;
101 class connection_largeobject;
102 class connection_notification_receiver;
103 class connection_pipeline;
104 class connection_sql_cursor;
105 class connection_stream_from;
106 class connection_stream_to;
107 class connection_transaction;
108 class const_connection_largeobject;
127 deprecated(
"Use connection::encrypt_password instead.")]] std::string
133 deprecated(
"Use connection::encrypt_password instead.")]]
inline std::string
136 #include "pqxx/internal/ignore-deprecated-pre.hxx"
138 #include "pqxx/internal/ignore-deprecated-post.hxx"
209 #if defined(PQXX_HAVE_CONCEPTS)
226 template<
internal::ZKey_ZValues MAPPING>
236 catch (std::exception
const &)
254 [[nodiscard]]
bool PQXX_PURE is_open() const noexcept;
257 void process_notice(
char const[]) noexcept;
262 void process_notice(
zview) noexcept;
265 void trace(std::FILE *) noexcept;
278 [[nodiscard]]
char const *dbname() const;
281 [[nodiscard]]
char const *username() const;
284 [[nodiscard]]
char const *hostname() const;
287 [[nodiscard]]
char const *port() const;
290 [[nodiscard]]
int PQXX_PURE backendpid() const noexcept;
308 [[nodiscard]]
int PQXX_PURE sock() const noexcept;
314 [[nodiscard]]
int PQXX_PURE protocol_version() const noexcept;
329 [[nodiscard]]
int PQXX_PURE server_version() const noexcept;
354 [[nodiscard]] std::
string get_client_encoding() const;
360 void set_client_encoding(
zview encoding)
362 set_client_encoding(encoding.c_str());
369 void set_client_encoding(
char const encoding[]);
372 [[nodiscard]]
int PQXX_PRIVATE encoding_id()
const;
392 void set_variable(std::string_view var, std::string_view value);
398 std::string get_variable(std::string_view);
431 int await_notification();
442 int await_notification(std::time_t seconds,
long microseconds);
476 [[nodiscard]] std::string
483 char const user[],
char const password[],
char const *algorithm =
nullptr);
543 void prepare(
char const name[],
char const definition[]);
553 void prepare(
char const definition[]);
557 void unprepare(std::string_view name);
565 [[nodiscard]] std::string adorn_name(std::string_view);
578 [[nodiscard]] std::string
esc(
char const text[], std::size_t maxlen)
const
580 return esc(std::string_view(
text, maxlen));
585 [[nodiscard]] std::string
esc(
char const text[])
const
587 return esc(std::string_view(
text));
595 [[nodiscard]] std::string esc(std::string_view text)
const;
601 [[deprecated(
"Use std::byte for binary data.")]] std::string
602 esc_raw(
unsigned char const bin[], std::size_t len)
const;
606 [[nodiscard]] std::string esc_raw(std::basic_string_view<std::byte>)
const;
615 return unesc_raw(
text.c_str());
623 [[nodiscard]] std::string unesc_raw(
char const text[])
const;
628 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
629 quote_raw(
unsigned char const bin[], std::size_t len)
const;
632 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
633 quote_raw(std::basic_string_view<std::byte>)
const;
637 [[nodiscard]] std::string quote_name(std::string_view identifier)
const;
644 [[nodiscard]] std::string quote_table(std::string_view name)
const;
656 [[nodiscard]] std::string quote_table(
table_path)
const;
668 template<
typename CONTAINER>
669 inline std::enable_if_t<
670 std::is_convertible_v<typename CONTAINER::value_type, std::string_view>,
672 quote_columns(CONTAINER
const &columns)
const;
680 [[nodiscard]]
inline std::string quote(T
const &t)
const;
683 [[nodiscard, deprecated(
"Use std::byte for binary data.")]] std::string
688 [[nodiscard]] std::string
689 quote(std::basic_string_view<std::byte> bytes)
const;
716 [[nodiscard]] std::string
717 esc_like(std::string_view text,
char escape_char =
'\\')
const;
752 [[nodiscard]] std::vector<errorhandler *> get_errorhandlers()
const;
761 [[nodiscard]] std::string connection_string()
const;
771 void init(
char const options[]);
773 void init(
char const *
params[],
char const *values[]);
774 void complete_init();
776 void wait_read()
const;
777 void wait_read(std::time_t seconds,
long microseconds)
const;
780 internal::pq::PGresult *pgr, std::shared_ptr<std::string>
const &query,
781 std::string_view desc =
""sv);
783 void PQXX_PRIVATE set_up_state();
785 int PQXX_PRIVATE PQXX_PURE status() const noexcept;
792 std::
size_t esc_to_buf(std::string_view text,
char *buf) const;
794 friend class internal::gate::const_connection_largeobject;
795 char const *PQXX_PURE err_msg() const noexcept;
797 void PQXX_PRIVATE process_notice_raw(
char const msg[]) noexcept;
799 result exec_prepared(std::string_view statement, internal::c_params const &);
802 void check_movable() const;
804 void check_overwritable() const;
806 friend class internal::gate::connection_errorhandler;
807 void PQXX_PRIVATE register_errorhandler(
errorhandler *);
808 void PQXX_PRIVATE unregister_errorhandler(
errorhandler *) noexcept;
810 friend class internal::gate::connection_transaction;
811 result PQXX_PRIVATE exec(std::string_view, std::string_view = ""sv);
813 PQXX_PRIVATE exec(std::shared_ptr<std::
string>, std::string_view = ""sv);
817 friend class internal::gate::connection_stream_from;
818 std::pair<std::unique_ptr<
char, std::function<
void(
char *)>>, std::
size_t>
819 PQXX_PRIVATE read_copy_line();
821 friend class internal::gate::connection_stream_to;
822 void PQXX_PRIVATE write_copy_line(std::string_view);
823 void PQXX_PRIVATE end_copy_write();
825 friend class internal::gate::connection_largeobject;
826 internal::pq::PGconn *raw_connection()
const {
return m_conn; }
828 friend class internal::gate::connection_notification_receiver;
832 friend class internal::gate::connection_pipeline;
833 void PQXX_PRIVATE start_exec(
char const query[]);
834 bool PQXX_PRIVATE consume_input() noexcept;
835 bool PQXX_PRIVATE is_busy() const noexcept;
836 internal::pq::PGresult *get_result();
838 friend class internal::gate::connection_dbtransaction;
839 friend class internal::gate::connection_sql_cursor;
841 result exec_params(std::string_view query, internal::c_params const &args);
844 internal::pq::PGconn *m_conn =
nullptr;
858 using receiver_list =
861 receiver_list m_receivers;
872 template<typename T> inline std::
string connection::quote(T const &t)
const
888 std::string buf{
'\''};
889 buf.resize(2 + 2 * std::size(
text) + 1);
890 auto const content_bytes{esc_to_buf(
text, buf.data() + 1)};
891 auto const closing_quote{1 + content_bytes};
892 buf[closing_quote] =
'\'';
893 auto const end{closing_quote + 1};
900 template<
typename CONTAINER>
901 inline std::enable_if_t<
902 std::is_convertible_v<typename CONTAINER::value_type, std::string_view>,
907 ","sv, std::cbegin(columns), std::cend(columns),
908 [
this](
auto col) {
return this->quote_name(*col); });
912 #if defined(PQXX_HAVE_CONCEPTS)
913 template<
internal::ZKey_ZValues MAPPING>
918 std::vector<char const *> keys, values;
919 if constexpr (std::ranges::sized_range<MAPPING>)
921 auto const size{std::ranges::size(
params) + 1};
923 values.reserve(size);
925 for (
auto const &[key, value] : params)
930 keys.push_back(
nullptr);
931 values.push_back(
nullptr);
932 init(keys.data(), values.data());
940 PQXX_LIBEXPORT
void wait_read(internal::pq::PGconn
const *);
942 internal::pq::PGconn
const *, std::time_t seconds,
long microseconds);
943 PQXX_LIBEXPORT
void wait_write(internal::pq::PGconn
const *);
946 #include "pqxx/internal/compiler-internal-post.hxx"
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
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:42
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:122
void check_version()
Definition: util.hxx:171
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:503
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:353
error_verbosity
Error verbosity levels.
Definition: connection.hxx:144
std::string encrypt_password(char const user[], char const password[])
Encrypt a password.
Definition: connection.cxx:95
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:74
void wait_write(internal::pq::PGconn const *)
Definition: connection.cxx:1011
constexpr char const * as_c_string(char const str[]) noexcept
Get a raw C string pointer.
Definition: zview.hxx:121
void wait_read(internal::pq::PGconn const *)
Definition: connection.cxx:992
Definition: connection.hxx:98
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:57
Connection to a database.
Definition: connection.hxx:184
std::string encrypt_password(zview user, zview password, zview algorithm)
Encrypt a password for a given user.
Definition: connection.hxx:477
connection(char const options[])
Connect to a database, using options string.
Definition: connection.hxx:189
connection()
Definition: connection.hxx:186
~connection()
Definition: connection.hxx:230
void prepare(zview name, zview definition)
Define a prepared statement.
Definition: connection.hxx:534
connection(zview options)
Connect to a database, using options string.
Definition: connection.hxx:196
std::string esc(char const text[]) const
Escape string for use as SQL string literal on this connection.
Definition: connection.hxx:585
void prepare(zview definition)
Definition: connection.hxx:554
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: connection.hxx:613
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:578
std::enable_if_t< std::is_convertible_v< typename CONTAINER::value_type, std::string_view >, std::string > quote_columns(CONTAINER const &columns) const
Quote and comma-separate a series of column names.
Definition: connection.hxx:904
connection & operator=(connection const &)=delete
connection(connection const &)=delete
Base class for error-handler callbacks.
Definition: errorhandler.hxx:53
Definition: notification.hxx:56
Build a parameter list for a parameterised or prepared statement.
Definition: prepared_statement.hxx:122
Result set containing data returned by a query or command.
Definition: result.hxx:71
Traits describing a type's "null value," if any.
Definition: strconv.hxx:87
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:75
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:85