您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

214 行
6.0 KiB

  1. #pragma once
  2. #include <memory>
  3. #include <cppmariadb/config.h>
  4. #include <cppmariadb/enums.h>
  5. #include <cppmariadb/impl/handle.h>
  6. namespace cppmariadb
  7. {
  8. /**
  9. * @brief MariaDB connection.
  10. */
  11. struct connection
  12. : public __impl::handle<MYSQL*>
  13. {
  14. private:
  15. using result_t = ::cppmariadb::result;
  16. using result_ptr_u = std::unique_ptr<result_t>;
  17. result_ptr_u _result; //!< REsult of the last query.
  18. public:
  19. /**
  20. * @breif Default constructor.
  21. */
  22. inline connection();
  23. /**
  24. * @brief Value constructor. Creates a new connection using the mariadb handle.
  25. */
  26. inline connection(MYSQL* h);
  27. /**
  28. * @brief Move constructor.
  29. */
  30. inline connection(connection&& other);
  31. /**
  32. * @brief Copy constructor.
  33. */
  34. inline connection(const connection& other) = delete;
  35. /**
  36. * @brief Destructor.
  37. */
  38. inline ~connection();
  39. /**
  40. * @brief Move assignment constructor.
  41. */
  42. inline connection& operator =(connection&& other);
  43. /**
  44. * @brief Copy assignment constructor.
  45. */
  46. inline connection& operator =(const connection& other) = delete;
  47. public: /* misc */
  48. /**
  49. * @brief Connect to the database.
  50. *
  51. * @param[in] host Host to connect to.
  52. * @param[in] port Port to connect to.
  53. * @param[in] user User to use for authentication.
  54. * @param[in] password Password to use for authentication.
  55. * @param[in] database Default database to connect to.
  56. * @param[in] flags Connection flags.
  57. */
  58. inline void connect(
  59. const std::string& host,
  60. const uint& port,
  61. const std::string& user,
  62. const std::string& password,
  63. const std::string& database,
  64. const client_flags& flags);
  65. /**
  66. * @brief Close this connection.
  67. */
  68. inline void close();
  69. /**
  70. * @brief Get the result of the last executed query.
  71. */
  72. inline result_t* result() const;
  73. /**
  74. * @brief Get the field count of the last executed query.
  75. */
  76. inline uint fieldcount() const;
  77. /**
  78. * @brief Get the error code of the last executed operation.
  79. */
  80. inline error_code error_code() const;
  81. /**
  82. * @brief Get the error code of the last executed operation.
  83. */
  84. inline std::string error_msg() const;
  85. /**
  86. * @brief Escape the passed string.
  87. *
  88. * @param[in] value String to escape.
  89. *
  90. * @return Escaped string.
  91. */
  92. inline std::string escape(const std::string& value) const;
  93. public: /* execute query */
  94. /**
  95. * @brief Execute the passed query.
  96. *
  97. * @param[in] cmd Query to execute.
  98. */
  99. inline void execute(const std::string& cmd);
  100. /**
  101. * @brief Execute the passed query.
  102. *
  103. * @param[in] cmd Query to execute.
  104. *
  105. * @return ID of the inserted/updated/effected row.
  106. */
  107. inline unsigned long long execute_id(const std::string& cmd);
  108. /**
  109. * @brief Execute the passed query.
  110. *
  111. * @param[in] cmd Query to execute.
  112. *
  113. * @return Number of affected rows.
  114. */
  115. inline unsigned long long execute_rows(const std::string& cmd);
  116. /**
  117. * @brief Execute the passed query.
  118. *
  119. * @param[in] cmd Query to execute.
  120. *
  121. * @return Stored result or nullptr if the query could not be executed.
  122. */
  123. inline result_stored* execute_stored(const std::string& cmd);
  124. /**
  125. * @brief Execute the passed query.
  126. *
  127. * @param[in] cmd Query to execute.
  128. *
  129. * @return Used result or nullptr if the query could not be executed.
  130. */
  131. inline result_used* execute_used(const std::string& cmd);
  132. public: /* execute statement */
  133. /**
  134. * @brief Render and execute the passed statement.
  135. *
  136. * @param[in] s Statement to render and execute.
  137. */
  138. inline void execute(const statement& s);
  139. /**
  140. * @brief Render and execute the passed statement.
  141. *
  142. * @param[in] s Statement to render and execute.
  143. *
  144. * @return ID of the inserted/updated/effected row.
  145. */
  146. inline unsigned long long execute_id(const statement& s);
  147. /**
  148. * @brief Render and execute the passed statement.
  149. *
  150. * @param[in] s Statement to render and execute.
  151. *
  152. * @return Number of affected rows.
  153. */
  154. inline unsigned long long execute_rows(const statement& s);
  155. /**
  156. * @brief Render and execute the passed statement.
  157. *
  158. * @param[in] s Statement to render and execute.
  159. *
  160. * @return Stored result of nullptr if the query could not be executed.
  161. */
  162. inline result_stored* execute_stored(const statement& s);
  163. /**
  164. * @brief Render and execute the passed statement.
  165. *
  166. * @param[in] s Statement to render and execute.
  167. *
  168. * @return Used result of nullptr if the query could not be executed.
  169. */
  170. inline result_used* execute_used(const statement& s);
  171. private:
  172. /**
  173. * @brief Execute the passed query and return the suitable result type.
  174. *
  175. * @tparam T Traits type that is used to execute the query and contains the result type.
  176. *
  177. * @param[in] cmd Query to execute.
  178. */
  179. template<class T>
  180. typename T::result_type* execute_internal(const std::string& cmd);
  181. };
  182. }