2010-10-07 15:35:36 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008-2010 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
|
|
|
*/
|
|
|
|
|
|
2010-08-21 08:08:49 +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(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(data);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-21 03:19:25 +02:00
|
|
|
void Transaction::Cleanup()
|
|
|
|
|
{
|
|
|
|
|
while (!m_queries.empty())
|
|
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
SQLElementData 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;
|
|
|
|
|
}
|
2010-08-21 03:19:25 +02:00
|
|
|
m_queries.pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TransactionTask::Execute()
|
|
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
std::queue<SQLElementData> &queries = m_trans->m_queries;
|
2010-08-21 03:19:25 +02:00
|
|
|
if (queries.empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_conn->BeginTransaction();
|
|
|
|
|
while (!queries.empty())
|
|
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
SQLElementData data = queries.front();
|
2010-09-04 16:49:23 +02:00
|
|
|
switch (data.type)
|
2010-08-21 03:19:25 +02:00
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
case SQL_ELEMENT_PREPARED:
|
2010-09-04 16:49:23 +02:00
|
|
|
{
|
|
|
|
|
PreparedStatement* stmt = data.element.stmt;
|
|
|
|
|
ASSERT(stmt);
|
|
|
|
|
if (!m_conn->Execute(stmt))
|
|
|
|
|
{
|
2010-12-23 23:25:44 +01:00
|
|
|
sLog->outSQLDriver("[Warning] Transaction aborted. %u queries not executed.", (uint32)queries.size());
|
2010-09-04 16:49:23 +02:00
|
|
|
m_conn->RollbackTransaction();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
const char* sql = data.element.query;
|
2010-09-18 15:34:01 +02:00
|
|
|
ASSERT(sql);
|
2010-09-04 16:49:23 +02:00
|
|
|
if (!m_conn->Execute(sql))
|
|
|
|
|
{
|
2010-12-23 23:25:44 +01:00
|
|
|
sLog->outSQLDriver("[Warning] Transaction aborted. %u queries not executed.", (uint32)queries.size());
|
2010-09-04 16:49:23 +02:00
|
|
|
m_conn->RollbackTransaction();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
free((void*)const_cast<char*>(sql));
|
|
|
|
|
}
|
|
|
|
|
break;
|
2010-08-21 03:19:25 +02:00
|
|
|
}
|
|
|
|
|
queries.pop();
|
|
|
|
|
}
|
|
|
|
|
m_conn->CommitTransaction();
|
|
|
|
|
return true;
|
|
|
|
|
}
|