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>."
44class transaction_subtransaction;
46class transaction_stream_to;
47class transaction_transaction_focus;
53using namespace std::literals;
154 transaction_base() =
delete;
155 transaction_base(transaction_base
const &) =
delete;
156 transaction_base(transaction_base &&) =
delete;
157 transaction_base &operator=(transaction_base
const &) =
delete;
158 transaction_base &operator=(transaction_base &&) =
delete;
160 virtual ~transaction_base() = 0;
195 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const
197 return conn().esc(std::forward<ARGS>(args)...);
212 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const
214 return conn().esc_raw(std::forward<ARGS>(args)...);
221 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
222 unesc_raw(zview text)
const
225 return conn().unesc_raw(text);
233 [[nodiscard]]
bytes unesc_bin(zview text) {
return conn().unesc_bin(text); }
239 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
240 unesc_raw(
char const *text)
const
243 return conn().unesc_raw(text);
253 return conn().unesc_bin(text);
258 template<
typename T> [[nodiscard]] std::string quote(T
const &t)
const
260 return conn().quote(t);
263 [[deprecated(
"Use bytes instead of binarystring.")]] std::string
264 quote(binarystring
const &t)
const
266 return conn().quote(t.bytes_view());
270 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
271 quote_raw(
unsigned char const bin[], std::size_t len)
const
277 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
278 quote_raw(zview bin)
const;
280#if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
283 template<binary DATA>
284 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
286 return conn().quote_raw(data);
291 [[nodiscard]] std::string quote_name(std::string_view identifier)
const
293 return conn().quote_name(identifier);
297 [[nodiscard]] std::string
298 esc_like(std::string_view bin,
char escape_char =
'\\')
const
300 return conn().esc_like(bin, escape_char);
346 [[deprecated(
"The desc parameter is going away.")]]
347 result exec(std::string_view query, std::string_view desc);
351 result exec(std::string_view query, params parms)
353 return internal_exec_params(query, parms.make_c_params());
361 result exec(std::string_view query)
364 return exec(query, std::string_view{});
375 "Pass your query as a std::string_view, not stringstream.")]] result
376 exec(std::stringstream
const &query, std::string_view desc)
379 return exec(query.str(), desc);
389 [[deprecated(
"Use exec(string_view) and call no_rows() on the result.")]]
390 result exec0(zview query, std::string_view desc)
393 return exec(query, desc).no_rows();
403 [[deprecated(
"Use exec() and call no_rows() on the result.")]]
404 result exec0(zview query)
406 return exec(query).no_rows();
416 [[deprecated(
"Use exec(string_view), and call one_row() on the result.")]]
417 row exec1(zview query, std::string_view desc)
420 return exec(query, desc).one_row();
431 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
432 row exec1(zview query)
434 return exec(query).one_row();
443 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
444 result exec_n(result::size_type rows, zview query, std::string_view desc);
452 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
453 result exec_n(result::size_type rows, zview query)
456 return exec(query, std::string_view{}).expect_rows(rows);
464 template<
typename TYPE>
465 [[deprecated(
"The desc parameter is going away.")]]
466 TYPE query_value(zview query, std::string_view desc)
469 return exec(query, desc).one_field().as<TYPE>();
480 template<
typename TYPE> TYPE query_value(zview query)
482 return exec(query).one_field().as<TYPE>();
493 template<
typename... TYPE>
494 [[nodiscard]] std::tuple<TYPE...> query1(zview query)
496 return exec(query).expect_columns(
sizeof...(TYPE)).one_row().as<TYPE...>();
507 template<
typename... TYPE>
508 [[nodiscard]] std::optional<std::tuple<TYPE...>> query01(zview query)
510 std::optional<row>
const r{exec(query).opt_row()};
512 return {r->as<TYPE...>()};
569 template<
typename... TYPE>
570 [[nodiscard]]
auto stream(std::string_view query) &
572 return pqxx::internal::stream_query<TYPE...>{*
this, query};
605 template<
typename CALLABLE>
606 auto for_stream(std::string_view query, CALLABLE &&func)
610 param_types
const *
const sample{
nullptr};
611 auto data_stream{stream_like(query, sample)};
612 for (
auto const &fields : data_stream) std::apply(func, fields);
615 template<
typename CALLABLE>
617 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
618 for_each(std::string_view query, CALLABLE &&func)
620 return for_stream(query, std::forward<CALLABLE>(func));
655 template<
typename... TYPE>
auto query(zview query)
657 return exec(query).iter<TYPE...>();
661 template<
typename... TYPE>
662 [[deprecated(
"Use query() instead, and call expect_rows() on the result.")]]
663 auto query_n(result::size_type rows, zview query)
665 return exec(query).expect_rows(rows).iter<TYPE...>();
678 template<
typename CALLABLE>
void for_query(zview query, CALLABLE &&func)
680 exec(query).for_each(std::forward<CALLABLE>(func));
718 template<
typename... Args>
719 [[deprecated(
"Use exec(zview, params) instead.")]]
720 result exec_params(std::string_view query, Args &&...args)
722 return exec(query, params{args...});
728 template<
typename... Args>
729 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
730 row exec_params1(zview query, Args &&...args)
732 return exec(query, params{args...}).one_row();
738 template<
typename... Args>
740 "Use exec(string_view, params) and call no_rows() on the result.")]]
741 result exec_params0(zview query, Args &&...args)
743 return exec(query, params{args...}).no_rows();
749 template<
typename... Args>
750 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
751 result exec_params_n(std::size_t rows, zview query, Args &&...args)
753 return exec(query, params{args...})
754 .expect_rows(check_cast<result_size_type>(rows,
"number of rows"));
760 template<
typename... Args>
761 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
762 result exec_params_n(result::size_type rows, zview query, Args &&...args)
764 return exec(query, params{args...}).expect_rows(rows);
801 template<
typename... TYPE>
auto query(zview query, params
const &parms)
803 return exec(query, parms).iter<TYPE...>();
816 template<
typename... TYPE>
817 [[deprecated(
"Use exec(), and call expect_rows() & iter() on the result.")]]
818 auto query_n(result::size_type rows, zview query, params
const &parms)
820 return exec(query, parms).expect_rows(rows).iter<TYPE...>();
830 template<
typename TYPE> TYPE query_value(zview query, params
const &parms)
832 return exec(query, parms).expect_columns(1).one_field().as<TYPE>();
843 template<
typename... TYPE>
845 std::tuple<TYPE...> query1(zview query, params
const &parms)
847 return exec(query, parms).one_row().as<TYPE...>();
858 template<
typename... TYPE>
859 [[nodiscard]] std::optional<std::tuple<TYPE...>>
860 query01(zview query, params
const &parms)
862 std::optional<row> r{exec(query, parms).opt_row()};
864 return {r->as<TYPE...>()};
882 template<
typename CALLABLE>
883 void for_query(zview query, CALLABLE &&func, params
const &parms)
885 exec(query, parms).for_each(std::forward<CALLABLE>(func));
904 void notify(std::string_view channel, std::string_view payload = {});
908 template<
typename... Args>
909 [[deprecated(
"Use exec(prepped, params) instead.")]]
910 result exec_prepared(zview statement, Args &&...args)
912 return exec(prepped{statement}, params{args...});
916 result exec(prepped statement)
919 return internal_exec_prepared(statement, pp.make_c_params());
928 template<
typename... TYPE>
929 auto query(prepped statement, params
const &parms = {})
931 return exec(statement, parms).iter<TYPE...>();
938 template<
typename TYPE>
939 TYPE query_value(prepped statement, params
const &parms = {})
941 return exec(statement, parms).expect_columns(1).one_field().as<TYPE>();
948 template<
typename CALLABLE>
949 void for_query(prepped statement, CALLABLE &&func, params
const &parms = {})
951 exec(statement, parms).for_each(std::forward<CALLABLE>(func));
955 result exec(prepped statement, params
const &parms)
957 return internal_exec_prepared(statement, parms.make_c_params());
963 template<
typename... Args>
965 "Use exec(string_view, params) and call one_row() on the result.")]]
966 row exec_prepared1(zview statement, Args &&...args)
968 return exec(prepped{statement}, params{args...}).one_row();
974 template<
typename... Args>
976 "Use exec(prepped, params), and call no_rows() on the result.")]]
977 result exec_prepared0(zview statement, Args &&...args)
979 return exec(prepped{statement}, params{args...}).no_rows();
986 template<
typename... Args>
988 "Use exec(prepped, params), and call expect_rows() on the result.")]]
990 exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
992 return exec(pqxx::prepped{statement}, params{args...}).expect_rows(rows);
1000 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
1002 void process_notice(zview msg)
const { m_conn.process_notice(msg); }
1006 [[nodiscard]]
constexpr connection &conn() const noexcept {
return m_conn; }
1024 [[deprecated(
"Set transaction-local variables using SQL SET statements.")]]
1025 void set_variable(std::string_view var, std::string_view value);
1031 [[deprecated(
"Read variables using SQL SHOW statements.")]]
1032 std::string get_variable(std::string_view);
1036 [[nodiscard]] std::string_view name() const & noexcept {
return m_name; }
1044 connection &cx, std::string_view tname,
1045 std::shared_ptr<std::string> rollback_cmd) :
1046 m_conn{cx}, m_name{tname}, m_rollback_cmd{rollback_cmd}
1055 transaction_base(connection &cx, std::string_view tname);
1058 explicit transaction_base(connection &cx);
1061 void register_transaction();
1064 void close() noexcept;
1067 virtual
void do_commit() = 0;
1073 virtual
void do_abort();
1076 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
1078 m_rollback_cmd = cmd;
1082 result direct_exec(std::string_view, std::string_view desc =
""sv);
1084 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
1097 result internal_exec_prepared(
1098 std::string_view statement, internal::c_params
const &args);
1101 internal_exec_params(std::string_view query, internal::c_params
const &args);
1104 [[nodiscard]] std::string description()
const;
1106 friend class pqxx::internal::gate::transaction_transaction_focus;
1108 PQXX_PRIVATE void unregister_focus(transaction_focus *)
noexcept;
1109 PQXX_PRIVATE void register_pending_error(zview)
noexcept;
1110 PQXX_PRIVATE void register_pending_error(std::string &&) noexcept;
1113 template<typename... ARGS>
1114 auto stream_like(std::string_view query, std::tuple<ARGS...> const *)
1116 return stream<ARGS...>(query);
1125 transaction_focus
const *m_focus =
nullptr;
1127 status m_status = status::active;
1128 bool m_registered =
false;
1130 std::string m_pending_error;
1133 std::shared_ptr<std::string> m_rollback_cmd;
1135 static constexpr std::string_view s_type_name{
"transaction"sv};
1142std::string_view transaction_base::query_value<std::string_view>(
1143 zview query, std::string_view desc) =
delete;
1146zview transaction_base::query_value<zview>(
1147 zview query, std::string_view desc) =
delete;
1155template<pqxx::isolation_level isolation, pqxx::write_policy rw>
1164 "BEGIN READ ONLY"_zv};
1167 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1170 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1173 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1176 "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:107
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