63 lines
2.3 KiB
C++
63 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "Models.h"
|
|
|
|
struct sqlite3;
|
|
|
|
// Works on an in-memory SQLite database that is loaded from, and saved to, a
|
|
// file on request (db save / backup / restore).
|
|
class Database {
|
|
public:
|
|
Database();
|
|
~Database();
|
|
Database(const Database&) = delete;
|
|
Database& operator=(const Database&) = delete;
|
|
|
|
// Loads `path` into memory if it exists, otherwise starts empty.
|
|
void open(const std::string& path);
|
|
void save();
|
|
std::string backup(const std::string& file); // empty = timestamped name; returns the file used
|
|
void restore(const std::string& file);
|
|
|
|
bool dirty() const;
|
|
const std::string& path() const;
|
|
|
|
// Accounts
|
|
std::optional<std::string> findAccount(const std::string& name) const; // canonical spelling
|
|
std::vector<std::string> listAccounts() const;
|
|
void addAccount(const std::string& name);
|
|
void removeAccount(const std::string& name);
|
|
void renameAccount(const std::string& name, const std::string& newName);
|
|
std::optional<AccountRow> getAccount(const std::string& name) const;
|
|
std::vector<AccountRow> listAccountRows() const;
|
|
void setAccountCooldown(const std::string& name, const std::optional<std::string>& cooldown);
|
|
|
|
// Tasks
|
|
std::optional<std::string> findTask(const std::string& name) const; // canonical spelling
|
|
std::optional<TaskRow> getTask(const std::string& name) const;
|
|
std::vector<TaskRow> listTasks() const;
|
|
void addTask(const std::string& name, const std::optional<std::string>& account, const std::string& status);
|
|
void removeTask(const std::string& name);
|
|
void renameTask(const std::string& name, const std::string& newName);
|
|
void setTaskAccount(const std::string& name, const std::optional<std::string>& account);
|
|
void setTaskStatus(const std::string& name, const std::string& status);
|
|
void setTaskNotes(const std::string& name, const std::string& notes);
|
|
|
|
private:
|
|
using Param = std::optional<std::string>;
|
|
using Row = std::vector<std::optional<std::string>>;
|
|
|
|
void createSchema();
|
|
void exec(const std::string& sql);
|
|
std::vector<Row> query(const std::string& sql, const std::vector<Param>& params = {}) const;
|
|
void run(const std::string& sql, const std::vector<Param>& params);
|
|
|
|
sqlite3* db_;
|
|
std::string path_;
|
|
bool dirty_;
|
|
};
|