MultipleConnections.cpp 1.5 KB

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