#pragma once #include #include #include #include #include #include #include #include #include template class CConnectionPtr { public: CConnectionPtr(T* conn, uint64_t& tick) :m_Conn(conn), m_Next(nullptr), OperationTick(tick) {} ~CConnectionPtr() { delete m_Conn; } T* operator->() { return m_Conn; } void SetOperationTick(uint64_t& tick); private: T* m_Conn; public: CConnectionPtr* m_Next; uint64_t OperationTick; }; template void CConnectionPtr::SetOperationTick(uint64_t& tick) { OperationTick = tick; } template class CQPtr { public: CQPtr(T* ptr); CQPtr& operator=(T* ptr); CQPtr& operator=(const CQPtr& ptr); //T* operator->() { return m_Conn; } ~CQPtr(); T* get(); private: T* m_Conn; }; class CDBConnectPool { public: static CDBConnectPool& GetInstance(); void initPool(std::string url_, std::string db, std::string user_, std::string password_, int maxSize_); void InitConnection(int32_t initSize); CConnectionPtr* CreateConnection(); CQPtr> QueryConnect(); void ReleaseConnection(CConnectionPtr* ptr); void DestroyConnection(CConnectionPtr* conn); void DestoryConnPool(); private: std::string User; std::string Password; std::string url; std::string db; int32_t MaxSize; int32_t CurSize; sql::Driver* SqlDriver; CConnectionPtr* Head; std::mutex DbLock; }; template T* CQPtr::get() { return m_Conn; } template CQPtr& CQPtr::operator=(const CQPtr& ptr) { if (m_Conn != nullptr) CDBConnectPool::GetInstance().ReleaseConnection(m_Conn); m_Conn = ptr.get(); ptr.m_Conn = nullptr; } template CQPtr::CQPtr(T* ptr) :m_Conn(ptr) { } template CQPtr& CQPtr::operator=(T* ptr) { if (m_Conn != nullptr) CDBConnectPool::GetInstance().ReleaseConnection(m_Conn); m_Conn = ptr; } template CQPtr::~CQPtr() { if (m_Conn != nullptr) CDBConnectPool::GetInstance().ReleaseConnection(m_Conn); }