libpqxx  7.0.2
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-2020, 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/encoding_group.hxx"
33 #include "pqxx/isolation.hxx"
34 #include "pqxx/result.hxx"
35 #include "pqxx/row.hxx"
36 
37 
38 namespace pqxx::internal
39 {
40 class sql_cursor;
41 
42 class PQXX_LIBEXPORT transactionfocus : public virtual namedclass
43 {
44 public:
46  namedclass{"transactionfocus"},
47  m_trans{t}
48  {}
49 
50  transactionfocus() = delete;
51  transactionfocus(transactionfocus const &) = delete;
52  transactionfocus &operator=(transactionfocus const &) = delete;
53 
54 protected:
55  void register_me();
56  void unregister_me() noexcept;
57  void reg_pending_error(std::string const &) noexcept;
58  bool registered() const noexcept { return m_registered; }
59 
61 
62 private:
63  bool m_registered = false;
64 };
65 } // namespace pqxx::internal
66 
67 
68 namespace pqxx::internal::gate
69 {
70 class transaction_subtransaction;
71 class transaction_sql_cursor;
72 class transaction_stream_to;
73 class transaction_transactionfocus;
74 } // namespace pqxx::internal::gate
75 
76 
77 namespace pqxx
78 {
91 
97 class PQXX_LIBEXPORT PQXX_NOVTABLE transaction_base
98  : public virtual internal::namedclass
99 {
100 public:
101  transaction_base() = delete;
102  transaction_base(transaction_base const &) = delete;
103  transaction_base &operator=(transaction_base const &) = delete;
104 
105  virtual ~transaction_base() = 0;
106 
108 
120  void commit();
121 
123 
126  void abort();
127 
132  [[nodiscard]] std::string esc(char const text[]) const
134  {
135  return conn().esc(text);
136  }
138  [[nodiscard]] std::string esc(char const text[], size_t maxlen) const
139  {
140  return conn().esc(text, maxlen);
141  }
143  [[nodiscard]] std::string esc(std::string const &text) const
144  {
145  return conn().esc(text);
146  }
147 
149 
160  [[nodiscard]] std::string
161  esc_raw(unsigned char const data[], size_t len) const
162  {
163  return conn().esc_raw(data, len);
164  }
166  [[nodiscard]] std::string esc_raw(std::string const &) const;
167 
169 
172  [[nodiscard]] std::string unesc_raw(std::string const &text) const
173  {
174  return conn().unesc_raw(text);
175  }
176 
178 
181  [[nodiscard]] std::string unesc_raw(char const *text) const
182  {
183  return conn().unesc_raw(text);
184  }
185 
187 
188  template<typename T>[[nodiscard]] std::string quote(T const &t) const
189  {
190  return conn().quote(t);
191  }
192 
194  [[nodiscard]] std::string
195  quote_raw(unsigned char const bin[], size_t len) const
196  {
197  return conn().quote_raw(bin, len);
198  }
199 
200  [[nodiscard]] std::string quote_raw(std::string const &bin) const;
201 
203  [[nodiscard]] std::string quote_name(std::string_view identifier) const
204  {
205  return conn().quote_name(identifier);
206  }
207 
209  [[nodiscard]] std::string
210  esc_like(std::string const &bin, char escape_char = '\\') const
211  {
212  return conn().esc_like(bin, escape_char);
213  }
215 
217 
231  result exec(std::string_view query, std::string const &desc = std::string{});
232 
233  result
234  exec(std::stringstream const &query, std::string const &desc = std::string{})
235  {
236  return exec(query.str(), desc);
237  }
238 
240 
245  result
246  exec0(std::string const &query, std::string const &desc = std::string{})
247  {
248  return exec_n(0, query, desc);
249  }
250 
252 
258  row exec1(std::string const &query, std::string const &desc = std::string{})
259  {
260  return exec_n(1, query, desc).front();
261  }
262 
264 
269  result exec_n(
270  result::size_type rows, std::string const &query,
271  std::string const &desc = std::string{});
272 
274  template<typename TYPE>
276  std::string const &query, std::string const &desc = std::string{})
277  {
278  row const r{exec1(query, desc)};
279  if (r.size() != 1)
280  throw usage_error{"Queried single value from result with " +
281  to_string(r.size()) + " columns."};
282  return r[0].as<TYPE>();
283  }
284 
314  template<typename... Args>
316  result exec_params(std::string const &query, Args &&... args)
317  {
318  return internal_exec_params(
319  query, internal::params(std::forward<Args>(args)...));
320  }
321 
322  // Execute parameterised statement, expect a single-row result.
325  template<typename... Args>
326  row exec_params1(std::string const &query, Args &&... args)
327  {
328  return exec_params_n(1, query, std::forward<Args>(args)...).front();
329  }
330 
331  // Execute parameterised statement, expect a result with zero rows.
334  template<typename... Args>
335  result exec_params0(std::string const &query, Args &&... args)
336  {
337  return exec_params_n(0, query, std::forward<Args>(args)...);
338  }
339 
340  // Execute parameterised statement, expect exactly a given number of rows.
343  template<typename... Args>
344  result exec_params_n(size_t rows, std::string const &query, Args &&... args)
345  {
346  auto const r{exec_params(query, std::forward<Args>(args)...)};
347  check_rowcount_params(rows, r.size());
348  return r;
349  }
351 
379 
381  template<typename... Args>
382  result exec_prepared(std::string const &statement, Args &&... args)
383  {
384  return internal_exec_prepared(
385  zview{statement.c_str(), statement.size()},
386  internal::params(std::forward<Args>(args)...));
387  }
388 
389  template<typename... Args>
390  result exec_prepared(zview statement, Args &&... args)
391  {
392  return internal_exec_prepared(
393  statement, internal::params(std::forward<Args>(args)...));
394  }
395 
397 
399  template<typename... Args>
400  row exec_prepared1(std::string const &statement, Args &&... args)
401  {
402  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
403  }
404 
405  template<typename... Args>
406  row exec_prepared1(zview statement, Args &&... args)
407  {
408  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
409  }
410 
412 
414  template<typename... Args>
415  result exec_prepared0(std::string const &statement, Args &&... args)
416  {
417  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
418  }
419 
420  template<typename... Args>
421  result exec_prepared0(zview statement, Args &&... args)
422  {
423  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
424  }
425 
427 
430  template<typename... Args>
432  result::size_type rows, std::string const &statement, Args &&... args)
433  {
434  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
435  check_rowcount_prepared(statement, rows, r.size());
436  return r;
437  }
438 
439  template<typename... Args>
440  result
441  exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
442  {
443  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
444  check_rowcount_prepared(statement, rows, r.size());
445  return r;
446  }
447 
449 
454  void process_notice(char const msg[]) const { m_conn.process_notice(msg); }
457  void process_notice(std::string const &msg) const
458  {
459  m_conn.process_notice(msg);
460  }
462 
464  [[nodiscard]] connection &conn() const { return m_conn; }
465 
467 
477  void set_variable(std::string_view var, std::string_view value);
478 
480 
483  std::string get_variable(std::string_view);
484 
485 protected:
487 
490  explicit transaction_base(connection &c);
491 
493  void register_transaction();
494 
496  void close() noexcept;
497 
499  virtual void do_commit() = 0;
501  virtual void do_abort() = 0;
502 
503  // For use by implementing class:
504 
506  result direct_exec(std::string_view);
507  result direct_exec(std::shared_ptr<std::string>);
508 
509 private:
510  enum class status
511  {
512  nascent,
513  active,
514  aborted,
515  committed,
516  in_doubt
517  };
518 
519  PQXX_PRIVATE void check_pending_error();
520 
521  template<typename T> bool parm_is_null(T *p) const noexcept
522  {
523  return p == nullptr;
524  }
525  template<typename T> bool parm_is_null(T) const noexcept { return false; }
526 
527  result internal_exec_prepared(zview statement, internal::params const &args);
528 
529  result
530  internal_exec_params(std::string const &query, internal::params const &args);
531 
533  void check_rowcount_prepared(
534  std::string const &statement, result::size_type expected_rows,
535  result::size_type actual_rows);
536 
538  void check_rowcount_params(size_t expected_rows, size_t actual_rows);
539 
540  friend class pqxx::internal::gate::transaction_transactionfocus;
541  PQXX_PRIVATE void register_focus(internal::transactionfocus *);
542  PQXX_PRIVATE void unregister_focus(internal::transactionfocus *) noexcept;
543  PQXX_PRIVATE void register_pending_error(std::string const &) noexcept;
544 
545  connection &m_conn;
546 
548  status m_status = status::active;
549  bool m_registered = false;
550  std::string m_pending_error;
551 };
552 } // namespace pqxx
553 
554 
555 namespace pqxx::internal
556 {
558 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
559 extern const zview begin_cmd;
560 
561 // These are not static members, so "constexpr" does not imply "inline".
562 template<>
563 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
564  "BEGIN"};
565 template<>
566 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
567  "BEGIN READ ONLY"};
568 template<>
569 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
570  "BEGIN ISOLATION LEVEL REPEATABLE READ"};
571 template<>
572 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
573  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
574 template<>
575 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
576  "BEGIN ISOLATION LEVEL SERIALIZABLE"};
577 template<>
578 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
579  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
580 } // namespace pqxx::internal
581 
582 #include "pqxx/internal/compiler-internal-post.hxx"
583 #endif
transactionfocus(transaction_base &t)
Definition: transaction_base.hxx:45
result exec_prepared(std::string const &statement, Args &&... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:382
const zview begin_cmd
The SQL command for starting a given type of transaction.
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: connection.hxx:59
row exec_params1(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:326
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:258
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:210
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:335
std::string esc(std::string const &text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:143
std::string esc_raw(unsigned char const data[], size_t len) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:161
row exec_prepared1(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:400
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:421
bool registered() const noexcept
Definition: transaction_base.hxx:58
std::string quote_raw(unsigned char const bin[], size_t len) const
Binary-escape and quote a binarystring for use as an SQL constant.
Definition: transaction_base.hxx:195
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:181
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Definition: transaction_base.hxx:441
TYPE query_value(std::string const &query, std::string const &desc=std::string{})
Execute query, expecting exactly 1 row with 1 field.
Definition: transaction_base.hxx:275
std::string esc(char const text[], size_t maxlen) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:138
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Connection to a database.
Definition: connection.hxx:135
std::string unesc_raw(std::string const &text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:172
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:41
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:252
reference front() const noexcept
Definition: row.cxx:56
result exec_params_n(size_t rows, std::string const &query, Args &&... args)
Definition: transaction_base.hxx:344
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:457
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:246
result exec_prepared0(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:415
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:316
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:97
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:203
Definition: connection.hxx:65
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:464
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:234
Result set containing data returned by a query or command.
Definition: result.hxx:70
result_size_type size_type
Definition: result.hxx:73
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:208
Definition: transaction_base.hxx:42
result exec_prepared(zview statement, Args &&... args)
Definition: transaction_base.hxx:390
transaction_base & m_trans
Definition: transaction_base.hxx:60
Reference to one row in a result.
Definition: row.hxx:38
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:406
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:188
result exec_prepared_n(result::size_type rows, std::string const &statement, Args &&... args)
Execute a prepared statement, expect a result with given number of rows.
Definition: transaction_base.hxx:431