libpqxx
transaction_base.hxx
1 
14 #ifndef PQXX_H_TRANSACTION_BASE
15 #define PQXX_H_TRANSACTION_BASE
16 
17 #include "pqxx/compiler-public.hxx"
18 #include "pqxx/compiler-internal-pre.hxx"
19 
20 /* End-user programs need not include this file, unless they define their own
21  * transaction classes. This is not something the typical program should want
22  * to do.
23  *
24  * However, reading this file is worthwhile because it defines the public
25  * interface for the available transaction classes such as transaction and
26  * nontransaction.
27  */
28 
29 #include "pqxx/connection_base.hxx"
30 #include "pqxx/isolation.hxx"
31 #include "pqxx/result.hxx"
32 #include "pqxx/row.hxx"
33 
34 // Methods tested in eg. test module test01 are marked with "//[t01]".
35 
36 namespace pqxx
37 {
38 namespace 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  m_registered(false)
49  {
50  }
51 
52  transactionfocus() =delete;
53  transactionfocus(const transactionfocus &) =delete;
54  transactionfocus &operator=(const transactionfocus &) =delete;
55 
56 protected:
57  void register_me();
58  void unregister_me() noexcept;
59  void reg_pending_error(const std::string &) noexcept;
60  bool registered() const noexcept { return m_registered; }
61 
63 
64 private:
65  bool m_registered;
66 };
67 
68 
70 
72 class PQXX_LIBEXPORT parameterized_invocation : statement_parameters
73 {
74 public:
75  parameterized_invocation(connection_base &, const std::string &query);
76 
77  parameterized_invocation &operator()() { add_param(); return *this; }
79  { add_binary_param(v, true); return *this; }
80  template<typename T> parameterized_invocation &operator()(const T &v)
81  { add_param(v, true); return *this; }
83  { add_binary_param(v, nonnull); return *this; }
84  template<typename T>
85  parameterized_invocation &operator()(const T &v, bool nonnull)
86  { add_param(v, nonnull); return *this; }
87 
88  result exec();
89 
90 private:
93 
94  connection_base &m_home;
95  const std::string m_query;
96 };
97 } // namespace internal
98 
99 
100 namespace internal
101 {
102 namespace gate
103 {
104 class transaction_subtransaction;
105 class transaction_tablereader;
106 class transaction_tablewriter;
107 class transaction_transactionfocus;
108 } // namespace internal::gate
109 } // namespace internal
110 
111 
124 
130 class PQXX_LIBEXPORT PQXX_NOVTABLE transaction_base :
131  public virtual internal::namedclass
132 {
133 public:
136 
137  transaction_base() =delete;
138  transaction_base(const transaction_base &) =delete;
139  transaction_base &operator=(const transaction_base &) =delete;
140 
141  virtual ~transaction_base() =0; //[t01]
142 
144 
156  void commit(); //[t01]
157 
159 
162  void abort(); //[t10]
163 
168  std::string esc(const char str[]) const { return conn().esc(str); }
171  std::string esc(const char str[], size_t maxlen) const
172  { return conn().esc(str, maxlen); }
174  std::string esc(const std::string &str) const { return conn().esc(str); }
175 
177 
188  std::string esc_raw(const unsigned char data[], size_t len) const //[t62]
189  { return conn().esc_raw(data, len); }
191  std::string esc_raw(const std::string &) const; //[t62]
192 
194 
197  std::string unesc_raw(const std::string &text) const
198  { return conn().unesc_raw(text); }
199 
201 
204  std::string unesc_raw(const char *text) const
205  { return conn().unesc_raw(text); }
206 
208 
209  template<typename T> std::string quote(const T &t) const
210  { return conn().quote(t); }
211 
213  std::string quote_raw(const unsigned char str[], size_t len) const
214  { return conn().quote_raw(str, len); }
215 
216  std::string quote_raw(const std::string &str) const;
217 
219  std::string quote_name(const std::string &identifier) const
220  { return conn().quote_name(identifier); }
222 
224 
239  result exec(
240  const std::string &Query,
241  const std::string &Desc=std::string()); //[t01]
242 
244  const std::stringstream &Query,
245  const std::string &Desc=std::string())
246  { return exec(Query.str(), Desc); }
247 
249 
255  const std::string &Query,
256  const std::string &Desc=std::string())
257  { return exec_n(0, Query, Desc); }
258 
260 
266  row exec1(const std::string &Query, const std::string &Desc=std::string())
267  { return exec_n(1, Query, Desc).front(); }
268 
270 
275  result exec_n(
276  size_t rows,
277  const std::string &Query,
278  const std::string &Desc=std::string());
279 
309  template<typename ...Args>
311  result exec_params(const std::string &query, Args ...args)
312  {
313  return internal_exec_params(query, internal::params(args...));
314  }
315 
316  // Execute parameterised statement, expect a single-row result.
319  template<typename ...Args>
320  row exec_params1(const std::string &query, Args... args)
321  {
322  return exec_params_n(1, query, args...).front();
323  }
324 
325  // Execute parameterised statement, expect a result with zero rows.
328  template<typename ...Args>
329  result exec_params0(const std::string &query, Args ...args)
330  {
331  return exec_params_n(0, query, args...);
332  }
333 
334  // Execute parameterised statement, expect exactly a given number of rows.
337  template<typename ...Args>
338  result exec_params_n(size_t rows, const std::string &query, Args ...args)
339  {
340  const auto r = exec_params(query, args...);
341  check_rowcount_params(rows, r.size());
342  return r;
343  }
344 
346  /* Use this to build up a parameterized statement invocation, then invoke it
347  * using @c exec()
348  *
349  * Example: @c trans.parameterized("SELECT $1 + 1")(1).exec();
350  *
351  * This is the old, pre-C++11 way of handling parameterised statements. As
352  * of libpqxx 6.0, it's made much easier using variadic templates.
353  */
354  internal::parameterized_invocation parameterized(const std::string &query);
356 
381 
383  template<typename ...Args>
384  result exec_prepared(const std::string &statement, Args... args)
385  {
386  return internal_exec_prepared(statement, internal::params(args...));
387  }
388 
390 
392  template<typename ...Args>
393  row exec_prepared1(const std::string &statement, Args... args)
394  {
395  return exec_prepared_n(1, statement, args...).front();
396  }
397 
399 
401  template<typename ...Args>
402  result exec_prepared0(const std::string &statement, Args... args)
403  {
404  return exec_prepared_n(0, statement, args...);
405  }
406 
408 
411  template<typename ...Args>
413  size_t rows,
414  const std::string &statement,
415  Args... args)
416  {
417  const auto r = exec_prepared(statement, args...);
418  check_rowcount_prepared(statement, rows, r.size());
419  return r;
420  }
421 
423 
461  prepare::invocation prepared(const std::string &statement=std::string());
462 
464 
469  void process_notice(const char Msg[]) const //[t14]
471  { m_conn.process_notice(Msg); }
473  void process_notice(const std::string &Msg) const //[t14]
474  { m_conn.process_notice(Msg); }
476 
478  connection_base &conn() const { return m_conn; } //[t04]
479 
481 
488  void set_variable(const std::string &Var, const std::string &Val); //[t61]
489 
491 
500  std::string get_variable(const std::string &); //[t61]
501 
502 protected:
504 
510  explicit transaction_base(connection_base &c, bool direct=true);
511 
513 
515  void Begin();
516 
518  void End() noexcept;
519 
521  virtual void do_begin() =0;
523  virtual result do_exec(const char Query[]) =0;
525  virtual void do_commit() =0;
527  virtual void do_abort() =0;
528 
529  // For use by implementing class:
530 
532 
540  result direct_exec(const char C[], int Retries=0);
541 
544  {m_reactivation_avoidance.clear();}
545 
546 protected:
548 
551 
552 private:
553  /* A transaction goes through the following stages in its lifecycle:
554  * <ul>
555  * <li> nascent: the transaction hasn't actually begun yet. If our connection
556  * fails at this stage, it may recover and the transaction can attempt to
557  * establish itself again.
558  * <li> active: the transaction has begun. Since no commit command has been
559  * issued, abortion is implicit if the connection fails now.
560  * <li> aborted: an abort has been issued; the transaction is terminated and
561  * its changes to the database rolled back. It will accept no further
562  * commands.
563  * <li> committed: the transaction has completed successfully, meaning that a
564  * commit has been issued. No further commands are accepted.
565  * <li> in_doubt: the connection was lost at the exact wrong time, and there
566  * is no way of telling whether the transaction was committed or aborted.
567  * </ul>
568  *
569  * Checking and maintaining state machine logic is the responsibility of the
570  * base class (ie., this one).
571  */
572  enum Status
573  {
574  st_nascent,
575  st_active,
576  st_aborted,
577  st_committed,
578  st_in_doubt
579  };
580 
582  PQXX_PRIVATE void activate();
583 
584  PQXX_PRIVATE void CheckPendingError();
585 
586  template<typename T> bool parm_is_null(T *p) const noexcept
587  { return !p; }
588  template<typename T> bool parm_is_null(T) const noexcept
589  { return false; }
590 
591  result internal_exec_prepared(
592  const std::string &statement,
593  const internal::params &args);
594 
595  result internal_exec_params(
596  const std::string &query,
597  const internal::params &args);
598 
600  void check_rowcount_prepared(
601  const std::string &statement,
602  size_t expected_rows,
603  size_t actual_rows);
604 
606  void check_rowcount_params(
607  size_t expected_rows, size_t actual_rows);
608 
609  friend class pqxx::internal::gate::transaction_transactionfocus;
610  PQXX_PRIVATE void register_focus(internal::transactionfocus *);
611  PQXX_PRIVATE void unregister_focus(internal::transactionfocus *) noexcept;
612  PQXX_PRIVATE void register_pending_error(const std::string &) noexcept;
613 
614  friend class pqxx::internal::gate::transaction_tablereader;
615  PQXX_PRIVATE void BeginCopyRead(const std::string &, const std::string &);
616  bool read_copy_line(std::string &);
617 
618  friend class pqxx::internal::gate::transaction_tablewriter;
619  PQXX_PRIVATE void BeginCopyWrite(
620  const std::string &Table,
621  const std::string &Columns);
622  void write_copy_line(const std::string &);
623  void end_copy_write();
624 
625  friend class pqxx::internal::gate::transaction_subtransaction;
626 
627  connection_base &m_conn;
628 
630  Status m_status = st_nascent;
631  bool m_registered = false;
632  std::map<std::string, std::string> m_vars;
633  std::string m_pending_error;
634 };
635 
636 } // namespace pqxx
637 
638 #include "pqxx/compiler-internal-post.hxx"
639 
640 #endif
Binary data corresponding to PostgreSQL&#39;s "BYTEA" binary-string type.
Definition: binarystring.hxx:53
result exec0(const std::string &Query, const std::string &Desc=std::string())
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:254
transactionfocus(transaction_base &t)
Definition: transaction_base.hxx:45
std::string quote_raw(const unsigned char str[], size_t len) const
Binary-escape and quote a binarystring for use as an SQL constant.
Definition: transaction_base.hxx:213
Definition: connection_base.hxx:47
Reference to one row in a result.
Definition: row.hxx:40
reference front() const noexcept
Definition: row.cxx:56
row exec_prepared1(const std::string &statement, Args... args)
Execute a prepared statement, and expect a single-row result.
Definition: transaction_base.hxx:393
result exec_prepared0(const std::string &statement, Args... args)
Execute a prepared statement, and expect a result with zero rows.
Definition: transaction_base.hxx:402
Helper class for passing parameters to, and executing, prepared statements.
Definition: prepared_statement.hxx:30
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
Definition: transaction_base.hxx:42
Ensure proper opening/closing of GUEST objects related to a "host" object.
Definition: util.hxx:208
bool registered() const noexcept
Definition: transaction_base.hxx:60
void process_notice(const std::string &Msg) const
Have connection process warning message.
Definition: transaction_base.hxx:473
std::string unesc_raw(const char *text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:204
std::string quote_name(const std::string &identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:219
result exec_params_n(size_t rows, const std::string &query, Args ...args)
Definition: transaction_base.hxx:338
parameterized_invocation & operator()(const T &v, bool nonnull)
Definition: transaction_base.hxx:85
parameterized_invocation & operator()(const binarystring &v)
Definition: transaction_base.hxx:78
std::string esc_raw(const unsigned char data[], size_t len) const
Escape binary data for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:188
connection_base & conn() const
Connection this transaction is running in.
Definition: transaction_base.hxx:478
Traits class to describe an isolation level; primarly for libpqxx&#39;s own use.
Definition: isolation.hxx:65
result exec(const std::stringstream &Query, const std::string &Desc=std::string())
Definition: transaction_base.hxx:243
Result set containing data returned by a query or command.
Definition: result.hxx:67
result exec_params0(const std::string &query, Args ...args)
Definition: transaction_base.hxx:329
result exec_prepared_n(size_t rows, const std::string &statement, Args... args)
Execute a prepared statement, expect a result with given number of rows.
Definition: transaction_base.hxx:412
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:130
std::string quote(const T &t) const
Represent object as SQL string, including quoting & escaping.
Definition: transaction_base.hxx:209
std::string esc(const std::string &str) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:174
internal::reactivation_avoidance_counter m_reactivation_avoidance
Resources allocated in this transaction that make reactivation impossible.
Definition: transaction_base.hxx:550
void reactivation_avoidance_clear() noexcept
Forget about any reactivation-blocking resources we tried to allocate.
Definition: transaction_base.hxx:543
Helper class to construct an invocation of a parameterised statement.
Definition: transaction_base.hxx:72
parameterized_invocation & operator()()
Definition: transaction_base.hxx:77
row exec1(const std::string &Query, const std::string &Desc=std::string())
Execute query returning a single row of data.
Definition: transaction_base.hxx:266
std::string unesc_raw(const std::string &text) const
Unescape binary data, e.g. from a table field or notification payload.
Definition: transaction_base.hxx:197
result exec_params(const std::string &query, Args ...args)
Execute an SQL statement with parameters.
Definition: transaction_base.hxx:311
parameterized_invocation & operator()(const binarystring &v, bool nonnull)
Definition: transaction_base.hxx:82
connection_base abstract base class; represents a connection to a database.
Definition: connection_base.hxx:138
result exec_prepared(const std::string &statement, Args... args)
Execute a prepared statement, with optional arguments.
Definition: transaction_base.hxx:384
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:167
row exec_params1(const std::string &query, Args... args)
Definition: transaction_base.hxx:320
parameterized_invocation & operator()(const T &v)
Definition: transaction_base.hxx:80
transaction_base & m_trans
Definition: transaction_base.hxx:62
std::string esc(const char str[], size_t maxlen) const
Escape string for use as SQL string literal in this transaction.
Definition: transaction_base.hxx:171