1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-29 07:37:45 +00:00
sapphire/src/common/Database/DbWorkerPool.h

96 lines
2.1 KiB
C
Raw Normal View History

#ifndef SAPPHIRE_DBWORKERPOOL_H
#define SAPPHIRE_DBWORKERPOOL_H
#include <array>
#include <string>
#include <vector>
#include <ResultSet.h>
2018-03-06 22:22:19 +01:00
#include "Util/LockedWaitQueue.h"
#include "DbConnection.h"
2018-10-28 21:53:21 +01:00
namespace Core::Db
{
2018-10-28 21:53:21 +01:00
template< typename T >
class LockedWaitQueue;
2018-10-28 21:53:21 +01:00
class Operation;
2018-10-28 21:53:21 +01:00
class PreparedStatement;
2018-10-28 21:53:21 +01:00
struct ConnectionInfo;
2018-10-28 21:53:21 +01:00
template< class T >
class DbWorkerPool
{
2018-10-28 21:53:21 +01:00
private:
enum InternalIndex
{
IDX_ASYNC,
IDX_SYNCH,
IDX_SIZE
};
2018-10-28 21:53:21 +01:00
public:
DbWorkerPool();
2018-10-28 21:53:21 +01:00
~DbWorkerPool();
2018-10-28 21:53:21 +01:00
void setConnectionInfo( const ConnectionInfo& info, uint8_t asyncThreads, uint8_t synchThreads );
2018-10-28 21:53:21 +01:00
uint32_t open();
2018-10-28 21:53:21 +01:00
void close();
2018-10-28 21:53:21 +01:00
bool prepareStatements();
2018-10-28 21:53:21 +01:00
inline ConnectionInfo getConnectionInfo() const
{
return m_connectionInfo;
}
2018-10-28 21:53:21 +01:00
// Async execution
void execute( const std::string& sql );
2018-10-28 21:53:21 +01:00
void execute( std::shared_ptr< PreparedStatement > stmt );
2018-10-28 21:53:21 +01:00
// Sync execution
void directExecute( const std::string& sql );
2018-10-28 21:53:21 +01:00
void directExecute( std::shared_ptr< PreparedStatement > stmt );
2018-10-28 21:53:21 +01:00
std::shared_ptr< Mysql::ResultSet >
query( const std::string& sql, std::shared_ptr< T > connection = nullptr );
2018-10-28 21:53:21 +01:00
std::shared_ptr< Mysql::PreparedResultSet > query( std::shared_ptr< PreparedStatement > stmt );
2018-10-28 21:53:21 +01:00
using PreparedStatementIndex = typename T::Statements;
2018-10-28 21:53:21 +01:00
std::shared_ptr< PreparedStatement > getPreparedStatement( PreparedStatementIndex index );
2018-10-28 21:53:21 +01:00
void escapeString( std::string& str );
2018-10-28 21:53:21 +01:00
void keepAlive();
2018-10-28 21:53:21 +01:00
private:
uint32_t openConnections( InternalIndex type, uint8_t numConnections );
2018-10-28 21:53:21 +01:00
unsigned long escapeString( char* to, const char* from, unsigned long length );
2018-10-28 21:53:21 +01:00
void enqueue( std::shared_ptr< Operation > op );
2018-10-28 21:53:21 +01:00
std::shared_ptr< T > getFreeConnection();
2018-10-28 21:53:21 +01:00
const std::string& getDatabaseName() const;
2018-10-28 21:53:21 +01:00
std::unique_ptr< Core::LockedWaitQueue< std::shared_ptr< Operation > > > m_queue;
std::array< std::vector< std::shared_ptr< T > >, IDX_SIZE > m_connections;
ConnectionInfo m_connectionInfo;
uint8_t m_asyncThreads;
uint8_t m_synchThreads;
};
}
#endif //SAPPHIRE_DBWORKERPOOL_H