mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
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:
parent
5bbe067d4e
commit
9bddaba8bc
@ -1,11 +1,54 @@
|
|||||||
#include "database_manager.h"
|
#include "database_manager.h"
|
||||||
|
|
||||||
void DatabaseManager::load() {
|
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() {
|
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() {
|
DatabaseManager::DatabaseManager() {
|
||||||
@ -15,9 +58,11 @@ DatabaseManager::DatabaseManager() {
|
|||||||
DatabaseManager::~DatabaseManager() {
|
DatabaseManager::~DatabaseManager() {
|
||||||
_instance = nullptr;
|
_instance = nullptr;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < databases.size(); ++i) {
|
for (uint32_t i = 0; i < databases.size(); ++i) {
|
||||||
delete databases[i];
|
delete databases[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DatabaseManager *DatabaseManager::_instance = nullptr;
|
DatabaseManager *DatabaseManager::_instance = nullptr;
|
||||||
|
|
||||||
|
std::map<std::string, std::function<Database *()> > DatabaseManager::_db_creation_func_map;
|
@ -2,6 +2,8 @@
|
|||||||
#define DATABASE_MANAGER_H
|
#define DATABASE_MANAGER_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
|
||||||
@ -11,13 +13,23 @@ public:
|
|||||||
|
|
||||||
void load();
|
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();
|
||||||
~DatabaseManager();
|
~DatabaseManager();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static DatabaseManager * _instance;
|
static DatabaseManager * _instance;
|
||||||
|
|
||||||
|
static std::map<std::string, std::function<Database *()> > _db_creation_func_map;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,6 +1,9 @@
|
|||||||
#ifndef MYSQL_CONNECTION
|
#ifndef MYSQL_CONNECTION
|
||||||
#define 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
|
//Brynet has it aswell, and because of using namespace it is defined here aswell
|
||||||
//later this will be fixed better
|
//later this will be fixed better
|
||||||
#ifdef IS_NUM
|
#ifdef IS_NUM
|
||||||
@ -9,17 +12,28 @@
|
|||||||
|
|
||||||
#include <mysql.h>
|
#include <mysql.h>
|
||||||
|
|
||||||
class MysqlConnection {
|
class MysqlConnection : public Database {
|
||||||
public:
|
public:
|
||||||
MysqlConnection() {
|
static Database *_creation_func() {
|
||||||
mysql = new MYSQL();
|
return new MysqlConnection();
|
||||||
}
|
}
|
||||||
~MysqlConnection()
|
|
||||||
{
|
static void _register() {
|
||||||
delete mysql;
|
DatabaseManager::_register_db_creation_func("mysql", MysqlConnection::_creation_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
MYSQL *mysql;
|
static void _unregister() {
|
||||||
|
DatabaseManager::_unregister_db_creation_func("mysql");
|
||||||
|
}
|
||||||
|
|
||||||
|
MysqlConnection() {
|
||||||
|
mysql = new MYSQL();
|
||||||
|
}
|
||||||
|
~MysqlConnection() {
|
||||||
|
delete mysql;
|
||||||
|
}
|
||||||
|
|
||||||
|
MYSQL *mysql;
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef IS_NUM
|
#undef IS_NUM
|
||||||
|
9
main.cpp
9
main.cpp
@ -9,12 +9,16 @@
|
|||||||
|
|
||||||
#include "modules/message_page/message_page.h"
|
#include "modules/message_page/message_page.h"
|
||||||
|
|
||||||
|
#include "database/mysql/mysql_connection.h"
|
||||||
|
|
||||||
#define MAIN_CLASS RDNApplication
|
#define MAIN_CLASS RDNApplication
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
#if MYSQL_PRESENT
|
#if MYSQL_PRESENT
|
||||||
printf("mysql present\n");
|
printf("mysql present\n");
|
||||||
|
|
||||||
|
MysqlConnection::_register();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PGSQL_PRESENT
|
#if PGSQL_PRESENT
|
||||||
@ -29,6 +33,9 @@ int main(int argc, char **argv) {
|
|||||||
file_cache->wwwroot = "./www";
|
file_cache->wwwroot = "./www";
|
||||||
file_cache->wwwroot_refresh_cache();
|
file_cache->wwwroot_refresh_cache();
|
||||||
|
|
||||||
|
DatabaseManager *dbm = new DatabaseManager();
|
||||||
|
//dbm->create_database("mysql");
|
||||||
|
|
||||||
Application *app = new MAIN_CLASS();
|
Application *app = new MAIN_CLASS();
|
||||||
|
|
||||||
app->setup_routes();
|
app->setup_routes();
|
||||||
@ -46,8 +53,10 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
delete server;
|
delete server;
|
||||||
delete app;
|
delete app;
|
||||||
|
delete dbm;
|
||||||
delete file_cache;
|
delete file_cache;
|
||||||
delete mp;
|
delete mp;
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user