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/internal/stream_query.hxx"
36#include "pqxx/isolation.hxx"
37#include "pqxx/result.hxx"
38#include "pqxx/row.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;
131 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const
133 return conn().esc(std::forward<ARGS>(args)...);
148 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const
150 return conn().esc_raw(std::forward<ARGS>(args)...);
157 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
160#include "pqxx/internal/ignore-deprecated-pre.hxx"
161 return conn().unesc_raw(
text);
162#include "pqxx/internal/ignore-deprecated-post.hxx"
171 return conn().unesc_bin(
text);
178 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
181#include "pqxx/internal/ignore-deprecated-pre.hxx"
182 return conn().unesc_raw(
text);
183#include "pqxx/internal/ignore-deprecated-post.hxx"
192 return conn().unesc_bin(
text);
197 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const
199 return conn().quote(t);
203 "Use std::basic_string<std::byte> instead of binarystring.")]] std::string
210 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
211 quote_raw(
unsigned char const bin[], std::size_t len)
const
217 [[deprecated(
"Use quote(std::basic_string_view<std::byte>).")]] std::string
218 quote_raw(
zview bin)
const;
220#if defined(PQXX_HAVE_CONCEPTS)
223 template<binary DATA>
224 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
226 return conn().quote_raw(data);
231 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
233 return conn().quote_name(identifier);
237 [[nodiscard]] std::string
238 esc_like(std::string_view bin,
char escape_char =
'\\')
const
240 return conn().esc_like(bin, escape_char);
292 [[deprecated(
"The desc parameter is going away.")]]
result
293 exec(std::string_view query, std::string_view desc);
302#include "pqxx/internal/ignore-deprecated-pre.hxx"
303 return exec(query, std::string_view{});
304#include "pqxx/internal/ignore-deprecated-post.hxx"
314 "Pass your query as a std::string_view, not stringstream.")]]
result
315 exec(std::stringstream
const &query, std::string_view desc)
317#include "pqxx/internal/ignore-deprecated-pre.hxx"
318 return exec(query.str(), desc);
319#include "pqxx/internal/ignore-deprecated-post.hxx"
328 [[deprecated(
"The desc parameter is going away.")]]
result
331#include "pqxx/internal/ignore-deprecated-pre.hxx"
332 return exec_n(0, query, desc);
333#include "pqxx/internal/ignore-deprecated-post.hxx"
351 [[deprecated(
"The desc parameter is going away.")]]
row
354#include "pqxx/internal/ignore-deprecated-pre.hxx"
355 return exec_n(1, query, desc).
front();
356#include "pqxx/internal/ignore-deprecated-post.hxx"
374 [[deprecated(
"The desc parameter is going away.")]]
result
385#include "pqxx/internal/ignore-deprecated-pre.hxx"
386 return exec_n(rows, query, std::string_view{});
387#include "pqxx/internal/ignore-deprecated-post.hxx"
394 template<
typename TYPE>
395 [[deprecated(
"The desc parameter is going away.")]] TYPE
398#include "pqxx/internal/ignore-deprecated-pre.hxx"
399 row const r{exec1(query, desc)};
400#include "pqxx/internal/ignore-deprecated-post.hxx"
401 if (std::size(r) != 1)
403 "Queried single value from result with ", std::size(r),
" columns.")};
404 return r[0].as<TYPE>();
416 row const r{exec1(query)};
417 if (std::size(r) != 1)
419 "Queried single value from result with ", std::size(r),
" columns.")};
420 return r[0].as<TYPE>();
431 template<
typename... TYPE>
434 return exec1(query).as<TYPE...>();
445 template<
typename... TYPE>
449 auto const rows{std::size(res)};
453 case 1:
return {res[0].as<TYPE...>()};
456 "Expected at most one row of data, got "sv, rows,
"."sv)};
505 template<
typename... TYPE>
506 [[nodiscard]]
auto stream(std::string_view query) &
508 return pqxx::internal::stream_query<TYPE...>{*
this, query};
537 template<
typename CALLABLE>
542 param_types
const *
const sample{
nullptr};
543 auto data_stream{stream_like(query, sample)};
544 for (
auto const &fields : data_stream) std::apply(func, fields);
547 template<
typename CALLABLE>
549 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
552 return for_stream(query, std::forward<CALLABLE>(func));
589 return exec(query).iter<TYPE...>();
603 return exec_n(rows, query).iter<TYPE...>();
618 exec(query).for_each(std::forward<CALLABLE>(func));
662 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
670 return exec_params_n(0, query, std::forward<Args>(args)...);
676 template<
typename... Args>
679 auto const r{exec_params(query, std::forward<Args>(args)...)};
680 check_rowcount_params(rows, std::size(r));
729 template<
typename... Args>
733 return internal_exec_prepared(statement, pp.
make_c_params());
739 template<
typename... Args>
742 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
748 template<
typename... Args>
751 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
758 template<
typename... Args>
762 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
763 check_rowcount_prepared(statement, rows, std::size(r));
799 "Set transaction-local variables using SQL SET statements.")]]
void
800 set_variable(std::string_view var, std::string_view value);
806 [[deprecated(
"Read variables using SQL SHOW statements.")]] std::string
807 get_variable(std::string_view);
811 [[nodiscard]] std::string_view
name() const &noexcept {
return m_name; }
820 std::shared_ptr<std::string> rollback_cmd) :
821 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
836 void register_transaction();
839 void close() noexcept;
842 virtual
void do_commit() = 0;
848 virtual
void do_abort();
851 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
853 m_rollback_cmd = cmd;
857 result direct_exec(std::string_view, std::string_view desc =
""sv);
859 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
870 PQXX_PRIVATE
void check_pending_error();
873 internal_exec_prepared(zview statement, internal::c_params
const &args);
875 result internal_exec_params(zview query, internal::c_params
const &args);
878 void check_rowcount_prepared(
884 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
887 [[nodiscard]] std::string description()
const;
889 friend class pqxx::internal::gate::transaction_transaction_focus;
892 PQXX_PRIVATE
void register_pending_error(
zview)
noexcept;
893 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
896 template<typename... ARGS>
897 auto stream_like(std::string_view query, std::tuple<ARGS...> const *)
899 return stream<ARGS...>(query);
910 status m_status = status::active;
911 bool m_registered =
false;
913 std::string m_pending_error;
916 std::shared_ptr<std::string> m_rollback_cmd;
918 static constexpr std::string_view s_type_name{
"transaction"sv};
925std::string_view transaction_base::query_value<std::string_view>(
926 zview query, std::string_view desc) =
delete;
929zview transaction_base::query_value<zview>(
930 zview query, std::string_view desc) =
delete;
938template<pqxx::isolation_level isolation, pqxx::write_policy rw>
939extern const zview begin_cmd;
943inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
946inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
947 "BEGIN READ ONLY"_zv};
949inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
950 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
952inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
953 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
955inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
956 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
958inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
959 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
962#include "pqxx/internal/stream_query_impl.hxx"
auto esc(ARGS &&...args) const
Escape string for use as SQL string literal in this transaction.
Definition transaction_base.hxx:131
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:33
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:303
Internal items for libpqxx' own use. Do not use these yourself.
Definition composite.hxx:84
decltype(strip_types(std::declval< TYPES... >())) strip_types_t
Take a tuple type and apply strip_t to its component types.
Definition util.hxx:527
Definition connection.hxx:112
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:253
Error in usage of libpqxx library, similar to std::logic_error.
Definition except.hxx:249
Query returned an unexpected number of rows.
Definition except.hxx:343
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:73
result_size_type size_type
Definition result.hxx:75
Reference to one row in a result.
Definition row.hxx:47
reference front() const noexcept
Definition row.cxx:60
Interface definition (and common code) for "transaction" classes.
Definition transaction_base.hxx:88
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition transaction_base.hxx:730
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:169
result exec0(zview query, std::string_view desc)
Execute command, which should return zero rows of data.
Definition transaction_base.hxx:329
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition transaction_base.hxx:780
auto for_each(std::string_view query, CALLABLE &&func)
Definition transaction_base.hxx:550
auto query(zview query)
Execute query, read full results, then iterate rows of data.
Definition transaction_base.hxx:587
void process_notice(zview msg) const
Have connection process a warning message.
Definition transaction_base.hxx:776
result exec0(zview query)
Execute command, which should return zero rows of data.
Definition transaction_base.hxx:342
auto query_n(result::size_type rows, zview query)
Perform query, expect given number of rows, iterate results.
Definition transaction_base.hxx:601
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition transaction_base.hxx:179
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:396
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:760
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:818
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition transaction_base.hxx:197
row exec_params1(zview query, Args &&...args)
Definition transaction_base.hxx:660
TYPE query_value(zview query)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition transaction_base.hxx:414
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:148
result exec(std::stringstream const &query, std::string_view desc)
Execute a command.
Definition transaction_base.hxx:315
std::string quote(binarystring const &t) const
Definition transaction_base.hxx:204
row exec1(zview query)
Execute command returning a single row of data.
Definition transaction_base.hxx:366
row exec1(zview query, std::string_view desc)
Execute command returning a single row of data.
Definition transaction_base.hxx:352
transaction_base(transaction_base &&)=delete
result exec_params(zview query, Args &&...args)
Execute an SQL statement with parameters.
Definition transaction_base.hxx:651
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:446
transaction_base()=delete
result exec_params0(zview query, Args &&...args)
Definition transaction_base.hxx:668
auto for_stream(std::string_view query, CALLABLE &&func)
Perform a streaming query, and for each result row, call func.
Definition transaction_base.hxx:538
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition transaction_base.hxx:158
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition transaction_base.hxx:749
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition transaction_base.hxx:238
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:190
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:383
std::tuple< TYPE... > query1(zview query)
Perform query returning exactly one row, and convert its fields.
Definition transaction_base.hxx:432
result exec(std::string_view query)
Execute a command.
Definition transaction_base.hxx:300
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition transaction_base.hxx:740
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition transaction_base.hxx:231
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:211
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition transaction_base.hxx:811
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition transaction_base.hxx:677
auto stream(std::string_view query) &
Execute a query, and loop over the results row by row.
Definition transaction_base.hxx:506
void for_query(zview query, CALLABLE &&func)
Execute a query, load the full result, and perform func for each row.
Definition transaction_base.hxx:616
void process_notice(char const msg[]) const
Have connection process a warning message.
Definition transaction_base.hxx:774
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