summaryrefslogtreecommitdiffstats
path: root/src/djinterop/enginelibrary/el_transaction_guard_impl.cpp
blob: 0b214d1a86152c9852bb26d777a89066f06aac7e (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
/*
    This file is part of libdjinterop.

    libdjinterop is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libdjinterop is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with libdjinterop.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <djinterop/enginelibrary/el_storage.hpp>
#include <djinterop/enginelibrary/el_transaction_guard_impl.hpp>

namespace djinterop
{
namespace enginelibrary
{
el_transaction_guard_impl::el_transaction_guard_impl(
    std::shared_ptr<el_storage> storage)
    : storage_{std::move(storage)}, savepoint_{++storage_->last_savepoint}
{
    // TODO (haslersn): Should el_storage::last_savepoint be atomic such that
    // this is thread-safe?
    storage_->db << ("SAVEPOINT s" + std::to_string(savepoint_));
}

el_transaction_guard_impl::~el_transaction_guard_impl()
{
    if (savepoint_ != 0)
    {
        try
        {
            storage_->db << ("ROLLBACK TO s" + std::to_string(savepoint_));
        }
        catch (...)
        {
            // The exception is intentionally swallowed. An exception could for
            // example arise if SQLite performed an automatic rollback, causing
            // the explicit rollback to return an error. Such an error does no
            // harm, so we swallow it.
            //
            // TODO (haslersn): We could still issue a warning
        }
    }
}

void el_transaction_guard_impl::commit()
{
    auto savepoint = savepoint_;
    savepoint_ = 0;
    storage_->db << ("RELEASE s" + std::to_string(savepoint));
}

}  // namespace enginelibrary
}  // namespace djinterop