2010-10-07 15:35:36 +02:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
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 12:36:48 +02:00
|
|
|
#include "QueryHolder.h"
|
2020-10-24 17:37:24 +02:00
|
|
|
#include "Errors.h"
|
2010-09-02 17:19:53 -06:00
|
|
|
#include "Log.h"
|
2020-10-24 17:37:24 +02:00
|
|
|
#include "MySQLConnection.h"
|
|
|
|
|
#include "PreparedStatement.h"
|
2017-05-12 18:49:51 +02:00
|
|
|
#include "QueryResult.h"
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2019-07-27 11:45:56 +02:00
|
|
|
bool SQLQueryHolderBase::SetPreparedQueryImpl(size_t index, PreparedStatementBase* stmt)
|
2010-09-20 18:20:40 +02:00
|
|
|
{
|
|
|
|
|
if (m_queries.size() <= index)
|
|
|
|
|
{
|
2023-01-08 21:16:53 +01:00
|
|
|
TC_LOG_ERROR("sql.sql", "Query index ({}) out of range (size: {}) for prepared statement", uint32(index), (uint32)m_queries.size());
|
2010-09-20 18:20:40 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
m_queries[index].first = stmt;
|
2010-09-20 18:20:40 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 17:37:24 +02:00
|
|
|
PreparedQueryResult SQLQueryHolderBase::GetPreparedResult(size_t index) const
|
2010-09-19 12:16:29 +02:00
|
|
|
{
|
|
|
|
|
// Don't call to this function if the index is of a prepared statement
|
2020-10-24 17:37:24 +02:00
|
|
|
ASSERT(index < m_queries.size(), "Query holder result index out of range, tried to access index " SZFMTD " but there are only " SZFMTD " results",
|
|
|
|
|
index, m_queries.size());
|
|
|
|
|
|
|
|
|
|
return m_queries[index].second;
|
2010-09-19 12:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-27 11:45:56 +02:00
|
|
|
void SQLQueryHolderBase::SetPreparedResult(size_t index, PreparedResultSet* result)
|
2010-09-19 12:16:29 +02:00
|
|
|
{
|
2012-08-26 21:24:15 +02:00
|
|
|
if (result && !result->GetRowCount())
|
|
|
|
|
{
|
|
|
|
|
delete result;
|
2020-08-14 17:06:03 +02:00
|
|
|
result = nullptr;
|
2012-08-26 21:24:15 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-19 12:16:29 +02:00
|
|
|
/// store the result in the holder
|
|
|
|
|
if (index < m_queries.size())
|
2017-05-12 18:49:51 +02:00
|
|
|
m_queries[index].second = PreparedQueryResult(result);
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2019-07-27 11:45:56 +02:00
|
|
|
SQLQueryHolderBase::~SQLQueryHolderBase()
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2020-02-29 13:20:05 +01:00
|
|
|
for (std::pair<PreparedStatementBase*, PreparedQueryResult>& query : m_queries)
|
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
|
2020-02-29 13:20:05 +01:00
|
|
|
delete query.first;
|
2008-10-02 16:23:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2019-07-27 11:45:56 +02:00
|
|
|
void SQLQueryHolderBase::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
|
|
|
|
2023-12-15 12:06:59 +01:00
|
|
|
bool SQLQueryHolderTask::Execute(MySQLConnection* conn, SQLQueryHolderBase* holder)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2017-05-12 18:49:51 +02:00
|
|
|
/// execute all queries in the holder and pass the results
|
2023-12-15 12:06:59 +01:00
|
|
|
for (size_t i = 0; i < holder->m_queries.size(); ++i)
|
|
|
|
|
if (PreparedStatementBase* stmt = holder->m_queries[i].first)
|
|
|
|
|
holder->SetPreparedResult(i, conn->Query(stmt));
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-08-18 02:25:52 +02:00
|
|
|
return true;
|
2010-08-18 07:17:04 +02:00
|
|
|
}
|
2020-07-31 23:27:26 +02:00
|
|
|
|
|
|
|
|
bool SQLQueryHolderCallback::InvokeIfReady()
|
|
|
|
|
{
|
|
|
|
|
if (m_future.valid() && m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
|
|
|
|
{
|
2020-10-24 17:37:24 +02:00
|
|
|
m_callback(*m_holder);
|
2020-07-31 23:27:26 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|