DBConnect.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include <mutex>
  3. #include <mysql_connection.h>
  4. #include <mysql_driver.h>
  5. #include <cppconn/exception.h>
  6. #include <cppconn/driver.h>
  7. #include <cppconn/connection.h>
  8. #include <cppconn/resultset.h>
  9. #include <cppconn/prepared_statement.h>
  10. #include <cppconn/statement.h>
  11. template<typename T>
  12. class CConnectionPtr
  13. {
  14. public:
  15. CConnectionPtr(T* conn, uint64_t& tick) :m_Conn(conn), m_Next(nullptr), OperationTick(tick) {}
  16. ~CConnectionPtr() {
  17. delete m_Conn;
  18. }
  19. T* operator->() { return m_Conn; }
  20. void SetOperationTick(uint64_t& tick);
  21. private:
  22. T* m_Conn;
  23. public:
  24. CConnectionPtr* m_Next;
  25. uint64_t OperationTick;
  26. };
  27. template<typename T>
  28. void CConnectionPtr<T>::SetOperationTick(uint64_t& tick)
  29. {
  30. OperationTick = tick;
  31. }
  32. template<typename T>
  33. class CQPtr
  34. {
  35. public:
  36. CQPtr(T* ptr);
  37. CQPtr& operator=(T* ptr);
  38. CQPtr& operator=(const CQPtr<T>& ptr);
  39. //T* operator->() { return m_Conn; }
  40. ~CQPtr();
  41. T* get();
  42. private:
  43. T* m_Conn;
  44. };
  45. class CDBConnectPool
  46. {
  47. public:
  48. static CDBConnectPool& GetInstance();
  49. void initPool(std::string url_, std::string db, std::string user_, std::string password_, int maxSize_);
  50. void InitConnection(int32_t initSize);
  51. CConnectionPtr<sql::Connection >* CreateConnection();
  52. CQPtr<CConnectionPtr<sql::Connection>> QueryConnect();
  53. void ReleaseConnection(CConnectionPtr<sql::Connection>* ptr);
  54. void DestroyConnection(CConnectionPtr<sql::Connection>* conn);
  55. void DestoryConnPool();
  56. private:
  57. std::string User;
  58. std::string Password;
  59. std::string url;
  60. std::string db;
  61. int32_t MaxSize;
  62. int32_t CurSize;
  63. sql::Driver* SqlDriver;
  64. CConnectionPtr<sql::Connection >* Head;
  65. std::mutex DbLock;
  66. };
  67. template<typename T>
  68. T* CQPtr<T>::get()
  69. {
  70. return m_Conn;
  71. }
  72. template<typename T>
  73. CQPtr<T>& CQPtr<T>::operator=(const CQPtr<T>& ptr)
  74. {
  75. if (m_Conn != nullptr)
  76. CDBConnectPool::GetInstance().ReleaseConnection(m_Conn);
  77. m_Conn = ptr.get();
  78. ptr.m_Conn = nullptr;
  79. }
  80. template<typename T>
  81. CQPtr<T>::CQPtr(T* ptr) :m_Conn(ptr)
  82. {
  83. }
  84. template<typename T>
  85. CQPtr<T>& CQPtr<T>::operator=(T* ptr)
  86. {
  87. if (m_Conn != nullptr)
  88. CDBConnectPool::GetInstance().ReleaseConnection(m_Conn);
  89. m_Conn = ptr;
  90. }
  91. template<typename T>
  92. CQPtr<T>::~CQPtr()
  93. {
  94. if (m_Conn != nullptr)
  95. CDBConnectPool::GetInstance().ReleaseConnection(m_Conn);
  96. }