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/prepared_statement.hxx"
38 #include "pqxx/result.hxx"
39 #include "pqxx/row.hxx"
40 #include "pqxx/util.hxx"
44 class transaction_subtransaction;
45 class transaction_sql_cursor;
46 class transaction_stream_to;
47 class transaction_transaction_focus;
56 class transaction_focus;
193 template<
typename... ARGS> [[nodiscard]]
auto esc(ARGS &&...args)
const
196 return conn().esc(std::forward<ARGS>(args)...);
211 template<
typename... ARGS> [[nodiscard]]
auto esc_raw(ARGS &&...args)
const
213 return conn().esc_raw(std::forward<ARGS>(args)...);
220 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
223 #include "pqxx/internal/ignore-deprecated-pre.hxx"
224 return conn().unesc_raw(text);
225 #include "pqxx/internal/ignore-deprecated-post.hxx"
238 [[nodiscard, deprecated(
"Use unesc_bin() instead.")]] std::string
241 #include "pqxx/internal/ignore-deprecated-pre.hxx"
242 return conn().unesc_raw(text);
243 #include "pqxx/internal/ignore-deprecated-post.hxx"
252 return conn().unesc_bin(text);
257 template<
typename T> [[nodiscard]] std::string
quote(T
const &t)
const
259 return conn().quote(t);
262 [[deprecated(
"Use bytes instead of binarystring.")]] std::string
269 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
270 quote_raw(
unsigned char const bin[], std::size_t len)
const
276 [[deprecated(
"Use quote(pqxx::bytes_view).")]] std::string
277 quote_raw(
zview bin)
const;
279 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
282 template<binary DATA>
283 [[nodiscard]] std::string quote_raw(DATA
const &data)
const
285 return conn().quote_raw(data);
290 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
292 return conn().quote_name(identifier);
296 [[nodiscard]] std::string
297 esc_like(std::string_view bin,
char escape_char =
'\\')
const
299 return conn().esc_like(bin, escape_char);
345 [[deprecated(
"The desc parameter is going away.")]]
346 result exec(std::string_view query, std::string_view desc);
362 #include "pqxx/internal/ignore-deprecated-pre.hxx"
363 return exec(query, std::string_view{});
364 #include "pqxx/internal/ignore-deprecated-post.hxx"
374 "Pass your query as a std::string_view, not stringstream.")]]
result
375 exec(std::stringstream
const &query, std::string_view desc)
377 #include "pqxx/internal/ignore-deprecated-pre.hxx"
378 return exec(query.str(), desc);
379 #include "pqxx/internal/ignore-deprecated-post.hxx"
388 [[deprecated(
"Use exec(string_view) and call no_rows() on the result.")]]
391 #include "pqxx/internal/ignore-deprecated-pre.hxx"
392 return exec(query, desc).
no_rows();
393 #include "pqxx/internal/ignore-deprecated-post.hxx"
402 [[deprecated(
"Use exec() and call no_rows() on the result.")]]
415 [[deprecated(
"Use exec(string_view), and call one_row() on the result.")]]
418 #include "pqxx/internal/ignore-deprecated-pre.hxx"
419 return exec(query, desc).one_row();
420 #include "pqxx/internal/ignore-deprecated-post.hxx"
430 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
433 return exec(query).one_row();
442 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
443 result exec_n(result::size_type rows,
zview query, std::string_view desc);
451 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
454 #include "pqxx/internal/ignore-deprecated-pre.hxx"
455 return exec(query, std::string_view{}).expect_rows(rows);
456 #include "pqxx/internal/ignore-deprecated-post.hxx"
463 template<
typename TYPE>
464 [[deprecated(
"The desc parameter is going away.")]]
467 #include "pqxx/internal/ignore-deprecated-pre.hxx"
468 return exec(query, desc).one_field().as<TYPE>();
469 #include "pqxx/internal/ignore-deprecated-post.hxx"
481 return exec(query).one_field().as<TYPE>();
492 template<
typename... TYPE>
495 return exec(query).expect_columns(
sizeof...(TYPE)).one_row().as<TYPE...>();
506 template<
typename... TYPE>
509 std::optional<row>
const r{exec(query).opt_row()};
511 return {r->as<TYPE...>()};
568 template<
typename... TYPE>
569 [[nodiscard]]
auto stream(std::string_view query) &
604 template<
typename CALLABLE>
609 param_types
const *
const sample{
nullptr};
610 auto data_stream{stream_like(query, sample)};
611 for (
auto const &fields : data_stream) std::apply(func, fields);
614 template<
typename CALLABLE>
616 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
617 for_each(std::string_view query, CALLABLE &&func)
619 return for_stream(query, std::forward<CALLABLE>(func));
656 return exec(query).iter<TYPE...>();
660 template<
typename... TYPE>
661 [[deprecated(
"Use query() instead, and call expect_rows() on the result.")]]
664 return exec(query).expect_rows(rows).iter<TYPE...>();
679 exec(query).for_each(std::forward<CALLABLE>(func));
717 template<
typename... Args>
718 [[deprecated(
"Use exec(zview, params) instead.")]]
721 return exec(query,
params{args...});
727 template<
typename... Args>
728 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
731 return exec(query,
params{args...}).one_row();
737 template<
typename... Args>
739 "Use exec(string_view, params) and call no_rows() on the result.")]]
742 return exec(query,
params{args...}).no_rows();
748 template<
typename... Args>
749 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
752 return exec(query,
params{args...})
753 .expect_rows(check_cast<result_size_type>(rows,
"number of rows"));
759 template<
typename... Args>
760 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
763 return exec(query,
params{args...}).expect_rows(rows);
802 return exec(query, parms).iter<TYPE...>();
815 template<
typename... TYPE>
816 [[deprecated(
"Use exec(), and call expect_rows() & iter() on the result.")]]
819 return exec(query, parms).expect_rows(rows).iter<TYPE...>();
831 return exec(query, parms).expect_columns(1).one_field().as<TYPE>();
842 template<
typename... TYPE>
846 return exec(query, parms).one_row().as<TYPE...>();
857 template<
typename... TYPE>
858 [[nodiscard]] std::optional<std::tuple<TYPE...>>
861 std::optional<row> r{exec(query, parms).opt_row()};
863 return {r->as<TYPE...>()};
881 template<
typename CALLABLE>
884 exec(query, parms).for_each(std::forward<CALLABLE>(func));
903 void notify(std::string_view channel, std::string_view payload = {});
907 template<
typename... Args>
908 [[deprecated(
"Use exec(prepped, params) instead.")]]
918 return internal_exec_prepared(statement, pp.
make_c_params());
927 template<
typename... TYPE>
930 return exec(statement, parms).iter<TYPE...>();
937 template<
typename TYPE>
940 return exec(statement, parms).expect_columns(1).one_field().as<TYPE>();
947 template<
typename CALLABLE>
950 exec(statement, parms).for_each(std::forward<CALLABLE>(func));
956 return internal_exec_prepared(statement, parms.
make_c_params());
962 template<
typename... Args>
964 "Use exec(string_view, params) and call one_row() on the result.")]]
973 template<
typename... Args>
975 "Use exec(prepped, params), and call no_rows() on the result.")]]
985 template<
typename... Args>
987 "Use exec(prepped, params), and call expect_rows() on the result.")]]
998 void process_notice(
char const msg[])
const { m_conn.process_notice(msg); }
1023 [[deprecated(
"Set transaction-local variables using SQL SET statements.")]]
1024 void set_variable(std::string_view var, std::string_view value);
1030 [[deprecated(
"Read variables using SQL SHOW statements.")]]
1031 std::string get_variable(std::string_view);
1035 [[nodiscard]] std::string_view
name() const & noexcept {
return m_name; }
1044 std::shared_ptr<std::string> rollback_cmd) :
1045 m_conn{cx}, m_name{tname}, m_rollback_cmd{rollback_cmd}
1054 transaction_base(connection &cx, std::string_view tname);
1057 explicit transaction_base(connection &cx);
1060 void register_transaction();
1063 void close() noexcept;
1066 virtual
void do_commit() = 0;
1072 virtual
void do_abort();
1075 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
1077 m_rollback_cmd = cmd;
1081 result direct_exec(std::string_view, std::string_view desc =
""sv);
1083 direct_exec(std::shared_ptr<std::string>, std::string_view desc =
""sv);
1094 PQXX_PRIVATE
void check_pending_error();
1096 result internal_exec_prepared(
1097 std::string_view statement, internal::c_params
const &args);
1100 internal_exec_params(std::string_view query, internal::c_params
const &args);
1103 [[nodiscard]] std::string description()
const;
1106 PQXX_PRIVATE
void register_focus(transaction_focus *);
1107 PQXX_PRIVATE
void unregister_focus(transaction_focus *) noexcept;
1108 PQXX_PRIVATE
void register_pending_error(zview) noexcept;
1109 PQXX_PRIVATE
void register_pending_error(std::
string &&) noexcept;
1112 template<typename... ARGS>
1113 auto stream_like(std::string_view query, std::tuple<ARGS...> const *)
1115 return stream<ARGS...>(query);
1124 transaction_focus
const *m_focus =
nullptr;
1126 status m_status = status::active;
1127 bool m_registered =
false;
1129 std::string m_pending_error;
1132 std::shared_ptr<std::string> m_rollback_cmd;
1134 static constexpr std::string_view s_type_name{
"transaction"sv};
1141 std::string_view transaction_base::query_value<std::string_view>(
1142 zview query, std::string_view desc) =
delete;
1145 zview transaction_base::query_value<zview>(
1146 zview query, std::string_view desc) =
delete;
1154 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
1159 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
1162 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
1163 "BEGIN READ ONLY"_zv};
1165 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
1166 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1168 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
1169 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1171 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
1172 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1174 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
1175 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
1178 #include "pqxx/internal/stream_query_impl.hxx"
auto query(zview query, params const &parms)
Execute parameterised query, read full results, iterate rows of data.
Definition: transaction_base.hxx:800
row exec1(zview query)
Execute command returning a single row of data.
Definition: transaction_base.hxx:431
row exec1(zview query, std::string_view desc)
Execute command returning a single row of data.
Definition: transaction_base.hxx:416
bytes_view binary_cast(TYPE const &data)
End a code block started by "ignore-deprecated-pre.hxx".
Definition: util.hxx:409
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a bytea field.
Definition: transaction_base.hxx:221
result exec(std::stringstream const &query, std::string_view desc)
Execute a command.
Definition: transaction_base.hxx:375
Definition: transaction-transaction_focus.hxx:7
auto stream(std::string_view query)&
Execute a query, in streaming fashion; loop over the results row by row.
Definition: transaction_base.hxx:569
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:965
result exec(prepped statement, params const &parms)
Execute a prepared statement with parameters.
Definition: transaction_base.hxx:954
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:909
transaction_base(connection &cx, 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:1042
Internal items for libpqxx' own use. Do not use these yourself.
Definition: encodings.cxx:32
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a bytea field.
Definition: transaction_base.hxx:239
auto for_stream(std::string_view query, CALLABLE &&func)
Perform a streaming query, and for each result row, call func.
Definition: transaction_base.hxx:605
void for_query(zview query, CALLABLE &&func)
Execute a query, load the full result, and perform func for each row.
Definition: transaction_base.hxx:677
result exec(std::string_view query)
Execute a command.
Definition: transaction_base.hxx:360
Reference to one row in a result.
Definition: row.hxx:46
A string that is the name of a prepared statement.
Definition: prepared_statement.hxx:69
bytes unesc_bin(char const text[])
Unescape binary data, e.g. from a bytea field.
Definition: transaction_base.hxx:250
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:729
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition: transaction_base.hxx:1005
result exec_params_n(result::size_type rows, zview query, Args &&...args)
Definition: transaction_base.hxx:761
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:465
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:1001
Stream query results from the database. Used by transaction_base::stream.
Definition: stream_query.hxx:79
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:290
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:95
auto esc_raw(ARGS &&...args) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:211
std::tuple< TYPE... > query1(zview query)
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:493
auto query_n(result::size_type rows, zview query, params const &parms)
Definition: transaction_base.hxx:817
Result set containing data returned by a query or command.
Definition: result.hxx:91
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:270
Definition: connection.hxx:106
TYPE query_value(prepped statement, params const &parms={})
Perform prepared statement returning exactly 1 value.
Definition: transaction_base.hxx:938
std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:1035
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:989
result exec0(zview query)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:403
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:882
auto query_n(result::size_type rows, zview query)
Perform query, expect given number of rows, iterate results.
Definition: transaction_base.hxx:662
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:507
auto query(prepped statement, params const &parms={})
Execute prepared statement, read full results, iterate rows of data.
Definition: transaction_base.hxx:928
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:257
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:976
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:750
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:32
bytes unesc_bin(zview text)
Unescape binary data, e.g. from a bytea field.
Definition: transaction_base.hxx:232
pqxx::bytes_view bytes_view() const
Read data as a bytes_view.
Definition: binarystring.hxx:177
std::string esc_like(std::string_view bin, char escape_char= '\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:297
const zview begin_cmd
The SQL command for starting a given type of transaction.
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:57
TYPE query_value(zview query, params const &parms)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:829
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
TYPE query_value(zview query)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:479
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
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
void for_query(prepped statement, CALLABLE &&func, params const &parms={})
Execute prepared statement, load result, perform func for each row.
Definition: transaction_base.hxx:948
result exec(prepped statement)
Execute a prepared statement taking no parameters.
Definition: transaction_base.hxx:915
std::tuple< TYPE... > query1(zview query, params const &parms)
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:844
result exec0(zview query, std::string_view desc)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:389
result no_rows() const
Expect that result contains no rows. Return result for convenience.
Definition: result.hxx:353
result exec_params(std::string_view query, Args &&...args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:719
auto query(zview query)
Execute query, read full results, then iterate rows of data.
Definition: transaction_base.hxx:654
Connection to a database.
Definition: connection.hxx:278
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:859
result exec_n(result::size_type rows, zview query)
Execute command, expect given number of rows.
Definition: transaction_base.hxx:452
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:740
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:150