Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Created by robin on 8/9/15.
  3. //
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fstream>
  8. #include "MySql.h"
  9. #define tmpTemplate "/tmp/pdns-slave.XXXXXX"
  10. MySql::MySql(const SqlConfiguration &masterConfig, const SqlConfiguration &slaveConfig)
  11. : AbstractSql(masterConfig, slaveConfig)
  12. {
  13. }
  14. MySql::~MySql()
  15. {
  16. if (!_dumpFilePath.empty())
  17. {
  18. unlink(_dumpFilePath.c_str());
  19. _dumpFilePath = "";
  20. }
  21. if (!_overrideFilePath.empty())
  22. {
  23. unlink(_overrideFilePath.c_str());
  24. _overrideFilePath = "";
  25. }
  26. }
  27. BResult MySql::dump()
  28. {
  29. BResult res;
  30. _dumpFilePath = getTempFile();
  31. int status = system(std::string("mysqldump '-h" + _masterConfig.getHost() + "'"
  32. + " '-u" + _masterConfig.getUser() + "'"
  33. + " '-p" + _masterConfig.getPassword() + "'"
  34. + " " + _masterConfig.getDatabase()
  35. + " > " + _dumpFilePath).c_str());
  36. if (status == 0)
  37. return res.ok(true);
  38. return res.error("mysqldump exited with code " + std::to_string(status));
  39. }
  40. const std::string MySql::getTempFile()
  41. {
  42. char tmp[sizeof(tmpTemplate)];
  43. strcpy(tmp, tmpTemplate);
  44. mkstemp(tmp);
  45. return std::string(tmp);
  46. }
  47. BResult MySql::insert()
  48. {
  49. return insertSlave(_dumpFilePath);
  50. }
  51. BResult MySql::override(const std::string &sql)
  52. {
  53. _overrideFilePath = getTempFile();
  54. std::ofstream file(_overrideFilePath);
  55. if (!file)
  56. return BResult().error("Could not open temp file " + _overrideFilePath);
  57. file << sql;
  58. file.close();
  59. return insertSlave(_overrideFilePath);
  60. }
  61. BResult MySql::insertSlave(const std::string &file)
  62. {
  63. BResult res;
  64. int status = system(std::string("cat " + file + " | mysql '-h" + _slaveConfig.getHost() + "'"
  65. + " '-u" + _slaveConfig.getUser() + "'"
  66. + " '-p" + _slaveConfig.getPassword() + "'"
  67. + " " + _slaveConfig.getDatabase()).c_str());
  68. if (status == 0)
  69. return res.ok(true);
  70. return res.error("mysql exited with code " + std::to_string(status));
  71. }