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

214 lines
5.4 KiB
C++
Raw Normal View History

/*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-10-02 16:23:55 -05:00
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
#include "SQLOperation.h"
#include "MySQLConnection.h"
#include "Log.h"
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
/*! Basic, ad-hoc queries. */
BasicStatementTask::BasicStatementTask(const char* sql)
{
m_sql = strdup(sql);
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
BasicStatementTask::~BasicStatementTask()
2008-10-02 16:23:55 -05:00
{
2010-08-18 02:25:52 +02:00
free((void*)m_sql);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
bool BasicStatementTask::Execute()
2008-10-02 16:23:55 -05:00
{
2010-08-18 02:25:52 +02:00
return m_conn->Execute(m_sql);
}
/*! Transactions. */
TransactionTask::TransactionTask()
{
}
TransactionTask::~TransactionTask()
{
}
2010-08-18 02:25:52 +02:00
void TransactionTask::ForcefulDelete()
{
while (!m_queries.empty())
{
2010-08-18 02:25:52 +02:00
free((void*)const_cast<char*>(m_queries.front()));
m_queries.pop();
}
2010-08-18 02:25:52 +02:00
}
2010-08-18 02:25:52 +02:00
bool TransactionTask::Execute()
{
if (m_queries.empty())
return false;
const char* sql;
2010-08-18 02:25:52 +02:00
m_conn->BeginTransaction();
while (!m_queries.empty())
{
sql = m_queries.front();
if (!m_conn->Execute(sql))
2008-10-02 16:23:55 -05:00
{
free((void*)const_cast<char*>(sql));
2010-08-18 02:25:52 +02:00
m_queries.pop();
m_conn->RollbackTransaction();
ForcefulDelete();
return false;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
free((void*)const_cast<char*>(sql));
2010-08-18 02:25:52 +02:00
m_queries.pop();
2008-10-02 16:23:55 -05:00
}
2010-08-18 02:25:52 +02:00
m_conn->CommitTransaction();
return true;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
/*! Callback statements/holders */
void SQLResultQueue::Update()
2008-10-02 16:23:55 -05:00
{
/// execute the callbacks waiting in the synchronization queue
Trinity::IQueryCallback* callback;
while (next(callback))
2008-10-02 16:23:55 -05:00
{
callback->Execute();
delete callback;
}
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
bool SQLQueryHolder::SetQuery(size_t index, const char *sql)
2008-10-02 16:23:55 -05:00
{
if (m_queries.size() <= index)
2008-10-02 16:23:55 -05:00
{
2010-08-18 02:25:52 +02:00
sLog.outError("Query index (%u) out of range (size: %u) for query: %s", index, (uint32)m_queries.size(), sql);
2008-10-02 16:23:55 -05:00
return false;
}
2009-10-17 15:51:44 -07:00
if (m_queries[index].first != NULL)
2008-10-02 16:23:55 -05:00
{
2008-11-14 17:03:03 -06:00
sLog.outError("Attempt assign query to holder index (%u) where other query stored (Old: [%s] New: [%s])",
2010-08-18 02:25:52 +02:00
index, m_queries[index].first, sql);
2008-10-02 16:23:55 -05:00
return false;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/// not executed yet, just stored (it's not called a holder for nothing)
2010-08-18 02:25:52 +02:00
m_queries[index] = SQLResultPair(strdup(sql), QueryResult_AutoPtr(NULL));
2008-10-02 16:23:55 -05:00
return true;
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
bool SQLQueryHolder::SetPQuery(size_t index, const char *format, ...)
2008-10-02 16:23:55 -05:00
{
if (!format)
2008-10-02 16:23:55 -05:00
{
sLog.outError("Query (index: %u) is empty.",index);
return false;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
va_list ap;
char szQuery [MAX_QUERY_LEN];
va_start(ap, format);
int res = vsnprintf(szQuery, MAX_QUERY_LEN, format, ap);
2008-10-02 16:23:55 -05:00
va_end(ap);
2009-10-17 15:51:44 -07:00
if (res==-1)
2008-10-02 16:23:55 -05:00
{
sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
return false;
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
return SetQuery(index, szQuery);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
QueryResult_AutoPtr SQLQueryHolder::GetResult(size_t index)
2008-10-02 16:23:55 -05:00
{
if (index < m_queries.size())
2008-10-02 16:23:55 -05:00
{
/// the query strings are freed on the first GetResult or in the destructor
if (m_queries[index].first != NULL)
2008-10-02 16:23:55 -05:00
{
free((void*)(const_cast<char*>(m_queries[index].first)));
m_queries[index].first = NULL;
}
/// when you get a result aways remember to delete it!
return m_queries[index].second;
}
else
return QueryResult_AutoPtr(NULL);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
void SQLQueryHolder::SetResult(size_t index, QueryResult_AutoPtr result)
2008-10-02 16:23:55 -05:00
{
/// store the result in the holder
if (index < m_queries.size())
2008-10-02 16:23:55 -05:00
m_queries[index].second = result;
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
SQLQueryHolder::~SQLQueryHolder()
2008-10-02 16:23:55 -05:00
{
2009-10-17 16:20:24 -07:00
for (size_t i = 0; i < m_queries.size(); i++)
2008-10-02 16:23:55 -05:00
{
/// if the result was never used, free the resources
/// results used already (getresult called) are expected to be deleted
if (m_queries[i].first != NULL)
2008-10-02 16:23:55 -05:00
free((void*)(const_cast<char*>(m_queries[i].first)));
}
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
void SQLQueryHolder::SetSize(size_t size)
2008-10-02 16:23:55 -05:00
{
/// to optimize push_back, reserve the number of queries about to be executed
m_queries.resize(size);
}
2009-10-17 15:51:44 -07:00
2010-08-18 02:25:52 +02:00
bool SQLQueryHolderTask::Execute()
2008-10-02 16:23:55 -05:00
{
if (!m_holder || !m_callback || !m_queue)
2010-08-18 02:25:52 +02:00
return false;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/// we can do this, we are friends
2010-08-18 02:25:52 +02:00
std::vector<SQLQueryHolder::SQLResultPair> &queries = m_holder->m_queries;
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for (size_t i = 0; i < queries.size(); i++)
2008-10-02 16:23:55 -05:00
{
/// execute all queries in the holder and pass the results
char const *sql = queries[i].first;
2010-08-18 02:25:52 +02:00
if (sql)
m_holder->SetResult(i, m_conn->Query(sql));
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/// sync with the caller thread
m_queue->add(m_callback);
2010-08-18 02:25:52 +02:00
return true;
2008-10-02 16:23:55 -05:00
}
2010-08-18 02:25:52 +02:00
bool SQLQueryTask::Execute()
{
if (!m_callback || !m_queue)
return false;
/// execute the query and store the result in the callback
m_callback->SetResult(m_conn->Query(m_sql));
/// add the callback to the sql result queue of the thread it originated from
m_queue->add(m_callback);
return true;
}