Core/Database: Enable perfect forwarding for database format calls.
(cherry picked from commit ca16dc8c1b8da86f55a790a5f8cc6913dd005fcc)
This commit is contained in:
1 parent
e011ae63d5
commit
7f377a4f1b
4 files changed
+45
-27
No files matched your search
@@ -162,7 +162,7 @@ class DatabaseWorkerPool
|
||||
//! This method should only be used for queries that are only executed once, e.g during startup.
|
||||
void Execute(const char* sql)
|
||||
{
|
||||
if (!sql)
|
||||
if (Trinity::IsFormatEmptyOrNull(sql))
|
||||
return;
|
||||
|
||||
BasicStatementTask* task = new BasicStatementTask(sql);
|
||||
@@ -171,13 +171,13 @@ class DatabaseWorkerPool
|
||||
|
||||
//! Enqueues a one-way SQL operation in string format -with variable args- that will be executed asynchronously.
|
||||
//! This method should only be used for queries that are only executed once, e.g during startup.
|
||||
template<typename... Args>
|
||||
void PExecute(const char* sql, Args const&... args)
|
||||
template<typename Format, typename... Args>
|
||||
void PExecute(Format&& sql, Args&&... args)
|
||||
{
|
||||
if (!sql)
|
||||
if (Trinity::IsFormatEmptyOrNull(sql))
|
||||
return;
|
||||
|
||||
Execute(Trinity::StringFormat(sql, args...).c_str());
|
||||
Execute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
||||
}
|
||||
|
||||
//! Enqueues a one-way SQL operation in prepared statement format that will be executed asynchronously.
|
||||
@@ -206,13 +206,13 @@ class DatabaseWorkerPool
|
||||
|
||||
//! Directly executes a one-way SQL operation in string format -with variable args-, that will block the calling thread until finished.
|
||||
//! This method should only be used for queries that are only executed once, e.g during startup.
|
||||
template<typename... Args>
|
||||
void DirectPExecute(const char* sql, Args const&... args)
|
||||
template<typename Format, typename... Args>
|
||||
void DirectPExecute(Format&& sql, Args&&... args)
|
||||
{
|
||||
if (!sql)
|
||||
if (Trinity::IsFormatEmptyOrNull(sql))
|
||||
return;
|
||||
|
||||
DirectExecute(Trinity::StringFormat(sql, args...).c_str());
|
||||
DirectExecute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
||||
}
|
||||
|
||||
//! Directly executes a one-way SQL operation in prepared statement format, that will block the calling thread until finished.
|
||||
@@ -233,7 +233,7 @@ class DatabaseWorkerPool
|
||||
|
||||
//! Directly executes an SQL query in string format that will block the calling thread until finished.
|
||||
//! Returns reference counted auto pointer, no need for manual memory management in upper level code.
|
||||
QueryResult Query(const char* sql, T* conn = NULL)
|
||||
QueryResult Query(const char* sql, T* conn = nullptr)
|
||||
{
|
||||
if (!conn)
|
||||
conn = GetFreeConnection();
|
||||
@@ -251,24 +251,24 @@ class DatabaseWorkerPool
|
||||
|
||||
//! Directly executes an SQL query in string format -with variable args- that will block the calling thread until finished.
|
||||
//! Returns reference counted auto pointer, no need for manual memory management in upper level code.
|
||||
template<typename... Args>
|
||||
QueryResult PQuery(const char* sql, T* conn, Args const&... args)
|
||||
template<typename Format, typename... Args>
|
||||
QueryResult PQuery(Format&& sql, T* conn, Args&&... args)
|
||||
{
|
||||
if (!sql)
|
||||
return QueryResult(NULL);
|
||||
if (Trinity::IsFormatEmptyOrNull(sql))
|
||||
return QueryResult(nullptr);
|
||||
|
||||
return Query(Trinity::StringFormat(sql, args...).c_str(), conn);
|
||||
return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str(), conn);
|
||||
}
|
||||
|
||||
//! Directly executes an SQL query in string format -with variable args- that will block the calling thread until finished.
|
||||
//! Returns reference counted auto pointer, no need for manual memory management in upper level code.
|
||||
template<typename... Args>
|
||||
QueryResult PQuery(const char* sql, Args const&... args)
|
||||
template<typename Format, typename... Args>
|
||||
QueryResult PQuery(Format&& sql, Args&&... args)
|
||||
{
|
||||
if (!sql)
|
||||
return QueryResult(NULL);
|
||||
return QueryResult(nullptr);
|
||||
|
||||
return Query(Trinity::StringFormat(sql, args...).c_str());
|
||||
return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
||||
}
|
||||
|
||||
//! Directly executes an SQL query in prepared format that will block the calling thread until finished.
|
||||
@@ -309,10 +309,10 @@ class DatabaseWorkerPool
|
||||
|
||||
//! Enqueues a query in string format -with variable args- that will set the value of the QueryResultFuture return object as soon as the query is executed.
|
||||
//! The return value is then processed in ProcessQueryCallback methods.
|
||||
template<typename... Args>
|
||||
QueryResultFuture AsyncPQuery(const char* sql, Args const&... args)
|
||||
template<typename Format, typename... Args>
|
||||
QueryResultFuture AsyncPQuery(Format&& sql, Args&&... args)
|
||||
{
|
||||
return AsyncQuery(Trinity::StringFormat(sql, args...).c_str());
|
||||
return AsyncQuery(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
||||
}
|
||||
|
||||
//! Enqueues a query in prepared format that will set the value of the PreparedQueryResultFuture return object as soon as the query is executed.
|
||||
|
||||
@@ -30,8 +30,11 @@ class SQLQueryHolder
|
||||
SQLQueryHolder() { }
|
||||
~SQLQueryHolder();
|
||||
bool SetQuery(size_t index, const char* sql);
|
||||
template<typename... Args>
|
||||
bool SetPQuery(size_t index, const char* sql, Args const&... args) { return SetQuery(index, Trinity::StringFormat(sql, args...).c_str()); }
|
||||
template<typename Format, typename... Args>
|
||||
bool SetPQuery(size_t index, Format&& sql, Args&&... args)
|
||||
{
|
||||
return SetQuery(index, Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
||||
}
|
||||
bool SetPreparedQuery(size_t index, PreparedStatement* stmt);
|
||||
void SetSize(size_t size);
|
||||
QueryResult GetResult(size_t index);
|
||||
|
||||
@@ -39,8 +39,11 @@ class Transaction
|
||||
|
||||
void Append(PreparedStatement* statement);
|
||||
void Append(const char* sql);
|
||||
template<typename... Args>
|
||||
void PAppend(const char* sql, Args const&... args) { Append(Trinity::StringFormat(sql, args...).c_str()); }
|
||||
template<typename Format, typename... Args>
|
||||
void PAppend(Format&& sql, Args&&... args)
|
||||
{
|
||||
Append(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
||||
}
|
||||
|
||||
size_t GetSize() const { return m_queries.size(); }
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#ifndef TRINITYCORE_STRING_FORMAT_H
|
||||
#define TRINITYCORE_STRING_FORMAT_H
|
||||
|
||||
#include <format.h>
|
||||
#include "format.h"
|
||||
|
||||
namespace Trinity
|
||||
{
|
||||
@@ -29,6 +29,18 @@ namespace Trinity
|
||||
{
|
||||
return fmt::sprintf(std::forward<Format>(fmt), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/// Returns true if the given char pointer is null.
|
||||
inline bool IsFormatEmptyOrNull(const char* fmt)
|
||||
{
|
||||
return fmt == nullptr;
|
||||
}
|
||||
|
||||
/// Returns true if the given std::string is empty.
|
||||
inline bool IsFormatEmptyOrNull(std::string const& fmt)
|
||||
{
|
||||
return fmt.empty();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user