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