Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
xact.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <stack>
7
8extern "C" {
9#include <access/xact.h>
10}
11
12namespace cppgres {
13
15 internal_subtransaction(bool commit = true)
16 : owner(::CurrentResourceOwner), commit(commit), name("") {
17 if (txns.empty()) {
18 ffi_guarded(::BeginInternalSubTransaction)(nullptr);
19 txns.push(this);
20 } else {
21 throw std::runtime_error("internal subtransaction already started");
22 }
23 }
24
25 internal_subtransaction(std::string_view name, bool commit = true)
26 : owner(::CurrentResourceOwner), commit(commit), name(name) {
27 if (txns.empty()) {
28 ffi_guarded(::BeginInternalSubTransaction)(this->name.c_str());
29 txns.push(this);
30 } else {
31 throw std::runtime_error("internal subtransaction already started");
32 }
33 }
34
36 txns.pop();
37 if (commit) {
38 ffi_guarded(::ReleaseCurrentSubTransaction)();
39 } else {
40 ffi_guarded(::RollbackAndReleaseCurrentSubTransaction)();
41 }
42 ::CurrentResourceOwner = owner;
43 }
44
45private:
46 ::ResourceOwner owner;
47 bool commit;
48 std::string name;
49 static inline std::stack<internal_subtransaction *> txns;
50};
51
52} // namespace cppgres
Definition: xact.hpp:14