summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/doc/reference/transaction.adoc
blob: b6bc449c515b60a030dd61231115ded5724217fc (plain)
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
== `sqlite/transaction.hpp`

=== `transaction`

A simple transaction guard implementing RAAI for transactions

.Definition
[source,cpp]
----
struct transaction
{
  // The mode of the transaction
  enum behaviour {deferred, immediate, exclusive};
  // A tag to use, to adopt an already initiated transaction.
  constexpr static struct adopt_transaction_t {} adopt_transaction{};

  // Create transaction guard on an existing transaction
  transaction(connection & conn, adopt_transaction_t);


  // Create transaction guard and initiate a transaction
  transaction(connection & conn);

  // Create transaction guard and initiate a transaction with the defined behaviour
  transaction(connection & conn, behaviour b) ;

  // see https://www.sqlite.org/lang_transaction.html re noexcept
  // rollback the transaction if not committed.
  ~transaction() noexcept(SQLITE_VERSION_NUMBER >= 3007011);


  // Commit the transaction.
  void commit();
  void commit(system::error_code & ec, error_info & ei);
  // Rollback the transaction explicitly.
  void rollback();
  void rollback(system::error_code & ec, error_info & ei);

};
----



.Example
[source,cpp]
----
sqlite::connection conn;
conn.connect("./my-database.db");

sqlite::transaction t{conn};
conn.prepare("insert into log (text) values ($1)").execute(std::make_tuple("booting up"));
t.commit();
----

=== `savepoint`

A simple transaction guard implementing RAAI for savepoints. Savepoints can be used recursively.

.Definition
[source,cpp]
----

struct savepoint
{
  // A tag to use, to adopt an already initiated transaction.
  constexpr static transaction::adopt_transaction_t adopt_transaction{};

  // Create savepoint guard on an existing savepoint
  savepoint(connection & conn, std::string name, transaction::adopt_transaction_t);

  // Create transaction guard and initiate it
  savepoint(connection & conn, std::string name);

  // rollback to the savepoint if not committed.
  ~savepoint() noexcept(SQLITE_VERSION_NUMBER >= 3007011);

  // Commit/Release the transaction.
  void commit();
  void commit(system::error_code & ec, error_info & ei);

  void release();
  void release(system::error_code & ec, error_info & ei);

  // Rollback the transaction explicitly.
  void rollback();
  void rollback(system::error_code & ec, error_info & ei);
  // The name of the savepoint.

  const std::string & name() const;
};
----


.Example
[source,cpp]
----
sqlite::connection conn;
conn.connect("./my-database.db");

sqlite::savepoint t{conn, "my-savepoint};
conn.prepare("insert into log (text) values ($1)").execute(std::make_tuple("booting up"));
t.commit();
----