libpqxx  7.5.0
transaction_base.hxx
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-2021, 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_H_TRANSACTION_BASE
15 #define PQXX_H_TRANSACTION_BASE
16 
17 #include "pqxx/compiler-public.hxx"
18 #include "pqxx/internal/compiler-internal-pre.hxx"
19 
20 #include <string_view>
21 
22 /* End-user programs need not include this file, unless they define their own
23  * transaction classes. This is not something the typical program should want
24  * to do.
25  *
26  * However, reading this file is worthwhile because it defines the public
27  * interface for the available transaction classes such as transaction and
28  * nontransaction.
29  */
30 
31 #include "pqxx/connection.hxx"
32 #include "pqxx/internal/concat.hxx"
33 #include "pqxx/internal/encoding_group.hxx"
34 #include "pqxx/isolation.hxx"
35 #include "pqxx/result.hxx"
36 #include "pqxx/row.hxx"
37 #include "pqxx/stream_from.hxx"
38 
39 namespace pqxx::internal::gate
40 {
41 class transaction_subtransaction;
42 class transaction_sql_cursor;
43 class transaction_stream_to;
44 class transaction_transaction_focus;
45 } // namespace pqxx::internal::gate
46 
47 
48 namespace pqxx
49 {
50 using namespace std::literals;
51 
52 
53 class transaction_focus;
54 
55 
68 
74 class PQXX_LIBEXPORT PQXX_NOVTABLE transaction_base
75 {
76 public:
77  transaction_base() = delete;
78  transaction_base(transaction_base const &) = delete;
80  transaction_base &operator=(transaction_base const &) = delete;
81  transaction_base &operator=(transaction_base &&) = delete;
82 
83  virtual ~transaction_base() = 0;
84 
86 
98  void commit();
99 
101 
104  void abort();
105 
110  [[nodiscard]] std::string esc(char const text[]) const
112  {
113  return conn().esc(text);
114  }
116  [[nodiscard]] std::string esc(char const text[], std::size_t maxlen) const
117  {
118  return conn().esc(text, maxlen);
119  }
121  [[nodiscard]] std::string esc(std::string_view text) const
122  {
123  return conn().esc(text);
124  }
125 
127 
138  [[nodiscard]] std::string
139  esc_raw(unsigned char const data[], std::size_t len) const
140  {
141 #include "pqxx/internal/ignore-deprecated-pre.hxx"
142  return conn().esc_raw(data, len);
143 #include "pqxx/internal/ignore-deprecated-post.hxx"
144  }
146  [[nodiscard]] std::string esc_raw(zview) const;
147 
149 
152  [[nodiscard]] std::string unesc_raw(zview text) const
153  {
154  return conn().unesc_raw(text);
155  }
156 
158 
161  [[nodiscard]] std::string unesc_raw(char const *text) const
162  {
163  return conn().unesc_raw(text);
164  }
165 
167 
168  template<typename T> [[nodiscard]] std::string quote(T const &t) const
169  {
170 #include "pqxx/internal/ignore-deprecated-pre.hxx"
171  return conn().quote(t);
172 #include "pqxx/internal/ignore-deprecated-post.hxx"
173  }
174 
176  [[nodiscard]] std::string
177  quote_raw(unsigned char const bin[], std::size_t len) const
178  {
179 #include "pqxx/internal/ignore-deprecated-pre.hxx"
180  return conn().quote_raw(bin, len);
181 #include "pqxx/internal/ignore-deprecated-post.hxx"
182  }
183 
185  [[nodiscard]] std::string quote_raw(zview bin) const;
186 
188  [[nodiscard]] std::string quote_name(std::string_view identifier) const
189  {
190  return conn().quote_name(identifier);
191  }
192 
194  [[nodiscard]] std::string
195  esc_like(std::string_view bin, char escape_char = '\\') const
196  {
197  return conn().esc_like(bin, escape_char);
198  }
200 
220 
222 
227  result
228  exec(std::string_view query, std::string_view desc = std::string_view{});
229 
231 
237  std::stringstream const &query, std::string_view desc = std::string_view{})
238  {
239  return exec(query.str(), desc);
240  }
241 
243 
248  result exec0(zview query, std::string_view desc = std::string_view{})
249  {
250  return exec_n(0, query, desc);
251  }
252 
254 
260  row exec1(zview query, std::string_view desc = std::string_view{})
261  {
262  return exec_n(1, query, desc).front();
263  }
264 
266 
271  result exec_n(
272  result::size_type rows, zview query,
273  std::string_view desc = std::string_view{});
274 
276 
279  template<typename TYPE>
280  TYPE query_value(zview query, std::string_view desc = std::string_view{})
281  {
282  row const r{exec1(query, desc)};
283  if (std::size(r) != 1)
284  throw usage_error{internal::concat(
285  "Queried single value from result with ", std::size(r), " columns.")};
286  return r[0].as<TYPE>();
287  }
288 
290 
338  template<typename... TYPE> [[nodiscard]] auto stream(std::string_view query)
339  {
340  return pqxx::internal::owning_stream_input_iteration<TYPE...>{
341  std::make_unique<pqxx::stream_from>(*this, from_query_t{}, query)};
342  }
343 
373  template<typename... Args> result exec_params(zview query, Args &&...args)
375  {
376  params pp(args...);
377  return internal_exec_params(query, pp.make_c_params());
378  }
379 
380  // Execute parameterised statement, expect a single-row result.
383  template<typename... Args> row exec_params1(zview query, Args &&...args)
384  {
385  return exec_params_n(1, query, std::forward<Args>(args)...).front();
386  }
387 
388  // Execute parameterised statement, expect a result with zero rows.
391  template<typename... Args> result exec_params0(zview query, Args &&...args)
392  {
393  return exec_params_n(0, query, std::forward<Args>(args)...);
394  }
395 
396  // Execute parameterised statement, expect exactly a given number of rows.
399  template<typename... Args>
400  result exec_params_n(std::size_t rows, zview query, Args &&...args)
401  {
402  auto const r{exec_params(query, std::forward<Args>(args)...)};
403  check_rowcount_params(rows, std::size(r));
404  return r;
405  }
407 
436 
438  template<typename... Args>
439  result exec_prepared(zview statement, Args &&...args)
440  {
441  params pp(args...);
442  return internal_exec_prepared(statement, pp.make_c_params());
443  }
444 
446 
448  template<typename... Args>
449  row exec_prepared1(zview statement, Args &&...args)
450  {
451  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
452  }
453 
455 
457  template<typename... Args>
458  result exec_prepared0(zview statement, Args &&...args)
459  {
460  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
461  }
462 
464 
467  template<typename... Args>
468  result
469  exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
470  {
471  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
472  check_rowcount_prepared(statement, rows, std::size(r));
473  return r;
474  }
475 
477 
482  void process_notice(char const msg[]) const { m_conn.process_notice(msg); }
485  void process_notice(zview msg) const { m_conn.process_notice(msg); }
487 
489  [[nodiscard]] connection &conn() const { return m_conn; }
490 
492 
502  void set_variable(std::string_view var, std::string_view value);
503 
505 
508  std::string get_variable(std::string_view);
509 
511  [[nodiscard]] std::string_view name() const noexcept { return m_name; }
512 
513 protected:
515 
519  connection &c, std::string_view tname,
520  std::shared_ptr<std::string> rollback_cmd) :
521  m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
522  {}
523 
525 
530  transaction_base(connection &c, std::string_view tname);
531 
533  explicit transaction_base(connection &c);
534 
536  void register_transaction();
537 
539  void close() noexcept;
540 
542  virtual void do_commit() = 0;
543 
545 
548  virtual void do_abort();
549 
551  void set_rollback_cmd(std::shared_ptr<std::string> cmd)
552  {
553  m_rollback_cmd = cmd;
554  }
555 
557  result direct_exec(std::string_view, std::string_view desc = ""sv);
558  result
559  direct_exec(std::shared_ptr<std::string>, std::string_view desc = ""sv);
560 
561 private:
562  enum class status
563  {
564  active,
565  aborted,
566  committed,
567  in_doubt
568  };
569 
570  PQXX_PRIVATE void check_pending_error();
571 
572  template<typename T> bool parm_is_null(T *p) const noexcept
573  {
574  return p == nullptr;
575  }
576  template<typename T> bool parm_is_null(T) const noexcept { return false; }
577 
578  result
579  internal_exec_prepared(zview statement, internal::c_params const &args);
580 
581  result internal_exec_params(zview query, internal::c_params const &args);
582 
584  void check_rowcount_prepared(
585  zview statement, result::size_type expected_rows,
586  result::size_type actual_rows);
587 
589  void
590  check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
591 
593  [[nodiscard]] std::string description() const;
594 
595  friend class pqxx::internal::gate::transaction_transaction_focus;
596  PQXX_PRIVATE void register_focus(transaction_focus *);
597  PQXX_PRIVATE void unregister_focus(transaction_focus *) noexcept;
598  PQXX_PRIVATE void register_pending_error(zview) noexcept;
599  PQXX_PRIVATE void register_pending_error(std::string &&) noexcept;
600 
601  connection &m_conn;
602 
604 
607  transaction_focus const *m_focus = nullptr;
608 
609  status m_status = status::active;
610  bool m_registered = false;
611  std::string m_name;
612  std::string m_pending_error;
613 
615  std::shared_ptr<std::string> m_rollback_cmd;
616 
617  constexpr static std::string_view s_type_name{"transaction"sv};
618 };
619 
620 
621 // TODO: C++20 "Conversion remains valid after underlying result dies" concept?
623 template<>
624 std::string_view transaction_base::query_value<std::string_view>(
625  zview query, std::string_view desc) = delete;
627 template<>
628 zview transaction_base::query_value<zview>(
629  zview query, std::string_view desc) = delete;
630 
631 } // namespace pqxx
632 
633 
634 namespace pqxx::internal
635 {
637 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
638 extern const zview begin_cmd;
639 
640 // These are not static members, so "constexpr" does not imply "inline".
641 template<>
642 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
643  "BEGIN"_zv};
644 template<>
645 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
646  "BEGIN READ ONLY"_zv};
647 template<>
648 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
649  "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
650 template<>
651 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
652  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
653 template<>
654 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
655  "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
656 template<>
657 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
658  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
659 } // namespace pqxx::internal
660 
661 #include "pqxx/internal/compiler-internal-post.hxx"
662 #endif
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:177
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:383
pqxx::internal::c_params make_c_params() const
For internal use: Generate a params object for use in calls.
Definition: params.cxx:93
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:161
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:469
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:188
std::string_view name() const noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:511
Base class for things that monopolise a transaction&#39;s attention.
Definition: transaction_focus.hxx:27
std::string esc(char const text[], std::size_t maxlen) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:116
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:391
reference front() const noexcept
Definition: row.cxx:54
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
Result set containing data returned by a query or command.
Definition: result.hxx:70
Definition: connection.hxx:97
const zview begin_cmd
The SQL command for starting a given type of transaction.
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:63
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
std::string esc_raw(unsigned char const data[], std::size_t len) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:139
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:439
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:518
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:485
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:400
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:168
Reference to one row in a result.
Definition: row.hxx:45
auto stream(std::string_view query)
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:338
row exec1(zview query, std::string_view desc=std::string_view{})
Execute command returning a single row of data.
Definition: transaction_base.hxx:260
STL namespace.
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:489
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:449
result_size_type size_type
Definition: result.hxx:73
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:195
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:74
void set_rollback_cmd(std::shared_ptr< std::string > cmd)
Set the rollback command.
Definition: transaction_base.hxx:551
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:458
TYPE query_value(zview query, std::string_view desc=std::string_view{})
Perform query, expecting exactly 1 row with 1 field, and convert it.
Definition: transaction_base.hxx:280
Connection to a database.
Definition: connection.hxx:183
Build a parameter list for a parameterised or prepared statement.
Definition: prepared_statement.hxx:121
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
result exec0(zview query, std::string_view desc=std::string_view{})
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:248
std::string unesc_raw(zview text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:152
result exec(std::stringstream const &query, std::string_view desc=std::string_view{})
Execute a command.
Definition: transaction_base.hxx:236
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:121