Leosac  0.7.0
OpenSourceAccessControl
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/details/format.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 }
This is the header file for a generated source file, GitSHA1.cpp.
void release()
Release the Savepoint if it wasn&#39;t released already, otherwise does nothing.
Definition: Savepoint.cpp:55
bool rollback_to()
Rollback to the Savepoint, unless the Savepoint was already released.
Definition: Savepoint.cpp:64
odb::database & db_
Definition: Savepoint.hpp:74
Provide a lightweight abstraction around the SAVEPOINT SQL concept.
Definition: Savepoint.hpp:40
std::string gen_uuid()
Generate a new UUID.
Definition: GenGuid.cpp:26
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
Savepoint(odb::database &db)
Construct a Savepoint with a random name.
Definition: Savepoint.cpp:38