libpqxx  7.1.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 #include "pqxx/stream_from.hxx"
37 
38 namespace pqxx::internal::gate
39 {
40 class transaction_subtransaction;
41 class transaction_sql_cursor;
42 class transaction_stream_to;
43 class transaction_transactionfocus;
44 } // namespace pqxx::internal::gate
45 
46 
47 namespace pqxx
48 {
61 
67 class PQXX_LIBEXPORT PQXX_NOVTABLE transaction_base
68  : public virtual internal::namedclass
69 {
70 public:
71  transaction_base() = delete;
72  transaction_base(transaction_base const &) = delete;
73  transaction_base &operator=(transaction_base const &) = delete;
74 
75  virtual ~transaction_base() = 0;
76 
78 
90  void commit();
91 
93 
96  void abort();
97 
102  [[nodiscard]] std::string esc(char const text[]) const
104  {
105  return conn().esc(text);
106  }
108  [[nodiscard]] std::string esc(char const text[], std::size_t maxlen) const
109  {
110  return conn().esc(text, maxlen);
111  }
113  [[nodiscard]] std::string esc(std::string const &text) const
114  {
115  return conn().esc(text);
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  return conn().esc_raw(data, len);
139  }
141  [[nodiscard]] std::string esc_raw(std::string const &) const;
142 
144 
147  [[nodiscard]] std::string unesc_raw(std::string const &text) const
148  {
149  return conn().unesc_raw(text);
150  }
151 
153 
156  [[nodiscard]] std::string unesc_raw(char const *text) const
157  {
158  return conn().unesc_raw(text);
159  }
160 
162 
163  template<typename T>[[nodiscard]] std::string quote(T const &t) const
164  {
165  return conn().quote(t);
166  }
167 
169  [[nodiscard]] std::string
170  quote_raw(unsigned char const bin[], std::size_t len) const
171  {
172  return conn().quote_raw(bin, len);
173  }
174 
175  [[nodiscard]] std::string quote_raw(std::string const &bin) const;
176 
178  [[nodiscard]] std::string quote_name(std::string_view identifier) const
179  {
180  return conn().quote_name(identifier);
181  }
182 
184  [[nodiscard]] std::string
185  esc_like(std::string const &bin, char escape_char = '\\') const
186  {
187  return conn().esc_like(bin, escape_char);
188  }
190 
192 
206  result exec(std::string_view query, std::string const &desc = std::string{});
207 
208  result
209  exec(std::stringstream const &query, std::string const &desc = std::string{})
210  {
211  return exec(query.str(), desc);
212  }
213 
215 
220  result
221  exec0(std::string const &query, std::string const &desc = std::string{})
222  {
223  return exec_n(0, query, desc);
224  }
225 
227 
233  row exec1(std::string const &query, std::string const &desc = std::string{})
234  {
235  return exec_n(1, query, desc).front();
236  }
237 
239 
244  result exec_n(
245  result::size_type rows, std::string const &query,
246  std::string const &desc = std::string{});
247 
249  template<typename TYPE>
251  std::string const &query, std::string const &desc = std::string{})
252  {
253  row const r{exec1(query, desc)};
254  if (r.size() != 1)
255  throw usage_error{"Queried single value from result with " +
256  to_string(r.size()) + " columns."};
257  return r[0].as<TYPE>();
258  }
259 
261 
300  template<typename... TYPE>[[nodiscard]] auto stream(std::string_view query)
301  {
302  return pqxx::internal::owning_stream_input_iteration<TYPE...>{
303  std::make_unique<pqxx::stream_from>(*this, from_query_t{}, query)};
304  }
305 
335  template<typename... Args>
337  result exec_params(std::string const &query, Args &&... args)
338  {
339  return internal_exec_params(
340  query, internal::params(std::forward<Args>(args)...));
341  }
342 
343  // Execute parameterised statement, expect a single-row result.
346  template<typename... Args>
347  row exec_params1(std::string const &query, Args &&... args)
348  {
349  return exec_params_n(1, query, std::forward<Args>(args)...).front();
350  }
351 
352  // Execute parameterised statement, expect a result with zero rows.
355  template<typename... Args>
356  result exec_params0(std::string const &query, Args &&... args)
357  {
358  return exec_params_n(0, query, std::forward<Args>(args)...);
359  }
360 
361  // Execute parameterised statement, expect exactly a given number of rows.
364  template<typename... Args>
365  result
366  exec_params_n(std::size_t rows, std::string const &query, Args &&... args)
367  {
368  auto const r{exec_params(query, std::forward<Args>(args)...)};
369  check_rowcount_params(rows, r.size());
370  return r;
371  }
373 
401 
403  template<typename... Args>
404  result exec_prepared(std::string const &statement, Args &&... args)
405  {
406  return internal_exec_prepared(
407  zview{statement.c_str(), statement.size()},
408  internal::params(std::forward<Args>(args)...));
409  }
410 
411  template<typename... Args>
412  result exec_prepared(zview statement, Args &&... args)
413  {
414  return internal_exec_prepared(
415  statement, internal::params(std::forward<Args>(args)...));
416  }
417 
419 
421  template<typename... Args>
422  row exec_prepared1(std::string const &statement, Args &&... args)
423  {
424  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
425  }
426 
427  template<typename... Args>
428  row exec_prepared1(zview statement, Args &&... args)
429  {
430  return exec_prepared_n(1, statement, std::forward<Args>(args)...).front();
431  }
432 
434 
436  template<typename... Args>
437  result exec_prepared0(std::string const &statement, Args &&... args)
438  {
439  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
440  }
441 
442  template<typename... Args>
443  result exec_prepared0(zview statement, Args &&... args)
444  {
445  return exec_prepared_n(0, statement, std::forward<Args>(args)...);
446  }
447 
449 
452  template<typename... Args>
454  result::size_type rows, std::string const &statement, Args &&... args)
455  {
456  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
457  check_rowcount_prepared(statement, rows, r.size());
458  return r;
459  }
460 
461  template<typename... Args>
462  result
463  exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
464  {
465  auto const r{exec_prepared(statement, std::forward<Args>(args)...)};
466  check_rowcount_prepared(statement, rows, r.size());
467  return r;
468  }
469 
471 
476  void process_notice(char const msg[]) const { m_conn.process_notice(msg); }
479  void process_notice(std::string const &msg) const
480  {
481  m_conn.process_notice(msg);
482  }
484 
486  [[nodiscard]] connection &conn() const { return m_conn; }
487 
489 
499  void set_variable(std::string_view var, std::string_view value);
500 
502 
505  std::string get_variable(std::string_view);
506 
507 protected:
509 
512  explicit transaction_base(connection &c);
513 
515  void register_transaction();
516 
518  void close() noexcept;
519 
521  virtual void do_commit() = 0;
523  virtual void do_abort() = 0;
524 
525  // For use by implementing class:
526 
528  result direct_exec(std::string_view);
529  result direct_exec(std::shared_ptr<std::string>);
530 
531 private:
532  enum class status
533  {
534  nascent,
535  active,
536  aborted,
537  committed,
538  in_doubt
539  };
540 
541  PQXX_PRIVATE void check_pending_error();
542 
543  template<typename T> bool parm_is_null(T *p) const noexcept
544  {
545  return p == nullptr;
546  }
547  template<typename T> bool parm_is_null(T) const noexcept { return false; }
548 
549  result internal_exec_prepared(zview statement, internal::params const &args);
550 
551  result
552  internal_exec_params(std::string const &query, internal::params const &args);
553 
555  void check_rowcount_prepared(
556  std::string const &statement, result::size_type expected_rows,
557  result::size_type actual_rows);
558 
560  void
561  check_rowcount_params(std::size_t expected_rows, std::size_t actual_rows);
562 
563  friend class pqxx::internal::gate::transaction_transactionfocus;
564  PQXX_PRIVATE void register_focus(internal::transactionfocus *);
565  PQXX_PRIVATE void unregister_focus(internal::transactionfocus *) noexcept;
566  PQXX_PRIVATE void register_pending_error(std::string const &) noexcept;
567 
568  connection &m_conn;
569 
571  status m_status = status::active;
572  bool m_registered = false;
573  std::string m_pending_error;
574 };
575 } // namespace pqxx
576 
577 
578 namespace pqxx::internal
579 {
581 template<pqxx::isolation_level isolation, pqxx::write_policy rw>
582 extern const zview begin_cmd;
583 
584 // These are not static members, so "constexpr" does not imply "inline".
585 template<>
586 inline constexpr zview begin_cmd<read_committed, write_policy::read_write>{
587  "BEGIN"};
588 template<>
589 inline constexpr zview begin_cmd<read_committed, write_policy::read_only>{
590  "BEGIN READ ONLY"};
591 template<>
592 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_write>{
593  "BEGIN ISOLATION LEVEL REPEATABLE READ"};
594 template<>
595 inline constexpr zview begin_cmd<repeatable_read, write_policy::read_only>{
596  "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY"};
597 template<>
598 inline constexpr zview begin_cmd<serializable, write_policy::read_write>{
599  "BEGIN ISOLATION LEVEL SERIALIZABLE"};
600 template<>
601 inline constexpr zview begin_cmd<serializable, write_policy::read_only>{
602  "BEGIN ISOLATION LEVEL SERIALIZABLE READ ONLY"};
603 } // namespace pqxx::internal
604 
605 #include "pqxx/internal/compiler-internal-post.hxx"
606 #endif
result exec0(std::string const &query, std::string const &desc=std::string{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:221
result exec_prepared0(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:437
const zview begin_cmd
The SQL command for starting a given type of transaction.
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:486
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: connection.hxx:60
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:53
Reference to one row in a result.
Definition: row.hxx:43
std::string esc_like(std::string const &bin, char escape_char='\\') const
Escape string for literal LIKE match.
Definition: transaction_base.hxx:185
result exec_prepared(zview statement, Args &&... args)
Definition: transaction_base.hxx:412
std::string esc(std::string const &text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:113
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:108
void process_notice(std::string const &msg) const
Have connection process a warning message.
Definition: transaction_base.hxx:479
result exec_params(std::string const &query, Args &&... args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:337
std::string unesc_raw(char const *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:156
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:453
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:477
result exec_params_n(std::size_t rows, std::string const &query, Args &&... args)
Definition: transaction_base.hxx:366
result exec(std::stringstream const &query, std::string const &desc=std::string{})
Definition: transaction_base.hxx:209
result exec_prepared(std::string const &statement, Args &&... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:404
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Connection to a database.
Definition: connection.hxx:136
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:147
row exec_params1(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:347
row exec1(std::string const &query, std::string const &desc=std::string{})
Execute query returning a single row of data.
Definition: transaction_base.hxx:233
result exec_params0(std::string const &query, Args &&... args)
Definition: transaction_base.hxx:356
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:214
auto stream(std::string_view query)
Execute a query, and loop over the results row by row.
Definition: transaction_base.hxx:300
row exec_prepared1(zview statement, Args &&... args)
Definition: transaction_base.hxx:428
reference front() const noexcept
Definition: row.cxx:56
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
std::string quote_raw(unsigned char const bin[], std::size_t len) const
Binary-escape and quote a binarystring for use as an SQL constant.
Definition: transaction_base.hxx:170
result exec_prepared_n(result::size_type rows, zview statement, Args &&... args)
Definition: transaction_base.hxx:463
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:250
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:178
Definition: connection.hxx:66
Result set containing data returned by a query or command.
Definition: result.hxx:70
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
result_size_type size_type
Definition: result.hxx:73
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:67
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:164
std::string esc(std::string_view text) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:118
row exec_prepared1(std::string const &statement, Args &&... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:422
result exec_prepared0(zview statement, Args &&... args)
Definition: transaction_base.hxx:443
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:59
std::string quote(T const &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:163