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;
149class PQXX_LIBEXPORT PQXX_NOVTABLE transaction_base
152 transaction_base() =
delete;
153 transaction_base(transaction_base
const &) =
delete;
154 transaction_base(transaction_base &&) =
delete;
155 transaction_base &operator=(transaction_base
const &) =
delete;
156 transaction_base &operator=(transaction_base &&) =
delete;
158 virtual ~transaction_base() = 0;
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
220 unesc_raw(zview text)
const
222#include "pqxx/internal/ignore-deprecated-pre.hxx"
223 return conn().unesc_raw(text);
224#include "pqxx/internal/ignore-deprecated-post.hxx"
231 [[nodiscard]]
bytes unesc_bin(zview text) {
return conn().unesc_bin(text); }
237 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
238 unesc_raw(
char const *text)
const
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
262 quote(binarystring
const &t)
const
264 return conn().quote(t.bytes_view());
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);
358 result exec(std::string_view query)
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
387 exec0(zview query, std::string_view desc)
389#include "pqxx/internal/ignore-deprecated-pre.hxx"
390 return exec_n(0, query, desc);
391#include "pqxx/internal/ignore-deprecated-post.hxx"
400 result exec0(zview query) {
return exec_n(0, query); }
409 [[deprecated(
"The desc parameter is going away.")]] row
410 exec1(zview query, std::string_view desc)
412#include "pqxx/internal/ignore-deprecated-pre.hxx"
413 return exec_n(1, query, desc).front();
414#include "pqxx/internal/ignore-deprecated-post.hxx"
424 row exec1(zview query) {
return exec_n(1, query).front(); }
432 [[deprecated(
"The desc parameter is going away.")]] result
433 exec_n(result::size_type rows, zview query, std::string_view desc);
441 result exec_n(result::size_type rows, zview query)
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
454 query_value(zview query, std::string_view desc)
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)
460 throw usage_error{internal::concat(
461 "Queried single value from result with ", std::size(r),
" columns.")};
462 return r[0].as<TYPE>();
472 template<
typename TYPE> TYPE query_value(zview query)
474 row
const r{exec1(query)};
475 if (std::size(r) != 1)
476 throw usage_error{internal::concat(
477 "Queried single value from result with ", std::size(r),
" columns.")};
478 return r[0].as<TYPE>();
489 template<
typename... TYPE>
490 [[nodiscard]] std::tuple<TYPE...> query1(zview query)
492 return exec1(query).as<TYPE...>();
503 template<
typename... TYPE>
504 [[nodiscard]] std::optional<std::tuple<TYPE...>> query01(zview query)
506 result res{exec(query)};
507 auto const rows{std::size(res)};
511 case 1:
return {res[0].as<TYPE...>()};
513 throw unexpected_rows{internal::concat(
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>
607 auto for_stream(std::string_view query, CALLABLE &&func)
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));
656 template<
typename... TYPE>
auto query(zview query)
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...>();
685 template<
typename CALLABLE>
void for_query(zview query, CALLABLE &&func)
687 exec(query).for_each(std::forward<CALLABLE>(func));
725 template<
typename... Args> result exec_params(zview query, Args &&...args)
728 return internal_exec_params(query, pp.make_c_params());
734 template<
typename... Args> row exec_params1(zview query, Args &&...args)
736 return exec_params_n(1, query, std::forward<Args>(args)...).front();
742 template<
typename... Args> result exec_params0(zview query, Args &&...args)
744 return exec_params_n(0, query, std::forward<Args>(args)...);
750 template<
typename... Args>
751 result exec_params_n(std::size_t rows, zview query, Args &&...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>
764 result exec_params_n(result::size_type rows, zview query, Args &&...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)));
808 template<
typename... TYPE>
auto query(zview query, params
const &parms)
810 return exec_params(query, parms).iter<TYPE...>();
823 template<
typename... TYPE>
824 auto query_n(result::size_type rows, zview query, params
const &parms)
826 return exec_params_n(rows, query, parms).iter<TYPE...>();
836 template<
typename TYPE> TYPE query_value(zview query, params
const &parms)
838 row
const r{exec_params1(query, parms)};
839 if (std::size(r) != 1)
840 throw usage_error{internal::concat(
841 "Queried single value from result with ", std::size(r),
" columns.")};
842 return r[0].as<TYPE>();
853 template<
typename... TYPE>
854 [[nodiscard]] std::tuple<TYPE...> query1(zview query, params
const &parms)
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...>>
870 query01(zview query, params
const &parms)
872 result res{exec_params(query, parms)};
873 auto const rows{std::size(res)};
877 case 1:
return {res[0].as<TYPE...>()};
879 throw unexpected_rows{internal::concat(
880 "Expected at most one row of data, got "sv, rows,
"."sv)};
897 template<
typename CALLABLE>
898 void for_query(zview query, CALLABLE &&func, params
const &parms)
900 exec_params(query, parms).for_each(std::forward<CALLABLE>(func));
947 template<
typename... Args>
948 result exec_prepared(zview statement, Args &&...args)
951 return internal_exec_prepared(statement, pp.make_c_params());
957 template<
typename... Args>
958 row exec_prepared1(zview statement, Args &&...args)
960 return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
966 template<
typename... Args>
967 result exec_prepared0(zview statement, Args &&...args)
969 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
976 template<
typename... Args>
978 exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
980 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
981 check_rowcount_prepared(statement, rows, std::size(r));
992 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
994 void process_notice(zview msg)
const { m_conn.process_notice(msg); }
998 [[nodiscard]]
constexpr connection &conn() const noexcept {
return m_conn; }
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; }
1037 connection &c, std::string_view tname,
1038 std::shared_ptr<std::string> rollback_cmd) :
1039 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
1048 transaction_base(connection &c, std::string_view tname);
1051 explicit transaction_base(connection &c);
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;
1107 friend class pqxx::internal::gate::transaction_transaction_focus;
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>
1165 "BEGIN READ ONLY"_zv};
1168 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1171 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1174 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1177 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
1180#include "pqxx/internal/stream_query_impl.hxx"
Stream query results from the database. Used by transaction_base::stream.
Definition stream_query.hxx:80
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
void PQXX_LIBEXPORT unesc_bin(std::string_view escaped_data, std::byte buffer[])
Reconstitute binary data from its escaped version.
Definition util.cxx:165
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
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:373
bytes_view binary_cast(TYPE const &data)
End a code block started by "ignore-deprecated-pre.hxx".
Definition util.hxx:409