2010-10-07 15:54:07 +02:00
|
|
|
/*
|
2017-01-01 16:21:30 +01:00
|
|
|
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
|
2010-10-07 15:54:07 +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/>.
|
2010-08-18 02:25:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _DATABASEWORKERPOOL_H
|
|
|
|
|
#define _DATABASEWORKERPOOL_H
|
|
|
|
|
|
2010-09-03 00:04:14 +02:00
|
|
|
#include "Common.h"
|
2010-08-18 02:25:52 +02:00
|
|
|
#include "MySQLConnection.h"
|
2010-09-03 00:04:14 +02:00
|
|
|
#include "Transaction.h"
|
2010-09-02 17:47:50 +02:00
|
|
|
#include "DatabaseWorker.h"
|
2010-09-03 00:08:20 +02:00
|
|
|
#include "PreparedStatement.h"
|
2010-09-02 17:19:53 -06:00
|
|
|
#include "Log.h"
|
2010-09-11 21:10:54 +02:00
|
|
|
#include "QueryResult.h"
|
2010-09-19 12:36:48 +02:00
|
|
|
#include "QueryHolder.h"
|
|
|
|
|
#include "AdhocStatement.h"
|
2015-04-02 21:32:58 +02:00
|
|
|
#include "StringFormat.h"
|
2010-08-18 02:25:52 +02:00
|
|
|
|
2015-02-04 19:44:34 +00:00
|
|
|
#include <mysqld_error.h>
|
2015-03-21 00:25:21 +01:00
|
|
|
#include <memory>
|
2016-03-02 02:17:37 +01:00
|
|
|
#include <array>
|
2015-02-04 19:44:34 +00:00
|
|
|
|
2017-01-13 21:38:03 +01:00
|
|
|
class QueryCallback;
|
|
|
|
|
|
2010-09-25 01:05:24 +02:00
|
|
|
class PingOperation : public SQLOperation
|
|
|
|
|
{
|
2012-03-27 11:49:16 +02:00
|
|
|
//! Operation for idle delaythreads
|
2014-08-09 20:17:40 +01:00
|
|
|
bool Execute() override
|
2010-09-25 01:05:24 +02:00
|
|
|
{
|
2012-03-27 11:49:16 +02:00
|
|
|
m_conn->Ping();
|
|
|
|
|
return true;
|
2010-09-25 01:05:24 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2010-09-02 17:47:50 +02:00
|
|
|
template <class T>
|
2010-08-18 02:25:52 +02:00
|
|
|
class DatabaseWorkerPool
|
|
|
|
|
{
|
2014-08-10 20:28:18 +02:00
|
|
|
private:
|
|
|
|
|
enum InternalIndex
|
|
|
|
|
{
|
|
|
|
|
IDX_ASYNC,
|
|
|
|
|
IDX_SYNCH,
|
|
|
|
|
IDX_SIZE
|
|
|
|
|
};
|
|
|
|
|
|
2010-08-18 02:25:52 +02:00
|
|
|
public:
|
2010-12-23 16:05:43 +01:00
|
|
|
/* Activity state */
|
2016-03-03 01:31:51 +01:00
|
|
|
DatabaseWorkerPool();
|
2010-09-02 17:47:50 +02:00
|
|
|
|
|
|
|
|
~DatabaseWorkerPool()
|
|
|
|
|
{
|
2014-07-21 20:12:20 +02:00
|
|
|
_queue->Cancel();
|
2015-03-21 00:25:21 +01:00
|
|
|
}
|
2014-07-21 20:12:20 +02:00
|
|
|
|
2016-03-03 01:31:51 +01:00
|
|
|
void SetConnectionInfo(std::string const& infoString, uint8 const asyncThreads, uint8 const synchThreads);
|
2010-12-04 21:50:36 +01:00
|
|
|
|
2016-03-03 01:31:51 +01:00
|
|
|
uint32 Open();
|
2010-09-02 17:47:50 +02:00
|
|
|
|
2016-03-03 01:31:51 +01:00
|
|
|
void Close();
|
2010-09-02 17:47:50 +02:00
|
|
|
|
2015-03-21 00:25:21 +01:00
|
|
|
//! Prepares all prepared statements
|
2016-03-03 01:31:51 +01:00
|
|
|
bool PrepareStatements();
|
2015-03-21 00:25:21 +01:00
|
|
|
|
|
|
|
|
inline MySQLConnectionInfo const* GetConnectionInfo() const
|
|
|
|
|
{
|
|
|
|
|
return _connectionInfo.get();
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-26 00:41:55 +01:00
|
|
|
/**
|
2010-12-23 16:05:43 +01:00
|
|
|
Delayed one-way statement methods.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//! Enqueues a one-way SQL operation in string format that will be executed asynchronously.
|
2012-03-19 18:08:32 +01:00
|
|
|
//! This method should only be used for queries that are only executed once, e.g during startup.
|
2010-09-02 17:47:50 +02:00
|
|
|
void Execute(const char* sql)
|
|
|
|
|
{
|
2015-07-22 01:03:40 +02:00
|
|
|
if (Trinity::IsFormatEmptyOrNull(sql))
|
2010-09-02 17:47:50 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
BasicStatementTask* task = new BasicStatementTask(sql);
|
|
|
|
|
Enqueue(task);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Enqueues a one-way SQL operation in string format -with variable args- that will be executed asynchronously.
|
2012-03-19 18:08:32 +01:00
|
|
|
//! This method should only be used for queries that are only executed once, e.g during startup.
|
2015-07-22 01:03:40 +02:00
|
|
|
template<typename Format, typename... Args>
|
|
|
|
|
void PExecute(Format&& sql, Args&&... args)
|
2010-09-02 17:47:50 +02:00
|
|
|
{
|
2015-07-22 01:03:40 +02:00
|
|
|
if (Trinity::IsFormatEmptyOrNull(sql))
|
2010-09-02 17:47:50 +02:00
|
|
|
return;
|
|
|
|
|
|
2015-07-22 01:03:40 +02:00
|
|
|
Execute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
2010-09-02 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Enqueues a one-way SQL operation in prepared statement format that will be executed asynchronously.
|
2011-12-19 11:11:25 +01:00
|
|
|
//! Statement must be prepared with CONNECTION_ASYNC flag.
|
2010-12-23 16:05:43 +01:00
|
|
|
void Execute(PreparedStatement* stmt)
|
|
|
|
|
{
|
|
|
|
|
PreparedStatementTask* task = new PreparedStatementTask(stmt);
|
|
|
|
|
Enqueue(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-03-19 18:08:32 +01:00
|
|
|
Direct synchronous one-way statement methods.
|
2010-12-23 16:05:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//! Directly executes a one-way SQL operation in string format, that will block the calling thread until finished.
|
2012-03-19 18:08:32 +01:00
|
|
|
//! This method should only be used for queries that are only executed once, e.g during startup.
|
2010-09-02 17:47:50 +02:00
|
|
|
void DirectExecute(const char* sql)
|
|
|
|
|
{
|
2016-04-22 13:45:23 +02:00
|
|
|
if (Trinity::IsFormatEmptyOrNull(sql))
|
2010-09-27 00:20:56 +02:00
|
|
|
return;
|
2010-12-13 22:37:56 +01:00
|
|
|
|
2016-03-02 02:17:37 +01:00
|
|
|
T* connection = GetFreeConnection();
|
|
|
|
|
connection->Execute(sql);
|
|
|
|
|
connection->Unlock();
|
2010-09-02 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Directly executes a one-way SQL operation in string format -with variable args-, that will block the calling thread until finished.
|
2012-03-19 18:08:32 +01:00
|
|
|
//! This method should only be used for queries that are only executed once, e.g during startup.
|
2015-07-22 01:03:40 +02:00
|
|
|
template<typename Format, typename... Args>
|
|
|
|
|
void DirectPExecute(Format&& sql, Args&&... args)
|
2010-09-02 17:47:50 +02:00
|
|
|
{
|
2015-07-22 01:03:40 +02:00
|
|
|
if (Trinity::IsFormatEmptyOrNull(sql))
|
2010-09-02 17:47:50 +02:00
|
|
|
return;
|
|
|
|
|
|
2015-07-22 01:03:40 +02:00
|
|
|
DirectExecute(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
2010-09-02 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Directly executes a one-way SQL operation in prepared statement format, that will block the calling thread until finished.
|
2011-12-19 11:11:25 +01:00
|
|
|
//! Statement must be prepared with the CONNECTION_SYNCH flag.
|
2010-12-23 16:05:43 +01:00
|
|
|
void DirectExecute(PreparedStatement* stmt)
|
|
|
|
|
{
|
2016-03-02 02:17:37 +01:00
|
|
|
T* connection = GetFreeConnection();
|
|
|
|
|
connection->Execute(stmt);
|
|
|
|
|
connection->Unlock();
|
2013-09-21 17:45:46 +02:00
|
|
|
|
|
|
|
|
//! Delete proxy-class. Not needed anymore
|
|
|
|
|
delete stmt;
|
2010-12-23 16:05:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-19 11:11:25 +01:00
|
|
|
Synchronous query (with resultset) methods.
|
2010-12-23 16:05:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//! 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.
|
2016-03-03 01:31:51 +01:00
|
|
|
QueryResult Query(const char* sql, T* connection = nullptr);
|
2010-09-02 17:47:50 +02:00
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! 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.
|
2015-07-22 01:03:40 +02:00
|
|
|
template<typename Format, typename... Args>
|
|
|
|
|
QueryResult PQuery(Format&& sql, T* conn, Args&&... args)
|
2010-09-27 00:20:56 +02:00
|
|
|
{
|
2015-07-22 01:03:40 +02:00
|
|
|
if (Trinity::IsFormatEmptyOrNull(sql))
|
|
|
|
|
return QueryResult(nullptr);
|
2010-09-27 00:20:56 +02:00
|
|
|
|
2015-07-22 01:03:40 +02:00
|
|
|
return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str(), conn);
|
2010-09-27 00:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! 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.
|
2015-07-22 01:03:40 +02:00
|
|
|
template<typename Format, typename... Args>
|
|
|
|
|
QueryResult PQuery(Format&& sql, Args&&... args)
|
2010-09-02 17:47:50 +02:00
|
|
|
{
|
2016-04-22 13:45:23 +02:00
|
|
|
if (Trinity::IsFormatEmptyOrNull(sql))
|
2015-07-22 01:03:40 +02:00
|
|
|
return QueryResult(nullptr);
|
2010-09-02 17:47:50 +02:00
|
|
|
|
2015-07-22 01:03:40 +02:00
|
|
|
return Query(Trinity::StringFormat(std::forward<Format>(sql), std::forward<Args>(args)...).c_str());
|
2010-09-02 17:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Directly executes an SQL query in prepared format that will block the calling thread until finished.
|
|
|
|
|
//! Returns reference counted auto pointer, no need for manual memory management in upper level code.
|
2011-12-19 11:11:25 +01:00
|
|
|
//! Statement must be prepared with CONNECTION_SYNCH flag.
|
2016-03-03 01:31:51 +01:00
|
|
|
PreparedQueryResult Query(PreparedStatement* stmt);
|
2010-12-23 16:05:43 +01:00
|
|
|
|
2011-01-26 00:41:55 +01:00
|
|
|
/**
|
2010-12-23 16:05:43 +01:00
|
|
|
Asynchronous query (with resultset) methods.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//! Enqueues a query in string format 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.
|
2017-01-13 21:38:03 +01:00
|
|
|
QueryCallback AsyncQuery(const char* sql);
|
2010-09-02 17:47:50 +02:00
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Enqueues a query in prepared format that will set the value of the PreparedQueryResultFuture return object as soon as the query is executed.
|
|
|
|
|
//! The return value is then processed in ProcessQueryCallback methods.
|
2011-12-19 11:11:25 +01:00
|
|
|
//! Statement must be prepared with CONNECTION_ASYNC flag.
|
2017-01-13 21:38:03 +01:00
|
|
|
QueryCallback AsyncQuery(PreparedStatement* stmt);
|
2010-12-23 16:05:43 +01:00
|
|
|
|
|
|
|
|
//! Enqueues a vector of SQL operations (can be both adhoc and prepared) that will set the value of the QueryResultHolderFuture
|
|
|
|
|
//! return object as soon as the query is executed.
|
|
|
|
|
//! The return value is then processed in ProcessQueryCallback methods.
|
2011-12-19 11:11:25 +01:00
|
|
|
//! Any prepared statements added to this holder need to be prepared with the CONNECTION_ASYNC flag.
|
2016-03-03 01:31:51 +01:00
|
|
|
QueryResultHolderFuture DelayQueryHolder(SQLQueryHolder* holder);
|
2010-09-12 01:40:27 +02:00
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
/**
|
|
|
|
|
Transaction context methods.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//! Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0)
|
2010-09-02 17:47:50 +02:00
|
|
|
SQLTransaction BeginTransaction()
|
|
|
|
|
{
|
|
|
|
|
return SQLTransaction(new Transaction);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Enqueues a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations
|
|
|
|
|
//! were appended to the transaction will be respected during execution.
|
2016-03-03 01:31:51 +01:00
|
|
|
void CommitTransaction(SQLTransaction transaction);
|
2010-08-18 02:25:52 +02:00
|
|
|
|
2011-01-13 20:07:09 +01:00
|
|
|
//! Directly executes a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations
|
|
|
|
|
//! were appended to the transaction will be respected during execution.
|
2016-03-03 01:31:51 +01:00
|
|
|
void DirectCommitTransaction(SQLTransaction& transaction);
|
2011-01-13 20:07:09 +01:00
|
|
|
|
2010-12-13 16:27:14 +01:00
|
|
|
//! Method used to execute prepared statements in a diverse context.
|
|
|
|
|
//! Will be wrapped in a transaction if valid object is present, otherwise executed standalone.
|
|
|
|
|
void ExecuteOrAppend(SQLTransaction& trans, PreparedStatement* stmt)
|
|
|
|
|
{
|
2014-07-01 00:54:09 +02:00
|
|
|
if (!trans)
|
2010-12-13 16:27:14 +01:00
|
|
|
Execute(stmt);
|
2010-12-13 22:37:56 +01:00
|
|
|
else
|
2010-12-13 16:27:14 +01:00
|
|
|
trans->Append(stmt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Method used to execute ad-hoc statements in a diverse context.
|
|
|
|
|
//! Will be wrapped in a transaction if valid object is present, otherwise executed standalone.
|
|
|
|
|
void ExecuteOrAppend(SQLTransaction& trans, const char* sql)
|
|
|
|
|
{
|
2014-07-01 00:54:09 +02:00
|
|
|
if (!trans)
|
2010-12-13 17:05:01 +01:00
|
|
|
Execute(sql);
|
2010-12-13 22:37:56 +01:00
|
|
|
else
|
2010-12-13 16:27:14 +01:00
|
|
|
trans->Append(sql);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
/**
|
|
|
|
|
Other
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-27 00:54:49 +02:00
|
|
|
typedef typename T::Statements PreparedStatementIndex;
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Automanaged (internally) pointer to a prepared statement object for usage in upper level code.
|
2013-09-21 17:45:46 +02:00
|
|
|
//! Pointer is deleted in this->DirectExecute(PreparedStatement*), this->Query(PreparedStatement*) or PreparedStatementTask::~PreparedStatementTask.
|
2010-12-23 16:05:43 +01:00
|
|
|
//! This object is not tied to the prepared statement on the MySQL context yet until execution.
|
2015-08-27 00:54:49 +02:00
|
|
|
PreparedStatement* GetPreparedStatement(PreparedStatementIndex index)
|
2010-09-02 20:54:43 +02:00
|
|
|
{
|
|
|
|
|
return new PreparedStatement(index);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Apply escape string'ing for current collation. (utf8)
|
2016-03-03 01:31:51 +01:00
|
|
|
void EscapeString(std::string& str);
|
2010-08-18 02:25:52 +02:00
|
|
|
|
2010-12-23 16:05:43 +01:00
|
|
|
//! Keeps all our MySQL connections alive, prevent the server from disconnecting us.
|
2016-03-03 01:31:51 +01:00
|
|
|
void KeepAlive();
|
2010-09-25 01:05:24 +02:00
|
|
|
|
2010-09-03 10:52:32 +02:00
|
|
|
private:
|
2016-03-03 01:31:51 +01:00
|
|
|
uint32 OpenConnections(InternalIndex type, uint8 numConnections);
|
2014-08-10 20:28:18 +02:00
|
|
|
|
2011-06-30 14:52:44 +02:00
|
|
|
unsigned long EscapeString(char *to, const char *from, unsigned long length)
|
2010-08-18 02:25:52 +02:00
|
|
|
{
|
|
|
|
|
if (!to || !from || !length)
|
|
|
|
|
return 0;
|
2010-12-13 22:37:56 +01:00
|
|
|
|
2016-03-02 02:17:37 +01:00
|
|
|
return mysql_real_escape_string(
|
|
|
|
|
_connections[IDX_SYNCH].front()->GetHandle(), to, from, length);
|
2010-08-18 02:25:52 +02:00
|
|
|
}
|
2010-09-12 01:40:27 +02:00
|
|
|
|
2010-08-18 02:25:52 +02:00
|
|
|
void Enqueue(SQLOperation* op)
|
|
|
|
|
{
|
2014-06-30 18:37:23 +02:00
|
|
|
_queue->Push(op);
|
2010-08-18 02:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
2012-03-27 11:49:16 +02:00
|
|
|
//! Gets a free connection in the synchronous connection pool.
|
|
|
|
|
//! Caller MUST call t->Unlock() after touching the MySQL context to prevent deadlocks.
|
2016-03-03 01:31:51 +01:00
|
|
|
T* GetFreeConnection();
|
2010-12-13 09:18:49 +01:00
|
|
|
|
2012-03-27 11:49:16 +02:00
|
|
|
char const* GetDatabaseName() const
|
|
|
|
|
{
|
2014-04-20 16:48:35 +02:00
|
|
|
return _connectionInfo->database.c_str();
|
2012-03-27 11:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
2015-03-21 00:25:21 +01:00
|
|
|
//! Queue shared by async worker threads.
|
|
|
|
|
std::unique_ptr<ProducerConsumerQueue<SQLOperation*>> _queue;
|
2016-03-02 02:17:37 +01:00
|
|
|
std::array<std::vector<std::unique_ptr<T>>, IDX_SIZE> _connections;
|
2015-03-21 00:25:21 +01:00
|
|
|
std::unique_ptr<MySQLConnectionInfo> _connectionInfo;
|
|
|
|
|
uint8 _async_threads, _synch_threads;
|
2010-08-18 02:25:52 +02:00
|
|
|
};
|
|
|
|
|
|
2010-08-18 07:17:04 +02:00
|
|
|
#endif
|