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().
This commit is contained in:
King_DuckZ 2020-08-22 01:57:14 +01:00
parent 217c844893
commit c5e0b01b06
6 changed files with 114 additions and 25 deletions

View file

@ -74,10 +74,11 @@ int main(int argc, char* argv[]) {
print_ping(oro_api); print_ping(oro_api);
oro::OriginsDB db(app_conf.db_path()); std::unique_ptr<oro::OriginsDB> db(oro::OriginsDB::make("sqlite", app_conf.db_path()));
duck::test( duck::test(
&oro_api, &oro_api,
&db, db.get(),
app_conf.fetch_extra_delay(), app_conf.fetch_extra_delay(),
app_conf.worker_threads() app_conf.worker_threads()
); );
@ -87,6 +88,11 @@ int main(int argc, char* argv[]) {
"\" failed: " << err.what() << '\n'; "\" failed: " << err.what() << '\n';
return 1; return 1;
} }
catch (const std::exception& err) {
std::cerr << "An error occurred during the program execution: " <<
err.what() << '\n';
return 1;
}
/* /*
{ {

View file

@ -89,6 +89,7 @@ executable(meson.project_name(),
'eventia/signal.cpp', 'eventia/signal.cpp',
'eventia/event.cpp', 'eventia/event.cpp',
'timer_oro_api.cpp', 'timer_oro_api.cpp',
'oro/originsdb.cpp',
project_config_file, project_config_file,
install: true, install: true,
dependencies: lib_deps, dependencies: lib_deps,

42
src/oro/originsdb.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "oro/originsdb.hpp"
#include "orotool_config.hpp"
#if defined(OROTOOL_WITH_SQLITE)
# include "oro/private/originsdb_sqlite.hpp"
#endif
#include <exception>
#include <sstream>
namespace oro {
std::unique_ptr<OriginsDB> 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<OriginsDBSQLite>(param);
}
#endif
else {
std::ostringstream oss;
oss << "Unable to create a databale backend of type \"" << name << '"';
throw std::invalid_argument(oss.str());
}
}
} //namespace oro

46
src/oro/originsdb.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "datatypes.hpp"
#include "oro/dboperation.hpp"
#include <string_view>
#include <memory>
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<OriginsDB> make (std::string_view name, std::string_view param);
};
} //namespace oro

View file

@ -18,7 +18,7 @@
#include "orotool_config.hpp" #include "orotool_config.hpp"
#if defined(OROTOOL_WITH_SQLITE) #if defined(OROTOOL_WITH_SQLITE)
#include "originsdb.hpp" #include "originsdb_sqlite.hpp"
#include "SQLiteCpp/Database.h" #include "SQLiteCpp/Database.h"
#include "SQLiteCpp/Statement.h" #include "SQLiteCpp/Statement.h"
#include "SQLiteCpp/Transaction.h" #include "SQLiteCpp/Transaction.h"
@ -324,7 +324,7 @@ namespace {
} }
} //unnamed namespace } //unnamed namespace
OriginsDB::OriginsDB (std::string_view path) : OriginsDBSQLite::OriginsDBSQLite (std::string_view path) :
m_path(path.data(), path.size()), m_path(path.data(), path.size()),
m_db(std::make_unique<SQLite::Database>(m_path, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)) m_db(std::make_unique<SQLite::Database>(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; auto& db = *m_db;
std::unique_lock<std::mutex> lock(m_db_mutex); std::unique_lock<std::mutex> lock(m_db_mutex);
update_last_access(db, DBOperation::Items, next_update); 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(); 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: //example:
//{ //{
// "item_id":501, // "item_id":501,
@ -437,7 +437,7 @@ void OriginsDB::update (const Icons& icons, const oro::Timestamp& next_update) {
transaction.commit(); 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: //example:
//{ //{
// "title":"• B\u003ePoison Bottle •", // "title":"• B\u003ePoison Bottle •",
@ -520,7 +520,7 @@ void OriginsDB::update (const Shops& shops, const oro::Timestamp& next_update) {
transaction.commit(); 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<std::mutex> lock(m_db_mutex); std::unique_lock<std::mutex> lock(m_db_mutex);
auto& db = *m_db; auto& db = *m_db;
update_last_access(db, DBOperation::Creators, next_update); 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(); 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(); constexpr auto select_last_acces_str = make_select_last_acces_str();
SQLite::Statement sel_last( SQLite::Statement sel_last(
*m_db, *m_db,

View file

@ -17,8 +17,7 @@
#pragma once #pragma once
#include "datatypes.hpp" #include "oro/originsdb.hpp"
#include "oro/dboperation.hpp"
#include <mutex> #include <mutex>
#include <string_view> #include <string_view>
#include <string> #include <string>
@ -30,22 +29,17 @@ class Database;
namespace oro { namespace oro {
struct Items; class OriginsDBSQLite : public OriginsDB {
struct Icons;
struct Shops;
struct Creators;
class OriginsDB {
public: public:
explicit OriginsDB(std::string_view path); explicit OriginsDBSQLite(std::string_view path);
~OriginsDB() noexcept; virtual ~OriginsDBSQLite() noexcept;
void update (const Items& items, 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); void update (const Icons& icons, const oro::Timestamp& next_update) override;
void update (const Shops& shops, const oro::Timestamp& next_update); void update (const Shops& shops, const oro::Timestamp& next_update) override;
void update (const Creators& creat, const oro::Timestamp& next_update); 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: private:
std::string m_path; std::string m_path;