database_controller.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * @Description: 数据库操作类
  3. * @Author: yct
  4. * @Date: 2020-07-14 14:09:35
  5. * @LastEditTime: 2020-07-18 23:57:10
  6. * @LastEditors: yct
  7. */
  8. #ifndef DATABASE_CONTROLLER_HH
  9. #define DATABASE_CONTROLLER_HH
  10. #include <mysql_driver.h>
  11. #include <mysql_connection.h>
  12. #include <cppconn/driver.h>
  13. #include <cppconn/exception.h>
  14. #include <cppconn/resultset.h>
  15. #include <cppconn/statement.h>
  16. #include "../tool/singleton.h"
  17. #include "../error_code/error_code.h"
  18. #include <stdio.h>
  19. #include <mutex>
  20. #include <queue>
  21. #include <thread>
  22. #include <atomic>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <iostream>
  26. #include <cstddef>
  27. #include <boost/shared_ptr.hpp>
  28. #define DB_UPDATE_INTERVAL_MILLI 20000
  29. class Database_controller : public Singleton<Database_controller>
  30. {
  31. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  32. friend class Singleton<Database_controller>;
  33. public:
  34. typedef struct DB_PARAM
  35. {
  36. std::string connect_string;
  37. std::string username;
  38. std::string pass;
  39. std::string db_name;
  40. } db_param;
  41. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  42. Database_controller(const Database_controller &) = delete;
  43. Database_controller &operator=(const Database_controller &) = delete;
  44. ~Database_controller() = default;
  45. //初始化
  46. Error_manager database_controller_init(std::string ip, int port, std::string username, std::string pass, std::string db_name, int conn_size=5);
  47. //反初始化
  48. Error_manager database_controller_uninit();
  49. //数据库连接状态
  50. bool is_connected();
  51. //****** 增删改查功能 *******
  52. //增
  53. Error_manager sql_insert(std::string sql_str);
  54. //删
  55. Error_manager sql_delete(std::string sql_str);
  56. //改
  57. Error_manager sql_update(std::string sql_str);
  58. //查
  59. Error_manager sql_query(std::string sql_str, boost::shared_ptr< sql::ResultSet > &query_result);
  60. private:
  61. // 父类的构造函数必须保护,子类的构造函数必须私有。
  62. Database_controller() = default;
  63. // 守护线程,维持连接数量,以及自身状态更新
  64. void database_status_update();
  65. // 检查连接状态
  66. Error_manager check_status();
  67. // 补充连接
  68. Error_manager fill_up_pool(int add_num);
  69. private:
  70. //mysql驱动句柄
  71. sql::Driver * m_mysql_driver;
  72. //连接状态
  73. std::atomic_bool mb_connected;
  74. //初始化状态
  75. std::atomic_bool mb_initialized;
  76. //系统关闭标记
  77. std::atomic_bool mb_exit;
  78. //连接池访问锁
  79. std::mutex m_conn_mutex;
  80. //数据库连接池
  81. std::queue< boost::shared_ptr< sql::Connection> > m_db_conn_pool;
  82. //守护线程
  83. std::thread *mp_guard_thread;
  84. //连接池大小
  85. int m_conn_pool_size;
  86. //连接参数
  87. db_param m_db_param;
  88. };
  89. #endif // !DATABASE_CONTROLLER_HH