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