Implement basic functionality of the DatabaseManager. Also made MysqlConnection inherint from Database, and it's now possible to create instances from it.

This commit is contained in:
Relintai 2020-12-01 14:47:24 +01:00
parent 5bbe067d4e
commit 9bddaba8bc
4 changed files with 98 additions and 18 deletions

View File

@ -1,11 +1,54 @@
#include "database_manager.h"
void DatabaseManager::load() {
//go thourgh settings, and create all the defined db backends
//go thourgh settings, and create all the defined db backends
}
uint32_t DatabaseManager::create_database(const std::string &name) {
Database *db = _create_database(name);
if (!db) {
printf("create_database: %s, returned db is null!", name.c_str());
return -1;
}
databases.push_back(db);
return databases.size() - 1;
}
DatabaseManager *DatabaseManager::get_singleton() {
return _instance;
return _instance;
}
void DatabaseManager::_register_db_creation_func(const std::string &name, std::function<Database *()> func) {
if (!func) {
printf("_register_db_creation_func: %s, func is wrong!", name.c_str());
return;
}
_db_creation_func_map[name] = func;
}
void DatabaseManager::_unregister_db_creation_func(const std::string &name) {
_db_creation_func_map.erase(name);
}
Database *DatabaseManager::_create_database(const std::string &name) {
std::function<Database *()> func = _db_creation_func_map[name];
if (!func) {
printf("_create_database: %s, func is wrong!", name.c_str());
return nullptr;
}
Database *db = func();
if (!db) {
printf("_create_database: %s, returned db is null!", name.c_str());
}
return db;
}
DatabaseManager::DatabaseManager() {
@ -15,9 +58,11 @@ DatabaseManager::DatabaseManager() {
DatabaseManager::~DatabaseManager() {
_instance = nullptr;
for (uint32_t i = 0; i < databases.size(); ++i) {
delete databases[i];
}
for (uint32_t i = 0; i < databases.size(); ++i) {
delete databases[i];
}
}
DatabaseManager *DatabaseManager::_instance = nullptr;
DatabaseManager *DatabaseManager::_instance = nullptr;
std::map<std::string, std::function<Database *()> > DatabaseManager::_db_creation_func_map;

View File

@ -2,6 +2,8 @@
#define DATABASE_MANAGER_H
#include <vector>
#include <map>
#include <functional>
#include "database.h"
@ -11,13 +13,23 @@ public:
void load();
DatabaseManager *get_singleton();
static DatabaseManager *get_singleton();
//note: not threadsafe, create these at the start of your program!
uint32_t create_database(const std::string &name);
static void _register_db_creation_func(const std::string &name, std::function<Database*()> func);
static void _unregister_db_creation_func(const std::string &name);
static Database *_create_database(const std::string &name);
DatabaseManager();
~DatabaseManager();
private:
static DatabaseManager * _instance;
static std::map<std::string, std::function<Database *()> > _db_creation_func_map;
};
#endif

View File

@ -1,6 +1,9 @@
#ifndef MYSQL_CONNECTION
#define MYSQL_CONNECTION
#include "core/database.h"
#include "core/database_manager.h"
//Brynet has it aswell, and because of using namespace it is defined here aswell
//later this will be fixed better
#ifdef IS_NUM
@ -9,17 +12,28 @@
#include <mysql.h>
class MysqlConnection {
public:
MysqlConnection() {
mysql = new MYSQL();
}
~MysqlConnection()
{
delete mysql;
}
MYSQL *mysql;
class MysqlConnection : public Database {
public:
static Database *_creation_func() {
return new MysqlConnection();
}
static void _register() {
DatabaseManager::_register_db_creation_func("mysql", MysqlConnection::_creation_func);
}
static void _unregister() {
DatabaseManager::_unregister_db_creation_func("mysql");
}
MysqlConnection() {
mysql = new MYSQL();
}
~MysqlConnection() {
delete mysql;
}
MYSQL *mysql;
};
#undef IS_NUM

View File

@ -9,12 +9,16 @@
#include "modules/message_page/message_page.h"
#include "database/mysql/mysql_connection.h"
#define MAIN_CLASS RDNApplication
int main(int argc, char **argv) {
#if MYSQL_PRESENT
printf("mysql present\n");
MysqlConnection::_register();
#endif
#if PGSQL_PRESENT
@ -29,6 +33,9 @@ int main(int argc, char **argv) {
file_cache->wwwroot = "./www";
file_cache->wwwroot_refresh_cache();
DatabaseManager *dbm = new DatabaseManager();
//dbm->create_database("mysql");
Application *app = new MAIN_CLASS();
app->setup_routes();
@ -46,8 +53,10 @@ int main(int argc, char **argv) {
delete server;
delete app;
delete dbm;
delete file_cache;
delete mp;
return 0;
}