More cleanups.

This commit is contained in:
Relintai 2022-07-05 23:30:14 +02:00
parent 134d94af5f
commit d5f5e9fec9
3 changed files with 18 additions and 90 deletions

View File

@ -1,7 +1,7 @@
#ifndef DATABASE_H
#define DATABASE_H
#include "core/string.h"
#include "core/ustring.h"
#include <memory>
#include "core/reference.h"
@ -12,19 +12,6 @@ class QueryResult;
class Database {
public:
//probably needs refcount, depending on what db engines do, todo
//virtual QueryResult *query(const String &sql);
//virtual QueryResult *query_async(const String &sql);
//or
//virtual QueryErrorCode query(QueryResult *result, const String &sql);
//virtual QueryErrorCode query_async(QueryResult *result, const String &sql);
//also
//virtual QueryResult *query_prepared(const String &sql, param1, param2, ...);
//query interface (codeigniter 3 style)
//virtual void where(""); etc
virtual void connect(const String &connection_str);
virtual Ref<QueryResult> query(const String &query);
virtual void query_run(const String &query);
@ -37,9 +24,6 @@ public:
Database();
~Database();
private:
//std::vector<QueryBuilder *> _builders;
};
#endif

View File

@ -2,68 +2,21 @@
void DatabaseManager::load() {
//go thourgh settings, and create all the defined db backends
//add them to ProjectSettings
}
uint32_t DatabaseManager::create_database(const String &name) {
Database *db = _create_database(name);
if (!db) {
RLOG_MSG("(DatabaseManager) create_database: " + name + ", returned db is null!");
return -1;
}
RLOG_MSG("(DatabaseManager) Database " + name + " successfully created!");
databases.push_back(db);
if (ddb == nullptr) {
RLOG_MSG("(DatabaseManager) Database " + name + " has been set as the default database!");
ddb = db;
}
return databases.size() - 1;
}
DatabaseManager *DatabaseManager::get_singleton() {
return _instance;
}
void DatabaseManager::_register_db_creation_func(const String &name, std::function<Database *()> func) {
ERR_FAIL_COND_MSG(!func, "_register_db_creation_func: " + name + ", func is wrong!");
_db_creation_func_map[name] = func;
}
void DatabaseManager::_unregister_db_creation_func(const String &name) {
_db_creation_func_map.erase(name);
}
Database *DatabaseManager::_create_database(const String &name) {
std::function<Database *()> func = _db_creation_func_map[name];
ERR_FAIL_COND_V_MSG(!func, nullptr, "_create_database: " + name + ", func is wrong!");
Database *db = func();
ERR_FAIL_COND_V_MSG(!db, nullptr, "_create_database: " + name + ", returned db is null!");
return db;
}
DatabaseManager::DatabaseManager() {
_instance = this;
ddb = nullptr;
}
DatabaseManager::~DatabaseManager() {
_instance = nullptr;
for (uint32_t i = 0; i < databases.size(); ++i) {
delete databases[i];
}
}
DatabaseManager *DatabaseManager::_instance = nullptr;
std::map<String, std::function<Database *()> > DatabaseManager::_db_creation_func_map;

View File

@ -1,42 +1,33 @@
#ifndef DATABASE_MANAGER_H
#define DATABASE_MANAGER_H
#include "core/string.h"
#include <vector>
#include <map>
#include <functional>
#include "core/reference.h"
#include "core/ustring.h"
#include "core/object.h"
#include "database.h"
class Database;
class DatabaseManager : public Object {
RCPP_OBJECT(DatabaseManager, Object);
GDCLASS(DatabaseManager, Object);
public:
std::vector<Database *> databases;
Database *ddb;
Ref<Database> get_ddb();
void set_ddb(const Ref<Database> &db);
void load();
static DatabaseManager *get_singleton();
//note: not threadsafe, create these at the start of your program!
uint32_t create_database(const String &name);
static void _register_db_creation_func(const String &name, std::function<Database*()> func);
static void _unregister_db_creation_func(const String &name);
static Database *_create_database(const String &name);
DatabaseManager();
~DatabaseManager();
private:
static DatabaseManager * _instance;
protected:
Vector<Ref<Database>> _databases;
Ref<Database> _ddb;
static std::map<String, std::function<Database *()> > _db_creation_func_map;
private:
static DatabaseManager *_instance;
};
#endif