From c5e0b01b065d8b6b680e067e56dbbc676aeebc6b Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 22 Aug 2020 01:57:14 +0100 Subject: [PATCH] Add OriginsDB base class and a make() function This fixes the build. The make function for the moment just takes a string since that's all that's ever needed. This is supposed to change since the postgresql database will need a port, an url, password etc. Maybe connection strings are still a thing, but if not then I'll probably need to find a way to accept arbitrary parameters in OriginsDB::make(). --- src/main.cpp | 10 ++++-- src/meson.build | 1 + src/oro/originsdb.cpp | 42 +++++++++++++++++++++++++ src/oro/originsdb.hpp | 46 ++++++++++++++++++++++++++++ src/oro/private/originsdb_sqlite.cpp | 16 +++++----- src/oro/private/originsdb_sqlite.hpp | 24 ++++++--------- 6 files changed, 114 insertions(+), 25 deletions(-) create mode 100644 src/oro/originsdb.cpp create mode 100644 src/oro/originsdb.hpp diff --git a/src/main.cpp b/src/main.cpp index c8c319a..e947104 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,10 +74,11 @@ int main(int argc, char* argv[]) { print_ping(oro_api); - oro::OriginsDB db(app_conf.db_path()); + std::unique_ptr db(oro::OriginsDB::make("sqlite", app_conf.db_path())); + duck::test( &oro_api, - &db, + db.get(), app_conf.fetch_extra_delay(), app_conf.worker_threads() ); @@ -87,6 +88,11 @@ int main(int argc, char* argv[]) { "\" failed: " << err.what() << '\n'; return 1; } + catch (const std::exception& err) { + std::cerr << "An error occurred during the program execution: " << + err.what() << '\n'; + return 1; + } /* { diff --git a/src/meson.build b/src/meson.build index 1d0eaad..419bd40 100644 --- a/src/meson.build +++ b/src/meson.build @@ -89,6 +89,7 @@ executable(meson.project_name(), 'eventia/signal.cpp', 'eventia/event.cpp', 'timer_oro_api.cpp', + 'oro/originsdb.cpp', project_config_file, install: true, dependencies: lib_deps, diff --git a/src/oro/originsdb.cpp b/src/oro/originsdb.cpp new file mode 100644 index 0000000..4fb5d8b --- /dev/null +++ b/src/oro/originsdb.cpp @@ -0,0 +1,42 @@ +/* Copyright 2020, Michele Santullo + * This file is part of orotool. + * + * Orotool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Orotool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Orotool. If not, see . + */ + +#include "oro/originsdb.hpp" +#include "orotool_config.hpp" +#if defined(OROTOOL_WITH_SQLITE) +# include "oro/private/originsdb_sqlite.hpp" +#endif +#include +#include + +namespace oro { + std::unique_ptr OriginsDB::make (std::string_view name, std::string_view param) { + if (false) { + return {}; + } +#if defined(OROTOOL_WITH_SQLITE) + else if (name == "sqlite") { + return std::make_unique(param); + } +#endif + else { + std::ostringstream oss; + oss << "Unable to create a databale backend of type \"" << name << '"'; + throw std::invalid_argument(oss.str()); + } + } +} //namespace oro diff --git a/src/oro/originsdb.hpp b/src/oro/originsdb.hpp new file mode 100644 index 0000000..98d22d4 --- /dev/null +++ b/src/oro/originsdb.hpp @@ -0,0 +1,46 @@ +/* Copyright 2020, Michele Santullo + * This file is part of orotool. + * + * Orotool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Orotool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Orotool. If not, see . + */ + +#pragma once + +#include "datatypes.hpp" +#include "oro/dboperation.hpp" +#include +#include + +namespace oro { + +struct Items; +struct Icons; +struct Shops; +struct Creators; + +class OriginsDB { +public: + OriginsDB() = default; + virtual ~OriginsDB() noexcept = default; + + virtual void update (const Items& items, const oro::Timestamp& next_update) = 0; + virtual void update (const Icons& icons, const oro::Timestamp& next_update) = 0; + virtual void update (const Shops& shops, const oro::Timestamp& next_update) = 0; + virtual void update (const Creators& creat, const oro::Timestamp& next_update) = 0; + + virtual Timestamp next_access_time (DBOperation op) const = 0; + + static std::unique_ptr make (std::string_view name, std::string_view param); +}; +} //namespace oro diff --git a/src/oro/private/originsdb_sqlite.cpp b/src/oro/private/originsdb_sqlite.cpp index 5f9234a..4fff2c7 100644 --- a/src/oro/private/originsdb_sqlite.cpp +++ b/src/oro/private/originsdb_sqlite.cpp @@ -18,7 +18,7 @@ #include "orotool_config.hpp" #if defined(OROTOOL_WITH_SQLITE) -#include "originsdb.hpp" +#include "originsdb_sqlite.hpp" #include "SQLiteCpp/Database.h" #include "SQLiteCpp/Statement.h" #include "SQLiteCpp/Transaction.h" @@ -324,7 +324,7 @@ namespace { } } //unnamed namespace -OriginsDB::OriginsDB (std::string_view path) : +OriginsDBSQLite::OriginsDBSQLite (std::string_view path) : m_path(path.data(), path.size()), m_db(std::make_unique(m_path, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)) { @@ -334,9 +334,9 @@ OriginsDB::OriginsDB (std::string_view path) : } } -OriginsDB::~OriginsDB() noexcept = default; +OriginsDBSQLite::~OriginsDBSQLite() noexcept = default; -void OriginsDB::update (const Items& items, const oro::Timestamp& next_update) { +void OriginsDBSQLite::update (const Items& items, const oro::Timestamp& next_update) { auto& db = *m_db; std::unique_lock lock(m_db_mutex); update_last_access(db, DBOperation::Items, next_update); @@ -396,7 +396,7 @@ void OriginsDB::update (const Items& items, const oro::Timestamp& next_update) { transaction.commit(); } -void OriginsDB::update (const Icons& icons, const oro::Timestamp& next_update) { +void OriginsDBSQLite::update (const Icons& icons, const oro::Timestamp& next_update) { //example: //{ // "item_id":501, @@ -437,7 +437,7 @@ void OriginsDB::update (const Icons& icons, const oro::Timestamp& next_update) { transaction.commit(); } -void OriginsDB::update (const Shops& shops, const oro::Timestamp& next_update) { +void OriginsDBSQLite::update (const Shops& shops, const oro::Timestamp& next_update) { //example: //{ // "title":"• B\u003ePoison Bottle •", @@ -520,7 +520,7 @@ void OriginsDB::update (const Shops& shops, const oro::Timestamp& next_update) { transaction.commit(); } -void OriginsDB::update (const Creators& creat, const oro::Timestamp& next_update) { +void OriginsDBSQLite::update (const Creators& creat, const oro::Timestamp& next_update) { std::unique_lock lock(m_db_mutex); auto& db = *m_db; update_last_access(db, DBOperation::Creators, next_update); @@ -543,7 +543,7 @@ void OriginsDB::update (const Creators& creat, const oro::Timestamp& next_update transaction.commit(); } -Timestamp OriginsDB::next_access_time (DBOperation op) const { +Timestamp OriginsDBSQLite::next_access_time (DBOperation op) const { constexpr auto select_last_acces_str = make_select_last_acces_str(); SQLite::Statement sel_last( *m_db, diff --git a/src/oro/private/originsdb_sqlite.hpp b/src/oro/private/originsdb_sqlite.hpp index 0fa765f..5fcd704 100644 --- a/src/oro/private/originsdb_sqlite.hpp +++ b/src/oro/private/originsdb_sqlite.hpp @@ -17,8 +17,7 @@ #pragma once -#include "datatypes.hpp" -#include "oro/dboperation.hpp" +#include "oro/originsdb.hpp" #include #include #include @@ -30,22 +29,17 @@ class Database; namespace oro { -struct Items; -struct Icons; -struct Shops; -struct Creators; - -class OriginsDB { +class OriginsDBSQLite : public OriginsDB { public: - explicit OriginsDB(std::string_view path); - ~OriginsDB() noexcept; + explicit OriginsDBSQLite(std::string_view path); + virtual ~OriginsDBSQLite() noexcept; - void update (const Items& items, const oro::Timestamp& next_update); - void update (const Icons& icons, const oro::Timestamp& next_update); - void update (const Shops& shops, const oro::Timestamp& next_update); - void update (const Creators& creat, const oro::Timestamp& next_update); + void update (const Items& items, const oro::Timestamp& next_update) override; + void update (const Icons& icons, const oro::Timestamp& next_update) override; + void update (const Shops& shops, const oro::Timestamp& next_update) override; + void update (const Creators& creat, const oro::Timestamp& next_update) override; - Timestamp next_access_time (DBOperation op) const; + Timestamp next_access_time (DBOperation op) const override; private: std::string m_path;