new db interface
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#include "utils.hpp"
|
||||
#include "database.hpp"
|
||||
#include "databasepool.h"
|
||||
#include <optional>
|
||||
|
||||
using namespace std;
|
||||
@@ -110,3 +110,5 @@ bool Database::open(){
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
DatabasePool dbpool(std::thread::hardware_concurrency());
|
||||
@@ -9,12 +9,16 @@
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include "crow.h"
|
||||
#include "sqlite_orm.h"
|
||||
#include "ShadowrunDb.hpp"
|
||||
|
||||
class Database {
|
||||
|
||||
typedef std::vector<std::variant<int64_t, std::string>> QueryData;
|
||||
|
||||
public:
|
||||
static constexpr std::string dbFile = "app.db";
|
||||
|
||||
Database();
|
||||
~Database();
|
||||
|
||||
@@ -74,6 +78,26 @@ private:
|
||||
sqlite3_stmt* prepareStmt(const std::string& sql);
|
||||
|
||||
sqlite3* m_db;
|
||||
|
||||
};
|
||||
|
||||
|
||||
inline auto make_database() {
|
||||
return sqlite_orm::make_storage(Database::dbFile,
|
||||
sqlite_orm::make_table("shadowrun_characters",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunCharacter::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("name", &shadowrun::ShadowrunCharacter::name, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunCharacter::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP"))
|
||||
),
|
||||
sqlite_orm::make_table("shadowrun_data",
|
||||
sqlite_orm::make_column("id", &shadowrun::ShadowrunData::id, sqlite_orm::primary_key()),
|
||||
sqlite_orm::make_column("character_id", &shadowrun::ShadowrunData::character_id, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("name", &shadowrun::ShadowrunData::name, sqlite_orm::not_null()),
|
||||
sqlite_orm::make_column("value", &shadowrun::ShadowrunData::value),
|
||||
sqlite_orm::make_column("created_at", &shadowrun::ShadowrunData::created_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::make_column("updated_at", &shadowrun::ShadowrunData::updated_at, sqlite_orm::default_value("CURRENT_TIMESTAMP")),
|
||||
sqlite_orm::foreign_key(&shadowrun::ShadowrunData::character_id).references(&shadowrun::ShadowrunCharacter::id).on_delete.cascade()
|
||||
));
|
||||
}
|
||||
|
||||
#endif // __DATABASE_H__
|
||||
40
src/database/databasepool.h
Normal file
40
src/database/databasepool.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include "database.hpp"
|
||||
|
||||
class DatabasePool {
|
||||
public:
|
||||
DatabasePool(size_t size) {
|
||||
for (size_t i = 0; i < size; ++i){
|
||||
auto db = std::make_shared<decltype(make_database())>(make_database());
|
||||
db->sync_schema();
|
||||
pool.push(db);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<decltype(make_database())> acquire() {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
cv.wait(lock, [&]{ return !pool.empty(); });
|
||||
auto db = pool.front();
|
||||
pool.pop();
|
||||
return db;
|
||||
}
|
||||
|
||||
void release(std::shared_ptr<decltype(make_database())> db) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
pool.push(db);
|
||||
lock.unlock();
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
private:
|
||||
std::queue<std::shared_ptr<decltype(make_database())>> pool;
|
||||
std::mutex mutex;
|
||||
std::condition_variable cv;
|
||||
};
|
||||
|
||||
|
||||
extern DatabasePool dbpool;
|
||||
24841
src/database/sqlite_orm.h
Normal file
24841
src/database/sqlite_orm.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user