1 // Copyright 2011 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "utils/sqlite/transaction.hpp"
31 #include "utils/format/macros.hpp"
32 #include "utils/logging/macros.hpp"
33 #include "utils/sanity.hpp"
34 #include "utils/sqlite/database.hpp"
35 #include "utils/sqlite/exceptions.hpp"
36 #include "utils/sqlite/statement.ipp"
38 namespace sqlite
= utils::sqlite
;
41 /// Internal implementation for the transaction.
42 struct utils::sqlite::transaction::impl
{
43 /// The database this transaction belongs to.
46 /// Possible statuses of a transaction.
53 /// The current status of the transaction.
56 /// Constructs a new transaction.
58 /// \param db_ The database this transaction belongs to.
59 /// \param status_ The status of the new transaction.
60 impl(database
& db_
, const statuses status_
) :
66 /// Destroys the transaction.
68 /// This rolls back the transaction if it is open.
71 if (status
== impl::open_status
) {
74 } catch (const sqlite::error
& e
) {
75 LW(F("Error while rolling back a transaction: %s") % e
.what());
80 /// Commits the transaction.
82 /// \throw api_error If there is any problem while committing the
87 PRE(status
== impl::open_status
);
89 status
= impl::committed_status
;
92 /// Rolls the transaction back.
94 /// \throw api_error If there is any problem while rolling the
99 PRE(status
== impl::open_status
);
101 status
= impl::rolled_back_status
;
106 /// Initializes a transaction object.
108 /// This is an internal function. Use database::begin_transaction() to
109 /// instantiate one of these objects.
111 /// \param db The database this transaction belongs to.
112 sqlite::transaction::transaction(database
& db
) :
113 _pimpl(new impl(db
, impl::open_status
))
118 /// Destructor for the transaction.
119 sqlite::transaction::~transaction(void)
124 /// Commits the transaction.
126 /// \throw api_error If there is any problem while committing the transaction.
128 sqlite::transaction::commit(void)
134 /// Rolls the transaction back.
136 /// \throw api_error If there is any problem while rolling the transaction back.
138 sqlite::transaction::rollback(void)