Leosac  0.8.0
Open Source Access Control
OptionalTransaction.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 "OptionalTransaction.hpp"
21 #include <tools/log.hpp>
22 
23 using namespace Leosac;
24 using namespace Leosac::db;
25 
26 
27 OptionalTransaction::OptionalTransaction(odb::transaction_impl *impl)
28 {
29  if (!odb::transaction::has_current())
30  transaction_ = std::make_unique<odb::transaction>(impl);
31 }
32 
34 {
35  transaction_.reset();
36 }
37 
39 {
40  if (transaction_)
41  transaction_->commit();
42 }
43 
44 odb::transaction *OptionalTransaction::get()
45 {
46  if (transaction_)
47  return transaction_.get();
48  ASSERT_LOG(odb::transaction::has_current(), "No current transaction.");
49  return &odb::transaction::current();
50 }
Leosac::db::OptionalTransaction::commit
void commit()
Commit the transaction, if there was no currently active transaction at the time of this object's cre...
Definition: OptionalTransaction.cpp:38
Leosac::db::OptionalTransaction::get
odb::transaction * get()
Retrieve the odb transaction object.
Definition: OptionalTransaction.cpp:44
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::db::OptionalTransaction::OptionalTransaction
OptionalTransaction(odb::transaction_impl *impl)
Definition: OptionalTransaction.cpp:27
Leosac::db::OptionalTransaction::transaction_
std::unique_ptr< odb::transaction > transaction_
Definition: OptionalTransaction.hpp:61
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Leosac::db::OptionalTransaction::~OptionalTransaction
~OptionalTransaction()
Definition: OptionalTransaction.cpp:33
Leosac::db
Definition: DatabaseTracer.hpp:28
log.hpp