14 #ifndef PQXX_TRANSACTION_BASE_HXX
15 #define PQXX_TRANSACTION_BASE_HXX
17 #if !defined(PQXX_HEADER_PRE)
18 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
21 #include <string_view>
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;
175 void commit(
sl = sl::current());
181 void abort(
sl = sl::current());
194 esc(
char const str[],
sl loc = sl::current())
196 return conn().esc(str, loc);
200 [[nodiscard]] std::string_view
esc(
201 std::string_view text, std::span<char> buffer,
202 sl loc = sl::current())
const
204 return conn().esc(
text, buffer, loc);
208 [[nodiscard]] std::string
209 esc(std::string_view text,
sl loc = sl::current())
const
211 return conn().esc(
text, loc);
215 template<binary DATA> [[nodiscard]] std::string
esc(DATA
const &data)
const
217 return conn().esc(data);
232 template<binary DATA>
234 esc(DATA
const &data, std::span<char> buffer,
sl loc = sl::current())
const
236 return conn().esc(data, buffer, loc);
240 template<binary DATA>
242 deprecated(
"Use esc(), not esc_raw(), even on binary data.")]]
zview
245 return conn().esc(data, loc);
249 template<binary DATA>
251 deprecated(
"Use esc(), not esc_raw(), even on binary data.")]]
zview
253 DATA
const &data, std::span<char> buffer,
sl loc = sl::current())
const
255 return conn().esc(data, buffer, loc);
264 return conn().unesc_bin(
text, loc);
274 return conn().unesc_bin(
text, loc);
281 [[nodiscard]] std::string
quote(T
const &t,
sl loc = sl::current())
const
283 return conn().quote(t, loc);
288 template<binary DATA>
289 [[nodiscard]] std::string
292 return conn().quote_raw(data, loc);
297 [[nodiscard]] std::string
quote_name(std::string_view identifier)
const
299 return conn().quote_name(identifier);
305 std::string_view bin,
char escape_char =
'\\',
306 sl loc = sl::current())
const
308 return conn().esc_like(bin, escape_char, loc);
353 [[deprecated(
"The desc parameter is going away.")]]
355 exec(std::string_view query, std::string_view desc,
sl = sl::current());
360 exec(std::string_view query,
params const &parms,
sl loc = sl::current())
362 return internal_exec_params(query, parms.
make_c_params(loc), loc);
370 result exec(std::string_view query,
sl = sl::current());
379 "Pass your query as a std::string_view, not stringstream.")]]
result
380 exec(std::stringstream
const &query, std::string_view desc)
383 return exec(query.str(), desc);
393 [[deprecated(
"Use exec(string_view) and call no_rows() on the result.")]]
397 return exec(query, desc).
no_rows();
407 [[deprecated(
"Use exec() and call no_rows() on the result.")]]
420 [[deprecated(
"Use exec(string_view), and call one_row() on the result.")]]
421 row exec1(std::string_view query, std::string_view desc)
424 return exec(query, desc).one_row();
435 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
438 return exec(query).one_row();
447 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
457 [[deprecated(
"Use exec() instead, and call expect_rows() on the result.")]]
461 return exec(query, std::string_view{}).expect_rows(rows);
469 template<not_borrowed TYPE>
470 [[deprecated(
"The desc parameter is going away.")]]
474 return exec(query, desc).one_field().as<TYPE>();
485 template<not_borrowed TYPE>
490 return exec(query, loc).one_field_ref(loc).as<TYPE>(loc);
502 [[nodiscard]] std::tuple<TYPE...>
503 query1(std::string_view query,
sl loc = sl::current())
507 return exec(query, loc)
508 .expect_columns(
sizeof...(TYPE), loc)
522 [[nodiscard]] std::optional<std::tuple<TYPE...>>
525 auto const res{exec(query, loc)};
526 std::optional<row_ref>
const r{res.opt_row_ref(loc)};
528 return {r->as<TYPE...>(loc)};
589 template<std::move_constructible... TYPE>
590 [[nodiscard]]
auto stream(std::string_view query,
sl loc = sl::current()) &
593 *
this, query, make_context(loc)};
625 template<
typename CALLABLE>
627 for_stream(std::string_view query, CALLABLE &&func,
sl loc = sl::current())
632 param_types
const *
const sample{
nullptr};
633 auto data_stream{stream_like(query, sample, loc)};
634 for (
auto const &fields : data_stream) std::apply(func, fields);
637 template<
typename CALLABLE>
639 "pqxx::transaction_base::for_each is now called for_stream.")]]
auto
640 for_each(std::string_view query, CALLABLE &&func,
sl loc = sl::current())
642 return for_stream(query, std::forward<CALLABLE>(func), loc);
677 template<
typename... TYPE>
678 auto query(std::string_view query,
sl loc = sl::current())
680 return exec(query, loc).iter<TYPE...>();
684 template<
typename... TYPE>
685 [[deprecated(
"Use query() instead, and call expect_rows() on the result.")]]
689 return exec(query, loc).expect_rows(rows, loc).iter<TYPE...>();
701 template<
typename CALLABLE>
703 for_query(std::string_view query, CALLABLE &&func,
sl loc = sl::current())
705 exec(query).for_each(std::forward<CALLABLE>(func), loc);
743 template<
typename... Args>
744 [[deprecated(
"Use exec(std::string_view, params) instead.")]]
747 return exec(query,
params{*
this, args...}, sl::current());
753 template<
typename... Args>
754 [[deprecated(
"Use exec() instead, and call one_row() on the result.")]]
757 return exec(query,
params{*
this, args...}).one_row();
763 template<
typename... Args>
765 "Use exec(string_view, params) and call no_rows() on the result.")]]
768 return exec(query,
params{*
this, args...}).no_rows();
774 template<
typename... Args>
775 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
779 sl const loc{m_created_loc};
780 return exec(query,
params{*
this, args...}, loc)
782 check_cast<result_size_type>(rows,
"number of rows", loc), loc);
788 template<
typename... Args>
789 [[deprecated(
"Use exec(), and call expect_rows() on the result.")]]
793 return exec(query,
params{*
this, args...}).expect_rows(rows);
830 template<
typename... TYPE>
834 return exec(query, parms, loc).iter<TYPE...>();
847 template<
typename... TYPE>
848 [[deprecated(
"Use exec(), and call expect_rows() & iter() on the result.")]]
852 return exec(query, parms).expect_rows(rows).iter<TYPE...>();
862 template<not_borrowed TYPE>
864 std::string_view query,
params const &parms,
sl loc = sl::current())
868 return exec(query, parms, loc)
869 .expect_columns(1, loc)
889 return exec(query, parms, loc).one_row_ref(loc).as<TYPE...>(loc);
901 [[nodiscard]] std::optional<std::tuple<TYPE...>>
904 auto const res{exec(query, parms, loc)};
905 std::optional<row_ref> r{res.opt_row_ref(loc)};
907 return {r->as<TYPE...>(loc)};
924 template<
typename CALLABLE>
926 std::string_view query, CALLABLE &&func,
params const &parms,
927 sl loc = sl::current())
929 exec(query, parms, loc).for_each(std::forward<CALLABLE>(func), loc);
949 std::string_view channel, std::string_view payload,
sl = sl::current());
963 void notify(std::string_view channel,
sl loc = sl::current())
965 notify(channel, {}, loc);
970 template<
typename... Args>
971 [[deprecated(
"Use exec(prepped, params) instead.")]]
981 return internal_exec_prepared(statement, pp.
make_c_params(loc), loc);
990 template<
typename... TYPE>
993 return exec(statement, parms, loc).iter<TYPE...>();
1002 template<
typename... TYPE>
1005 return exec(statement, {}, loc).iter<TYPE...>();
1012 template<not_borrowed TYPE>
1018 return exec(statement, parms, loc)
1019 .expect_columns(1, loc)
1028 template<not_borrowed TYPE>
1031 return exec(statement, {}, loc)
1032 .expect_columns(1, loc)
1041 template<
typename CALLABLE>
1044 sl loc = sl::current())
1046 exec(statement, parms, loc).for_each(std::forward<CALLABLE>(func), loc);
1053 template<
typename CALLABLE>
1056 exec(statement, {}, loc).for_each(std::forward<CALLABLE>(func), loc);
1062 return internal_exec_prepared(statement, parms.
make_c_params(loc), loc);
1068 template<
typename... Args>
1070 "Use exec(string_view, params) and call one_row() on the result.")]]
1073 sl const loc{m_created_loc};
1074 return exec(
prepped{statement},
params{*
this, args...}).one_row(loc);
1080 template<
typename... Args>
1082 "Use exec(prepped, params), and call no_rows() on the result.")]]
1085 sl const loc{m_created_loc};
1086 return exec(
prepped{statement},
params{*
this, args...}).no_rows(loc);
1093 template<
typename... Args>
1095 "Use exec(prepped, params), and call expect_rows() on the result.")]]
1099 sl const loc{m_created_loc};
1101 .expect_rows(rows, loc);
1111 m_conn.process_notice(msg);
1136 [[deprecated(
"Set transaction-local variables using SQL SET statements.")]]
1137 void set_variable(std::string_view var, std::string_view value);
1143 [[deprecated(
"Read variables using SQL SHOW statements.")]]
1144 std::string get_variable(std::string_view);
1147 [[nodiscard]] constexpr std::string_view
name() const & noexcept
1158 connection &, std::string_view, std::shared_ptr<std::string> rollback_cmd,
1159 sl loc = sl::current());
1168 connection &cx, std::string_view tname,
sl loc = sl::current());
1174 void register_transaction();
1177 void close(
sl = sl::current()) noexcept;
1180 virtual
void do_commit(
sl) = 0;
1186 virtual
void do_abort(
sl);
1189 void set_rollback_cmd(std::shared_ptr<std::
string> cmd)
1191 m_rollback_cmd = std::move(cmd);
1195 result direct_exec(std::string_view, std::string_view desc,
sl);
1198 return direct_exec(query,
"", loc);
1200 result direct_exec(std::shared_ptr<std::string>, std::string_view desc,
sl);
1203 return direct_exec(std::move(query),
"", loc);
1223 [[nodiscard]] conversion_context make_context(
sl)
const;
1227 result internal_exec_prepared(
1228 std::string_view statement, internal::c_params
const &args,
sl);
1230 result internal_exec_params(
1231 std::string_view query, internal::c_params
const &args,
sl);
1234 [[nodiscard]] std::string description()
const;
1240 PQXX_PRIVATE void register_pending_error(std::string &&,
sl) noexcept;
1243 template<
typename... ARGS>
1245 std::string_view query, std::tuple<ARGS...>
const *,
1246 sl loc = sl::current())
1248 return stream<ARGS...>(query, loc);
1260 std::string m_pending_error;
1263 std::shared_ptr<std::string> m_rollback_cmd;
1265 status m_status = status::active;
1267 bool m_registered =
false;
1272 static constexpr std::string_view s_type_name{
"transaction"sv};
1280 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
1285 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
1288 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
1289 "BEGIN READ ONLY"_zv};
1291 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
1292 "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1294 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
1295 "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1297 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
1298 "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1300 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
1301 "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
Connection to a database.
Definition: connection.hxx:273
Definition: transaction-transaction_focus.hxx:12
Stream query results from the database. Used by transaction_base::stream.
Definition: stream_query.hxx:68
Build a parameter list for a parameterised or prepared statement.
Definition: params.hxx:65
pqxx::internal::c_params make_c_params(sl loc) const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:101
A string that is the name of a prepared statement.
Definition: prepared_statement.hxx:70
Result set containing data returned by a query or command.
Definition: result.hxx:101
result_size_type size_type
Definition: result.hxx:103
result const & no_rows(sl loc=sl::current()) const
Expect that result contains no rows. Return result for convenience.
Definition: result.hxx:465
Reference to one row in a result.
Definition: row.hxx:415
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:55
PQXX_ZARGS std::string esc(char const str[], sl loc=sl::current())
Definition: transaction_base.hxx:194
zview esc_raw(DATA const &data, sl loc=sl::current()) const
Escape binary string for use as SQL string literal, into buffer.
Definition: transaction_base.hxx:243
auto query_n(result::size_type rows, std::string_view query, params const &parms)
Definition: transaction_base.hxx:850
void for_query(std::string_view query, CALLABLE &&func, params const &parms, sl loc=sl::current())
Execute a query, load the full result, and perform func for each row.
Definition: transaction_base.hxx:925
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:972
std::string esc_like(std::string_view bin, char escape_char='\\', sl loc=sl::current()) const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:304
PQXX_ZARGS void process_notice(char const msg[]) const
Have connection process a warning message.
Definition: transaction_base.hxx:1109
TYPE query_value(prepped statement, sl loc=sl::current())
Perform prepared statement returning exactly 1 value.
Definition: transaction_base.hxx:1029
std::string esc(std::string_view text, sl loc=sl::current()) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:209
auto stream(std::string_view query, sl loc=sl::current()) &
Execute a query, in streaming fashion; loop over the results row by row.
Definition: transaction_base.hxx:590
row exec_params1(std::string_view query, Args &&...args)
Definition: transaction_base.hxx:755
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:1114
bytes unesc_bin(zview text, sl loc=sl::current())
Unescape binary data, e.g. from a bytea field.
Definition: transaction_base.hxx:262
auto for_each(std::string_view query, CALLABLE &&func, sl loc=sl::current())
Definition: transaction_base.hxx:640
result exec0(std::string_view query)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:408
TYPE query_value(std::string_view query, sl loc=sl::current())
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:486
transaction_base & operator=(transaction_base &&)=delete
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:1097
result direct_exec(std::string_view query, sl loc)
Definition: transaction_base.hxx:1196
transaction_base(transaction_base const &)=delete
std::string_view esc(std::string_view text, std::span< char > buffer, sl loc=sl::current()) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:200
auto query(prepped statement, sl loc=sl::current())
Execute prepared statement, read full results, iterate rows of data.
Definition: transaction_base.hxx:1003
std::string quote(T const &t, sl loc=sl::current()) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:281
zview esc(DATA const &data, std::span< char > buffer, sl loc=sl::current()) const
Escape binary string for use as SQL string literal, into buffer.
Definition: transaction_base.hxx:234
row exec1(std::string_view query)
Execute command returning a single row of data.
Definition: transaction_base.hxx:436
void for_query(prepped statement, CALLABLE &&func, params const &parms, sl loc=sl::current())
Execute prepared statement, load result, perform func for each row.
Definition: transaction_base.hxx:1042
auto query(std::string_view query, sl loc=sl::current())
Execute query, read full results, then iterate rows of data.
Definition: transaction_base.hxx:678
zview esc_raw(DATA const &data, std::span< char > buffer, sl loc=sl::current()) const
Escape binary string for use as SQL string literal, into buffer.
Definition: transaction_base.hxx:252
std::string esc(DATA const &data) const
Escape binary string for use as SQL string literal.
Definition: transaction_base.hxx:215
row exec1(std::string_view query, std::string_view desc)
Execute command returning a single row of data.
Definition: transaction_base.hxx:421
void for_query(std::string_view query, CALLABLE &&func, sl loc=sl::current())
Execute a query, load the full result, and perform func for each row.
Definition: transaction_base.hxx:703
result exec_params_n(result::size_type rows, std::string_view query, Args &&...args)
Definition: transaction_base.hxx:791
result exec(std::stringstream const &query, std::string_view desc)
Execute a command.
Definition: transaction_base.hxx:380
auto query_n(result::size_type rows, std::string_view query, sl loc=sl::current())
Perform query, expect given number of rows, iterate results.
Definition: transaction_base.hxx:686
auto query(std::string_view query, params const &parms, sl loc=sl::current())
Execute parameterised query, read full results, iterate rows of data.
Definition: transaction_base.hxx:832
void notify(std::string_view channel, sl loc=sl::current())
Send a notification (without payload).
Definition: transaction_base.hxx:963
result exec_n(result::size_type rows, std::string_view query)
Execute command, expect given number of rows.
Definition: transaction_base.hxx:458
transaction_base(transaction_base &&)=delete
auto for_stream(std::string_view query, CALLABLE &&func, sl loc=sl::current())
Perform a streaming query, and for each result row, call func.
Definition: transaction_base.hxx:627
TYPE query_value(std::string_view query, std::string_view desc)
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:471
PQXX_ZARGS bytes unesc_bin(char const text[], sl loc=sl::current())
Unescape binary data, e.g. from a bytea field.
Definition: transaction_base.hxx:272
sl created_loc() const
The std::source_location for this transaction's creation.
Definition: transaction_base.hxx:1208
result exec_params_n(std::size_t rows, std::string_view query, Args &&...args)
Definition: transaction_base.hxx:777
transaction_base()=delete
std::optional< std::tuple< TYPE... > > query01(std::string_view query, sl loc=sl::current())
Query at most one row of data, and if there is one, convert it.
Definition: transaction_base.hxx:523
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:1083
auto query(prepped statement, params const &parms, sl loc=sl::current())
Execute prepared statement, read full results, iterate rows of data.
Definition: transaction_base.hxx:991
constexpr std::string_view name() const &noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:1147
void for_query(prepped statement, CALLABLE &&func, sl loc=sl::current())
Execute prepared statement, load result, perform func for each row.
Definition: transaction_base.hxx:1054
result exec_params(std::string_view query, Args &&...args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:745
std::optional< std::tuple< TYPE... > > query01(std::string_view query, params const &parms, sl loc=sl::current())
Query at most one row of data, and if there is one, convert it.
Definition: transaction_base.hxx:902
std::string quote_raw(DATA const &data, sl loc=sl::current()) const
Binary-escape and quote a binary string for use as an SQL constant.
Definition: transaction_base.hxx:290
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:1071
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:297
TYPE query_value(prepped statement, params const &parms, sl loc=sl::current())
Perform prepared statement returning exactly 1 value.
Definition: transaction_base.hxx:1014
TYPE query_value(std::string_view query, params const &parms, sl loc=sl::current())
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:863
result exec_params0(std::string_view query, Args &&...args)
Definition: transaction_base.hxx:766
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition: transaction_base.hxx:1118
std::tuple< TYPE... > query1(std::string_view query, sl loc=sl::current())
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:503
result direct_exec(std::shared_ptr< std::string > query, sl loc)
Definition: transaction_base.hxx:1201
result exec(prepped statement, params const &parms, sl loc=sl::current())
Execute a prepared statement with parameters.
Definition: transaction_base.hxx:1060
result exec0(std::string_view query, std::string_view desc)
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:394
transaction_base & operator=(transaction_base const &)=delete
result exec(std::string_view query, params const &parms, sl loc=sl::current())
Definition: transaction_base.hxx:360
std::tuple< TYPE... > query1(std::string_view query, params const &parms, sl loc=sl::current())
Perform query returning exactly one row, and convert its fields.
Definition: transaction_base.hxx:885
result exec(prepped statement, sl loc=sl::current())
Execute a prepared statement taking no parameters.
Definition: transaction_base.hxx:978
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:151
Definition: connection.hxx:94
Private namespace for libpqxx's internal use; do not access.
Definition: connection.cxx:333
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 std::remove_cvref_t to its component types.
Definition: util.hxx:612
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
concept not_borrowed
Concept: A value that's not just a reference to values elsewhere.
Definition: types.hxx:206
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
std::vector< std::byte > bytes
Type alias for a container containing bytes.
Definition: util.hxx:240