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>."
45class transaction_subtransaction;
47class transaction_stream_to;
48class transaction_transaction_focus;
54using namespace std::literals;
155 transaction_base() =
delete;
156 transaction_base(transaction_base
const &) =
delete;
157 transaction_base(transaction_base &&) =
delete;
158 transaction_base &operator=(transaction_base
const &) =
delete;
159 transaction_base &operator=(transaction_base &&) =
delete;
161 virtual ~transaction_base() = 0;
196 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const
198 return conn().esc(std::forward<ARGS>(args)...);
213 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const
215 return conn().esc_raw(std::forward<ARGS>(args)...);
222 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
223 unesc_raw(zview text)
const
226 return conn().unesc_raw(text);
234 [[nodiscard]]
bytes unesc_bin(zview text) {
return conn().unesc_bin(text); }
240 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
241 unesc_raw(
char const *text)
const
244 return conn().unesc_raw(text);
254 return conn().unesc_bin(text);
259 template<
typename T> [[nodiscard]] std::string quote(T
const &t)
const
261 return conn().quote(t);
264 [[deprecated(
"Use bytes instead of binarystring.")]] std::string
265 quote(binarystring
const &t)
const
267 return conn().quote(t.bytes_view());
271 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
272 quote_raw(
unsigned char const bin[], std::size_t len)
const
278 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
279 quote_raw(zview bin)
const;
281#if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
284 template<binary DATA>
285 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
287 return conn().quote_raw(data);
292 [[nodiscard]] std::string quote_name(std::string_view identifier)
const
294 return conn().quote_name(identifier);
298 [[nodiscard]] std::string
299 esc_like(std::string_view bin,
char escape_char =
'\\')
const
301 return conn().esc_like(bin, escape_char);
347 [[deprecated(
"The desc parameter is going away.")]]
348 result exec(std::string_view query, std::string_view desc);
352 result exec(std::string_view query, params parms)
354 return internal_exec_params(query, parms.make_c_params());
362 result exec(std::string_view query)
365 return exec(query, std::string_view{});
376 "Pass your query as a std::string_view, not stringstream.")]] result
377 exec(std::stringstream
const &query, std::string_view desc)
380 return exec(query.str(), desc);
390 [[deprecated(
"Use exec(string_view) and call no_rows() on the result.")]]
391 result exec0(zview query, std::string_view desc)
394 return exec(query, desc).no_rows();
404 [[deprecated(
"Use exec() and call no_rows() on the result.")]]
405 result exec0(zview query)
407 return exec(query).no_rows();
417 [[deprecated(
"Use exec(string_view), and call one_row() on the result.")]]
418 row exec1(zview query, std::string_view desc)
421 return exec(query, desc).one_row();
432 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
433 row exec1(zview query)
435 return exec(query).one_row();
444 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
445 result exec_n(result::size_type rows, zview query, std::string_view desc);
453 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
454 result exec_n(result::size_type rows, zview query)
457 return exec(query, std::string_view{}).expect_rows(rows);
465 template<
typename TYPE>
466 [[deprecated(
"The desc parameter is going away.")]]
467 TYPE query_value(zview query, std::string_view desc)
470 return exec(query, desc).one_field().as<TYPE>();
481 template<
typename TYPE> TYPE query_value(zview query)
483 return exec(query).one_field().as<TYPE>();
494 template<
typename... TYPE>
495 [[nodiscard]] std::tuple<TYPE...> query1(zview query)
497 return exec(query).expect_columns(
sizeof...(TYPE)).one_row().as<TYPE...>();
508 template<
typename... TYPE>
509 [[nodiscard]] std::optional<std::tuple<TYPE...>> query01(zview query)
511 std::optional<row>
const r{exec(query).opt_row()};
513 return {r->as<TYPE...>()};
570 template<
typename... TYPE>
571 [[nodiscard]]
auto stream(std::string_view query) &
573 return pqxx::internal::stream_query<TYPE...>{*
this, 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...>();
662 template<
typename... TYPE>
663 [[deprecated(
"Use query() instead, and call expect_rows() on the result.")]]
664 auto query_n(result::size_type rows, zview query)
666 return exec(query).expect_rows(rows).iter<TYPE...>();
679 template<
typename CALLABLE>
void for_query(zview query, CALLABLE &&func)
681 exec(query).for_each(std::forward<CALLABLE>(func));
719 template<
typename... Args>
720 [[deprecated(
"Use exec(zview, params) instead.")]]
721 result exec_params(std::string_view query, Args &&...args)
723 return exec(query, params{args...});
729 template<
typename... Args>
730 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
731 row exec_params1(zview query, Args &&...args)
733 return exec(query, params{args...}).one_row();
739 template<
typename... Args>
741 "Use exec(string_view, params) and call no_rows() on the result.")]]
742 result exec_params0(zview query, Args &&...args)
744 return exec(query, params{args...}).no_rows();
750 template<
typename... Args>
751 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
752 result exec_params_n(std::size_t rows, zview query, Args &&...args)
754 return exec(query, params{args...})
755 .expect_rows(check_cast<result_size_type>(rows,
"number of rows"));
761 template<
typename... Args>
762 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
763 result exec_params_n(result::size_type rows, zview query, Args &&...args)
765 return exec(query, params{args...}).expect_rows(rows);
802 template<
typename... TYPE>
auto query(zview query, params
const &parms)
804 return exec(query, parms).iter<TYPE...>();
817 template<
typename... TYPE>
818 [[deprecated(
"Use exec(), and call expect_rows() & iter() on the result.")]]
819 auto query_n(result::size_type rows, zview query, params
const &parms)
821 return exec(query, parms).expect_rows(rows).iter<TYPE...>();
831 template<
typename TYPE> TYPE query_value(zview query, params
const &parms)
833 return exec(query, parms).expect_columns(1).one_field().as<TYPE>();
844 template<
typename... TYPE>
846 std::tuple<TYPE...> query1(zview query, params
const &parms)
848 return exec(query, parms).one_row().as<TYPE...>();
859 template<
typename... TYPE>
860 [[nodiscard]] std::optional<std::tuple<TYPE...>>
861 query01(zview query, params
const &parms)
863 std::optional<row> r{exec(query, parms).opt_row()};
865 return {r->as<TYPE...>()};
883 template<
typename CALLABLE>
884 void for_query(zview query, CALLABLE &&func, params
const &parms)
886 exec(query, parms).for_each(std::forward<CALLABLE>(func));
905 void notify(std::string_view channel, std::string_view payload = {});
909 template<
typename... Args>
910 [[deprecated(
"Use exec(prepped, params) instead.")]]
911 result exec_prepared(zview statement, Args &&...args)
913 return exec(prepped{statement}, params{args...});
917 result exec(prepped statement)
920 return internal_exec_prepared(statement, pp.make_c_params());
929 template<
typename... TYPE>
930 auto query(prepped statement, params
const &parms = {})
932 return exec(statement, parms).iter<TYPE...>();
939 template<
typename TYPE>
940 TYPE query_value(prepped statement, params
const &parms = {})
942 return exec(statement, parms).expect_columns(1).one_field().as<TYPE>();
949 template<
typename CALLABLE>
950 void for_query(prepped statement, CALLABLE &&func, params
const &parms = {})
952 exec(statement, parms).for_each(std::forward<CALLABLE>(func));
956 result exec(prepped statement, params
const &parms)
958 return internal_exec_prepared(statement, parms.make_c_params());
964 template<
typename... Args>
966 "Use exec(string_view, params) and call one_row() on the result.")]]
967 row exec_prepared1(zview statement, Args &&...args)
969 return exec(prepped{statement}, params{args...}).one_row();
975 template<
typename... Args>
977 "Use exec(prepped, params), and call no_rows() on the result.")]]
978 result exec_prepared0(zview statement, Args &&...args)
980 return exec(prepped{statement}, params{args...}).no_rows();
987 template<
typename... Args>
989 "Use exec(prepped, params), and call expect_rows() on the result.")]]
991 exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
993 return exec(pqxx::prepped{statement}, params{args...}).expect_rows(rows);
1001 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
1003 void process_notice(zview msg)
const { m_conn.process_notice(msg); }
1007 [[nodiscard]]
constexpr connection &conn() const noexcept {
return m_conn; }
1025 [[deprecated(
"Set transaction-local variables using SQL SET statements.")]]
1026 void set_variable(std::string_view var, std::string_view value);
1032 [[deprecated(
"Read variables using SQL SHOW statements.")]]
1033 std::string get_variable(std::string_view);
1037 [[nodiscard]] std::string_view name() const & noexcept {
return m_name; }
1045 connection &cx, std::string_view tname,
1046 std::shared_ptr<std::string> rollback_cmd) :
1047 m_conn{cx}, m_name{tname}, m_rollback_cmd{rollback_cmd}
1056 transaction_base(connection &cx, std::string_view tname);
1059 explicit transaction_base(connection &cx);
1062 void register_transaction();
1065 void close() noexcept;
1068 virtual
void do_commit() = 0;
1074 virtual
void do_abort();
1077 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
1079 m_rollback_cmd = cmd;
1083 result direct_exec(std::string_view, std::string_view desc =
""sv);
1085 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
1098 result internal_exec_prepared(
1099 std::string_view statement, internal::c_params
const &args);
1102 internal_exec_params(std::string_view query, internal::c_params
const &args);
1105 [[nodiscard]] std::string description()
const;
1107 friend class pqxx::internal::gate::transaction_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};
Definition transaction-sql_cursor.hxx:6
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
Definition connection.hxx:108
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