summaryrefslogtreecommitdiffhomepage
path: root/storage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'storage.cpp')
-rw-r--r--storage.cpp23
1 files changed, 18 insertions, 5 deletions
diff --git a/storage.cpp b/storage.cpp
index ffdb2f4..6fb5b64 100644
--- a/storage.cpp
+++ b/storage.cpp
@@ -43,12 +43,22 @@ uint64_t Storage::getNumberOfDocuments()
return m_stmt_getNumberOfDocuments.getColumn<int64_t>(0);
}
+namespace {
+ uint64_t unixepoch()
+ {
+ const auto p1 = std::chrono::system_clock::now();
+ return std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count();
+ }
+} // namespace
+
void Storage::cleanup()
{
if (m_maxage == 0)
return;
- m_stmt_cleanup.init("DELETE FROM documents WHERE timestamp + "s + std::to_string(static_cast<int64_t>(m_maxage)) + " < unixepoch()"s);
+ m_stmt_cleanup.init("DELETE FROM documents WHERE timestamp + ? < ?");
+ m_stmt_cleanup.bind(1, static_cast<int64_t>(m_maxage));
+ m_stmt_cleanup.bind(2, static_cast<int64_t>(unixepoch()));
m_stmt_cleanup.execute();
}
@@ -106,16 +116,18 @@ std::tuple<std::string, int, int> Storage::getRow(const std::string& id)
void Storage::setDocument(const std::string& id, const std::string& document)
{
- m_stmt_setDocument.init("UPDATE documents SET value = ? WHERE id = ?");
+ m_stmt_setDocument.init("UPDATE documents SET value = ?, timestamp = ?, rev = rev + 1 WHERE id = ?");
m_stmt_setDocument.bind(1, document);
- m_stmt_setDocument.bind(2, id);
+ m_stmt_setDocument.bind(2, static_cast<int64_t>(unixepoch()));
+ m_stmt_setDocument.bind(3, id);
if (!m_stmt_setDocument.execute()) {
- m_stmt_setDocument_new.init("INSERT INTO documents (id, value, rev, cursorpos, timestamp) values (?, ?, ?, ?, unixepoch())");
+ m_stmt_setDocument_new.init("INSERT INTO documents (id, value, rev, cursorpos, timestamp) values (?, ?, ?, ?, ?)");
m_stmt_setDocument_new.bind(1, id);
m_stmt_setDocument_new.bind(2, document);
m_stmt_setDocument_new.bind(3, 0);
m_stmt_setDocument_new.bind(4, 0);
+ m_stmt_setDocument_new.bind(5, static_cast<int64_t>(unixepoch()));
m_stmt_setDocument_new.execute();
}
}
@@ -142,11 +154,12 @@ void Storage::setCursorPos(const std::string& id, int cursorPos)
void Storage::setRow(const std::string& id, const std::string& document, int rev, int cursorPos)
{
- m_stmt_setRow.init("INSERT OR REPLACE INTO documents (id, value, rev, cursorpos, timestamp) values (?, ?, ?, ?, unixepoch())");
+ m_stmt_setRow.init("INSERT OR REPLACE INTO documents (id, value, rev, cursorpos, timestamp) values (?, ?, ?, ?, ?)");
m_stmt_setRow.bind(1, id);
m_stmt_setRow.bind(2, document);
m_stmt_setRow.bind(3, rev);
m_stmt_setRow.bind(4, cursorPos);
+ m_stmt_setRow.bind(5, static_cast<int64_t>(unixepoch()));
if (!m_stmt_setRow.execute())
throw std::runtime_error("Unable to insert row with id "s + id);
}