MultipleThreads.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "MultipleThreads.h"
  2. #include "MySQLClient.h"
  3. #include <Logging.h>
  4. #include <sstream>
  5. namespace Test {
  6. bool Thread1()
  7. {
  8. std::string hostname1 = "MySQL";
  9. std::string username1 = "datalog";
  10. std::string password1 = "NfhdUwjjdRbslR";
  11. std::string database1 = "datalog";
  12. MySQL::MySQLClient MySQLClient1;
  13. MySQLClient1.Connect(hostname1, username1, password1, database1);
  14. if (!MySQLClient1.Connected())
  15. return false;
  16. std::stringstream query1;
  17. query1 << "SELECT * FROM `datalog` LIMIT 1;";
  18. auto result1 = MySQLClient1.ExecuteQuery(query1.str());
  19. if (result1.RowsCount() == 0)
  20. return false;
  21. result1.First();
  22. auto id1 = result1.Int("device_id");
  23. if (id1 == 0)
  24. return false;
  25. return true;
  26. }
  27. bool Thread2()
  28. {
  29. std::string hostname2 = "Pi";
  30. std::string username2 = "root";
  31. std::string password2 = "Wh1sK3y";
  32. std::string database2 = "domotica";
  33. MySQL::MySQLClient MySQLClient2;
  34. MySQLClient2.Connect(hostname2, username2, password2, database2);
  35. if (!MySQLClient2.Connected())
  36. return false;
  37. std::stringstream query2;
  38. query2 << "SELECT * FROM `device` LIMIT 1;";
  39. auto result2 = MySQLClient2.ExecuteQuery(query2.str());
  40. if (result2.RowsCount() == 0)
  41. return false;
  42. result2.First();
  43. auto id2 = result2.Int("id");
  44. if (id2 == 0)
  45. return false;
  46. return true;
  47. }
  48. bool MultipleThreads()
  49. {
  50. Logging::Log(Logging::Severity::Info, "MultipleThreads");
  51. try
  52. {
  53. bool thread1 = [] { Thread1(); };
  54. bool thread2 = [] { Thread2(); };
  55. if (!thread1 || !thread2)
  56. return false;
  57. }
  58. catch (const std::exception& e)
  59. {
  60. std::stringstream ss;
  61. ss << "ERROR: " << e.what() << std::endl;
  62. Logging::Log(Logging::Severity::Error, ss.str());
  63. return false;
  64. }
  65. return true;
  66. }
  67. } // namespace Test