MySQLClientImpl.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. InternalConnect();
  65. else
  66. throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server");
  67. }
  68. try
  69. {
  70. std::unique_ptr<sql::Statement> statement(m_pConnection->createStatement());
  71. statement->execute(query);
  72. }
  73. catch (sql::SQLException &e)
  74. {
  75. std::stringstream ss;
  76. ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl;
  77. ss << "Query: " << query << std::endl;
  78. ss << "(MySQL error code: " << e.getErrorCode();
  79. ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
  80. Logging::Log(Logging::Severity::Error, ss.str());
  81. int errorCode = e.getErrorCode();
  82. if (errorCode == 2006 || errorCode == 2013)
  83. InternalDisconnect();
  84. }
  85. catch (const std::exception& e)
  86. {
  87. std::stringstream ss;
  88. ss << "MySQLClientImpl::Execute() - Error: " << e.what() << std::endl;
  89. ss << "Query: " << query << std::endl;
  90. Logging::Log(Logging::Severity::Error, ss.str());
  91. InternalDisconnect();
  92. }
  93. }
  94. std::shared_ptr<MySQLResultSetImpl> MySQLClientImpl::ExecuteQuery(const std::string& query)
  95. {
  96. std::unique_lock<std::mutex> lock(m_mutex);
  97. if (!m_connected)
  98. {
  99. if (m_automaticReconnect)
  100. {
  101. InternalConnect();
  102. if (!m_connected)
  103. {
  104. std::stringstream ss;
  105. ss << "MySQLClientImpl::ExecuteQuery() - Error: Reconnect failed" << std::endl;
  106. ss << "Query: " << query << std::endl;
  107. Logging::Log(Logging::Severity::Error, ss.str());
  108. }
  109. }
  110. else
  111. {
  112. throw std::runtime_error("MySQLClientImpl::ExecuteQuery() - Not connected to a Database Server");
  113. }
  114. }
  115. try
  116. {
  117. std::unique_ptr<sql::Statement> statement(m_pConnection->createStatement());
  118. std::shared_ptr<sql::ResultSet> pResult(statement->executeQuery(query));
  119. return std::make_shared<MySQLResultSetImpl>(pResult);
  120. }
  121. catch (sql::SQLException &e)
  122. {
  123. std::stringstream ss;
  124. ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl;
  125. ss << "Query: " << query << std::endl;
  126. ss << "(MySQL error code: " << e.getErrorCode();
  127. ss << ", SQLState: " << e.getSQLState() << " )" << std::endl;
  128. Logging::Log(Logging::Severity::Error, ss.str());
  129. int errorCode = e.getErrorCode();
  130. if (errorCode == 2006 || errorCode == 2013)
  131. InternalDisconnect();
  132. }
  133. catch (const std::exception& e)
  134. {
  135. std::stringstream ss;
  136. ss << "MySQLClientImpl::ExecuteQuery() - Error: " << e.what() << std::endl;
  137. ss << "Query: " << query << std::endl;
  138. Logging::Log(Logging::Severity::Error, ss.str());
  139. InternalDisconnect();
  140. }
  141. return std::make_shared<MySQLResultSetImpl>(nullptr);
  142. }
  143. void MySQLClientImpl::InternalConnect()
  144. {
  145. try
  146. {
  147. if (m_pConnection)
  148. InternalDisconnect();
  149. std::string database;
  150. if (!m_database.empty())
  151. database = m_database;
  152. else
  153. database = "information_schema";
  154. sql::ConnectOptionsMap options;
  155. options["hostName"] = m_hostname;
  156. options["userName"] = m_username;
  157. options["password"] = m_password;
  158. options["schema"] = database;
  159. options["port"] = 3306;
  160. options["OPT_RECONNECT"] = m_automaticReconnect;
  161. m_pConnection = std::shared_ptr<sql::Connection>(m_pDriver->connect(options));
  162. m_connected = true;
  163. }
  164. catch (sql::SQLException &e)
  165. {
  166. std::stringstream ss;
  167. ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
  168. Logging::Log(Logging::Severity::Error, ss.str());
  169. InternalDisconnect();
  170. }
  171. catch (const std::exception& e)
  172. {
  173. std::stringstream ss;
  174. ss << "MySQLClientImpl::Connect() - Error: " << e.what() << std::endl;
  175. Logging::Log(Logging::Severity::Error, ss.str());
  176. InternalDisconnect();
  177. }
  178. }
  179. void MySQLClientImpl::InternalDisconnect()
  180. {
  181. m_pConnection.reset();
  182. m_connected = false;
  183. }
  184. } // namespace MySQL