libpqxx  7.0.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-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 
302  template<typename... Args>
304  result exec_params(std::string const &query, Args &&... args)
305  {
306  return internal_exec_params(
307  query, internal::params(std::forward<Args>(args)...));
308  }
309 
310  // Execute parameterised statement, expect a single-row result.
313  template<typename... Args>
314  row exec_params1(std::string const &query, Args &&... args)
315  {
316  return exec_params_n(1, query, std::forward<Args>(args)...).front();
317  }
318 
319  // Execute parameterised statement, expect a result with zero rows.
322  template<typename... Args>
323  result exec_params0(std::string const &query, Args &&... args)
324  {
325  return exec_params_n(0, query, std::forward<Args>(args)...);
326  }
327 
328  // Execute parameterised statement, expect exactly a given number of rows.
331  template<typename... Args>
332  result exec_params_n(size_t rows, std::string const &query, Args &&... args)
333  {
334  auto const r{exec_params(query, std::forward<Args>(args)...)};
335  check_rowcount_params(rows, r.size());
336  return r;
337  }
339 
367 
369  template<typename... Args>
370  result exec_prepared(std::string const &statement, Args &&... args)
371  {
372  return internal_exec_prepared(
373  zview{statement.c_str(), statement.size()},
374  internal::params(std::forward<Args>(args)...));
375  }
376 
377  template<typename... Args>
378  result exec_prepared(zview statement, Args &&... args)
379  {
380  return internal_exec_prepared(
381  statement, internal::params(std::forward<Args>(args)...));
382  }
383 
385 
387  template<typename... Args>
388  row exec_prepared1(std::string const &statement, Args &&... args)
389  {
390  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
391  }
392 
393  template<typename... Args>
394  row exec_prepared1(zview statement, Args &&... args)
395  {
396  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
397  }
398 
400 
402  template<typename... Args>
403  result exec_prepared0(std::string const &statement, Args &&... args)
404  {
405  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
406  }
407 
408  template<typename... Args>
409  result exec_prepared0(zview statement, Args &&... args)
410  {
411  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
412  }
413 
415 
418  template<typename... Args>
420  result::size_type rows, std::string const &statement, Args &&... args)
421  {
422  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
423  check_rowcount_prepared(statement, rows, r.size());
424  return r;
425  }
426 
427  template<typename... Args>
428  result
429  exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
430  {
431  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
432  check_rowcount_prepared(statement, rows, r.size());
433  return r;
434  }
435 
437 
442  void process_notice(char const msg[]) const { m_conn.process_notice(msg); }
445  void process_notice(std::string const &msg) const
446  {
447  m_conn.process_notice(msg);
448  }
450 
452  [[nodiscard]] connection &conn() const { return m_conn; }
453 
455 
465  void set_variable(std::string_view var, std::string_view value);
466 
468 
471  std::string get_variable(std::string_view);
472 
473 protected:
475 
478  explicit transaction_base(connection &c);
479 
481  void register_transaction();
482 
484  void close() noexcept;
485 
487  virtual void do_commit() = 0;
489  virtual void do_abort() = 0;
490 
491  // For use by implementing class:
492 
494  result direct_exec(std::string_view);
495  result direct_exec(std::shared_ptr<std::string>);
496 
497 private:
498  enum class status
499  {
500  nascent,
501  active,
502  aborted,
503  committed,
504  in_doubt
505  };
506 
507  PQXX_PRIVATE void check_pending_error();
508 
509  template<typename T> bool parm_is_null(T *p) const noexcept
510  {
511  return p == nullptr;
512  }
513  template<typename T> bool parm_is_null(T) const noexcept { return false; }
514 
515  result internal_exec_prepared(zview statement, internal::params const &args);
516 
517  result
518  internal_exec_params(std::string const &query, internal::params const &args);
519 
521  void check_rowcount_prepared(
522  std::string const &statement, result::size_type expected_rows,
523  result::size_type actual_rows);
524 
526  void check_rowcount_params(size_t expected_rows, size_t actual_rows);
527 
528  friend class pqxx::internal::gate::transaction_transactionfocus;
529  PQXX_PRIVATE void register_focus(internal::transactionfocus *);
530  PQXX_PRIVATE void unregister_focus(internal::transactionfocus *) noexcept;
531  PQXX_PRIVATE void register_pending_error(std::string const &) noexcept;
532 
533  connection &m_conn;
534 
536  status m_status = status::active;
537  bool m_registered = false;
538  std::string m_pending_error;
539 };
540 } // namespace pqxx
541 
542 
543 namespace pqxx::internal
544 {
546 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
547 extern const zview begin_cmd;
548 
549 // These are not static members, so "constexpr" does not imply "inline".
550 template<>
551 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
552  "BEGIN"};
553 template<>
554 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
555  "BEGIN READ ONLY"};
556 template<>
557 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
558  "BEGIN ISOLATION LEVEL REPEATABLE READ"};
559 template<>
560 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
561  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
562 template<>
563 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
564  "BEGIN ISOLATION LEVEL SERIALIZABLE"};
565 template<>
566 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
567  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
568 } // namespace pqxx::internal
569 
570 #include "pqxx/internal/compiler-internal-post.hxx"
571 #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:370
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:314
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:323
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:388
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:409
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:429
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
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:332
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:445
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:403
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:304
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:452
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:378
transaction_base & m_trans
Definition: transaction_base.hxx:60
Reference to one row in a result.
Definition: row.hxx:38
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:394
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:419