Core/DBLayer: Add nicer api for SQLQueryHolders

(cherry picked from commit 9b806c6b5d)
This commit is contained in:
Shauren
2020-07-31 23:27:26 +02:00
parent 61bf51874d
commit d5dcf02196
8 changed files with 76 additions and 25 deletions

View File

@@ -86,6 +86,8 @@ using HotfixDatabaseQueryHolder = SQLQueryHolder<HotfixDatabaseConnection>;
using LoginDatabaseQueryHolder = SQLQueryHolder<LoginDatabaseConnection>;
using WorldDatabaseQueryHolder = SQLQueryHolder<WorldDatabaseConnection>;
class SQLQueryHolderCallback;
// mysql
struct MySQLHandle;
struct MySQLResult;

View File

@@ -227,7 +227,7 @@ QueryCallback DatabaseWorkerPool<T>::AsyncQuery(PreparedStatement<T>* stmt)
}
template <class T>
QueryResultHolderFuture DatabaseWorkerPool<T>::DelayQueryHolder(SQLQueryHolder<T>* holder)
SQLQueryHolderCallback DatabaseWorkerPool<T>::DelayQueryHolder(SQLQueryHolder<T>* holder)
{
SQLQueryHolderTask* task = new SQLQueryHolderTask(holder);
// Store future result before enqueueing - task might get already processed and deleted before returning from this method

View File

@@ -160,7 +160,7 @@ class DatabaseWorkerPool
//! return object as soon as the query is executed.
//! The return value is then processed in ProcessQueryCallback methods.
//! Any prepared statements added to this holder need to be prepared with the CONNECTION_ASYNC flag.
QueryResultHolderFuture DelayQueryHolder(SQLQueryHolder<T>* holder);
SQLQueryHolderCallback DelayQueryHolder(SQLQueryHolder<T>* holder);
/**
Transaction context methods.

View File

@@ -92,3 +92,14 @@ bool SQLQueryHolderTask::Execute()
m_result.set_value(m_holder);
return true;
}
bool SQLQueryHolderCallback::InvokeIfReady()
{
if (m_future.valid() && m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
m_callback(m_future.get());
return true;
}
return false;
}

View File

@@ -63,4 +63,23 @@ class TC_DATABASE_API SQLQueryHolderTask : public SQLOperation
QueryResultHolderFuture GetFuture() { return m_result.get_future(); }
};
class TC_DATABASE_API SQLQueryHolderCallback
{
public:
SQLQueryHolderCallback(QueryResultHolderFuture&& future) : m_future(std::move(future)) { }
SQLQueryHolderCallback(SQLQueryHolderCallback&&) = default;
SQLQueryHolderCallback& operator=(SQLQueryHolderCallback&&) = default;
void AfterComplete(std::function<void(SQLQueryHolderBase*)> callback) &
{
m_callback = std::move(callback);
}
bool InvokeIfReady();
QueryResultHolderFuture m_future;
std::function<void(SQLQueryHolderBase*)> m_callback;
};
#endif