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