Added binds + methods to the DatabaseManager, and set it up as a singleton.

This commit is contained in:
Relintai 2022-07-23 13:37:24 +02:00
parent 0c1d58f921
commit 0cac8406ec
3 changed files with 88 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include "database_manager.h"
#include "core/object.h"
#include "database.h"
Ref<Database> DatabaseManager::get_ddb() {
@ -7,6 +8,52 @@ Ref<Database> DatabaseManager::get_ddb() {
}
void DatabaseManager::set_ddb(const Ref<Database> &db) {
_ddb = db;
emit_signal("default_database_changed", db);
}
void DatabaseManager::add_database(const Ref<Database> &db, bool set_as_default) {
ERR_FAIL_COND(!db.is_valid());
_databases.push_back(db);
emit_signal("database_added", db);
if (set_as_default) {
_ddb = db;
emit_signal("default_database_changed", db);
}
}
void DatabaseManager::remove_database(const int index, const bool unset_if_default) {
ERR_FAIL_INDEX(index, _databases.size());
Ref<Database> db = _databases[index];
_databases.remove(index);
emit_signal("database_removed", db);
if (unset_if_default && _ddb == db) {
_ddb.unref();
emit_signal("default_database_changed", db);
}
}
Ref<Database> DatabaseManager::get_database(const int index) {
ERR_FAIL_INDEX_V(index, _databases.size(), Ref<Database>());
return _databases[index];
}
int DatabaseManager::get_database_count() {
return _databases.size();
}
Vector<Ref<Database>> DatabaseManager::get_databases() {
return _databases;
}
Vector<Variant> DatabaseManager::get_databases_bind() {
Vector<Variant> r;
for (int i = 0; i < _databases.size(); i++) {
r.push_back(_databases[i].get_ref_ptr());
}
return r;
}
void DatabaseManager::load() {
@ -26,4 +73,23 @@ DatabaseManager::~DatabaseManager() {
_instance = nullptr;
}
void DatabaseManager::_bind_methods() {
ADD_SIGNAL(MethodInfo("default_database_changed", PropertyInfo(Variant::OBJECT, "db", PROPERTY_HINT_RESOURCE_TYPE, "Database")));
ADD_SIGNAL(MethodInfo("database_added", PropertyInfo(Variant::OBJECT, "db", PROPERTY_HINT_RESOURCE_TYPE, "Database")));
ADD_SIGNAL(MethodInfo("database_removed", PropertyInfo(Variant::OBJECT, "db", PROPERTY_HINT_RESOURCE_TYPE, "Database")));
ClassDB::bind_method(D_METHOD("get_ddb"), &DatabaseManager::get_ddb);
ClassDB::bind_method(D_METHOD("set_ddb"), &DatabaseManager::set_ddb);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ddb", PROPERTY_HINT_RESOURCE_TYPE, "Database"), "set_ddb", "get_ddb");
ClassDB::bind_method(D_METHOD("add_database", "db", "set_as_default"), &DatabaseManager::add_database, true);
ClassDB::bind_method(D_METHOD("remove_database", "index", "unset_if_default"), &DatabaseManager::remove_database, true);
ClassDB::bind_method(D_METHOD("get_database", "index"), &DatabaseManager::get_database);
ClassDB::bind_method(D_METHOD("get_database_count"), &DatabaseManager::get_database_count);
ClassDB::bind_method(D_METHOD("get_databases"), &DatabaseManager::get_databases_bind);
ClassDB::bind_method(D_METHOD("load"), &DatabaseManager::load);
}
DatabaseManager *DatabaseManager::_instance = nullptr;

View File

@ -3,6 +3,7 @@
#include "core/reference.h"
#include "core/ustring.h"
#include "core/vector.h"
#include "core/object.h"
@ -12,9 +13,18 @@ class DatabaseManager : public Object {
GDCLASS(DatabaseManager, Object);
public:
//ddb = default database
Ref<Database> get_ddb();
void set_ddb(const Ref<Database> &db);
void add_database(const Ref<Database> &db, bool set_as_default = true);
void remove_database(const int index, const bool unset_if_default = true);
Ref<Database> get_database(const int index);
int get_database_count();
Vector<Ref<Database>> get_databases();
Vector<Variant> get_databases_bind();
void load();
static DatabaseManager *get_singleton();
@ -27,6 +37,8 @@ protected:
Ref<Database> _ddb;
private:
static void _bind_methods();
static DatabaseManager *_instance;
};

View File

@ -31,6 +31,10 @@ SOFTWARE.
#include "query_result.h"
#include "table_builder.h"
#include "core/engine.h"
DatabaseManager *_database_manager = nullptr;
void register_database_types() {
ClassDB::register_class<Database>();
ClassDB::register_class<DatabaseConnection>();
@ -40,7 +44,13 @@ void register_database_types() {
ClassDB::register_class<QueryBuilder>();
ClassDB::register_class<QueryResult>();
ClassDB::register_class<TableBuilder>();
_database_manager = memnew(DatabaseManager);
Engine::get_singleton()->add_singleton(Engine::Singleton("DatabaseManager", DatabaseManager::get_singleton()));
}
void unregister_database_types() {
if (_database_manager) {
memdelete(_database_manager);
}
}