6 #include "tgbotxx/utils/DateTimeUtils.hpp"
7 #include "sqlite_orm/sqlite_orm.h"
10 namespace fs = std::filesystem;
11 using namespace sqlite_orm;
16 static std::mutex m_mutex;
26 static auto& getStorage() {
27 static auto storage = make_storage(fs::path(RES_DIR) /
"Database.db",
32 static bool schemaSynced =
false;
33 if (!storage.on_open) {
34 storage.on_open = []([[maybe_unused]] sqlite3 *handle) {
35 if (not schemaSynced) {
36 int rc = sqlite3_exec(handle,
"PRAGMA synchronous=NORMAL;"
37 "PRAGMA locking_mode=EXCLUSIVE;"
38 "PRAGMA journal_mode=WAL;"
39 "PRAGMA cache_size=50000;"
40 "PRAGMA temp_store=MEMORY;"
41 "PRAGMA auto_vacuum=0;",
42 nullptr,
nullptr,
nullptr);
43 if (rc != SQLITE_OK) {
44 sqlite_orm::throw_translated_sqlite_error(handle);
46 storage.sync_schema(
true);
static std::vector< models::Repository > getUserRepos(const models::UserId watcherId)
Returns all Repositories objects that a User is watching.
static models::User getUser(const models::UserId userId)
Returns User object by id.
static void removeUserRepo(const models::UserId watcherId, const models::RepositoryId repoId)
Removes Repository from User's watch list.
static void addUser(const models::User &newUser)
Adds a new user to the database.
static bool repoExists(const models::RepositoryId repoId)
Returns true if a Repository exists with same id.
static void updateUserStatus(const models::UserId userId, const models::UserStatus newStatus)
Update User's status by id.
static std::int64_t addLog(const models::Log &newLog)
Adds a new Log object to the database.
static void addRepo(const models::Repository &newRepo)
Adds a new Repository object to the database.
static void iterateRepos(const std::function< void(const models::Repository &)> &callback)
Lock secure iterate over repositories to not hold the db mutex for a long time.
static void updateUser(const models::User &updatedUser)
Updates existing user changed properties.
static void backup()
Call this periodically to backup the database in res/DbBackups/ periodically.
static bool userExists(const models::UserId userId)
Returns true if User with id exists in the database.
static void updateRepo(const models::Repository &updatedRepo)
Updates existing repository changed properties.
static models::UserStatus getUserStatus(const models::UserId userId)
Returns UserStatus by id.
static int userReposCount(const models::UserId userId)
Returns the count of repositories this user is watching.
static bool repoExistsByFullName(const std::string &full_name)
Returns true if Repository exists with same full_name (example: "torvalds/linux")
static std::vector< std::string > getUserReposFullnames(const models::UserId watcherId)
Returns full names of all repositories that a User is watching.
static std::mutex & getDbMutex() noexcept
Returns Database mutex to be used by multiple threads for writing/reading into/from the database.
std::int64_t RepositoryId
decltype(tgbotxx::User::id) UserId