ArchiveStrategy.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // ArchiveStrategy.cpp
  3. //
  4. // Library: Data
  5. // Package: Logging
  6. // Module: ArchiveStrategy
  7. //
  8. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/Data/ArchiveStrategy.h"
  14. #include "Poco/Ascii.h"
  15. namespace Poco {
  16. namespace Data {
  17. using namespace Keywords;
  18. //
  19. // ArchiveStrategy
  20. //
  21. const std::string ArchiveStrategy::DEFAULT_ARCHIVE_DESTINATION = "T_POCO_LOG_ARCHIVE";
  22. ArchiveStrategy::ArchiveStrategy(const std::string& connector,
  23. const std::string& connect,
  24. const std::string& source,
  25. const std::string& destination):
  26. _connector(connector),
  27. _connect(connect),
  28. _source(source),
  29. _destination(destination)
  30. {
  31. open();
  32. }
  33. ArchiveStrategy::~ArchiveStrategy()
  34. {
  35. }
  36. void ArchiveStrategy::open()
  37. {
  38. if (_connector.empty() || _connect.empty())
  39. throw IllegalStateException("Connector and connect string must be non-empty.");
  40. _pSession = new Session(_connector, _connect);
  41. }
  42. //
  43. // ArchiveByAgeStrategy
  44. //
  45. ArchiveByAgeStrategy::ArchiveByAgeStrategy(const std::string& connector,
  46. const std::string& connect,
  47. const std::string& sourceTable,
  48. const std::string& destinationTable,
  49. const std::string& age):
  50. ArchiveStrategy(connector, connect, sourceTable, destinationTable)
  51. {
  52. initStatements();
  53. if (!age.empty()) setThreshold(age);
  54. }
  55. ArchiveByAgeStrategy::~ArchiveByAgeStrategy()
  56. {
  57. }
  58. void ArchiveByAgeStrategy::archive()
  59. {
  60. if (!session().isConnected()) open();
  61. DateTime now;
  62. _archiveDateTime = now - _maxAge;
  63. getCountStatement().execute();
  64. if (_archiveCount > 0)
  65. {
  66. getCopyStatement().execute();
  67. getDeleteStatement().execute();
  68. }
  69. }
  70. void ArchiveByAgeStrategy::initStatements()
  71. {
  72. std::string src = getSource();
  73. std::string dest = getDestination();
  74. setCountStatement();
  75. _archiveCount = 0;
  76. std::string sql;
  77. Poco::format(sql, "SELECT COUNT(*) FROM %s WHERE DateTime < ?", src);
  78. getCountStatement() << sql, into(_archiveCount), use(_archiveDateTime);
  79. setCopyStatement();
  80. sql.clear();
  81. Poco::format(sql, "INSERT INTO %s SELECT * FROM %s WHERE DateTime < ?", dest, src);
  82. getCopyStatement() << sql, use(_archiveDateTime);
  83. setDeleteStatement();
  84. sql.clear();
  85. Poco::format(sql, "DELETE FROM %s WHERE DateTime < ?", src);
  86. getDeleteStatement() << sql, use(_archiveDateTime);
  87. }
  88. void ArchiveByAgeStrategy::setThreshold(const std::string& age)
  89. {
  90. std::string::const_iterator it = age.begin();
  91. std::string::const_iterator end = age.end();
  92. int n = 0;
  93. while (it != end && Ascii::isSpace(*it)) ++it;
  94. while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; }
  95. while (it != end && Ascii::isSpace(*it)) ++it;
  96. std::string unit;
  97. while (it != end && Ascii::isAlpha(*it)) unit += *it++;
  98. Timespan::TimeDiff factor = Timespan::SECONDS;
  99. if (unit == "minutes")
  100. factor = Timespan::MINUTES;
  101. else if (unit == "hours")
  102. factor = Timespan::HOURS;
  103. else if (unit == "days")
  104. factor = Timespan::DAYS;
  105. else if (unit == "weeks")
  106. factor = 7*Timespan::DAYS;
  107. else if (unit == "months")
  108. factor = 30*Timespan::DAYS;
  109. else if (unit != "seconds")
  110. throw InvalidArgumentException("setMaxAge", age);
  111. _maxAge = factor * n;
  112. }
  113. } } // namespace Poco::Data