database_controller.h 4.1 KB

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