Leosac  0.8.0
Open Source Access Control
Savepoint.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "Savepoint.hpp"
21 #include "tools/GenGuid.h"
22 #include <boost/algorithm/string.hpp>
23 #include <spdlog/fmt/fmt.h>
24 
25 namespace Leosac
26 {
27 namespace db
28 {
29 
33 static std::string fixup_uuid(const std::string &uuid)
34 {
35  return boost::replace_all_copy(uuid, "-", "_");
36 }
37 
38 Savepoint::Savepoint(odb::database &db)
39  : Savepoint(db, fixup_uuid(gen_uuid()))
40 {
41 }
42 
43 Savepoint::Savepoint(odb::database &db, const std::string &name)
44  : db_(db)
45  , name_(name)
46 {
47  db_.execute(fmt::format("SAVEPOINT {}", name_));
48 }
49 
51 {
52  release();
53 }
54 
56 {
57  if (released_)
58  return;
59 
60  db_.execute(fmt::format("RELEASE SAVEPOINT {}", name_));
61  released_ = true;
62 }
63 
65 {
66  if (released_)
67  return false;
68 
69  db_.execute(fmt::format("ROLLBACK TO SAVEPOINT {}", name_));
70  return true;
71 }
72 }
73 }
Leosac::db::Savepoint::Savepoint
Savepoint(odb::database &db)
Construct a Savepoint with a random name.
Definition: Savepoint.cpp:38
Leosac::db::Savepoint::rollback_to
bool rollback_to()
Rollback to the Savepoint, unless the Savepoint was already released.
Definition: Savepoint.cpp:64
Leosac::db::Savepoint::released_
bool released_
Definition: Savepoint.hpp:75
Leosac::db::Savepoint
Provide a lightweight abstraction around the SAVEPOINT SQL concept.
Definition: Savepoint.hpp:40
Savepoint.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::db::Savepoint::db_
odb::database & db_
Definition: Savepoint.hpp:74
GenGuid.h
Leosac::gen_uuid
std::string gen_uuid()
Generate a new UUID.
Definition: GenGuid.cpp:26
Leosac::Colorize::detail::format
std::string format(const std::string &escape_code, const T &in)
Return a string containing the escape code, a string representation of T and the clear escape string.
Definition: Colorize.hpp:49
Leosac::db::Savepoint::~Savepoint
~Savepoint()
Definition: Savepoint.cpp:50
Leosac::db::Savepoint::name_
std::string name_
Definition: Savepoint.hpp:76
Leosac::db::Savepoint::release
void release()
Release the Savepoint if it wasn't released already, otherwise does nothing.
Definition: Savepoint.cpp:55