TrinityCore
Loading...
Searching...
No Matches
QueryHolder.h
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#ifndef _QUERYHOLDER_H
19#define _QUERYHOLDER_H
20
21#include "SQLOperation.h"
22#include <vector>
23
25{
26 friend class SQLQueryHolderTask;
27 private:
28 std::vector<std::pair<PreparedStatementBase*, PreparedQueryResult>> m_queries;
29 public:
30 SQLQueryHolderBase() = default;
31 virtual ~SQLQueryHolderBase();
32 void SetSize(size_t size);
33 PreparedQueryResult GetPreparedResult(size_t index) const;
34 void SetPreparedResult(size_t index, PreparedResultSet* result);
35
36 protected:
37 bool SetPreparedQueryImpl(size_t index, PreparedStatementBase* stmt);
38};
39
40template<typename T>
42{
43public:
44 bool SetPreparedQuery(size_t index, PreparedStatement<T>* stmt)
45 {
46 return SetPreparedQueryImpl(index, stmt);
47 }
48};
49
51{
52 private:
53 std::shared_ptr<SQLQueryHolderBase> m_holder;
55
56 public:
57 explicit SQLQueryHolderTask(std::shared_ptr<SQLQueryHolderBase> holder)
58 : m_holder(std::move(holder)) { }
59
61
62 bool Execute() override;
63 QueryResultHolderFuture GetFuture() { return m_result.get_future(); }
64};
65
67{
68public:
69 SQLQueryHolderCallback(std::shared_ptr<SQLQueryHolderBase>&& holder, QueryResultHolderFuture&& future)
70 : m_holder(std::move(holder)), m_future(std::move(future)) { }
71
73
75
76 void AfterComplete(std::function<void(SQLQueryHolderBase const&)> callback) &
77 {
78 m_callback = std::move(callback);
79 }
80
81 bool InvokeIfReady();
82
83 std::shared_ptr<SQLQueryHolderBase> m_holder;
85 std::function<void(SQLQueryHolderBase const&)> m_callback;
86};
87
88inline bool InvokeAsyncCallbackIfReady(SQLQueryHolderCallback& callback) { return callback.InvokeIfReady(); }
89
90#endif
std::shared_ptr< PreparedResultSet > PreparedQueryResult
std::promise< void > QueryResultHolderPromise
std::future< void > QueryResultHolderFuture
#define TC_DATABASE_API
Definition Define.h:102
bool InvokeAsyncCallbackIfReady(SQLQueryHolderCallback &callback)
Definition QueryHolder.h:88
SQLQueryHolderBase()=default
std::vector< std::pair< PreparedStatementBase *, PreparedQueryResult > > m_queries
Definition QueryHolder.h:28
bool SetPreparedQueryImpl(size_t index, PreparedStatementBase *stmt)
SQLQueryHolderCallback & operator=(SQLQueryHolderCallback &&)=default
std::shared_ptr< SQLQueryHolderBase > m_holder
Definition QueryHolder.h:83
void AfterComplete(std::function< void(SQLQueryHolderBase const &)> callback) &
Definition QueryHolder.h:76
QueryResultHolderFuture m_future
Definition QueryHolder.h:84
SQLQueryHolderCallback(SQLQueryHolderCallback &&)=default
std::function< void(SQLQueryHolderBase const &)> m_callback
Definition QueryHolder.h:85
SQLQueryHolderCallback(std::shared_ptr< SQLQueryHolderBase > &&holder, QueryResultHolderFuture &&future)
Definition QueryHolder.h:69
SQLQueryHolderTask(std::shared_ptr< SQLQueryHolderBase > holder)
Definition QueryHolder.h:57
std::shared_ptr< SQLQueryHolderBase > m_holder
Definition QueryHolder.h:53
QueryResultHolderFuture GetFuture()
Definition QueryHolder.h:63
QueryResultHolderPromise m_result
Definition QueryHolder.h:54
bool SetPreparedQuery(size_t index, PreparedStatement< T > *stmt)
Definition QueryHolder.h:44
STL namespace.