Files
TCPlayerbotCore/src/server/shared/Database/Transaction.cpp
T

93 lines
2.4 KiB
C++
Raw Normal View History

/*
2015-01-01 00:27:46 +01:00
* Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2010-08-21 03:19:25 +02:00
*/
#include "DatabaseEnv.h"
2010-08-21 03:19:25 +02:00
#include "Transaction.h"
2010-09-04 16:49:23 +02:00
//- Append a raw ad-hoc query to the transaction
2010-08-21 03:19:25 +02:00
void Transaction::Append(const char* sql)
{
2010-09-19 12:16:29 +02:00
SQLElementData data;
data.type = SQL_ELEMENT_RAW;
2010-09-04 16:49:23 +02:00
data.element.query = strdup(sql);
m_queries.push_back(data);
2010-08-21 03:19:25 +02:00
}
void Transaction::PAppend(const char* sql, ...)
{
va_list ap;
char szQuery [MAX_QUERY_LEN];
va_start(ap, sql);
2010-08-21 20:08:47 +02:00
vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
2010-08-21 03:19:25 +02:00
va_end(ap);
Append(szQuery);
}
2010-09-04 16:49:23 +02:00
//- Append a prepared statement to the transaction
void Transaction::Append(PreparedStatement* stmt)
{
2010-09-19 12:16:29 +02:00
SQLElementData data;
data.type = SQL_ELEMENT_PREPARED;
2010-09-04 16:49:23 +02:00
data.element.stmt = stmt;
m_queries.push_back(data);
2010-09-04 16:49:23 +02:00
}
2010-08-21 03:19:25 +02:00
void Transaction::Cleanup()
{
// This might be called by explicit calls to Cleanup or by the auto-destructor
2011-06-18 19:51:28 +02:00
if (_cleanedUp)
return;
2010-08-21 03:19:25 +02:00
while (!m_queries.empty())
{
SQLElementData const &data = m_queries.front();
2010-09-04 16:49:23 +02:00
switch (data.type)
{
2010-09-19 12:16:29 +02:00
case SQL_ELEMENT_PREPARED:
2010-09-04 16:49:23 +02:00
delete data.element.stmt;
break;
2010-09-19 12:16:29 +02:00
case SQL_ELEMENT_RAW:
2010-09-04 16:49:23 +02:00
free((void*)(data.element.query));
break;
}
m_queries.pop_front();
2010-08-21 03:19:25 +02:00
}
_cleanedUp = true;
2010-08-21 03:19:25 +02:00
}
bool TransactionTask::Execute()
{
2011-01-13 20:07:09 +01:00
if (m_conn->ExecuteTransaction(m_trans))
return true;
2010-08-21 03:19:25 +02:00
2011-01-13 20:07:09 +01:00
if (m_conn->GetLastError() == 1213)
2010-08-21 03:19:25 +02:00
{
2011-01-13 20:07:09 +01:00
uint8 loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself
for (uint8 i = 0; i < loopBreaker; ++i)
if (m_conn->ExecuteTransaction(m_trans))
return true;
2010-08-21 03:19:25 +02:00
}
2011-01-13 20:07:09 +01:00
// Clean up now.
m_trans->Cleanup();
2011-01-13 20:07:09 +01:00
return false;
2010-08-21 03:19:25 +02:00
}