From 0cac8406ec3356f3b6a1dfd55f712068e9838da4 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 23 Jul 2022 13:37:24 +0200 Subject: [PATCH] Added binds + methods to the DatabaseManager, and set it up as a singleton. --- modules/database/database_manager.cpp | 66 +++++++++++++++++++++++++++ modules/database/database_manager.h | 12 +++++ modules/database/register_types.cpp | 10 ++++ 3 files changed, 88 insertions(+) diff --git a/modules/database/database_manager.cpp b/modules/database/database_manager.cpp index f687b2f4b..4cc893bf5 100644 --- a/modules/database/database_manager.cpp +++ b/modules/database/database_manager.cpp @@ -1,5 +1,6 @@ #include "database_manager.h" +#include "core/object.h" #include "database.h" Ref DatabaseManager::get_ddb() { @@ -7,6 +8,52 @@ Ref DatabaseManager::get_ddb() { } void DatabaseManager::set_ddb(const Ref &db) { _ddb = db; + emit_signal("default_database_changed", db); +} + +void DatabaseManager::add_database(const Ref &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 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 DatabaseManager::get_database(const int index) { + ERR_FAIL_INDEX_V(index, _databases.size(), Ref()); + + return _databases[index]; +} +int DatabaseManager::get_database_count() { + return _databases.size(); +} + +Vector> DatabaseManager::get_databases() { + return _databases; +} + +Vector DatabaseManager::get_databases_bind() { + Vector 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; diff --git a/modules/database/database_manager.h b/modules/database/database_manager.h index eedba6db9..167cb9c86 100644 --- a/modules/database/database_manager.h +++ b/modules/database/database_manager.h @@ -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 get_ddb(); void set_ddb(const Ref &db); + void add_database(const Ref &db, bool set_as_default = true); + void remove_database(const int index, const bool unset_if_default = true); + Ref get_database(const int index); + int get_database_count(); + + Vector> get_databases(); + Vector get_databases_bind(); + void load(); static DatabaseManager *get_singleton(); @@ -27,6 +37,8 @@ protected: Ref _ddb; private: + static void _bind_methods(); + static DatabaseManager *_instance; }; diff --git a/modules/database/register_types.cpp b/modules/database/register_types.cpp index 7528774e2..c4bdde877 100644 --- a/modules/database/register_types.cpp +++ b/modules/database/register_types.cpp @@ -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(); ClassDB::register_class(); @@ -40,7 +44,13 @@ void register_database_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + + _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); + } }