14#ifndef PQXX_H_TRANSACTION_BASE
15#define PQXX_H_TRANSACTION_BASE
17#if !defined(PQXX_HEADER_PRE)
18# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
32#include "pqxx/connection.hxx"
33#include "pqxx/internal/concat.hxx"
34#include "pqxx/internal/encoding_group.hxx"
35#include "pqxx/isolation.hxx"
36#include "pqxx/result.hxx"
37#include "pqxx/row.hxx"
38#include "pqxx/stream_from.hxx"
39#include "pqxx/util.hxx"
43class transaction_subtransaction;
44class transaction_sql_cursor;
45class transaction_stream_to;
46class transaction_transaction_focus;
52using namespace std::literals;
55class transaction_focus;
120 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const
122 return conn().esc(std::forward<ARGS>(args)...);
137 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const
139 return conn().esc_raw(std::forward<ARGS>(args)...);
146 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
149#include "pqxx/internal/ignore-deprecated-pre.hxx"
150 return conn().unesc_raw(
text);
151#include "pqxx/internal/ignore-deprecated-post.hxx"
160 return conn().unesc_bin(
text);
167 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
170#include "pqxx/internal/ignore-deprecated-pre.hxx"
171 return conn().unesc_raw(
text);
172#include "pqxx/internal/ignore-deprecated-post.hxx"
179 [[nodiscard]] std::basic_string<std::byte>
unesc_bin(
char const text[])
181 return conn().unesc_bin(
text);
186 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const
188 return conn().quote(t);
192 "Use std::basic_string<std::byte> instead of binarystring.")]] std::string
199 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
200 quote_raw(
unsigned char const bin[], std::size_t len)
const
206 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
207 quote_raw(
zview bin)
const;
209#if defined(PQXX_HAVE_CONCEPTS)
212 template<binary DATA>
213 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
215 return conn().quote_raw(data);
220 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
222 return conn().quote_name(identifier);
226 [[nodiscard]] std::string
227 esc_like(std::string_view bin,
char escape_char =
'\\')
const
229 return conn().esc_like(bin, escape_char);
280 [[deprecated(
"The desc parameter is going away.")]]
result
281 exec(std::string_view query, std::string_view desc);
290#include "pqxx/internal/ignore-deprecated-pre.hxx"
291 return exec(query, std::string_view{});
292#include "pqxx/internal/ignore-deprecated-post.hxx"
302 "Pass your query as a std::string_view, not stringstream.")]]
result
303 exec(std::stringstream
const &query, std::string_view desc)
305#include "pqxx/internal/ignore-deprecated-pre.hxx"
306 return exec(query.str(), desc);
307#include "pqxx/internal/ignore-deprecated-post.hxx"
316 [[deprecated(
"The desc parameter is going away.")]]
result
319#include "pqxx/internal/ignore-deprecated-pre.hxx"
320 return exec_n(0, query, desc);
321#include "pqxx/internal/ignore-deprecated-post.hxx"
332 return exec_n(0, query);
342 [[deprecated(
"The desc parameter is going away.")]]
row
345#include "pqxx/internal/ignore-deprecated-pre.hxx"
346 return exec_n(1, query, desc).
front();
347#include "pqxx/internal/ignore-deprecated-post.hxx"
359 return exec_n(1, query).
front();
368 [[deprecated(
"The desc parameter is going away.")]]
result
379#include "pqxx/internal/ignore-deprecated-pre.hxx"
380 return exec_n(rows, query, std::string_view{});
381#include "pqxx/internal/ignore-deprecated-post.hxx"
388 template<
typename TYPE>
389 [[deprecated(
"The desc parameter is going away.")]] TYPE
392#include "pqxx/internal/ignore-deprecated-pre.hxx"
393 row const r{exec1(query, desc)};
394#include "pqxx/internal/ignore-deprecated-post.hxx"
395 if (std::size(r) != 1)
397 "Queried single value from result with ", std::size(r),
" columns.")};
398 return r[0].as<TYPE>();
410 row const r{exec1(query)};
411 if (std::size(r) != 1)
413 "Queried single value from result with ", std::size(r),
" columns.")};
414 return r[0].as<TYPE>();
425 template<
typename... TYPE>
428 return exec1(query).as<TYPE...>();
439 template<
typename... TYPE>
443 auto const rows{std::size(res)};
447 case 1:
return {res[0].as<TYPE...>()};
450 "Expected at most one row of data, got "sv, rows,
"."sv)};
501 template<
typename... TYPE>
502 [[nodiscard]]
auto stream(std::string_view query) &
505 return pqxx::internal::owning_stream_input_iteration<TYPE...>{
506 std::unique_ptr<stream_from>{
531 template<
typename CALLABLE>
536 param_types
const *
const sample{
nullptr};
537 auto data_stream{stream_like(query, sample)};
538 for (
auto const &fields : data_stream) std::apply(func, fields);
541 template<
typename CALLABLE>
543 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
546 return for_stream(query, std::forward<CALLABLE>(func));
583 return exec(query).iter<TYPE...>();
597 return exec_n(rows, query).iter<TYPE...>();
612 exec(query).for_each(std::forward<CALLABLE>(func));
656 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
664 return exec_params_n(0, query, std::forward<Args>(args)...);
670 template<
typename... Args>
673 auto const r{exec_params(query, std::forward<Args>(args)...)};
674 check_rowcount_params(rows, std::size(r));
712 template<
typename... Args>
716 return internal_exec_prepared(statement, pp.
make_c_params());
722 template<
typename... Args>
725 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
731 template<
typename... Args>
734 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
741 template<
typename... Args>
745 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
746 check_rowcount_prepared(statement, rows, std::size(r));
759 m_conn.process_notice(msg);
764 m_conn.process_notice(msg);
791 "Set transaction-local variables using SQL SET statements.")]]
void
792 set_variable(std::string_view var, std::string_view value);
798 [[deprecated(
"Read variables using SQL SHOW statements.")]] std::string
799 get_variable(std::string_view);
803 [[nodiscard]] std::string_view
name() const &noexcept
815 std::shared_ptr<std::string> rollback_cmd) :
816 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
831 void register_transaction();
834 void close() noexcept;
837 virtual
void do_commit() = 0;
843 virtual
void do_abort();
846 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
848 m_rollback_cmd = cmd;
852 result direct_exec(std::string_view, std::string_view desc =
""sv);
854 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
865 PQXX_PRIVATE
void check_pending_error();
868 internal_exec_prepared(zview statement, internal::c_params
const &args);
870 result internal_exec_params(zview query, internal::c_params
const &args);
873 void check_rowcount_prepared(
879 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
882 [[nodiscard]] std::string description()
const;
884 friend class pqxx::internal::gate::transaction_transaction_focus;
887 PQXX_PRIVATE
void register_pending_error(
zview)
noexcept;
888 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
891 template<typename... ARGS>
892 auto stream_like(std::string_view query, std::tuple<ARGS...> const *)
894 return stream<ARGS...>(query);
905 status m_status = status::active;
906 bool m_registered =
false;
908 std::string m_pending_error;
911 std::shared_ptr<std::string> m_rollback_cmd;
913 static constexpr std::string_view s_type_name{
"transaction"sv};
920std::string_view transaction_base::query_value<std::string_view>(
921 zview query, std::string_view desc) =
delete;
924zview transaction_base::query_value<zview>(
925 zview query, std::string_view desc) =
delete;
933template<pqxx::isolation_level isolation, pqxx::write_policy rw>
938inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
941inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
942 "BEGIN READ ONLY"_zv};
944inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
945 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
947inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
948 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
950inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
951 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
953inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
954 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
auto esc(ARGS &&...args) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:120
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:27
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
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:83
const zview begin_cmd
The SQL command for starting a given type of transaction.
decltype(strip_types(std::declval< TYPES... >())) strip_types_t
Take a tuple type and apply strip_t to its component types.
Definition: util.hxx:529
Definition: connection.hxx:99
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:59
std::basic_string_view< std::byte > bytes_view() const
Read data as a std::basic_string_view<std::byte>.
Definition: binarystring.hxx:178
Connection to a database.
Definition: connection.hxx:185
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:174
Query returned an unexpected number of rows.
Definition: except.hxx:209
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:220
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:96
Result set containing data returned by a query or command.
Definition: result.hxx:74
result_size_type size_type
Definition: result.hxx:76
Reference to one row in a result.
Definition: row.hxx:47
reference front() const noexcept
Definition: row.cxx:60
Stream data from the database.
Definition: stream_from.hxx:78
static stream_from query(transaction_base &tx, std::string_view q)
Factory: Execute query, and stream the results.
Definition: stream_from.hxx:93
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:77
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:713
std::basic_string< std::byte > unesc_bin(zview text)
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:158
result exec0(zview query, std::string_view desc)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:317
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition: transaction_base.hxx:769
auto for_each(std::string_view query, CALLABLE &&func)
Definition: transaction_base.hxx:544
auto query(zview query)
Execute query, read full results, then iterate rows of data.
Definition: transaction_base.hxx:581
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:762
result exec0(zview query)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:330
auto query_n(result::size_type rows, zview query)
Perform query, expect given number of rows, iterate results.
Definition: transaction_base.hxx:595
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:168
TYPE query_value(zview query, std::string_view desc)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:390
result exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
Execute a prepared statement, expect a result with given number of rows.
Definition: transaction_base.hxx:743
transaction_base(transaction_base const &)=delete
transaction_base(connection &c, std::string_view tname, std::shared_ptr< std::string > rollback_cmd)
Create a transaction (to be called by implementation classes only).
Definition: transaction_base.hxx:813
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:186
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:654
TYPE query_value(zview query)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:408
transaction_base & operator=(transaction_base const &)=delete
auto esc_raw(ARGS &&...args) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:137
result exec(std::stringstream const &query, std::string_view desc)
Execute a command.
Definition: transaction_base.hxx:303
std::string quote(binarystring const &t) const
Definition: transaction_base.hxx:193
row exec1(zview query)
Execute command returning a single row of data.
Definition: transaction_base.hxx:357
row exec1(zview query, std::string_view desc)
Execute command returning a single row of data.
Definition: transaction_base.hxx:343
transaction_base(transaction_base &&)=delete
result exec_params(zview query, Args &&...args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:645
std::optional< std::tuple< TYPE... > > query01(zview query)
Query at most one row of data, and if there is one, convert it.
Definition: transaction_base.hxx:440
transaction_base()=delete
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:662
auto for_stream(std::string_view query, CALLABLE &&func)
Perform a streaming query, and for each result row, call func.
Definition: transaction_base.hxx:532
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:147
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:732
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:227
std::basic_string< std::byte > unesc_bin(char const text[])
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:179
transaction_base & operator=(transaction_base &&)=delete
result exec_n(result::size_type rows, zview query)
Execute command, expect given number of rows.
Definition: transaction_base.hxx:377
std::tuple< TYPE... > query1(zview query)
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:426
result exec(std::string_view query)
Execute a command.
Definition: transaction_base.hxx:288
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:723
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:220
std::string quote_raw(unsigned char const bin[], std::size_t len) const
Binary-escape and quote a binary string for use as an SQL constant.
Definition: transaction_base.hxx:200
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:803
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:671
auto stream(std::string_view query) &
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:502
void for_query(zview query, CALLABLE &&func)
Execute a query, load the full result, and perform func for each row.
Definition: transaction_base.hxx:610
void process_notice(char const msg[]) const
Have connection process a warning message.
Definition: transaction_base.hxx:757
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:29
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38