MySQLClientImpl.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "MySQLClientImpl.h"
  2. #include "MySQLResultSetImpl.h"
  3. #include <Logging.h>
  4. #include <sstream>
  5. namespace MySQL {
  6. MySQLClientImpl::MySQLClientImpl() :
  7. m_connected(false),
  8. m_automaticReconnect(false),
  9. m_pDriver(sql::mysql::get_mysql_driver_instance())
  10. {
  11. }
  12. MySQLClientImpl::~MySQLClientImpl()
  13. {
  14. }
  15. void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, bool automaticReconnect)
  16. {
  17. std::unique_lock<std::mutex> lock(m_mutex);
  18. m_hostname = hostname;
  19. m_username = username;
  20. m_password = password;
  21. m_database = "";
  22. m_automaticReconnect = automaticReconnect;
  23. InternalConnect();
  24. }
  25. void MySQLClientImpl::Connect(const std::string& hostname, const std::string& username, const std::string& password, const std::string& database, bool automaticReconnect)
  26. {
  27. std::unique_lock<std::mutex> lock(m_mutex);
  28. m_hostname = hostname;
  29. m_username = username;
  30. m_password = password;
  31. m_database = database;
  32. m_automaticReconnect = automaticReconnect;
  33. InternalConnect();
  34. }
  35. void MySQLClientImpl::Disconnect()
  36. {
  37. std::unique_lock<std::mutex> lock(m_mutex);
  38. InternalDisconnect();
  39. }
  40. void MySQLClientImpl::Reconnect(bool automaticReconnect)
  41. {
  42. std::unique_lock<std::mutex> lock(m_mutex);
  43. InternalDisconnect();
  44. m_automaticReconnect = automaticReconnect;
  45. InternalConnect();
  46. }
  47. bool MySQLClientImpl::Connected() const
  48. {
  49. std::unique_lock<std::mutex> lock(m_mutex);
  50. return m_connected;
  51. }
  52. std::string MySQLClientImpl::Database() const
  53. {
  54. if (!m_connected)
  55. return std::string();
  56. return m_database;
  57. }
  58. void MySQLClientImpl::Execute(const std::string& query)
  59. {
  60. std::unique_lock<std::mutex> lock(m_mutex);
  61. if (!m_connected)
  62. {
  63. if (m_automaticReconnect)
  64. {
  65. InternalConnect();
  66. if (!m_connected)
  67. {
  68. std::stringstream ss;
  69. ss << "MySQLClientImpl::Execute() - Error: Reconnect failed" << std::endl;
  70. ss << "Query: " << query << std::endl;
  71. Logging::Log(Logging::Severity::Error, ss.str());
  72. return;
  73. }
  74. }
  75. else
  76. {
  77. throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server");
  78. }
  79. }
  80. try
  81. {
  82. std::unique_ptr<sql::Statement> statement(m_pConnection->createStatement());
  83. statement->execute(query);
  84. }
  85. catch (sql::SQLException &e)
  86. {
  87. std::stringstream ss;
  88. ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl;
  89. ss << "Query: " << query << std::endl;
  90. ss << "(MySQL error code: " << e.getErrorCode();
  91. ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
  92. Logging::Log(Logging::Severity::Error, ss.str());
  93. int errorCode = e.getErrorCode();
  94. if (errorCode == 2006 || errorCode == 2013)
  95. InternalDisconnect();
  96. }
  97. catch (const std::exception& e)
  98. {
  99. std::stringstream ss;
  100. ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl;
  101. ss << "Query: " << query << std::endl;
  102. Logging::Log(Logging::Severity::Error, ss.str());
  103. InternalDisconnect();
  104. }
  105. }
  106. std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::string& query)
  107. {
  108. std::unique_lock<std::mutex> lock(m_mutex);
  109. if (!m_connected)
  110. {
  111. if (m_automaticReconnect)
  112. {
  113. InternalConnect();
  114. if (!m_connected)
  115. {
  116. std::stringstream ss;
  117. ss << "MySQLClientImpl::ExecuteQuery() - Error: Reconnect failed" << std::endl;
  118. ss << "Query: " << query << std::endl;
  119. Logging::Log(Logging::Severity::Error, ss.str());
  120. }
  121. }
  122. else
  123. {
  124. throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server");
  125. }
  126. }
  127. try
  128. {
  129. std::unique_ptr<sql::Statement> statement(m_pConnection->createStatement());
  130. std::shared_ptr<sql::ResultSet> pResult(statement->executeQuery(query));
  131. return std::make_shared<MySQLResultSetImpl>(pResult);
  132. }
  133. catch (sql::SQLException &e)
  134. {
  135. std::stringstream ss;
  136. ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl;
  137. ss << "Query: " << query << std::endl;
  138. ss << "(MySQL error code: " << e.getErrorCode();
  139. ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
  140. Logging::Log(Logging::Severity::Error, ss.str());
  141. int errorCode = e.getErrorCode();
  142. if (errorCode == 2006 || errorCode == 2013)
  143. InternalDisconnect();
  144. }
  145. catch (const std::exception& e)
  146. {
  147. std::stringstream ss;
  148. ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl;
  149. ss << "Query: " << query << std::endl;
  150. Logging::Log(Logging::Severity::Error, ss.str());
  151. InternalDisconnect();
  152. }
  153. return std::make_shared<MySQLResultSetImpl>(nullptr);
  154. }
  155. void MySQLClientImpl::InternalConnect()
  156. {
  157. try
  158. {
  159. if (m_pConnection)
  160. InternalDisconnect();
  161. std::string database;
  162. if (!m_database.empty())
  163. database = m_database;
  164. else
  165. database = "information_schema";
  166. sql::ConnectOptionsMap options;
  167. options["hostName"] = m_hostname;
  168. options["userName"] = m_username;
  169. options["password"] = m_password;
  170. options["schema"] = database;
  171. options["port"] = 3306;
  172. options["OPT_RECONNECT"] = m_automaticReconnect;
  173. m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(options));
  174. m_connected = true;
  175. }
  176. catch (sql::SQLException &e)
  177. {
  178. std::stringstream ss;
  179. ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
  180. Logging::Log(Logging::Severity::Error, ss.str());
  181. InternalDisconnect();
  182. }
  183. catch (const std::exception& e)
  184. {
  185. std::stringstream ss;
  186. ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
  187. Logging::Log(Logging::Severity::Error, ss.str());
  188. InternalDisconnect();
  189. }
  190. }
  191. void MySQLClientImpl::InternalDisconnect()
  192. {
  193. m_pConnection.reset();
  194. m_connected = false;
  195. }
  196. } // namespace MySQL