1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-29 23:57:46 +00:00
sapphire/src/common/Database/DbConnection.h

120 lines
2.7 KiB
C
Raw Normal View History

#ifndef _SAPPHIRE_DBCONNECTION_H
#define _SAPPHIRE_DBCONNECTION_H
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
2018-03-06 22:22:19 +01:00
#include "Util/LockedWaitQueue.h"
2018-10-28 21:53:21 +01:00
namespace Mysql
{
class Connection;
class ResultSet;
class PreparedResultSet;
class PreparedStatement;
}
2018-10-28 21:53:21 +01:00
namespace Core::Db
{
2018-10-28 21:53:21 +01:00
class DatabaseWorker;
class PreparedStatement;
class Operation;
class DbWorker;
using PreparedStmtScopedPtr = std::unique_ptr< PreparedStatement >;
2018-10-28 21:53:21 +01:00
enum ConnectionFlags
{
CONNECTION_ASYNC = 0x1,
CONNECTION_SYNC = 0x2,
CONNECTION_BOTH = CONNECTION_ASYNC | CONNECTION_SYNC
};
2018-10-28 21:53:21 +01:00
struct ConnectionInfo
{
std::string user;
std::string password;
std::string database;
std::string host;
uint16_t port;
uint8_t syncThreads;
uint8_t asyncThreads;
};
using PreparedStatementMap = std::map< uint32_t, std::pair< std::string, ConnectionFlags > >;
class DbConnection :
public std::enable_shared_from_this< DbConnection >
{
public:
// Constructor for synchronous connections.
DbConnection( ConnectionInfo& connInfo );
2018-10-28 21:53:21 +01:00
// Constructor for asynchronous connections.
DbConnection( Core::LockedWaitQueue< std::shared_ptr< Operation > >* queue, ConnectionInfo& connInfo );
2018-10-28 21:53:21 +01:00
virtual ~DbConnection();
2018-10-28 21:53:21 +01:00
virtual 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
bool execute( const std::string& sql );
2018-10-28 21:53:21 +01:00
bool execute( std::shared_ptr< PreparedStatement > stmt );
2018-10-28 21:53:21 +01:00
std::shared_ptr< Mysql::ResultSet > query( const std::string& sql );
2018-10-28 21:53:21 +01:00
std::shared_ptr< Mysql::ResultSet > query( std::shared_ptr< PreparedStatement > stmt );
2018-10-28 21:53:21 +01:00
void beginTransaction();
2018-10-28 21:53:21 +01:00
void rollbackTransaction();
2018-10-28 21:53:21 +01:00
void commitTransaction();
2018-10-28 21:53:21 +01:00
bool ping();
2018-10-28 21:53:21 +01:00
uint32_t getLastError();
2018-10-28 21:53:21 +01:00
bool lockIfReady();
2018-10-28 21:53:21 +01:00
void unlock();
2018-10-28 21:53:21 +01:00
std::shared_ptr< Mysql::Connection > getConnection()
{
return m_pConnection;
}
2018-10-28 21:53:21 +01:00
std::shared_ptr< Mysql::PreparedStatement > getPreparedStatement( uint32_t index );
2018-10-28 21:53:21 +01:00
void prepareStatement( uint32_t index, const std::string& sql, ConnectionFlags flags );
2018-10-28 21:53:21 +01:00
virtual void doPrepareStatements() = 0;
2018-10-28 21:53:21 +01:00
protected:
std::vector< std::shared_ptr< Mysql::PreparedStatement > > m_stmts;
PreparedStatementMap m_queries;
bool m_reconnecting;
bool m_prepareError;
2018-10-28 21:53:21 +01:00
private:
LockedWaitQueue< std::shared_ptr< Operation > >* m_queue;
std::shared_ptr< DbWorker > m_worker;
std::shared_ptr< Mysql::Connection > m_pConnection;
ConnectionInfo& m_connectionInfo;
ConnectionFlags m_connectionFlags;
std::mutex m_mutex;
2018-10-28 21:53:21 +01:00
DbConnection( DbConnection const& right ) = delete;
2018-10-28 21:53:21 +01:00
DbConnection& operator=( DbConnection const& right ) = delete;
};
2018-10-28 21:53:21 +01:00
}
#endif