libpqxx  7.3.1
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_transactionfocus;
45 } // namespace pqxx::internal::gate
46 
47 
48 namespace pqxx
49 {
50 using namespace std::literals;
51 
52 
65 
71 class PQXX_LIBEXPORT PQXX_NOVTABLE transaction_base
72 {
73 public:
74  transaction_base() = delete;
75  transaction_base(transaction_base const &) = delete;
77  transaction_base &operator=(transaction_base const &) = delete;
78  transaction_base &operator=(transaction_base &&) = delete;
79 
80  virtual ~transaction_base() = 0;
81 
83 
95  void commit();
96 
98 
101  void abort();
102 
107  [[nodiscard]] std::string esc(char const text[]) const
109  {
110  return conn().esc(text);
111  }
113  [[nodiscard]] std::string esc(char const text[], std::size_t maxlen) const
114  {
115  return conn().esc(text, maxlen);
116  }
118  [[nodiscard]] std::string esc(std::string_view text) const
119  {
120  return conn().esc(text);
121  }
122 
124 
135  [[nodiscard]] std::string
136  esc_raw(unsigned char const data[], std::size_t len) const
137  {
138 #include "pqxx/internal/ignore-deprecated-pre.hxx"
139  return conn().esc_raw(data, len);
140 #include "pqxx/internal/ignore-deprecated-post.hxx"
141  }
143  [[nodiscard]] std::string esc_raw(zview) const;
144 
146 
149  [[nodiscard]] std::string unesc_raw(zview text) const
150  {
151  return conn().unesc_raw(text);
152  }
153 
155 
158  [[nodiscard]] std::string unesc_raw(char const *text) const
159  {
160  return conn().unesc_raw(text);
161  }
162 
164 
165  template<typename T> [[nodiscard]] std::string quote(T const &t) const
166  {
167 #include "pqxx/internal/ignore-deprecated-pre.hxx"
168  return conn().quote(t);
169 #include "pqxx/internal/ignore-deprecated-post.hxx"
170  }
171 
173  [[nodiscard]] std::string
174  quote_raw(unsigned char const bin[], std::size_t len) const
175  {
176 #include "pqxx/internal/ignore-deprecated-pre.hxx"
177  return conn().quote_raw(bin, len);
178 #include "pqxx/internal/ignore-deprecated-post.hxx"
179  }
180 
182  [[nodiscard]] std::string quote_raw(zview bin) const;
183 
185  [[nodiscard]] std::string quote_name(std::string_view identifier) const
186  {
187  return conn().quote_name(identifier);
188  }
189 
191  [[nodiscard]] std::string
192  esc_like(std::string_view bin, char escape_char = '\\') const
193  {
194  return conn().esc_like(bin, escape_char);
195  }
197 
217 
219 
224  result
225  exec(std::string_view query, std::string_view desc = std::string_view{});
226 
228 
234  std::stringstream const &query, std::string_view desc = std::string_view{})
235  {
236  return exec(query.str(), desc);
237  }
238 
240 
245  result exec0(zview query, std::string_view desc = std::string_view{})
246  {
247  return exec_n(0, query, desc);
248  }
249 
251 
257  row exec1(zview query, std::string_view desc = std::string_view{})
258  {
259  return exec_n(1, query, desc).front();
260  }
261 
263 
268  result exec_n(
269  result::size_type rows, zview query,
270  std::string_view desc = std::string_view{});
271 
273 
276  template<typename TYPE>
277  TYPE query_value(zview query, std::string_view desc = std::string_view{})
278  {
279  row const r{exec1(query, desc)};
280  if (std::size(r) != 1)
281  throw usage_error{internal::concat(
282  "Queried single value from result with ", std::size(r), " columns.")};
283  return r[0].as<TYPE>();
284  }
285 
287 
327  template<typename... TYPE> [[nodiscard]] auto stream(std::string_view query)
328  {
329  return pqxx::internal::owning_stream_input_iteration<TYPE...>{
330  std::make_unique<pqxx::stream_from>(*this, from_query_t{}, query)};
331  }
332 
362  template<typename... Args> result exec_params(zview query, Args &&...args)
364  {
365  return internal_exec_params(
366  query, internal::params(std::forward<Args>(args)...));
367  }
368 
369  // Execute parameterised statement, expect a single-row result.
372  template<typename... Args> row exec_params1(zview query, Args &&...args)
373  {
374  return exec_params_n(1, query, std::forward<Args>(args)...).front();
375  }
376 
377  // Execute parameterised statement, expect a result with zero rows.
380  template<typename... Args> result exec_params0(zview query, Args &&...args)
381  {
382  return exec_params_n(0, query, std::forward<Args>(args)...);
383  }
384 
385  // Execute parameterised statement, expect exactly a given number of rows.
388  template<typename... Args>
389  result exec_params_n(std::size_t rows, zview query, Args &&...args)
390  {
391  auto const r{exec_params(query, std::forward<Args>(args)...)};
392  check_rowcount_params(rows, std::size(r));
393  return r;
394  }
396 
425 
427  template<typename... Args>
428  result exec_prepared(zview statement, Args &&...args)
429  {
430  return internal_exec_prepared(
431  statement, internal::params(std::forward<Args>(args)...));
432  }
433 
435 
437  template<typename... Args>
438  row exec_prepared1(zview statement, Args &&...args)
439  {
440  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
441  }
442 
444 
446  template<typename... Args>
447  result exec_prepared0(zview statement, Args &&...args)
448  {
449  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
450  }
451 
453 
456  template<typename... Args>
457  result
458  exec_prepared_n(result::size_type rows, zview statement, Args &&...args)
459  {
460  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
461  check_rowcount_prepared(statement, rows, std::size(r));
462  return r;
463  }
464 
466 
471  void process_notice(char const msg[]) const { m_conn.process_notice(msg); }
474  void process_notice(zview msg) const { m_conn.process_notice(msg); }
476 
478  [[nodiscard]] connection &conn() const { return m_conn; }
479 
481 
491  void set_variable(std::string_view var, std::string_view value);
492 
494 
497  std::string get_variable(std::string_view);
498 
500  [[nodiscard]] std::string_view name() const noexcept { return m_name; }
501 
502 protected:
504 
508  connection &c, std::string_view tname,
509  std::shared_ptr<std::string> rollback_cmd) :
510  m_conn{c}, m_name{tname}, m_rollback_cmd{rollback_cmd}
511  {}
512 
514 
519  transaction_base(connection &c, std::string_view tname);
520 
522  explicit transaction_base(connection &c);
523 
525  void register_transaction();
526 
528  void close() noexcept;
529 
531  virtual void do_commit() = 0;
532 
534 
537  virtual void do_abort();
538 
540  void set_rollback_cmd(std::shared_ptr<std::string> cmd)
541  {
542  m_rollback_cmd = cmd;
543  }
544 
546  result direct_exec(std::string_view, std::string_view desc = ""sv);
547  result
548  direct_exec(std::shared_ptr<std::string>, std::string_view desc = ""sv);
549 
550 private:
551  enum class status
552  {
553  active,
554  aborted,
555  committed,
556  in_doubt
557  };
558 
559  PQXX_PRIVATE void check_pending_error();
560 
561  template<typename T> bool parm_is_null(T *p) const noexcept
562  {
563  return p == nullptr;
564  }
565  template<typename T> bool parm_is_null(T) const noexcept { return false; }
566 
567  result internal_exec_prepared(zview statement, internal::params const &args);
568 
569  result internal_exec_params(zview query, internal::params const &args);
570 
572  void check_rowcount_prepared(
573  zview statement, result::size_type expected_rows,
574  result::size_type actual_rows);
575 
577  void
578  check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
579 
581  [[nodiscard]] std::string description() const;
582 
583  friend class pqxx::internal::gate::transaction_transactionfocus;
584  PQXX_PRIVATE void register_focus(internal::transactionfocus *);
585  PQXX_PRIVATE void unregister_focus(internal::transactionfocus *) noexcept;
586  PQXX_PRIVATE void register_pending_error(zview) noexcept;
587  PQXX_PRIVATE void register_pending_error(std::string &&) noexcept;
588 
589  connection &m_conn;
590 
592 
595  internal::transactionfocus const *m_focus = nullptr;
596 
597  status m_status = status::active;
598  bool m_registered = false;
599  std::string m_name;
600  std::string m_pending_error;
601 
603  std::shared_ptr<std::string> m_rollback_cmd;
604 
605  constexpr static std::string_view s_type_name{"transaction"sv};
606 };
607 
608 
609 // TODO: C++20 "Conversion remains valid after underlying result dies" concept?
611 template<>
612 std::string_view transaction_base::query_value<std::string_view>(
613  zview query, std::string_view desc) = delete;
615 template<>
616 zview transaction_base::query_value<zview>(
617  zview query, std::string_view desc) = delete;
618 
619 } // namespace pqxx
620 
621 
622 namespace pqxx::internal
623 {
625 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
626 extern const zview begin_cmd;
627 
628 // These are not static members, so "constexpr" does not imply "inline".
629 template<>
630 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
631  "BEGIN"_zv};
632 template<>
633 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
634  "BEGIN READ ONLY"_zv};
635 template<>
636 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
637  "BEGIN ISOLATION LEVEL REPEATABLE READ"_zv};
638 template<>
639 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
640  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"_zv};
641 template<>
642 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
643  "BEGIN ISOLATION LEVEL SERIALIZABLE"_zv};
644 template<>
645 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
646  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"_zv};
647 } // namespace pqxx::internal
648 
649 #include "pqxx/internal/compiler-internal-post.hxx"
650 #endif
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:71
reference front() const noexcept
Definition: row.cxx:54
void set_rollback_cmd(std::shared_ptr< std::string > cmd)
Set the rollback command.
Definition: transaction_base.hxx:540
auto stream(std::string_view query)
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:327
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:277
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
result_size_type size_type
Definition: result.hxx:73
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:158
result exec(std::stringstream const &query, std::string_view desc=std::string_view{})
Execute a command.
Definition: transaction_base.hxx:233
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:507
result exec_params_n(std::size_t rows, zview query, Args &&...args)
Definition: transaction_base.hxx:389
row exec_params1(zview query, Args &&...args)
Definition: transaction_base.hxx:372
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:59
result exec0(zview query, std::string_view desc=std::string_view{})
Execute command, which should return zero rows of data.
Definition: transaction_base.hxx:245
STL namespace.
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:174
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
std::string_view name() const noexcept
Transaction name, if you passed one to the constructor; or empty string.
Definition: transaction_base.hxx:500
result exec_prepared0(zview statement, Args &&...args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:447
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 table field or notification payload.
Definition: transaction_base.hxx:149
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:478
Reference to one row in a result.
Definition: row.hxx:45
result exec_params0(zview query, Args &&...args)
Definition: transaction_base.hxx:380
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:118
Result set containing data returned by a query or command.
Definition: result.hxx:70
Definition: connection.hxx:94
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:136
result exec_prepared(zview statement, Args &&...args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:428
const zview begin_cmd
The SQL command for starting a given type of transaction.
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:458
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
Connection to a database.
Definition: connection.hxx:169
row exec1(zview query, std::string_view desc=std::string_view{})
Execute command returning a single row of data.
Definition: transaction_base.hxx:257
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:185
void process_notice(zview msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:474
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:165
row exec_prepared1(zview statement, Args &&...args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:438
std::string esc_like(std::string_view bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:192
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:113