database_controller.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "../tool/thread_condition.h"
  18. #include "../error_code/error_code.h"
  19. #include "./database_communication_configuration.pb.h"
  20. #include <stdio.h>
  21. #include <mutex>
  22. #include <queue>
  23. #include <thread>
  24. #include <atomic>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <iostream>
  28. #include <cstddef>
  29. #include <boost/shared_ptr.hpp>
  30. #include <proto_tool.h>
  31. #include <glog/logging.h>
  32. #define DB_UPDATE_INTERVAL_MILLI 20000
  33. //胡力 数据库连接线路的默认个数 默认5个
  34. #define DB_CONNECT_QUEUE_SIZE 5
  35. //胡力 数据库尝试重新连接的次数 默认3次
  36. #define DB_RECONNECTION_NUMBER 3
  37. #define DATABASE_COMMUNICATION_PARAMETER_PATH "../setting/database_communication.prototxt"
  38. class Database_controller : public Singleton<Database_controller>
  39. {
  40. // 子类必须把父类设定为友元函数,这样父类才能使用子类的私有构造函数。
  41. friend class Singleton<Database_controller>;
  42. public:
  43. typedef struct DB_PARAM
  44. {
  45. std::string connect_string;
  46. std::string username;
  47. std::string pass;
  48. std::string db_name;
  49. } db_param;
  50. //huli 新增数据库的状态
  51. enum Database_controller_status
  52. {
  53. E_UNKNOWN = 0, //默认未知
  54. E_READY = 1, //正常待机
  55. E_DISCONNECT = 2, //断连
  56. E_FAULT = 3, //故障
  57. };
  58. struct Database_connect_channel
  59. {
  60. boost::shared_ptr< sql::Connection> mp_connect_channel;
  61. std::mutex m_occupancy_lock;
  62. };
  63. private:
  64. // 父类的构造函数必须保护,子类的构造函数必须私有。
  65. Database_controller() ;
  66. public:
  67. // 必须关闭拷贝构造和赋值构造,只能通过 get_instance 函数来进行操作唯一的实例。
  68. Database_controller(const Database_controller &) = delete;
  69. Database_controller &operator=(const Database_controller &) = delete;
  70. ~Database_controller();
  71. //初始化
  72. Error_manager database_controller_init();
  73. //从文本导入
  74. Error_manager database_controller_init_from_protobuf(std::string prototxt_path);
  75. Error_manager database_controller_init(std::string ip, int port, std::string username, std::string pass, std::string db_name, int conn_size=DB_CONNECT_QUEUE_SIZE);
  76. //反初始化
  77. Error_manager database_controller_uninit();
  78. Database_controller_status get_database_controller_status();
  79. Error_manager check_status();
  80. //数据库连接状态
  81. bool is_connected();
  82. //****** 增删改查功能 *******
  83. //增
  84. Error_manager sql_insert(std::string sql_str);
  85. //删
  86. Error_manager sql_delete(std::string sql_str);
  87. //改
  88. Error_manager sql_update(std::string sql_str);
  89. //查
  90. Error_manager sql_query(std::string sql_str, boost::shared_ptr< sql::ResultSet > &query_result);
  91. //控制语句 LOCK UNLOCK COMMIT 等
  92. Error_manager sql_control(std::string sql_str);
  93. private:
  94. // 守护线程,维持连接数量,以及自身状态更新
  95. void database_reconnect_thread();
  96. // 检查gengxin连接
  97. Error_manager updata_connect_channel();
  98. //创建补充 数据库连接的通路
  99. Error_manager fill_up_connect_channel();
  100. //zhan you jieyong
  101. Error_manager occupancy_connect_channel(boost::shared_ptr<Database_connect_channel> & p_database_connect_channel);
  102. //guihuan
  103. Error_manager giveback_connect_channel(boost::shared_ptr<Database_connect_channel> p_database_connect_channel);
  104. private:
  105. //huli 新增数据库的状态
  106. Database_controller_status m_database_controller_status;
  107. //数据库连接通路的集合
  108. std::list< boost::shared_ptr<Database_connect_channel> > m_database_connect_channel_list;
  109. //维持连接数量,以及自身状态更新条件变量
  110. Thread_condition m_database_reconnect_condition;
  111. //重连线程
  112. std::thread *mp_reconnect_thread;
  113. //连接池大小
  114. int m_conn_pool_size;
  115. //连接参数
  116. db_param m_db_param;
  117. };
  118. #endif // !DATABASE_CONTROLLER_HH