1 // Copyright 2011 The Kyua Authors.
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/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"
39 namespace sqlite = utils::sqlite;
42 /// Internal implementation for the transaction.
43 struct utils::sqlite::transaction::impl : utils::noncopyable {
44 /// The database this transaction belongs to.
47 /// Possible statuses of a transaction.
54 /// The current status of the transaction.
57 /// Constructs a new transaction.
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_) :
67 /// Destroys the transaction.
69 /// This rolls back the transaction if it is open.
72 if (status == impl::open_status) {
75 } catch (const sqlite::error& e) {
76 LW(F("Error while rolling back a transaction: %s") % e.what());
81 /// Commits the transaction.
83 /// \throw api_error If there is any problem while committing the
88 PRE(status == impl::open_status);
90 status = impl::committed_status;
93 /// Rolls the transaction back.
95 /// \throw api_error If there is any problem while rolling the
100 PRE(status == impl::open_status);
102 status = impl::rolled_back_status;
107 /// Initializes a transaction object.
109 /// This is an internal function. Use database::begin_transaction() to
110 /// instantiate one of these objects.
112 /// \param db The database this transaction belongs to.
113 sqlite::transaction::transaction(database& db) :
114 _pimpl(new impl(db, impl::open_status))
119 /// Destructor for the transaction.
120 sqlite::transaction::~transaction(void)
125 /// Commits the transaction.
127 /// \throw api_error If there is any problem while committing the transaction.
129 sqlite::transaction::commit(void)
135 /// Rolls the transaction back.
137 /// \throw api_error If there is any problem while rolling the transaction back.
139 sqlite::transaction::rollback(void)