Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
Transaction.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "DatabaseEnv.h"
7#include "Transaction.h"
8#include "CompilerDefs.h"
9
10//- Append a raw ad-hoc query to the transaction
11void Transaction::Append(const char* sql)
12{
13 SQLElementData data;
14 data.type = SQL_ELEMENT_RAW;
15 data.element.query = strdup(sql);
16 m_queries.push_back(data);
17}
18
19void Transaction::PAppend(const char* sql, ...)
20{
21 va_list ap;
22 char szQuery[MAX_QUERY_LEN];
23 va_start(ap, sql);
24 vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
25 va_end(ap);
26
27 Append(szQuery);
28}
29
30//- Append a prepared statement to the transaction
32{
33 SQLElementData data;
35 data.element.stmt = stmt;
36 m_queries.push_back(data);
37}
38
40{
41 // This might be called by explicit calls to Cleanup or by the auto-destructor
42 if (_cleanedUp)
43 return;
44
45 while (!m_queries.empty())
46 {
47 SQLElementData const& data = m_queries.front();
48 switch (data.type)
49 {
51 delete data.element.stmt;
52 break;
53 case SQL_ELEMENT_RAW:
54 free((void*)(data.element.query));
55 break;
56 }
57
58 m_queries.pop_front();
59 }
60
61 _cleanedUp = true;
62}
63
65{
66 if (m_conn->ExecuteTransaction(m_trans))
67 return true;
68
69 if (m_conn->GetLastError() == 1213)
70 {
71 uint8 loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself
72 for (uint8 i = 0; i < loopBreaker; ++i)
73 if (m_conn->ExecuteTransaction(m_trans))
74 return true;
75 }
76
77 // Clean up now.
78 m_trans->Cleanup();
79
80 return false;
81}
#define vsnprintf
Definition Common.h:100
#define MAX_QUERY_LEN
Definition Common.h:195
std::uint8_t uint8
Definition Define.h:79
@ SQL_ELEMENT_RAW
@ SQL_ELEMENT_PREPARED
MySQLConnection * m_conn
std::list< SQLElementData > m_queries
Definition Transaction.h:36
void PAppend(const char *sql,...)
void Append(PreparedStatement *statement)
bool _cleanedUp
Definition Transaction.h:39
SQLTransaction m_trans
Definition Transaction.h:57
SQLElementUnion element
SQLElementDataType type
const char * query
PreparedStatement * stmt