Re-enabled database support for WebNodes.

This commit is contained in:
Relintai 2022-07-06 23:03:44 +02:00
parent 25e0c1097a
commit afef61dabf
3 changed files with 62 additions and 43 deletions

View File

@ -2,6 +2,13 @@
#include "database.h"
Ref<Database> DatabaseManager::get_ddb() {
return _ddb;
}
void DatabaseManager::set_ddb(const Ref<Database> &db) {
_ddb = db;
}
void DatabaseManager::load() {
//go thourgh settings, and create all the defined db backends
//add them to ProjectSettings

View File

@ -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<Database> WebNode::get_database() {
if (_database.is_valid()) {
return _database;
}
return DatabaseManager::get_singleton()->ddb;
return DatabaseManager::get_singleton()->get_ddb();
}
Ref<TableBuilder> WebNode::get_table_builder() {
Database *db = get_database();
ERR_FAIL_COND_V(!db, Ref<TableBuilder>());
return db->get_table_builder();
}
Ref<QueryBuilder> WebNode::get_query_builder() {
Database *db = get_database();
ERR_FAIL_COND_V(!db, Ref<QueryBuilder>());
return db->get_query_builder();
}
void WebNode::set_database(Database *db) {
void WebNode::set_database(const Ref<Database> &db) {
_database = db;
// todo send event to children when it's implemented?
}
Ref<TableBuilder> WebNode::get_table_builder() {
Ref<Database> db = get_database();
ERR_FAIL_COND_V(!db.is_valid(), Ref<TableBuilder>());
Ref<DatabaseConnection> conn = db->get_connection();
ERR_FAIL_COND_V(!conn.is_valid(), Ref<TableBuilder>());
return conn->get_table_builder();
}
Ref<QueryBuilder> WebNode::get_query_builder() {
Ref<Database> db = get_database();
ERR_FAIL_COND_V(!db.is_valid(), Ref<QueryBuilder>());
Ref<DatabaseConnection> conn = db->get_connection();
ERR_FAIL_COND_V(!conn.is_valid(), Ref<QueryBuilder>());
return conn->get_query_builder();
}
#endif
void WebNode::handle_request_main(Ref<WebServerRequest> 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<TableBuilder> get_table_builder();
// Ref<QueryBuilder> 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);

View File

@ -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<Database> get_database();
void set_database(const Ref<Database> &db);
Ref<TableBuilder> get_table_builder();
Ref<QueryBuilder> get_query_builder();
void set_database(Database *db);
#endif
void handle_request_main(Ref<WebServerRequest> request);
@ -116,8 +117,8 @@ protected:
Settings *_settings;
#endif
#ifdef DATABASES_ENABLED
Database *_database;
#ifdef MODULE_DATABASE_ENABLED
Ref<Database> _database;
#endif
bool _routing_enabled;