aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/include/pqxx/subtransaction.hxx
blob: 2b353e63db609b9b97da213cd57224c9c8133fdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/** Definition of the pqxx::subtransaction class.
 *
 * pqxx::subtransaction is a nested transaction, i.e. one within a transaction.
 *
 * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/subtransaction instead.
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#ifndef PQXX_H_SUBTRANSACTION
#define PQXX_H_SUBTRANSACTION

#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"

#include "pqxx/dbtransaction.hxx"


/* Methods tested in eg. self-test program test1 are marked with "//[t01]"
 */


namespace pqxx
{

/**
 * @ingroup transaction
 */
/// "Transaction" nested within another transaction
/** A subtransaction can be executed inside a backend transaction, or inside
 * another subtransaction.  This can be useful when, for example, statements in
 * a transaction may harmlessly fail and you don't want them to abort the entire
 * transaction.  Here's an example of how a temporary table may be dropped
 * before re-creating it, without failing if the table did not exist:
 *
 * @code
 * void do_job(connection_base &C)
 * {
 *   const string temptable = "fleetingtable";
 *
 *   // Since we're dealing with a temporary table here, disallow automatic
 *   // recovery of the connection in case it breaks.
 *   C.inhibit_reactivation(true);
 *
 *   work W(C, "do_job");
 *   do_firstpart(W);
 *
 *   // Attempt to delete our temporary table if it already existed
 *   try
 *   {
 *     subtransaction S(W, "droptemp");
 *     S.exec0("DROP TABLE " + temptable);
 *     S.commit();
 *   }
 *   catch (const undefined_table &)
 *   {
 *     // Table did not exist.  Which is what we were hoping to achieve anyway.
 *     // Carry on without regrets.
 *   }
 *
 *   // S may have gone into a failed state and been destroyed, but the
 *   // upper-level transaction W is still fine.  We can continue to use it.
 *   W.exec("CREATE TEMP TABLE " + temptable + "(bar integer, splat varchar)");
 *
 *   do_lastpart(W);
 * }
 * @endcode
 *
 * (This is just an example.  If you really wanted to do drop a table without an
 * error if it doesn't exist, you'd use DROP TABLE IF EXISTS.)
 *
 * There are no isolation levels inside a transaction.  They are not needed
 * because all actions within the same backend transaction are always performed
 * sequentially anyway.
 */
class PQXX_LIBEXPORT subtransaction :
  public internal::transactionfocus,
  public dbtransaction
{
public:
  /// Nest a subtransaction nested in another transaction.
  explicit subtransaction(						//[t88]
	dbtransaction &T, const std::string &Name=std::string{});

  /// Nest a subtransaction in another subtransaction.
  explicit subtransaction(
	subtransaction &T, const std::string &Name=std::string{});

  virtual ~subtransaction() noexcept
	{ End(); }

private:
  virtual void do_begin() override;					//[t88]
  virtual void do_commit() override;					//[t88]
  virtual void do_abort() override;					//[t88]

  dbtransaction &m_parent;
};
}

#include "pqxx/compiler-internal-post.hxx"
#endif