db.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef __DB_HPP__
  2. #define __DB_HPP__
  3. #include "util.hpp"
  4. #include <mutex>
  5. #include <cassert>
  6. /*用户数据管理模块 -- 用于管理数据库数据,为数据库中的每张表都设计一个类,然后通过类对象来操作数据库表中的数据*/
  7. /*用户信息表*/
  8. class user_table {
  9. public:
  10. user_table(const std::string &host, const std::string &user, const std::string &passwd, \
  11. const std::string db = "gobang", uint16_t port = 4106)
  12. {
  13. _mysql = mysql_util::mysql_create(host, user, passwd, db, port);
  14. assert(_mysql != nullptr);
  15. LOG(DEBUG, "用户数据管理模块初识化完毕");
  16. }
  17. ~user_table() {
  18. if(_mysql != nullptr) {
  19. mysql_util::mysql_destroy(_mysql);
  20. _mysql = nullptr;
  21. }
  22. LOG(DEBUG, "用户数据管理模块已被销毁");
  23. }
  24. /*新用户注册*/
  25. bool registers(Json::Value &user) {
  26. if(user["username"].isNull() || user["password"].isNull()) {
  27. LOG(NORMAL, "please input username and password");
  28. return false;
  29. }
  30. // 由于用户名有唯一键约束,所以不需要担心用户已被注册的情况
  31. char sql[1024];
  32. #define INSERT_USER "insert into user values(null, '%s', password('%s'), 1000, 0, 0)"
  33. sprintf(sql, INSERT_USER, user["username"].asCString(), user["password"].asCString());
  34. // LOG(DEBUG, "%s", sql);
  35. if(mysql_util::mysql_execute(_mysql, sql) == false) {
  36. LOG(NORMAL, "user register failed");
  37. return false;
  38. }
  39. LOG(NORMAL, "%s register success", user["username"].asCString());
  40. return true;
  41. }
  42. /*用户登录验证*/
  43. bool login(Json::Value &user) {
  44. // 与数据库中的用户名+密码进行比对
  45. // 注意:数据库的password是经过mysql password函数转换后的,所以sql查询时也需要对user["password"].asString()进行转化
  46. #define SELECT_USER "select id, score, total_count, win_count from user where username = '%s' and password = password('%s')"
  47. char sql[1024];
  48. sprintf(sql, SELECT_USER, user["username"].asCString(), user["password"].asCString());
  49. MYSQL_RES *res = nullptr;
  50. {
  51. // mysql查询与查询结果的本地保存两步操作需要加锁,避免多线程使用同一句柄进行操作的情况下发送结果集的数据覆盖问题
  52. // 将锁交给RAII unique_lock进行管理
  53. std::unique_lock<std::mutex> lock(_mutex);
  54. if(mysql_util::mysql_execute(_mysql, sql) == false) return false;;
  55. // 获取查询到的结果--一行记录
  56. res = mysql_store_result(_mysql);
  57. // 注意:当mysql查询结果为空时,mysql_store_result也不会返回空,所以不能在这里判断用户名密码是否正确
  58. if(res == nullptr) {
  59. LOG(NORMAL, "mysql store failed: ", mysql_error(_mysql));
  60. return false;
  61. }
  62. }
  63. int row_count = mysql_num_rows(res);
  64. int col_count = mysql_num_fields(res);
  65. // row_count 为0说明查询不到与当前用户名+密码匹配的数据,即用户名或密码错误
  66. if(row_count == 0) {
  67. LOG(NORMAL, "the username or password error, please input again");
  68. return false;
  69. }
  70. // 用户名存在唯一键约束
  71. if(row_count > 1) {
  72. LOG(ERROR, "there are same user %s in the database", user["username"].asCString());
  73. return false;
  74. }
  75. LOG(NORMAL, "%s login success", user["username"].asCString());
  76. // 填充该用户的其他详细信息
  77. MYSQL_ROW row = mysql_fetch_row(res);
  78. user["id"] = std::stoi(row[0]);
  79. user["score"] = std::stoi(row[1]);
  80. user["total_count"] = std::stoi(row[2]);
  81. user["win_count"] = std::stoi(row[3]);
  82. mysql_free_result(res);
  83. return true;
  84. }
  85. /*使用用户名查找用户的详细信息*/
  86. bool select_by_name(const std::string &name, Json::Value &user) {
  87. #define SELECT_BY_USERNAME "select id, score, total_count, win_count from user where username = '%s'"
  88. char sql[1024];
  89. sprintf(sql, SELECT_BY_USERNAME, name.c_str());
  90. MYSQL_RES *res = nullptr;
  91. {
  92. // 加锁
  93. std::unique_lock<std::mutex> lock(_mutex);
  94. if(mysql_util::mysql_execute(_mysql, sql) == false) return false;
  95. // 获取查询到的结果--一行记录
  96. res = mysql_store_result(_mysql);
  97. // 注意:当mysql查询结果为空时,mysql_store_result也不会返回空,所以不能在这里判断用户是否存在
  98. if(res == nullptr) {
  99. LOG(DEBUG, "mysql store failed: ", mysql_error(_mysql));
  100. return false;
  101. }
  102. }
  103. int row_count = mysql_num_rows(res);
  104. int col_count = mysql_num_fields(res);
  105. // row_count为0说明查询不到与当前用户名匹配的数据,即用户不存在
  106. if(row_count == 0) {
  107. LOG(DEBUG, "the user with name %s does not exist", name.c_str());
  108. return false;
  109. }
  110. // 用户名存在唯一键约束
  111. if(row_count > 1) {
  112. LOG(ERROR, "there are same user name %s in the database", name.c_str());
  113. return false;
  114. }
  115. MYSQL_ROW row = mysql_fetch_row(res);
  116. // password是转换后的,获取无意义
  117. user["id"] = std::stoi(row[0]);
  118. user["username"] = name.c_str();
  119. user["score"] = std::stoi(row[1]);
  120. user["total_count"] = std::stoi(row[2]);
  121. user["win_count"] = std::stoi(row[3]);
  122. mysql_free_result(res);
  123. return true;
  124. }
  125. /*使用用户ID查找用户的详细信息*/
  126. bool select_by_id(uint64_t id, Json::Value &user) {
  127. #define SELECT_BY_ID "select username, score, total_count, win_count from user where id = %d"
  128. char sql[1024];
  129. sprintf(sql, SELECT_BY_ID, id);
  130. MYSQL_RES *res = nullptr;
  131. {
  132. // 加锁
  133. std::unique_lock<std::mutex> lock(_mutex);
  134. if(mysql_util::mysql_execute(_mysql, sql) == false) return false;
  135. // 获取查询到的结果--一行记录
  136. res = mysql_store_result(_mysql);
  137. // 注意:当mysql查询结果为空时,mysql_store_result也不会返回空,所以不能在这里判断用户是否存在
  138. if(res == nullptr) {
  139. LOG(DEBUG, "mysql store failed: ", mysql_error(_mysql));
  140. return false;
  141. }
  142. }
  143. int row_count = mysql_num_rows(res);
  144. int col_count = mysql_num_fields(res);
  145. // row_count为0说明查询不到与当前用户名ID匹配的数据,即用户不存在
  146. if(row_count == 0) {
  147. LOG(DEBUG, "the user with ID %d does not exist", id);
  148. return false;
  149. }
  150. // 用户名存在唯一键约束
  151. if(row_count > 1) {
  152. LOG(ERROR, "there are same user with ID %d in the database", id);
  153. return false;
  154. }
  155. MYSQL_ROW row = mysql_fetch_row(res);
  156. // password是转换后的,获取无意义
  157. user["id"] = (Json::UInt64)id;
  158. user["username"] = row[0];
  159. user["score"] = std::stoi(row[1]);
  160. user["total_count"] = std::stoi(row[2]);
  161. user["win_count"] = std::stoi(row[3]);
  162. mysql_free_result(res);
  163. return true;
  164. }
  165. /*用户对战胜利,修改分数以及比赛和胜利场次,胜利一场增加30分*/
  166. bool win(uint64_t id) {
  167. #define UPDATE_WIN "update user set score=score+30, total_count=total_count+1, win_count=win_count+1 where id = %d"
  168. char sql[1024];
  169. sprintf(sql, UPDATE_WIN, id);
  170. if(mysql_util::mysql_execute(_mysql, sql) == false) {
  171. LOG(ERROR, "update the user info of win failed");
  172. return false;
  173. }
  174. return true;
  175. }
  176. /*用户对战失败,修改分数以及比赛场次*,失败一场减少30分*/
  177. bool lose(uint64_t id) {
  178. #define UPDATE_LOSE "update user set score=score-30, total_count=total_count+1 where id = %d"
  179. char sql[1024];
  180. sprintf(sql, UPDATE_LOSE, id);
  181. if(mysql_util::mysql_execute(_mysql, sql) == false) {
  182. LOG(ERROR, "update the user info of lose failed");
  183. return false;
  184. }
  185. return true;
  186. }
  187. private:
  188. MYSQL *_mysql; // mysql操作句柄
  189. std::mutex _mutex; // 解决多线程使用同一类对象(句柄)访问数据库时可能发生的线程安全问题
  190. };
  191. #endif