From afef61dabf225048d6a9b858a6e5d6b1dde8d4a7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 6 Jul 2022 23:03:44 +0200 Subject: [PATCH] Re-enabled database support for WebNodes. --- modules/database/database_manager.cpp | 7 +++ modules/web/http/web_node.cpp | 83 +++++++++++++++------------ modules/web/http/web_node.h | 15 ++--- 3 files changed, 62 insertions(+), 43 deletions(-) diff --git a/modules/database/database_manager.cpp b/modules/database/database_manager.cpp index 655ac6d0e..f687b2f4b 100644 --- a/modules/database/database_manager.cpp +++ b/modules/database/database_manager.cpp @@ -2,6 +2,13 @@ #include "database.h" +Ref DatabaseManager::get_ddb() { + return _ddb; +} +void DatabaseManager::set_ddb(const Ref &db) { + _ddb = db; +} + void DatabaseManager::load() { //go thourgh settings, and create all the defined db backends //add them to ProjectSettings diff --git a/modules/web/http/web_node.cpp b/modules/web/http/web_node.cpp index 2d5fd8e30..8329f3875 100644 --- a/modules/web/http/web_node.cpp +++ b/modules/web/http/web_node.cpp @@ -6,6 +6,7 @@ #include "core/object.h" #include "core/print_string.h" #include "http_server_enums.h" +#include "modules/database/database_connection.h" #include "web_server_request.h" #if WEB_SETTINGS_ENABLED @@ -15,12 +16,12 @@ #include "web_permission.h" #include "web_server.h" -#ifdef DATABASES_ENABLED -#include "database/database.h" -#include "database/database_manager.h" -#include "database/query_builder.h" -#include "database/query_result.h" -#include "database/table_builder.h" +#ifdef MODULE_DATABASE_ENABLED +#include "../../database/database.h" +#include "../../database/database_manager.h" +#include "../../database/query_builder.h" +#include "../../database/query_result.h" +#include "../../database/table_builder.h" #endif String WebNode::get_uri_segment() { @@ -105,38 +106,46 @@ void WebNode::set_routing_enabled(const bool value) { } } -#ifdef DATABASES_ENABLED +#ifdef MODULE_DATABASE_ENABLED -Database *WebNode::get_database() { - if (_database) { +Ref WebNode::get_database() { + if (_database.is_valid()) { return _database; } - return DatabaseManager::get_singleton()->ddb; + return DatabaseManager::get_singleton()->get_ddb(); } -Ref WebNode::get_table_builder() { - Database *db = get_database(); - - ERR_FAIL_COND_V(!db, Ref()); - - return db->get_table_builder(); -} - -Ref WebNode::get_query_builder() { - Database *db = get_database(); - - ERR_FAIL_COND_V(!db, Ref()); - - return db->get_query_builder(); -} - -void WebNode::set_database(Database *db) { +void WebNode::set_database(const Ref &db) { _database = db; // todo send event to children when it's implemented? } +Ref WebNode::get_table_builder() { + Ref db = get_database(); + + ERR_FAIL_COND_V(!db.is_valid(), Ref()); + + Ref conn = db->get_connection(); + + ERR_FAIL_COND_V(!conn.is_valid(), Ref()); + + return conn->get_table_builder(); +} + +Ref WebNode::get_query_builder() { + Ref db = get_database(); + + ERR_FAIL_COND_V(!db.is_valid(), Ref()); + + Ref conn = db->get_connection(); + + ERR_FAIL_COND_V(!conn.is_valid(), Ref()); + + return conn->get_query_builder(); +} + #endif void WebNode::handle_request_main(Ref request) { @@ -403,9 +412,9 @@ void WebNode::_notification(const int what) { WebNode::WebNode() { // should look this up in parents when parented (and node parenting is implemented) // should have an event later when a parent gets one -#ifdef DATABASES_ENABLED - _database = nullptr; -#endif + //#ifdef MODULE_DATABASE_ENABLED + //_database = nullptr; + //#endif #if WEB_SETTINGS_ENABLED // same for this @@ -440,12 +449,14 @@ void WebNode::_bind_methods() { ClassDB::bind_method(D_METHOD("set_routing_enabled", "val"), &WebNode::set_routing_enabled); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "routing_enabled"), "set_routing_enabled", "get_routing_enabled"); - //#ifdef DATABASES_ENABLED - // Database *get_database(); - // Ref get_table_builder(); - // Ref get_query_builder(); - // void set_database(Database * db); - //#endif +#ifdef MODULE_DATABASE_ENABLED + ClassDB::bind_method(D_METHOD("get_database"), &WebNode::get_database); + ClassDB::bind_method(D_METHOD("set_database", "val"), &WebNode::set_database); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "database", PROPERTY_HINT_RESOURCE_TYPE, "Database", 0), "set_database", "get_database"); + + ClassDB::bind_method(D_METHOD("get_table_builder"), &WebNode::get_table_builder); + ClassDB::bind_method(D_METHOD("get_query_builder"), &WebNode::get_query_builder); +#endif BIND_VMETHOD(MethodInfo("_handle_request_main", PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest"))); ClassDB::bind_method(D_METHOD("handle_request_main", "request"), &WebNode::handle_request_main); diff --git a/modules/web/http/web_node.h b/modules/web/http/web_node.h index 2cb6412a3..e7c457fc0 100644 --- a/modules/web/http/web_node.h +++ b/modules/web/http/web_node.h @@ -1,6 +1,7 @@ #ifndef WEB_NODE_H #define WEB_NODE_H +#include "../../modules_enabled.gen.h" #include "core/hash_map.h" #include "core/os/rw_lock.h" #include "core/reference.h" @@ -14,8 +15,8 @@ class Settings; class WebServer; class WebPermission; -#ifdef DATABASES_ENABLED -class DataBase; +#ifdef MODULE_DATABASE_ENABLED +class Database; class TableBuilder; class QueryBuilder; #endif @@ -49,11 +50,11 @@ public: bool get_routing_enabled(); void set_routing_enabled(const bool value); -#ifdef DATABASES_ENABLED - Database *get_database(); +#ifdef MODULE_DATABASE_ENABLED + Ref get_database(); + void set_database(const Ref &db); Ref get_table_builder(); Ref get_query_builder(); - void set_database(Database *db); #endif void handle_request_main(Ref request); @@ -116,8 +117,8 @@ protected: Settings *_settings; #endif -#ifdef DATABASES_ENABLED - Database *_database; +#ifdef MODULE_DATABASE_ENABLED + Ref _database; #endif bool _routing_enabled;