TrinityCore
QueryHolder.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "QueryHolder.h"
19#include "Errors.h"
20#include "Log.h"
21#include "MySQLConnection.h"
22#include "PreparedStatement.h"
23#include "QueryResult.h"
24
26{
27 if (m_queries.size() <= index)
28 {
29 TC_LOG_ERROR("sql.sql", "Query index ({}) out of range (size: {}) for prepared statement", uint32(index), (uint32)m_queries.size());
30 return false;
31 }
32
33 m_queries[index].first = stmt;
34 return true;
35}
36
38{
39 // Don't call to this function if the index is of a prepared statement
40 ASSERT(index < m_queries.size(), "Query holder result index out of range, tried to access index " SZFMTD " but there are only " SZFMTD " results",
41 index, m_queries.size());
42
43 return m_queries[index].second;
44}
45
47{
48 if (result && !result->GetRowCount())
49 {
50 delete result;
51 result = nullptr;
52 }
53
55 if (index < m_queries.size())
56 m_queries[index].second = PreparedQueryResult(result);
57}
58
60{
61 for (std::pair<PreparedStatementBase*, PreparedQueryResult>& query : m_queries)
62 {
65 delete query.first;
66 }
67}
68
70{
72 m_queries.resize(size);
73}
74
76{
78 for (size_t i = 0; i < holder->m_queries.size(); ++i)
79 if (PreparedStatementBase* stmt = holder->m_queries[i].first)
80 holder->SetPreparedResult(i, conn->Query(stmt));
81
82 return true;
83}
84
86{
87 if (m_future.valid() && m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
88 {
90 return true;
91 }
92
93 return false;
94}
std::shared_ptr< PreparedResultSet > PreparedQueryResult
uint32_t uint32
Definition: Define.h:142
#define SZFMTD
Definition: Define.h:132
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
ResultSet * Query(char const *sql)
uint64 GetRowCount() const
Definition: QueryResult.h:60
void SetSize(size_t size)
Definition: QueryHolder.cpp:69
std::vector< std::pair< PreparedStatementBase *, PreparedQueryResult > > m_queries
Definition: QueryHolder.h:31
PreparedQueryResult GetPreparedResult(size_t index) const
Definition: QueryHolder.cpp:37
bool SetPreparedQueryImpl(size_t index, PreparedStatementBase *stmt)
Definition: QueryHolder.cpp:25
virtual ~SQLQueryHolderBase()
Definition: QueryHolder.cpp:59
void SetPreparedResult(size_t index, PreparedResultSet *result)
Definition: QueryHolder.cpp:46
std::shared_ptr< SQLQueryHolderBase > m_holder
Definition: QueryHolder.h:76
QueryResultHolderFuture m_future
Definition: QueryHolder.h:77
std::function< void(SQLQueryHolderBase const &)> m_callback
Definition: QueryHolder.h:78
static bool Execute(MySQLConnection *conn, SQLQueryHolderBase *holder)
Definition: QueryHolder.cpp:75
constexpr std::size_t size()
Definition: UpdateField.h:796