SqlConnection.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef SQL_SQLCONNECTION_H_
  11. #define SQL_SQLCONNECTION_H_
  12. #include <cstdio>
  13. #include <cstdarg>
  14. #include <cstring>
  15. #include <string>
  16. #include <vector>
  17. #include <list>
  18. #include <deque>
  19. #include <sstream>
  20. #include <iostream>
  21. #include <stdexcept>
  22. #include "logger.h"
  23. #include "util.h"
  24. #include <mysql.h>
  25. #if defined(_WIN32)
  26. #pragma comment (lib,"libmysql")
  27. #endif
  28. namespace toolkit {
  29. /**
  30. * 数据库异常类
  31. */
  32. class SqlException : public std::exception {
  33. public:
  34. SqlException(const std::string &sql, const std::string &err) {
  35. _sql = sql;
  36. _err = err;
  37. }
  38. virtual const char *what() const noexcept {
  39. return _err.data();
  40. }
  41. const std::string &getSql() const {
  42. return _sql;
  43. }
  44. private:
  45. std::string _sql;
  46. std::string _err;
  47. };
  48. /**
  49. * mysql连接
  50. */
  51. class SqlConnection {
  52. public:
  53. /**
  54. * 构造函数
  55. * @param url 数据库地址
  56. * @param port 数据库端口号
  57. * @param dbname 数据库名
  58. * @param username 用户名
  59. * @param password 用户密码
  60. * @param character 字符集
  61. */
  62. SqlConnection(const std::string &url, unsigned short port,
  63. const std::string &dbname, const std::string &username,
  64. const std::string &password, const std::string &character = "utf8mb4") {
  65. mysql_init(&_sql);
  66. unsigned int timeout = 3;
  67. mysql_options(&_sql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
  68. if (!mysql_real_connect(&_sql, url.data(), username.data(),
  69. password.data(), dbname.data(), port, nullptr, 0)) {
  70. mysql_close(&_sql);
  71. throw SqlException("mysql_real_connect", mysql_error(&_sql));
  72. }
  73. //兼容bool与my_bool
  74. uint32_t reconnect = 0x01010101;
  75. mysql_options(&_sql, MYSQL_OPT_RECONNECT, &reconnect);
  76. mysql_set_character_set(&_sql, character.data());
  77. }
  78. ~SqlConnection() {
  79. mysql_close(&_sql);
  80. }
  81. /**
  82. * 以printf样式执行sql,无数据返回
  83. * @param rowId insert时的插入rowid
  84. * @param fmt printf类型fmt
  85. * @param arg 可变参数列表
  86. * @return 影响行数
  87. */
  88. template<typename Fmt, typename ...Args>
  89. int64_t query(int64_t &rowId, Fmt &&fmt, Args &&...arg) {
  90. check();
  91. auto tmp = queryString(std::forward<Fmt>(fmt), std::forward<Args>(arg)...);
  92. if (doQuery(tmp)) {
  93. throw SqlException(tmp, mysql_error(&_sql));
  94. }
  95. rowId = mysql_insert_id(&_sql);
  96. return mysql_affected_rows(&_sql);
  97. }
  98. /**
  99. * 以printf样式执行sql,并且返回list类型的结果(不包含数据列名)
  100. * @param rowId insert时的插入rowid
  101. * @param ret 返回数据列表
  102. * @param fmt printf类型fmt
  103. * @param arg 可变参数列表
  104. * @return 影响行数
  105. */
  106. template<typename Fmt, typename ...Args>
  107. int64_t query(int64_t &rowId, std::vector<std::vector<std::string> > &ret, Fmt &&fmt, Args &&...arg) {
  108. return queryList(rowId, ret, std::forward<Fmt>(fmt), std::forward<Args>(arg)...);
  109. }
  110. template<typename Fmt, typename... Args>
  111. int64_t query(int64_t &rowId, std::vector<std::list<std::string>> &ret, Fmt &&fmt, Args &&...arg) {
  112. return queryList(rowId, ret, std::forward<Fmt>(fmt), std::forward<Args>(arg)...);
  113. }
  114. template<typename Fmt, typename ...Args>
  115. int64_t query(int64_t &rowId, std::vector<std::deque<std::string> > &ret, Fmt &&fmt, Args &&...arg) {
  116. return queryList(rowId, ret, std::forward<Fmt>(fmt), std::forward<Args>(arg)...);
  117. }
  118. /**
  119. * 以printf样式执行sql,并且返回Map类型的结果(包含数据列名)
  120. * @param rowId insert时的插入rowid
  121. * @param ret 返回数据列表
  122. * @param fmt printf类型fmt
  123. * @param arg 可变参数列表
  124. * @return 影响行数
  125. */
  126. template<typename Map, typename Fmt, typename ...Args>
  127. int64_t query(int64_t &rowId, std::vector<Map> &ret, Fmt &&fmt, Args &&...arg) {
  128. check();
  129. auto tmp = queryString(std::forward<Fmt>(fmt), std::forward<Args>(arg)...);
  130. if (doQuery(tmp)) {
  131. throw SqlException(tmp, mysql_error(&_sql));
  132. }
  133. ret.clear();
  134. MYSQL_RES *res = mysql_store_result(&_sql);
  135. if (!res) {
  136. rowId = mysql_insert_id(&_sql);
  137. return mysql_affected_rows(&_sql);
  138. }
  139. MYSQL_ROW row;
  140. unsigned int column = mysql_num_fields(res);
  141. MYSQL_FIELD *fields = mysql_fetch_fields(res);
  142. while ((row = mysql_fetch_row(res)) != nullptr) {
  143. ret.emplace_back();
  144. auto &back = ret.back();
  145. for (unsigned int i = 0; i < column; i++) {
  146. back[std::string(fields[i].name, fields[i].name_length)] = (row[i] ? row[i] : "");
  147. }
  148. }
  149. mysql_free_result(res);
  150. rowId = mysql_insert_id(&_sql);
  151. return mysql_affected_rows(&_sql);
  152. }
  153. std::string escape(const std::string &str) {
  154. char *out = new char[str.length() * 2 + 1];
  155. mysql_real_escape_string(&_sql, out, str.c_str(), str.size());
  156. std::string ret(out);
  157. delete[] out;
  158. return ret;
  159. }
  160. template<typename ...Args>
  161. static std::string queryString(const char *fmt, Args &&...arg) {
  162. char *ptr_out = nullptr;
  163. if (asprintf(&ptr_out, fmt, arg...) > 0 && ptr_out) {
  164. std::string ret(ptr_out);
  165. free(ptr_out);
  166. return ret;
  167. }
  168. return "";
  169. }
  170. template<typename ...Args>
  171. static std::string queryString(const std::string &fmt, Args &&...args) {
  172. return queryString(fmt.data(), std::forward<Args>(args)...);
  173. }
  174. static const char *queryString(const char *fmt) {
  175. return fmt;
  176. }
  177. static const std::string &queryString(const std::string &fmt) {
  178. return fmt;
  179. }
  180. private:
  181. template<typename List, typename Fmt, typename... Args>
  182. int64_t queryList(int64_t &rowId, std::vector<List> &ret, Fmt &&fmt, Args &&...arg) {
  183. check();
  184. auto tmp = queryString(std::forward<Fmt>(fmt), std::forward<Args>(arg)...);
  185. if (doQuery(tmp)) {
  186. throw SqlException(tmp, mysql_error(&_sql));
  187. }
  188. ret.clear();
  189. MYSQL_RES *res = mysql_store_result(&_sql);
  190. if (!res) {
  191. rowId = mysql_insert_id(&_sql);
  192. return mysql_affected_rows(&_sql);
  193. }
  194. MYSQL_ROW row;
  195. unsigned int column = mysql_num_fields(res);
  196. while ((row = mysql_fetch_row(res)) != nullptr) {
  197. ret.emplace_back();
  198. auto &back = ret.back();
  199. for (unsigned int i = 0; i < column; i++) {
  200. back.emplace_back(row[i] ? row[i] : "");
  201. }
  202. }
  203. mysql_free_result(res);
  204. rowId = mysql_insert_id(&_sql);
  205. return mysql_affected_rows(&_sql);
  206. }
  207. inline void check() {
  208. if (mysql_ping(&_sql) != 0) {
  209. throw SqlException("mysql_ping", "Mysql connection ping failed");
  210. }
  211. }
  212. int doQuery(const std::string &sql) {
  213. return mysql_query(&_sql, sql.data());
  214. }
  215. int doQuery(const char *sql) {
  216. return mysql_query(&_sql, sql);
  217. }
  218. private:
  219. MYSQL _sql;
  220. };
  221. } /* namespace toolkit */
  222. #endif /* SQL_SQLCONNECTION_H_ */