libpqxx
The C++ client library for PostgreSQL
transaction_base.hxx
Go to the documentation of this file.
1 /* Common code and definitions for the transaction classes.
2  *
3  * pqxx::transaction_base defines the interface for any abstract class that
4  * represents a database transaction.
5  *
6  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/transaction_base instead.
7  *
8  * Copyright (c) 2000-2026, Jeroen T. Vermeulen.
9  *
10  * See COPYING for copyright license. If you did not receive a file called
11  * COPYING with this source code, please notify the distributor of this
12  * mistake, or contact the author.
13  */
14 #ifndef PQXX_TRANSACTION_BASE_HXX
15 #define PQXX_TRANSACTION_BASE_HXX
16 
17 #if !defined(PQXX_HEADER_PRE)
18 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
19 #endif
20 
21 #include <string_view>
22 
23 /* End-user programs need not include this file, unless they define their own
24  * transaction classes. This is not something the typical program should want
25  * to do.
26  *
27  * However, reading this file is worthwhile because it defines the public
28  * interface for the available transaction classes such as transaction and
29  * nontransaction.
30  */
31 
32 #include "pqxx/connection.hxx"
33 #include "pqxx/encoding_group.hxx"
35 #include "pqxx/isolation.hxx"
37 #include "pqxx/result.hxx"
38 #include "pqxx/row.hxx"
39 #include "pqxx/util.hxx"
40 
41 namespace pqxx::internal::gate
42 {
43 class transaction_subtransaction;
44 class transaction_sql_cursor;
45 class transaction_stream_to;
46 class transaction_transaction_focus;
47 } // namespace pqxx::internal::gate
48 
49 
50 namespace pqxx
51 {
52 using namespace std::literals;
53 
54 
55 class transaction_focus;
56 
57 
145 
151 {
152 public:
153  transaction_base() = delete;
158 
159  virtual ~transaction_base() = 0;
160 
162 
175  void commit(sl = sl::current());
176 
178 
181  void abort(sl = sl::current());
182 
193  PQXX_ZARGS [[nodiscard]] std::string
194  esc(char const str[], sl loc = sl::current())
195  {
196  return conn().esc(str, loc);
197  }
198 
200  [[nodiscard]] std::string_view esc(
201  std::string_view text, std::span<char> buffer,
202  sl loc = sl::current()) const
203  {
204  return conn().esc(text, buffer, loc);
205  }
206 
208  [[nodiscard]] std::string
209  esc(std::string_view text, sl loc = sl::current()) const
210  {
211  return conn().esc(text, loc);
212  }
213 
215  template<binary DATA> [[nodiscard]] std::string esc(DATA const &data) const
216  {
217  return conn().esc(data);
218  }
219 
221 
232  template<binary DATA>
233  [[nodiscard]] zview
234  esc(DATA const &data, std::span<char> buffer, sl loc = sl::current()) const
235  {
236  return conn().esc(data, buffer, loc);
237  }
238 
240  template<binary DATA>
241  [[nodiscard,
242  deprecated("Use esc(), not esc_raw(), even on binary data.")]] zview
243  esc_raw(DATA const &data, sl loc = sl::current()) const
244  {
245  return conn().esc(data, loc);
246  }
247 
249  template<binary DATA>
250  [[nodiscard,
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
254  {
255  return conn().esc(data, buffer, loc);
256  }
257 
259 
262  [[nodiscard]] bytes unesc_bin(zview text, sl loc = sl::current())
263  {
264  return conn().unesc_bin(text, loc);
265  }
266 
268 
271  PQXX_ZARGS [[nodiscard]] bytes
272  unesc_bin(char const text[], sl loc = sl::current())
273  {
274  return conn().unesc_bin(text, loc);
275  }
276 
277  // TODO: "Into buffer" variant.
279 
280  template<typename T>
281  [[nodiscard]] std::string quote(T const &t, sl loc = sl::current()) const
282  {
283  return conn().quote(t, loc);
284  }
285 
287 
288  template<binary DATA>
289  [[nodiscard]] std::string
290  quote_raw(DATA const &data, sl loc = sl::current()) const
291  {
292  return conn().quote_raw(data, loc);
293  }
294 
295  // TODO: "Into buffer" variant.
297  [[nodiscard]] std::string quote_name(std::string_view identifier) const
298  {
299  return conn().quote_name(identifier);
300  }
301 
302  // TODO: "Into buffer" variant.
304  [[nodiscard]] std::string esc_like(
305  std::string_view bin, char escape_char = '\\',
306  sl loc = sl::current()) const
307  {
308  return conn().esc_like(bin, escape_char, loc);
309  }
311 
347 
349 
353  [[deprecated("The desc parameter is going away.")]]
354  result
355  exec(std::string_view query, std::string_view desc, sl = sl::current());
356 
357  // TODO: Wrap PQdescribePrepared().
358 
359  result
360  exec(std::string_view query, params const &parms, sl loc = sl::current())
361  {
362  return internal_exec_params(query, parms.make_c_params(loc), loc);
363  }
364 
366 
370  result exec(std::string_view query, sl = sl::current());
371 
373 
378  [[deprecated(
379  "Pass your query as a std::string_view, not stringstream.")]] result
380  exec(std::stringstream const &query, std::string_view desc)
381  {
383  return exec(query.str(), desc);
385  }
386 
388 
393  [[deprecated("Use exec(string_view) and call no_rows() on the result.")]]
394  result exec0(std::string_view query, std::string_view desc)
395  {
397  return exec(query, desc).no_rows();
399  }
400 
402 
407  [[deprecated("Use exec() and call no_rows() on the result.")]]
408  result exec0(std::string_view query)
409  {
410  return exec(query).no_rows();
411  }
412 
414 
420  [[deprecated("Use exec(string_view), and call one_row() on the result.")]]
421  row exec1(std::string_view query, std::string_view desc)
422  {
424  return exec(query, desc).one_row();
426  }
427 
429 
435  [[deprecated("Use exec() instead, and call one_row() on the result.")]]
436  row exec1(std::string_view query)
437  {
438  return exec(query).one_row();
439  }
440 
442 
447  [[deprecated("Use exec() instead, and call expect_rows() on the result.")]]
448  result exec_n(
449  result::size_type rows, std::string_view query, std::string_view desc);
450 
452 
457  [[deprecated("Use exec() instead, and call expect_rows() on the result.")]]
458  result exec_n(result::size_type rows, std::string_view query)
459  {
461  return exec(query, std::string_view{}).expect_rows(rows);
463  }
464 
466 
469  template<not_borrowed TYPE>
470  [[deprecated("The desc parameter is going away.")]]
471  TYPE query_value(std::string_view query, std::string_view desc)
472  {
474  return exec(query, desc).one_field().as<TYPE>();
476  }
477 
479 
485  template<not_borrowed TYPE>
486  TYPE query_value(std::string_view query, sl loc = sl::current())
487  {
488  // The result and field_ref objects are temporaries, but under C++20
489  // lifetime rules they'll live until after we convert the field to TYPE.
490  return exec(query, loc).one_field_ref(loc).as<TYPE>(loc);
491  }
492 
494 
501  template<not_borrowed... TYPE>
502  [[nodiscard]] std::tuple<TYPE...>
503  query1(std::string_view query, sl loc = sl::current())
504  {
505  // The result and row_ref objects are temporaries, but under C++20 lifetime
506  // rules they'll live until after we convert the field to TYPE.
507  return exec(query, loc)
508  .expect_columns(sizeof...(TYPE), loc)
509  .one_row_ref(loc)
510  .as<TYPE...>(loc);
511  }
512 
514 
521  template<not_borrowed... TYPE>
522  [[nodiscard]] std::optional<std::tuple<TYPE...>>
523  query01(std::string_view query, sl loc = sl::current())
524  {
525  auto const res{exec(query, loc)};
526  std::optional<row_ref> const r{res.opt_row_ref(loc)};
527  if (r)
528  return {r->as<TYPE...>(loc)};
529  else
530  return {};
531  }
532 
534 
589  template<std::move_constructible... TYPE>
590  [[nodiscard]] auto stream(std::string_view query, sl loc = sl::current()) &
591  {
592  return pqxx::internal::stream_query<TYPE...>{
593  *this, query, make_context(loc)};
594  }
595 
597 
625  template<typename CALLABLE>
626  auto
627  for_stream(std::string_view query, CALLABLE &&func, sl loc = sl::current())
628  {
629  // TODO: Can we pass loc into func if appropriate?
630  using param_types =
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);
635  }
636 
637  template<typename CALLABLE>
638  [[deprecated(
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())
641  {
642  return for_stream(query, std::forward<CALLABLE>(func), loc);
643  }
644 
646 
677  template<typename... TYPE>
678  auto query(std::string_view query, sl loc = sl::current())
679  {
680  return exec(query, loc).iter<TYPE...>();
681  }
682 
684  template<typename... TYPE>
685  [[deprecated("Use query() instead, and call expect_rows() on the result.")]]
686  auto query_n(
687  result::size_type rows, std::string_view query, sl loc = sl::current())
688  {
689  return exec(query, loc).expect_rows(rows, loc).iter<TYPE...>();
690  }
691 
693 
701  template<typename CALLABLE>
702  void
703  for_query(std::string_view query, CALLABLE &&func, sl loc = sl::current())
704  {
705  exec(query).for_each(std::forward<CALLABLE>(func), loc);
706  }
707 
737 
739 
743  template<typename... Args>
744  [[deprecated("Use exec(std::string_view, params) instead.")]]
745  result exec_params(std::string_view query, Args &&...args)
746  {
747  return exec(query, params{*this, args...}, sl::current());
748  }
749 
750  // Execute parameterised statement, expect a single-row result.
753  template<typename... Args>
754  [[deprecated("Use exec() instead, and call one_row() on the result.")]]
755  row exec_params1(std::string_view query, Args &&...args)
756  {
757  return exec(query, params{*this, args...}).one_row();
758  }
759 
760  // Execute parameterised statement, expect a result with zero rows.
763  template<typename... Args>
764  [[deprecated(
765  "Use exec(string_view, params) and call no_rows() on the result.")]]
766  result exec_params0(std::string_view query, Args &&...args)
767  {
768  return exec(query, params{*this, args...}).no_rows();
769  }
770 
771  // Execute parameterised statement, expect exactly a given number of rows.
774  template<typename... Args>
775  [[deprecated("Use exec(), and call expect_rows() on the result.")]]
776  result
777  exec_params_n(std::size_t rows, std::string_view query, Args &&...args)
778  {
779  sl const loc{m_created_loc};
780  return exec(query, params{*this, args...}, loc)
781  .expect_rows(
782  check_cast<result_size_type>(rows, "number of rows", loc), loc);
783  }
784 
785  // Execute parameterised statement, expect exactly a given number of rows.
788  template<typename... Args>
789  [[deprecated("Use exec(), and call expect_rows() on the result.")]]
790  result
791  exec_params_n(result::size_type rows, std::string_view query, Args &&...args)
792  {
793  return exec(query, params{*this, args...}).expect_rows(rows);
794  }
795 
797 
830  template<typename... TYPE>
831  auto
832  query(std::string_view query, params const &parms, sl loc = sl::current())
833  {
834  return exec(query, parms, loc).iter<TYPE...>();
835  }
836 
839 
847  template<typename... TYPE>
848  [[deprecated("Use exec(), and call expect_rows() & iter() on the result.")]]
849  auto
850  query_n(result::size_type rows, std::string_view query, params const &parms)
851  {
852  return exec(query, parms).expect_rows(rows).iter<TYPE...>();
853  }
854 
856 
862  template<not_borrowed TYPE>
864  std::string_view query, params const &parms, sl loc = sl::current())
865  {
866  // The result and field_ref objects are temporaries, but under C++20
867  // lifetime rules they'll live until after we convert the field to TYPE.
868  return exec(query, parms, loc)
869  .expect_columns(1, loc)
870  .one_field_ref(loc)
871  .as<TYPE>(loc);
872  }
873 
875 
882  template<not_borrowed... TYPE>
883  [[nodiscard]]
884  std::tuple<TYPE...>
885  query1(std::string_view query, params const &parms, sl loc = sl::current())
886  {
887  // The result and row_ref objects are temporaries, but under C++20 lifetime
888  // rules they'll live until after we convert the field to TYPE.
889  return exec(query, parms, loc).one_row_ref(loc).as<TYPE...>(loc);
890  }
891 
893 
900  template<not_borrowed... TYPE>
901  [[nodiscard]] std::optional<std::tuple<TYPE...>>
902  query01(std::string_view query, params const &parms, sl loc = sl::current())
903  {
904  auto const res{exec(query, parms, loc)};
905  std::optional<row_ref> r{res.opt_row_ref(loc)};
906  if (r)
907  return {r->as<TYPE...>(loc)};
908  else
909  return {};
910  }
911 
913 
924  template<typename CALLABLE>
925  void for_query(
926  std::string_view query, CALLABLE &&func, params const &parms,
927  sl loc = sl::current())
928  {
929  exec(query, parms, loc).for_each(std::forward<CALLABLE>(func), loc);
930  }
931 
933 
948  void notify(
949  std::string_view channel, std::string_view payload, sl = sl::current());
950 
952 
963  void notify(std::string_view channel, sl loc = sl::current())
964  {
965  notify(channel, {}, loc);
966  }
968 
970  template<typename... Args>
971  [[deprecated("Use exec(prepped, params) instead.")]]
972  result exec_prepared(zview statement, Args &&...args)
973  {
974  return exec(prepped{statement}, params{*this, args...});
975  }
976 
978  result exec(prepped statement, sl loc = sl::current())
979  {
980  params const pp;
981  return internal_exec_prepared(statement, pp.make_c_params(loc), loc);
982  }
983 
985 
990  template<typename... TYPE>
991  auto query(prepped statement, params const &parms, sl loc = sl::current())
992  {
993  return exec(statement, parms, loc).iter<TYPE...>();
994  }
995 
997 
1002  template<typename... TYPE>
1003  auto query(prepped statement, sl loc = sl::current())
1004  {
1005  return exec(statement, {}, loc).iter<TYPE...>();
1006  }
1007 
1009 
1012  template<not_borrowed TYPE>
1013  TYPE
1014  query_value(prepped statement, params const &parms, sl loc = sl::current())
1015  {
1016  // The result and field_ref objects are temporaries, but under C++20
1017  // lifetime rules they'll live until after we convert the field to TYPE.
1018  return exec(statement, parms, loc)
1019  .expect_columns(1, loc)
1020  .one_field_ref(loc)
1021  .as<TYPE>(loc);
1022  }
1023 
1025 
1028  template<not_borrowed TYPE>
1029  TYPE query_value(prepped statement, sl loc = sl::current())
1030  {
1031  return exec(statement, {}, loc)
1032  .expect_columns(1, loc)
1033  .one_field_ref(loc)
1034  .as<TYPE>(loc);
1035  }
1036 
1038 
1041  template<typename CALLABLE>
1043  prepped statement, CALLABLE &&func, params const &parms,
1044  sl loc = sl::current())
1045  {
1046  exec(statement, parms, loc).for_each(std::forward<CALLABLE>(func), loc);
1047  }
1048 
1050 
1053  template<typename CALLABLE>
1054  void for_query(prepped statement, CALLABLE &&func, sl loc = sl::current())
1055  {
1056  exec(statement, {}, loc).for_each(std::forward<CALLABLE>(func), loc);
1057  }
1058 
1060  result exec(prepped statement, params const &parms, sl loc = sl::current())
1061  {
1062  return internal_exec_prepared(statement, parms.make_c_params(loc), loc);
1063  }
1064 
1066 
1068  template<typename... Args>
1069  [[deprecated(
1070  "Use exec(string_view, params) and call one_row() on the result.")]]
1071  row exec_prepared1(zview statement, Args &&...args)
1072  {
1073  sl const loc{m_created_loc};
1074  return exec(prepped{statement}, params{*this, args...}).one_row(loc);
1075  }
1076 
1078 
1080  template<typename... Args>
1081  [[deprecated(
1082  "Use exec(prepped, params), and call no_rows() on the result.")]]
1083  result exec_prepared0(zview statement, Args &&...args)
1084  {
1085  sl const loc{m_created_loc};
1086  return exec(prepped{statement}, params{*this, args...}).no_rows(loc);
1087  }
1088 
1090 
1093  template<typename... Args>
1094  [[deprecated(
1095  "Use exec(prepped, params), and call expect_rows() on the result.")]]
1096  result
1097  exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
1098  {
1099  sl const loc{m_created_loc};
1100  return exec(pqxx::prepped{statement}, params{*this, args...})
1101  .expect_rows(rows, loc);
1102  }
1103 
1109  PQXX_ZARGS void process_notice(char const msg[]) const
1110  {
1111  m_conn.process_notice(msg);
1112  }
1114  void process_notice(zview msg) const { m_conn.process_notice(msg); }
1116 
1118  [[nodiscard]] constexpr connection &conn() const noexcept { return m_conn; }
1119 
1121 
1136  [[deprecated("Set transaction-local variables using SQL SET statements.")]]
1137  void set_variable(std::string_view var, std::string_view value);
1138 
1140 
1143  [[deprecated("Read variables using SQL SHOW statements.")]]
1144  std::string get_variable(std::string_view);
1145 
1147  [[nodiscard]] constexpr std::string_view name() const & noexcept
1148  {
1149  return m_name;
1150  }
1151 
1152 protected:
1154 
1158  connection &, std::string_view, std::shared_ptr<std::string> rollback_cmd,
1159  sl loc = sl::current());
1160 
1162 
1168  connection &cx, std::string_view tname, sl loc = sl::current());
1169 
1171  explicit transaction_base(connection &cx, sl loc = sl::current());
1172 
1174  void register_transaction();
1175 
1177  void close(sl = sl::current()) noexcept;
1178 
1180  virtual void do_commit(sl) = 0;
1181 
1183 
1186  virtual void do_abort(sl);
1187 
1189  void set_rollback_cmd(std::shared_ptr<std::string> cmd)
1190  {
1191  m_rollback_cmd = std::move(cmd);
1192  }
1193 
1195  result direct_exec(std::string_view, std::string_view desc, sl);
1196  result direct_exec(std::string_view query, sl loc)
1197  {
1198  return direct_exec(query, "", loc);
1199  }
1200  result direct_exec(std::shared_ptr<std::string>, std::string_view desc, sl);
1201  result direct_exec(std::shared_ptr<std::string> query, sl loc)
1202  {
1203  return direct_exec(std::move(query), "", loc);
1204  }
1205 
1206  // TODO: Can this be noexcept?
1208  [[nodiscard]] sl created_loc() const { return m_created_loc; }
1209 
1210 private:
1211  enum class status
1212  {
1213  active,
1214  aborted,
1215  committed,
1216  in_doubt
1217  };
1218 
1220 
1223  [[nodiscard]] conversion_context make_context(sl) const;
1224 
1225  PQXX_PRIVATE void check_pending_error();
1226 
1227  result internal_exec_prepared(
1228  std::string_view statement, internal::c_params const &args, sl);
1229 
1230  result internal_exec_params(
1231  std::string_view query, internal::c_params const &args, sl);
1232 
1234  [[nodiscard]] std::string description() const;
1235 
1237  PQXX_PRIVATE void register_focus(transaction_focus *);
1238  PQXX_PRIVATE void unregister_focus(transaction_focus *) noexcept;
1239  PQXX_PRIVATE void register_pending_error(zview, sl) noexcept;
1240  PQXX_PRIVATE void register_pending_error(std::string &&, sl) noexcept;
1241 
1243  template<typename... ARGS>
1244  auto stream_like(
1245  std::string_view query, std::tuple<ARGS...> const *,
1246  sl loc = sl::current())
1247  {
1248  return stream<ARGS...>(query, loc);
1249  }
1250 
1251  connection &m_conn;
1252 
1254 
1257  transaction_focus const *m_focus = nullptr;
1258 
1259  std::string m_name;
1260  std::string m_pending_error;
1261 
1263  std::shared_ptr<std::string> m_rollback_cmd;
1264 
1265  status m_status = status::active;
1266 
1267  bool m_registered = false;
1268 
1270  sl m_created_loc;
1271 
1272  static constexpr std::string_view s_type_name{"transaction"sv};
1273 };
1274 } // namespace pqxx
1275 
1276 
1277 namespace pqxx::internal
1278 {
1280 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
1281 extern const zview begin_cmd;
1282 
1283 // These are not static members, so "constexpr" does not imply "inline".
1284 template<>
1285 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
1286  "BEGIN"_zv};
1287 template<>
1288 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
1289  "BEGIN READ ONLY"_zv};
1290 template<>
1291 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
1292  "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
1293 template<>
1294 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
1295  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
1296 template<>
1297 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
1298  "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
1299 template<>
1300 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
1301  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
1302 } // namespace pqxx::internal
1303 
1305 #endif
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
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
#define PQXX_ZARGS
Definition: header-pre.hxx:136
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
#define PQXX_NOVTABLE
Definition: header-pre.hxx:221
#define PQXX_PRIVATE
Definition: header-pre.hxx:207
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