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>."
21 #include <string_view>
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"
43 class transaction_subtransaction;
44 class transaction_sql_cursor;
45 class transaction_stream_to;
46 class transaction_transaction_focus;
52 using namespace std::literals;
55 class 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"
175 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
178 #include "pqxx/internal/ignore-deprecated-pre.hxx"
179 return conn().unesc_raw(
text);
180 #include "pqxx/internal/ignore-deprecated-post.hxx"
189 return conn().unesc_bin(
text);
194 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const
196 return conn().quote(t);
199 [[deprecated(
"Use bytes instead of binarystring.")]] std::string
206 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
207 quote_raw(
unsigned char const bin[], std::size_t len)
const
213 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
214 quote_raw(
zview bin)
const;
216 #if defined(PQXX_HAVE_CONCEPTS)
219 template<binary DATA>
220 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
222 return conn().quote_raw(data);
227 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
229 return conn().quote_name(identifier);
233 [[nodiscard]] std::string
234 esc_like(std::string_view bin,
char escape_char =
'\\')
const
236 return conn().esc_like(bin, escape_char);
288 [[deprecated(
"The desc parameter is going away.")]]
result
289 exec(std::string_view query, std::string_view desc);
298 #include "pqxx/internal/ignore-deprecated-pre.hxx"
299 return exec(query, std::string_view{});
300 #include "pqxx/internal/ignore-deprecated-post.hxx"
310 "Pass your query as a std::string_view, not stringstream.")]]
result
311 exec(std::stringstream
const &query, std::string_view desc)
313 #include "pqxx/internal/ignore-deprecated-pre.hxx"
314 return exec(query.str(), desc);
315 #include "pqxx/internal/ignore-deprecated-post.hxx"
324 [[deprecated(
"The desc parameter is going away.")]]
result
327 #include "pqxx/internal/ignore-deprecated-pre.hxx"
328 return exec_n(0, query, desc);
329 #include "pqxx/internal/ignore-deprecated-post.hxx"
347 [[deprecated(
"The desc parameter is going away.")]]
row
350 #include "pqxx/internal/ignore-deprecated-pre.hxx"
351 return exec_n(1, query, desc).
front();
352 #include "pqxx/internal/ignore-deprecated-post.hxx"
370 [[deprecated(
"The desc parameter is going away.")]]
result
381 #include "pqxx/internal/ignore-deprecated-pre.hxx"
382 return exec_n(rows, query, std::string_view{});
383 #include "pqxx/internal/ignore-deprecated-post.hxx"
390 template<
typename TYPE>
391 [[deprecated(
"The desc parameter is going away.")]] TYPE
394 #include "pqxx/internal/ignore-deprecated-pre.hxx"
395 row const r{exec1(query, desc)};
396 #include "pqxx/internal/ignore-deprecated-post.hxx"
397 if (std::size(r) != 1)
399 "Queried single value from result with ", std::size(r),
" columns.")};
400 return r[0].as<TYPE>();
412 row const r{exec1(query)};
413 if (std::size(r) != 1)
415 "Queried single value from result with ", std::size(r),
" columns.")};
416 return r[0].as<TYPE>();
427 template<
typename... TYPE>
430 return exec1(query).as<TYPE...>();
441 template<
typename... TYPE>
445 auto const rows{std::size(res)};
449 case 1:
return {res[0].as<TYPE...>()};
452 "Expected at most one row of data, got "sv, rows,
"."sv)};
508 template<
typename... TYPE>
509 [[nodiscard]]
auto stream(std::string_view query) &
511 return pqxx::internal::stream_query<TYPE...>{*
this, query};
544 template<
typename CALLABLE>
549 param_types
const *
const sample{
nullptr};
550 auto data_stream{stream_like(query, sample)};
551 for (
auto const &fields : data_stream) std::apply(func, fields);
554 template<
typename CALLABLE>
556 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
559 return for_stream(query, std::forward<CALLABLE>(func));
596 return exec(query).iter<TYPE...>();
610 return exec_n(rows, query).iter<TYPE...>();
625 exec(query).for_each(std::forward<CALLABLE>(func));
666 return internal_exec_params(query, pp.make_c_params());
674 return exec_params_n(1, query, std::forward<Args>(args)...).
front();
682 return exec_params_n(0, query, std::forward<Args>(args)...);
688 template<
typename... Args>
691 auto const r{exec_params(query, std::forward<Args>(args)...)};
694 check_rowcount_params(rows,
static_cast<unsigned>(std::size(r)));
701 template<
typename... Args>
704 auto const r{exec_params(query, std::forward<Args>(args)...)};
707 check_rowcount_params(
708 static_cast<unsigned>(rows),
static_cast<unsigned>(std::size(r)));
748 return exec_params(query, parms).iter<TYPE...>();
761 template<
typename... TYPE>
764 return exec_params_n(rows, query, parms).iter<TYPE...>();
776 row const r{exec_params1(query, parms)};
777 if (std::size(r) != 1)
779 "Queried single value from result with ", std::size(r),
" columns.")};
780 return r[0].as<TYPE>();
791 template<
typename... TYPE>
794 result const r{exec_params_n(1, query, parms)};
795 return r[0].as<TYPE...>();
806 template<
typename... TYPE>
807 [[nodiscard]] std::optional<std::tuple<TYPE...>>
810 result res{exec_params(query, parms)};
811 auto const rows{std::size(res)};
815 case 1:
return {res[0].as<TYPE...>()};
818 "Expected at most one row of data, got "sv, rows,
"."sv)};
835 template<
typename CALLABLE>
838 exec_params(query, parms).for_each(std::forward<CALLABLE>(func));
885 template<
typename... Args>
889 return internal_exec_prepared(statement, pp.
make_c_params());
895 template<
typename... Args>
898 return exec_prepared_n(1, statement, std::forward<Args>(args)...).
front();
904 template<
typename... Args>
907 return exec_prepared_n(0, statement, std::forward<Args>(args)...);
914 template<
typename... Args>
918 auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
919 check_rowcount_prepared(statement, rows, std::size(r));
955 "Set transaction-local variables using SQL SET statements.")]]
void
956 set_variable(std::string_view var, std::string_view value);
962 [[deprecated(
"Read variables using SQL SHOW statements.")]] std::string
963 get_variable(std::string_view);
967 [[nodiscard]] std::string_view
name() const & noexcept {
return m_name; }
976 std::shared_ptr<std::string> rollback_cmd) :
977 m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
992 void register_transaction();
995 void close() noexcept;
998 virtual
void do_commit() = 0;
1004 virtual
void do_abort();
1007 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
1009 m_rollback_cmd = cmd;
1013 result direct_exec(std::string_view, std::string_view desc =
""sv);
1015 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
1026 PQXX_PRIVATE
void check_pending_error();
1029 internal_exec_prepared(zview statement, internal::c_params
const &args);
1031 result internal_exec_params(zview query, internal::c_params
const &args);
1034 void check_rowcount_prepared(
1040 check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
1043 [[nodiscard]] std::string description()
const;
1045 friend class pqxx::internal::gate::transaction_transaction_focus;
1048 PQXX_PRIVATE
void register_pending_error(
zview) noexcept;
1049 PQXX_PRIVATE
void register_pending_error(std::string &&) noexcept;
1052 template<typename... ARGS>
1053 auto stream_like(std::string_view query, std::tuple<ARGS...> const *)
1055 return stream<ARGS...>(query);
1066 status m_status = status::active;
1067 bool m_registered =
false;
1069 std::string m_pending_error;
1072 std::shared_ptr<std::string> m_rollback_cmd;
1074 static constexpr std::string_view s_type_name{
"transaction"sv};
1081 std::string_view transaction_base::query_value<std::string_view>(
1082 zview query, std::string_view desc) =
delete;
1085 zview transaction_base::query_value<zview>(
1086 zview query, std::string_view desc) =
delete;
1094 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
1099 inline constexpr
zview begin_cmd<read_committed, write_policy::read_write>{
1102 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
1103 "BEGIN READ ONLY"_zv};
1105 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
1106 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1108 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
1109 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1111 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
1112 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1114 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
1115 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
1118 #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
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:407
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
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:84
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:627
Definition: connection.hxx:109
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:58
pqxx::bytes_view bytes_view() const
Read data as a bytes_view.
Definition: binarystring.hxx:177
Connection to a database.
Definition: connection.hxx:233
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: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
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:886
result exec_params_n(result::size_type rows, zview query, Args &&...args)
Definition: transaction_base.hxx:702
result exec0(zview query, std::string_view desc)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:325
bytes unesc_bin(char const text[])
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:187
auto for_each(std::string_view query, CALLABLE &&func)
Definition: transaction_base.hxx:557
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:836
auto query(zview query)
Execute query, read full results, then iterate rows of data.
Definition: transaction_base.hxx:594
bytes unesc_bin(zview text)
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:169
std::tuple< TYPE... > query1(zview query, params const &parms)
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:792
TYPE query_value(zview query, params const &parms)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:774
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:932
result exec0(zview query)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:338
auto query_n(result::size_type rows, zview query)
Perform query, expect given number of rows, iterate results.
Definition: transaction_base.hxx:608
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:176
transaction_base & operator=(transaction_base &&)=delete
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:392
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:916
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:974
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:194
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:672
TYPE query_value(zview query)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:410
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:311
std::string quote(binarystring const &t) const
Definition: transaction_base.hxx:200
row exec1(zview query)
Execute command returning a single row of data.
Definition: transaction_base.hxx:362
row exec1(zview query, std::string_view desc)
Execute command returning a single row of data.
Definition: transaction_base.hxx:348
transaction_base(transaction_base &&)=delete
std::tuple< TYPE... > query1(zview query)
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:428
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:442
result exec_params(zview query, Args &&...args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:663
transaction_base()=delete
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:680
auto for_stream(std::string_view query, CALLABLE &&func)
Perform a streaming query, and for each result row, call func.
Definition: transaction_base.hxx:545
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:905
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:234
result exec_n(result::size_type rows, zview query)
Execute command, expect given number of rows.
Definition: transaction_base.hxx:379
auto query_n(result::size_type rows, zview query, params const &parms)
Definition: transaction_base.hxx:762
result exec(std::string_view query)
Execute a command.
Definition: transaction_base.hxx:296
auto query(zview query, params const &parms)
Execute parameterised query, read full results, iterate rows of data.
Definition: transaction_base.hxx:746
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:808
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:896
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:227
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:207
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:967
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition: transaction_base.hxx:936
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:689
auto stream(std::string_view query) &
Execute a query, in streaming fashion; loop over the results row by row.
Definition: transaction_base.hxx:509
void for_query(zview query, CALLABLE &&func)
Execute a query, load the full result, and perform func for each row.
Definition: transaction_base.hxx:623
transaction_base & operator=(transaction_base const &)=delete
void process_notice(char const msg[]) const
Have connection process a warning message.
Definition: transaction_base.hxx:930
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