xref: /freebsd/contrib/kyua/utils/sqlite/transaction.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/sqlite/transaction.hpp"
30 
31 #include "utils/format/macros.hpp"
32 #include "utils/logging/macros.hpp"
33 #include "utils/noncopyable.hpp"
34 #include "utils/sanity.hpp"
35 #include "utils/sqlite/database.hpp"
36 #include "utils/sqlite/exceptions.hpp"
37 #include "utils/sqlite/statement.ipp"
38 
39 namespace sqlite = utils::sqlite;
40 
41 
42 /// Internal implementation for the transaction.
43 struct utils::sqlite::transaction::impl : utils::noncopyable {
44     /// The database this transaction belongs to.
45     database& db;
46 
47     /// Possible statuses of a transaction.
48     enum statuses {
49         open_status,
50         committed_status,
51         rolled_back_status,
52     };
53 
54     /// The current status of the transaction.
55     statuses status;
56 
57     /// Constructs a new transaction.
58     ///
59     /// \param db_ The database this transaction belongs to.
60     /// \param status_ The status of the new transaction.
61     impl(database& db_, const statuses status_) :
62         db(db_),
63         status(status_)
64     {
65     }
66 
67     /// Destroys the transaction.
68     ///
69     /// This rolls back the transaction if it is open.
70     ~impl(void)
71     {
72         if (status == impl::open_status) {
73             try {
74                 rollback();
75             } catch (const sqlite::error& e) {
76                 LW(F("Error while rolling back a transaction: %s") % e.what());
77             }
78         }
79     }
80 
81     /// Commits the transaction.
82     ///
83     /// \throw api_error If there is any problem while committing the
84     ///     transaction.
85     void
86     commit(void)
87     {
88         PRE(status == impl::open_status);
89         db.exec("COMMIT");
90         status = impl::committed_status;
91     }
92 
93     /// Rolls the transaction back.
94     ///
95     /// \throw api_error If there is any problem while rolling the
96     ///     transaction back.
97     void
98     rollback(void)
99     {
100         PRE(status == impl::open_status);
101         db.exec("ROLLBACK");
102         status = impl::rolled_back_status;
103     }
104 };
105 
106 
107 /// Initializes a transaction object.
108 ///
109 /// This is an internal function.  Use database::begin_transaction() to
110 /// instantiate one of these objects.
111 ///
112 /// \param db The database this transaction belongs to.
113 sqlite::transaction::transaction(database& db) :
114     _pimpl(new impl(db, impl::open_status))
115 {
116 }
117 
118 
119 /// Destructor for the transaction.
120 sqlite::transaction::~transaction(void)
121 {
122 }
123 
124 
125 /// Commits the transaction.
126 ///
127 /// \throw api_error If there is any problem while committing the transaction.
128 void
129 sqlite::transaction::commit(void)
130 {
131     _pimpl->commit();
132 }
133 
134 
135 /// Rolls the transaction back.
136 ///
137 /// \throw api_error If there is any problem while rolling the transaction back.
138 void
139 sqlite::transaction::rollback(void)
140 {
141     _pimpl->rollback();
142 }
143