#pragma once
#include <cstdint>
#include <memory>
#include <string>
#include <mw/database.hpp>
#include <mw/error.hpp>
namespace telegrammer
{
/// Holds an advisory exclusive lock for one running Telegrammer daemon.
class DatabaseLock
{
public:
/// Acquire the non-blocking lock associated with a database path.
explicit DatabaseLock(const std::string& db_path);
/// Release the lock when the daemon has stopped.
~DatabaseLock();
DatabaseLock(const DatabaseLock&) = delete;
DatabaseLock& operator=(const DatabaseLock&) = delete;
private:
std::string lock_path_;
int fd_ = -1;
};
/// Open a configured SQLite connection for one short operation.
mw::E<std::unique_ptr<mw::SQLite>> openDatabase(const std::string& db_path);
/// Create the development schema and reject incompatible old layouts.
mw::E<void> initializeDatabase(const std::string& db_path);
/// Return the current wall-clock time as Unix seconds.
int64_t nowSeconds();
} // namespace telegrammer