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"
41namespace pqxx::internal::gate
43class transaction_subtransaction;
44class transaction_sql_cursor;
45class transaction_stream_to;
46class transaction_transaction_focus;
52using namespace std::literals;
55class transaction_focus;
193 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const
195 return conn().esc(std::forward<ARGS>(args)...);
210 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const
212 return conn().esc_raw(std::forward<ARGS>(args)...);
219 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
222#include "pqxx/internal/ignore-deprecated-pre.hxx"
223 return conn().unesc_raw(text);
224#include "pqxx/internal/ignore-deprecated-post.hxx"
237 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
240#include "pqxx/internal/ignore-deprecated-pre.hxx"
241 return conn().unesc_raw(text);
242#include "pqxx/internal/ignore-deprecated-post.hxx"
251 return conn().unesc_bin(text);
256 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const
258 return conn().quote(t);
261 [[deprecated(
"Use bytes instead of binarystring.")]] std::string
268 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
269 quote_raw(
unsigned char const bin[], std::size_t len)
const
275 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
276 quote_raw(
zview bin)
const;
278#if defined(PQXX_HAVE_CONCEPTS)
281 template<binary DATA>
282 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
284 return conn().quote_raw(data);
289 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
291 return conn().quote_name(identifier);
295 [[nodiscard]] std::string
296 esc_like(std::string_view bin,
char escape_char =
'\\')
const
298 return conn().esc_like(bin, escape_char);
350 [[deprecated(
"The desc parameter is going away.")]]
result
351 exec(std::string_view query, std::string_view desc);
360#include "pqxx/internal/ignore-deprecated-pre.hxx"
361 return exec(query, std::string_view{});
362#include "pqxx/internal/ignore-deprecated-post.hxx"
372 "Pass your query as a std::string_view, not stringstream.")]]
result
373 exec(std::stringstream
const &query, std::string_view desc)
375#include "pqxx/internal/ignore-deprecated-pre.hxx"
376 return exec(query.str(), desc);
377#include "pqxx/internal/ignore-deprecated-post.hxx"
386 [[deprecated(
"The desc parameter is going away.")]]
result
389#include "pqxx/internal/ignore-deprecated-pre.hxx"
390 return exec_n(0, query, desc);
391#include "pqxx/internal/ignore-deprecated-post.hxx"
409 [[deprecated(
"The desc parameter is going away.")]]
row
412#include "pqxx/internal/ignore-deprecated-pre.hxx"
413 return exec_n(1, query, desc).front();
414#include "pqxx/internal/ignore-deprecated-post.hxx"
432 [[deprecated(
"The desc parameter is going away.")]]
result
433 exec_n(result::size_type rows,
zview query, std::string_view desc);
443#include "pqxx/internal/ignore-deprecated-pre.hxx"
444 return exec_n(rows, query, std::string_view{});
445#include "pqxx/internal/ignore-deprecated-post.hxx"
452 template<
typename TYPE>
453 [[deprecated(
"The desc parameter is going away.")]] TYPE
456#include "pqxx/internal/ignore-deprecated-pre.hxx"
457 row const r{exec1(query, desc)};
458#include "pqxx/internal/ignore-deprecated-post.hxx"
459 if (std::size(r) != 1)
461 "Queried single value from result with ", std::size(r),
" columns.")};
462 return r[0].as<TYPE>();
474 row const r{exec1(query)};
475 if (std::size(r) != 1)
477 "Queried single value from result with ", std::size(r),
" columns.")};
478 return r[0].as<TYPE>();
489 template<
typename... TYPE>
492 return exec1(query).as<TYPE...>();
503 template<
typename... TYPE>
507 auto const rows{std::size(res)};
511 case 1:
return {res[0].as<TYPE...>()};
514 "Expected at most one row of data, got "sv, rows,
"."sv)};
570 template<
typename... TYPE>
571 [[nodiscard]]
auto stream(std::string_view query) &
606 template<
typename CALLABLE>
611 param_types
const *
const sample{
nullptr};
612 auto data_stream{stream_like(query, sample)};
613 for (
auto const &fields : data_stream) std::apply(func, fields);
616 template<
typename CALLABLE>
618 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
619 for_each(std::string_view query, CALLABLE &&func)
621 return for_stream(query, std::forward<CALLABLE>(func));
658 return exec(query).iter<TYPE...>();
670 template<
typename... TYPE>
auto query_n(result::size_type rows,
zview query)
672 return exec_n(rows, query).iter<TYPE...>();
687 exec(query).for_each(std::forward<CALLABLE>(func));
728 return internal_exec_params(query, pp.make_c_params());
736 return exec_params_n(1, query, std::forward<Args>(args)...).front();
744 return exec_params_n(0, query, std::forward<Args>(args)...);
750 template<
typename... Args>
753 auto const r{exec_params(query, std::forward<Args>(args)...)};
756 check_rowcount_params(rows,
static_cast<unsigned>(std::size(r)));
763 template<
typename... Args>
766 auto const r{exec_params(query, std::forward<Args>(args)...)};
769 check_rowcount_params(
770 static_cast<unsigned>(rows),
static_cast<unsigned>(std::size(r)));
810 return exec_params(query, parms).iter<TYPE...>();
823 template<
typename... TYPE>
826 return exec_params_n(rows, query, parms).iter<TYPE...>();
838 row const r{exec_params1(query, parms)};
839 if (std::size(r) != 1)
841 "Queried single value from result with ", std::size(r),
" columns.")};
842 return r[0].as<TYPE>();
853 template<
typename... TYPE>
856 result const r{exec_params_n(1, query, parms)};
857 return r[0].as<TYPE...>();
868 template<
typename... TYPE>
869 [[nodiscard]] std::optional<std::tuple<TYPE...>>
872 result res{exec_params(query, parms)};
873 auto const rows{std::size(res)};
877 case 1:
return {res[0].as<TYPE...>()};
880 "Expected at most one row of data, got "sv, rows,
"."sv)};
897 template<
typename CALLABLE>
900 exec_params(query, parms).for_each(std::forward<CALLABLE>(func));
947 template<
typename... Args>
951 return internal_exec_prepared(statement, pp.
make_c_params());
957 template<
typename... Args>
960 return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
966 template<
typename... Args>
969 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
976 template<
typename... Args>
980 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
981 check_rowcount_prepared(statement, rows, std::size(r));
1017 "Set transaction-local variables using SQL SET statements.")]]
void
1018 set_variable(std::string_view var, std::string_view value);
1024 [[deprecated(
"Read variables using SQL SHOW statements.")]] std::string
1025 get_variable(std::string_view);
1029 [[nodiscard]] std::string_view
name() const & noexcept {
return m_name; }
1038 std::shared_ptr<std::string> rollback_cmd) :
1039 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
1054 void register_transaction();
1057 void close() noexcept;
1060 virtual
void do_commit() = 0;
1066 virtual
void do_abort();
1069 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
1071 m_rollback_cmd = cmd;
1075 result direct_exec(std::string_view, std::string_view desc =
""sv);
1077 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
1088 PQXX_PRIVATE
void check_pending_error();
1091 internal_exec_prepared(zview statement, internal::c_params
const &args);
1093 result internal_exec_params(zview query, internal::c_params
const &args);
1096 void check_rowcount_prepared(
1097 zview statement, result::size_type expected_rows,
1098 result::size_type actual_rows);
1102 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
1105 [[nodiscard]] std::string description()
const;
1108 PQXX_PRIVATE
void register_focus(transaction_focus *);
1109 PQXX_PRIVATE
void unregister_focus(transaction_focus *)
noexcept;
1110 PQXX_PRIVATE
void register_pending_error(zview)
noexcept;
1111 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
1114 template<typename... ARGS>
1115 auto stream_like(std::string_view query, std::tuple<ARGS...> const *)
1117 return stream<ARGS...>(query);
1126 transaction_focus
const *m_focus =
nullptr;
1128 status m_status = status::active;
1129 bool m_registered =
false;
1131 std::string m_pending_error;
1134 std::shared_ptr<std::string> m_rollback_cmd;
1136 static constexpr std::string_view s_type_name{
"transaction"sv};
1143std::string_view transaction_base::query_value<std::string_view>(
1144 zview query, std::string_view desc) =
delete;
1147zview transaction_base::query_value<zview>(
1148 zview query, std::string_view desc) =
delete;
1156template<pqxx::isolation_level isolation, pqxx::write_policy rw>
1161inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
1164inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
1165 "BEGIN READ ONLY"_zv};
1167inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
1168 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1170inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
1171 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1173inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
1174 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1176inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
1177 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
1180#include "pqxx/internal/stream_query_impl.hxx"
Connection to a database.
Definition connection.hxx:230
Definition transaction-transaction_focus.hxx:8
Stream query results from the database. Used by transaction_base::stream.
Definition stream_query.hxx:80
Build a parameter list for a parameterised or prepared statement.
Definition params.hxx:46
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition params.cxx:95
Result set containing data returned by a query or command.
Definition result.hxx:73
Reference to one row in a result.
Definition row.hxx:47
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
pqxx::bytes_view bytes_view() const
Read data as a bytes_view.
Definition binarystring.hxx:177
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition binarystring.hxx:58
auto esc(ARGS &&...args) const
Escape string for use as SQL string literal in this transaction.
Definition transaction_base.hxx:193
Query returned an unexpected number of rows.
Definition except.hxx:343
Error in usage of libpqxx library, similar to std::logic_error.
Definition except.hxx:249
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition transaction_base.hxx:948
result exec_params_n(result::size_type rows, zview query, Args &&...args)
Definition transaction_base.hxx:764
result exec0(zview query, std::string_view desc)
Execute command, which should return zero rows of data.
Definition transaction_base.hxx:387
bytes unesc_bin(char const text[])
Unescape binary data, e.g. from a table field or notification payload.
Definition transaction_base.hxx:249
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition transaction_base.hxx:998
void for_query(zview query, CALLABLE &&func, params const &parms)
Execute a query, load the full result, and perform func for each row.
Definition transaction_base.hxx:898
auto query(zview query)
Execute query, read full results, then iterate rows of data.
Definition transaction_base.hxx:656
bytes unesc_bin(zview text)
Unescape binary data, e.g. from a table field or notification payload.
Definition transaction_base.hxx:231
TYPE query_value(zview query, params const &parms)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition transaction_base.hxx:836
void process_notice(zview msg) const
Have connection process a warning message.
Definition transaction_base.hxx:994
result exec0(zview query)
Execute command, which should return zero rows of data.
Definition transaction_base.hxx:400
auto query_n(result::size_type rows, zview query)
Perform query, expect given number of rows, iterate results.
Definition transaction_base.hxx:670
std::optional< std::tuple< TYPE... > > query01(zview query, params const &parms)
Query at most one row of data, and if there is one, convert it.
Definition transaction_base.hxx:870
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition transaction_base.hxx:238
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:454
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:978
std::tuple< TYPE... > query1(zview query, params const &parms)
Perform query returning exactly one row, and convert its fields.
Definition transaction_base.hxx:854
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:1036
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition transaction_base.hxx:256
row exec_params1(zview query, Args &&...args)
Definition transaction_base.hxx:734
TYPE query_value(zview query)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition transaction_base.hxx:472
auto esc_raw(ARGS &&...args) const
Escape binary data for use as SQL string literal in this transaction.
Definition transaction_base.hxx:210
result exec(std::stringstream const &query, std::string_view desc)
Execute a command.
Definition transaction_base.hxx:373
row exec1(zview query)
Execute command returning a single row of data.
Definition transaction_base.hxx:424
row exec1(zview query, std::string_view desc)
Execute command returning a single row of data.
Definition transaction_base.hxx:410
result exec_params(zview query, Args &&...args)
Execute an SQL statement with parameters.
Definition transaction_base.hxx:725
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:504
result exec_params0(zview query, Args &&...args)
Definition transaction_base.hxx:742
auto for_stream(std::string_view query, CALLABLE &&func)
Perform a streaming query, and for each result row, call func.
Definition transaction_base.hxx:607
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition transaction_base.hxx:220
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition transaction_base.hxx:967
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition transaction_base.hxx:296
result exec_n(result::size_type rows, zview query)
Execute command, expect given number of rows.
Definition transaction_base.hxx:441
auto query_n(result::size_type rows, zview query, params const &parms)
Definition transaction_base.hxx:824
std::tuple< TYPE... > query1(zview query)
Perform query returning exactly one row, and convert its fields.
Definition transaction_base.hxx:490
result exec(std::string_view query)
Execute a command.
Definition transaction_base.hxx:358
auto query(zview query, params const &parms)
Execute parameterised query, read full results, iterate rows of data.
Definition transaction_base.hxx:808
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition transaction_base.hxx:958
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition transaction_base.hxx:289
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:269
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition transaction_base.hxx:1029
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition transaction_base.hxx:751
auto stream(std::string_view query) &
Execute a query, in streaming fashion; loop over the results row by row.
Definition transaction_base.hxx:571
void for_query(zview query, CALLABLE &&func)
Execute a query, load the full result, and perform func for each row.
Definition transaction_base.hxx:685
void process_notice(char const msg[]) const
Have connection process a warning message.
Definition transaction_base.hxx:992
Interface definition (and common code) for "transaction" classes.
Definition transaction_base.hxx:150
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
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:629
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition util.hxx:409
std::conditional< has_generic_bytes_char_traits, std::basic_string< std::byte >, std::basic_string< std::byte, byte_char_traits > >::type bytes
Type alias for a container containing bytes.
Definition util.hxx:375