2009-02-04 10:33:18 -06:00
|
|
|
/*
|
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
|
2008-11-20 17:40:13 -06:00
|
|
|
* 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
|
2008-11-20 17:40:13 -06:00
|
|
|
* 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-21 19:40:05 +02:00
|
|
|
#include "Common.h"
|
2010-08-18 02:25:52 +02:00
|
|
|
#include "SQLOperation.h"
|
|
|
|
|
#include "MySQLConnection.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
|
|
|
/*! Basic, ad-hoc queries. */
|
2010-08-18 19:48:51 +02:00
|
|
|
BasicStatementTask::BasicStatementTask(const char* sql) :
|
|
|
|
|
m_has_result(false)
|
|
|
|
|
{
|
|
|
|
|
m_sql = strdup(sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BasicStatementTask::BasicStatementTask(const char* sql, QueryResultFuture result) :
|
2010-08-21 20:08:47 +02:00
|
|
|
m_has_result(true),
|
|
|
|
|
m_result(result)
|
2010-08-18 02:25:52 +02:00
|
|
|
{
|
|
|
|
|
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 19:48:51 +02:00
|
|
|
if (m_has_result)
|
|
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
ResultSet* result = m_conn->Query(m_sql);
|
|
|
|
|
if (!result || !result->GetRowCount())
|
|
|
|
|
{
|
|
|
|
|
m_result.set(QueryResult(NULL));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
result->NextRow();
|
|
|
|
|
m_result.set(QueryResult(result));
|
2010-08-18 19:48:51 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-18 02:25:52 +02:00
|
|
|
return m_conn->Execute(m_sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-08-31 11:39:51 +02: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);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-08-31 11:39:51 +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-03-09 15:45:34 +03: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-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:16:29 +02:00
|
|
|
// Don't call to this function if the index is of a prepared statement
|
2010-03-09 15:45:34 +03: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
|
2010-09-19 12:16:29 +02:00
|
|
|
if (SQLElementData* data = &m_queries[index].first)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-09-19 12:16:29 +02:00
|
|
|
free((void*)(const_cast<char*>(data->element.query)));
|
|
|
|
|
data->element.query = NULL;
|
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())
|
|
|
|
|
{
|
|
|
|
|
/// the query strings are freed on the first GetResult or in the destructor
|
|
|
|
|
if (SQLElementData* data = &m_queries[index].first)
|
|
|
|
|
{
|
|
|
|
|
delete data->element.stmt;
|
|
|
|
|
data->element.stmt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PreparedResultSet* result = m_queries[index].second.presult;
|
|
|
|
|
if (!result || !result->GetRowCount())
|
|
|
|
|
return PreparedQueryResult(NULL);
|
|
|
|
|
|
|
|
|
|
result->NextRow();
|
|
|
|
|
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:
|
|
|
|
|
{
|
|
|
|
|
char const *sql = data->element.query;
|
|
|
|
|
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
|
|
|
}
|