2010-10-07 15:35:36 +02:00
|
|
|
/*
|
2012-01-01 00:32:13 +01:00
|
|
|
* Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
|
2010-10-07 15:35:36 +02: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 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/>.
|
2008-10-02 16:23:55 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-09-19 17:53:45 +02:00
|
|
|
#include "MySQLConnection.h"
|
2010-09-19 12:36:48 +02:00
|
|
|
#include "QueryHolder.h"
|
|
|
|
|
#include "PreparedStatement.h"
|
2010-09-02 17:19:53 -06:00
|
|
|
#include "Log.h"
|
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
|
|
|
{
|
2010-03-09 15:45:34 +03:00
|
|
|
if (m_queries.size() <= index)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-12-23 23:25:44 +01:00
|
|
|
sLog->outError("Query index (%zu) 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
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
/// not executed yet, just stored (it's not called a holder for nothing)
|
2010-09-19 12:16:29 +02:00
|
|
|
SQLElementData element;
|
|
|
|
|
element.type = SQL_ELEMENT_RAW;
|
|
|
|
|
element.element.query = strdup(sql);
|
2010-09-25 22:03:57 +02:00
|
|
|
|
2010-09-19 12:16:29 +02:00
|
|
|
SQLResultSetUnion result;
|
|
|
|
|
result.qresult = NULL;
|
|
|
|
|
|
|
|
|
|
m_queries[index] = SQLResultPair(element, result);
|
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
|
|
|
{
|
2010-03-09 15:45:34 +03:00
|
|
|
if (!format)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2011-04-29 20:47:02 +02:00
|
|
|
sLog->outError("Query (index: %zu) is empty.", index);
|
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
|
|
|
va_list ap;
|
|
|
|
|
char szQuery [MAX_QUERY_LEN];
|
|
|
|
|
va_start(ap, format);
|
2010-03-09 15:45:34 +03:00
|
|
|
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
|
|
|
|
2010-09-19 12:36:48 +02:00
|
|
|
if (res == -1)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2011-04-29 20:47:02 +02:00
|
|
|
sLog->outError("SQL Query truncated (and not execute) for format: %s", format);
|
2008-10-02 16:23:55 -05:00
|
|
|
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-09-20 18:20:40 +02:00
|
|
|
bool SQLQueryHolder::SetPreparedQuery(size_t index, PreparedStatement* stmt)
|
|
|
|
|
{
|
|
|
|
|
if (m_queries.size() <= index)
|
|
|
|
|
{
|
2010-12-23 23:25:44 +01:00
|
|
|
sLog->outError("Query index (%zu) out of range (size: %u) for prepared statement", index, (uint32)m_queries.size());
|
2010-09-20 18:20:40 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// not executed yet, just stored (it's not called a holder for nothing)
|
|
|
|
|
SQLElementData element;
|
|
|
|
|
element.type = SQL_ELEMENT_PREPARED;
|
|
|
|
|
element.element.stmt = stmt;
|
2010-09-25 22:03:57 +02:00
|
|
|
|
2010-09-20 18:20:40 +02:00
|
|
|
SQLResultSetUnion result;
|
|
|
|
|
result.presult = NULL;
|
|
|
|
|
|
|
|
|
|
m_queries[index] = SQLResultPair(element, result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-11 21:10:54 +02:00
|
|
|
QueryResult SQLQueryHolder::GetResult(size_t index)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-09-19 12:36:48 +02:00
|
|
|
// Don't call to this function if the index is of an ad-hoc statement
|
2010-03-09 15:45:34 +03:00
|
|
|
if (index < m_queries.size())
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
ResultSet* result = m_queries[index].second.qresult;
|
|
|
|
|
if (!result || !result->GetRowCount())
|
|
|
|
|
return QueryResult(NULL);
|
|
|
|
|
|
|
|
|
|
result->NextRow();
|
|
|
|
|
return QueryResult(result);
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
|
|
|
|
else
|
2010-09-11 21:10:54 +02:00
|
|
|
return QueryResult(NULL);
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-09-19 12:16:29 +02:00
|
|
|
PreparedQueryResult SQLQueryHolder::GetPreparedResult(size_t index)
|
|
|
|
|
{
|
|
|
|
|
// Don't call to this function if the index is of a prepared statement
|
|
|
|
|
if (index < m_queries.size())
|
|
|
|
|
{
|
|
|
|
|
PreparedResultSet* result = m_queries[index].second.presult;
|
|
|
|
|
if (!result || !result->GetRowCount())
|
|
|
|
|
return PreparedQueryResult(NULL);
|
|
|
|
|
|
|
|
|
|
return PreparedQueryResult(result);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return PreparedQueryResult(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SQLQueryHolder::SetResult(size_t index, ResultSet* result)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
|
|
|
|
/// store the result in the holder
|
2010-03-09 15:45:34 +03:00
|
|
|
if (index < m_queries.size())
|
2010-09-19 12:16:29 +02:00
|
|
|
m_queries[index].second.qresult = result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SQLQueryHolder::SetPreparedResult(size_t index, PreparedResultSet* result)
|
|
|
|
|
{
|
|
|
|
|
/// store the result in the holder
|
|
|
|
|
if (index < m_queries.size())
|
|
|
|
|
m_queries[index].second.presult = result;
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
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
|
2010-09-19 12:16:29 +02:00
|
|
|
if (SQLElementData* data = &m_queries[i].first)
|
|
|
|
|
{
|
|
|
|
|
switch (data->type)
|
|
|
|
|
{
|
|
|
|
|
case SQL_ELEMENT_RAW:
|
|
|
|
|
free((void*)(const_cast<char*>(data->element.query)));
|
|
|
|
|
break;
|
|
|
|
|
case SQL_ELEMENT_PREPARED:
|
|
|
|
|
delete data->element.stmt;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
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::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
|
|
|
{
|
2010-08-18 19:48:51 +02:00
|
|
|
if (!m_holder)
|
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
|
2010-09-19 12:16:29 +02:00
|
|
|
if (SQLElementData* data = &queries[i].first)
|
|
|
|
|
{
|
|
|
|
|
switch (data->type)
|
|
|
|
|
{
|
|
|
|
|
case SQL_ELEMENT_RAW:
|
|
|
|
|
{
|
2011-09-15 14:08:17 +02:00
|
|
|
char const* sql = data->element.query;
|
2010-09-19 12:16:29 +02:00
|
|
|
if (sql)
|
|
|
|
|
m_holder->SetResult(i, m_conn->Query(sql));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SQL_ELEMENT_PREPARED:
|
|
|
|
|
{
|
|
|
|
|
PreparedStatement* stmt = data->element.stmt;
|
|
|
|
|
if (stmt)
|
|
|
|
|
m_holder->SetPreparedResult(i, m_conn->Query(stmt));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-08-18 19:48:51 +02:00
|
|
|
m_result.set(m_holder);
|
2010-08-18 02:25:52 +02:00
|
|
|
return true;
|
2010-08-18 07:17:04 +02:00
|
|
|
}
|