The Battle for Wesnoth  1.15.3+dev
transaction.cpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright Sylvain Rochette Langlois 2013,
5 // Frantisek Boranek 2015,
6 // The ViaDuck Project 2016 - 2018.
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 
11 #include <mysql.h>
12 #include <mariadb++/connection.hpp>
14 
15 using namespace mariadb;
16 
17 namespace {
18 const char* g_isolation_level[] = {
19  "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;",
20  "SET TRANSACTION ISOLATION LEVEL READ COMMITTED;",
21  "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;",
22  "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;",
23 };
24 
25 const char* g_consistent_snapshot[] = {
26  "START TRANSACTION;", "START TRANSACTION WITH CONSISTENT SNAPSHOT;",
27 };
28 }
29 
30 transaction::transaction(connection* conn, isolation::level level, bool consistent_snapshot)
31  : m_connection(conn) {
32  conn->execute(g_isolation_level[level]);
33  conn->execute(g_consistent_snapshot[consistent_snapshot]);
34 }
35 
37  if (!m_connection) return;
38 
39  mysql_rollback(m_connection->m_mysql);
40  cleanup();
41 }
42 
45 
46  m_save_points.clear();
47 }
48 
50  if (!m_connection) return;
51 
52  mysql_commit(m_connection->m_mysql);
53  cleanup();
54  m_connection = nullptr;
55 }
56 
58  if (!m_connection) return save_point_ref();
59 
60  save_point* sp = new save_point(this);
61  m_save_points.push_back(sp);
62  return save_point_ref(sp);
63 }
64 
66  m_save_points.erase(std::remove(m_save_points.begin(), m_save_points.end(), sv_point));
67 }
void remove()
Removes a tip.
Definition: tooltip.cpp:189
connection * m_connection
Definition: transaction.hpp:73
std::vector< save_point * > m_save_points
Definition: transaction.hpp:75
transaction * m_transaction
Definition: save_point.hpp:45
save_point_ref create_save_point()
Create named savepoint Note: only valid until the transaction is destroyed or committed.
Definition: transaction.cpp:57
Class used to represent a MariaDB savepoint having automatic rollback functionality.
Definition: save_point.hpp:22
void remove_save_point(save_point *sv_point)
Removes a savepoint from the list of savepoints.
Definition: transaction.cpp:65
transaction(connection *conn, isolation::level level, bool consistent_snapshot)
Create a transaction with given isolation level and snapshot setting.
Definition: transaction.cpp:30
Wraps a Database connection.
Definition: connection.hpp:27
std::shared_ptr< save_point > save_point_ref
Definition: save_point.hpp:50
void cleanup()
Cleans up the transaction, releases all savepoints.
Definition: transaction.cpp:43
u64 execute(const std::string &query)
Execute a query without interest in a result.
Definition: connection.cpp:150
friend class save_point
Definition: transaction.hpp:27
void commit()
Commits the changes, releases all savepoints.
Definition: transaction.cpp:49
virtual ~transaction()
Destructor initiates automatic rollback if changes were not committed.
Definition: transaction.cpp:36