libpqxx  7.0.7
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  }
148  [[nodiscard]] std::string esc(std::string_view text) const
149  {
150  return conn().esc(text);
151  }
152 
154 
165  [[nodiscard]] std::string
166  esc_raw(unsigned char const data[], size_t len) const
167  {
168  return conn().esc_raw(data, len);
169  }
171  [[nodiscard]] std::string esc_raw(std::string const &) const;
172 
174 
177  [[nodiscard]] std::string unesc_raw(std::string const &text) const
178  {
179  return conn().unesc_raw(text);
180  }
181 
183 
186  [[nodiscard]] std::string unesc_raw(char const *text) const
187  {
188  return conn().unesc_raw(text);
189  }
190 
192 
193  template<typename T>[[nodiscard]] std::string quote(T const &t) const
194  {
195  return conn().quote(t);
196  }
197 
199  [[nodiscard]] std::string
200  quote_raw(unsigned char const bin[], size_t len) const
201  {
202  return conn().quote_raw(bin, len);
203  }
204 
205  [[nodiscard]] std::string quote_raw(std::string const &bin) const;
206 
208  [[nodiscard]] std::string quote_name(std::string_view identifier) const
209  {
210  return conn().quote_name(identifier);
211  }
212 
214  [[nodiscard]] std::string
215  esc_like(std::string const &bin, char escape_char = '\\') const
216  {
217  return conn().esc_like(bin, escape_char);
218  }
220 
222 
236  result exec(std::string_view query, std::string const &desc = std::string{});
237 
238  result
239  exec(std::stringstream const &query, std::string const &desc = std::string{})
240  {
241  return exec(query.str(), desc);
242  }
243 
245 
250  result
251  exec0(std::string const &query, std::string const &desc = std::string{})
252  {
253  return exec_n(0, query, desc);
254  }
255 
257 
263  row exec1(std::string const &query, std::string const &desc = std::string{})
264  {
265  return exec_n(1, query, desc).front();
266  }
267 
269 
274  result exec_n(
275  result::size_type rows, std::string const &query,
276  std::string const &desc = std::string{});
277 
279  template<typename TYPE>
281  std::string const &query, std::string const &desc = std::string{})
282  {
283  row const r{exec1(query, desc)};
284  if (r.size() != 1)
285  throw usage_error{"Queried single value from result with " +
286  to_string(r.size()) + " columns."};
287  return r[0].as<TYPE>();
288  }
289 
319  template<typename... Args>
321  result exec_params(std::string const &query, Args &&... args)
322  {
323  return internal_exec_params(
324  query, internal::params(std::forward<Args>(args)...));
325  }
326 
327  // Execute parameterised statement, expect a single-row result.
330  template<typename... Args>
331  row exec_params1(std::string const &query, Args &&... args)
332  {
333  return exec_params_n(1, query, std::forward<Args>(args)...).front();
334  }
335 
336  // Execute parameterised statement, expect a result with zero rows.
339  template<typename... Args>
340  result exec_params0(std::string const &query, Args &&... args)
341  {
342  return exec_params_n(0, query, std::forward<Args>(args)...);
343  }
344 
345  // Execute parameterised statement, expect exactly a given number of rows.
348  template<typename... Args>
349  result exec_params_n(size_t rows, std::string const &query, Args &&... args)
350  {
351  auto const r{exec_params(query, std::forward<Args>(args)...)};
352  check_rowcount_params(rows, r.size());
353  return r;
354  }
356 
384 
386  template<typename... Args>
387  result exec_prepared(std::string const &statement, Args &&... args)
388  {
389  return internal_exec_prepared(
390  zview{statement.c_str(), statement.size()},
391  internal::params(std::forward<Args>(args)...));
392  }
393 
394  template<typename... Args>
395  result exec_prepared(zview statement, Args &&... args)
396  {
397  return internal_exec_prepared(
398  statement, internal::params(std::forward<Args>(args)...));
399  }
400 
402 
404  template<typename... Args>
405  row exec_prepared1(std::string const &statement, Args &&... args)
406  {
407  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
408  }
409 
410  template<typename... Args>
411  row exec_prepared1(zview statement, Args &&... args)
412  {
413  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
414  }
415 
417 
419  template<typename... Args>
420  result exec_prepared0(std::string const &statement, Args &&... args)
421  {
422  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
423  }
424 
425  template<typename... Args>
426  result exec_prepared0(zview statement, Args &&... args)
427  {
428  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
429  }
430 
432 
435  template<typename... Args>
437  result::size_type rows, std::string const &statement, Args &&... args)
438  {
439  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
440  check_rowcount_prepared(statement, rows, r.size());
441  return r;
442  }
443 
444  template<typename... Args>
445  result
446  exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
447  {
448  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
449  check_rowcount_prepared(statement, rows, r.size());
450  return r;
451  }
452 
454 
459  void process_notice(char const msg[]) const { m_conn.process_notice(msg); }
462  void process_notice(std::string const &msg) const
463  {
464  m_conn.process_notice(msg);
465  }
467 
469  [[nodiscard]] connection &conn() const { return m_conn; }
470 
472 
482  void set_variable(std::string_view var, std::string_view value);
483 
485 
488  std::string get_variable(std::string_view);
489 
490 protected:
492 
495  explicit transaction_base(connection &c);
496 
498  void register_transaction();
499 
501  void close() noexcept;
502 
504  virtual void do_commit() = 0;
506  virtual void do_abort() = 0;
507 
508  // For use by implementing class:
509 
511  result direct_exec(std::string_view);
512  result direct_exec(std::shared_ptr<std::string>);
513 
514 private:
515  enum class status
516  {
517  nascent,
518  active,
519  aborted,
520  committed,
521  in_doubt
522  };
523 
524  PQXX_PRIVATE void check_pending_error();
525 
526  template<typename T> bool parm_is_null(T *p) const noexcept
527  {
528  return p == nullptr;
529  }
530  template<typename T> bool parm_is_null(T) const noexcept { return false; }
531 
532  result internal_exec_prepared(zview statement, internal::params const &args);
533 
534  result
535  internal_exec_params(std::string const &query, internal::params const &args);
536 
538  void check_rowcount_prepared(
539  std::string const &statement, result::size_type expected_rows,
540  result::size_type actual_rows);
541 
543  void check_rowcount_params(size_t expected_rows, size_t actual_rows);
544 
545  friend class pqxx::internal::gate::transaction_transactionfocus;
546  PQXX_PRIVATE void register_focus(internal::transactionfocus *);
547  PQXX_PRIVATE void unregister_focus(internal::transactionfocus *) noexcept;
548  PQXX_PRIVATE void register_pending_error(std::string const &) noexcept;
549 
550  connection &m_conn;
551 
553  status m_status = status::active;
554  bool m_registered = false;
555  std::string m_pending_error;
556 };
557 } // namespace pqxx
558 
559 
560 namespace pqxx::internal
561 {
563 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
564 extern const zview begin_cmd;
565 
566 // These are not static members, so "constexpr" does not imply "inline".
567 template<>
568 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
569  "BEGIN"};
570 template<>
571 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
572  "BEGIN READ ONLY"};
573 template<>
574 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
575  "BEGIN ISOLATION LEVEL REPEATABLE READ"};
576 template<>
577 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
578  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
579 template<>
580 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
581  "BEGIN ISOLATION LEVEL SERIALIZABLE"};
582 template<>
583 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
584  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
585 } // namespace pqxx::internal
586 
587 #include "pqxx/internal/compiler-internal-post.hxx"
588 #endif
pqxx::row
Reference to one row in a result.
Definition: row.hxx:38
pqxx::internal::transactionfocus::reg_pending_error
void reg_pending_error(std::string const &) noexcept
Definition: transaction_base.cxx:443
pqxx::result::empty
PQXX_PURE bool empty() const noexcept
Definition: result.cxx:108
pqxx::internal::transactionfocus::transactionfocus
transactionfocus(transaction_base &t)
Definition: transaction_base.hxx:45
pqxx::transaction_base::exec_prepared0
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:426
pqxx::transaction_base::query_value
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:280
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
pqxx::transaction_base::unesc_raw
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:177
pqxx::zview::c_str
constexpr const char * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:41
pqxx::internal::gate
Definition: connection.hxx:65
pqxx::to_string
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
pqxx::transaction_base::esc
std::string esc(std::string const &text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:143
pqxx::transaction_base::process_notice
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:462
pqxx::internal::transactionfocus::unregister_me
void unregister_me() noexcept
Definition: transaction_base.cxx:436
pqxx::transaction_base::exec_prepared
result exec_prepared(std::string const &statement, Args &&... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:387
pqxx::transaction_base::direct_exec
result direct_exec(std::string_view)
Execute query on connection directly.
Definition: transaction_base.cxx:377
pqxx::transaction_base::register_transaction
void register_transaction()
Register this transaction with the connection.
Definition: transaction_base.cxx:61
pqxx::transaction_base::quote_raw
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:200
pqxx::transaction_base::exec_prepared1
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:411
pqxx::failure
Run-time failure encountered by libpqxx, similar to std::runtime_error.
Definition: except.hxx:42
pqxx::internal::unique
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:253
pqxx::transaction_base::abort
void abort()
Abort the transaction.
Definition: transaction_base.cxx:142
pqxx::internal
Private namespace for libpqxx's internal use; do not access.
Definition: connection.hxx:59
pqxx::transaction_base::exec_params1
row exec_params1(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:331
pqxx::transaction_base::get_variable
std::string get_variable(std::string_view)
Read session variable using SQL "SHOW" command.
Definition: transaction_base.cxx:302
pqxx::transaction_base::exec_prepared_n
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Definition: transaction_base.hxx:446
pqxx::transaction_base::commit
void commit()
Commit the transaction.
Definition: transaction_base.cxx:69
pqxx::transaction_base::exec_params
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:321
pqxx::result
Result set containing data returned by a query or command.
Definition: result.hxx:70
pqxx::transaction_base::esc
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
pqxx::transaction_base::esc
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:148
pqxx::internal::namedclass
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:209
pqxx::transaction_base::exec0
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:251
pqxx::transaction_base::set_variable
void set_variable(std::string_view var, std::string_view value)
Set session variable using SQL "SET" command.
Definition: transaction_base.cxx:295
pqxx::transaction_base::exec_prepared
result exec_prepared(zview statement, Args &&... args)
Definition: transaction_base.hxx:395
pqxx::transaction_base::conn
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:469
pqxx::transaction_base
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:97
pqxx::broken_connection
Exception class for lost or failed backend connection.
Definition: except.hxx:67
pqxx::internal_error
Internal error in libpqxx library.
Definition: except.hxx:157
pqxx::zview
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
pqxx::internal::transactionfocus::m_trans
transaction_base & m_trans
Definition: transaction_base.hxx:60
pqxx::result::size_type
result_size_type size_type
Definition: result.hxx:73
pqxx::transaction_base::exec_params0
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:340
pqxx::internal::transactionfocus::register_me
void register_me()
Definition: transaction_base.cxx:428
pqxx::transaction_base::exec
result exec(std::string_view query, std::string const &desc=std::string{})
Execute query.
Definition: transaction_base.cxx:198
pqxx::transaction_base::exec_n
result exec_n(result::size_type rows, std::string const &query, std::string const &desc=std::string{})
Execute query, expect given number of rows.
Definition: transaction_base.cxx:235
pqxx::transaction_base::exec
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:239
pqxx::transaction_base::exec1
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:263
pqxx::transaction_base::exec_params_n
result exec_params_n(size_t rows, std::string const &query, Args &&... args)
Definition: transaction_base.hxx:349
pqxx::transaction_base::quote_name
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:208
pqxx::transaction_base::exec_prepared_n
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:436
pqxx::transaction_base::~transaction_base
virtual ~transaction_base()=0
Definition: transaction_base.cxx:33
pqxx::connection
Connection to a database.
Definition: connection.hxx:135
pqxx::row::front
reference front() const noexcept
Definition: row.cxx:56
pqxx::transaction_base::esc_raw
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:166
pqxx::unexpected_rows
Query returned an unexpected number of rows.
Definition: except.hxx:199
pqxx::transaction_base::close
void close() noexcept
End transaction. To be called by implementing class' destructor.
Definition: transaction_base.cxx:308
pqxx::internal::transactionfocus
Definition: transaction_base.hxx:42
pqxx::transaction_base::unesc_raw
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:186
pqxx::transaction_base::transaction_base
transaction_base()=delete
pqxx::in_doubt_error
"Help, I don't know whether transaction was committed successfully!"
Definition: except.hxx:106
pqxx::transaction_base::esc_like
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:215
pqxx::usage_error
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
pqxx::transaction_base::exec_prepared0
result exec_prepared0(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:420
pqxx::transaction_base::quote
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:193
pqxx::internal::begin_cmd
const zview begin_cmd
The SQL command for starting a given type of transaction.
pqxx::transaction_base::exec_prepared1
row exec_prepared1(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:405