Broke up Database into 2 classes, to be able to better handle multi threading for the database systems that work better with having a connection for each thread. Haven't yet tested which backends would work better like this though.

This commit is contained in:
Relintai 2022-07-06 09:30:50 +02:00
parent b26ad4cab9
commit 2ec4fe922f
2 changed files with 66 additions and 17 deletions

View File

@ -4,28 +4,58 @@
#include "query_result.h" #include "query_result.h"
#include "table_builder.h" #include "table_builder.h"
void Database::connect(const String &connection_str) { void DatabaseConnection::connect(const String &connection_str) {
} }
Ref<QueryResult> Database::query(const String &query) { Ref<QueryResult> DatabaseConnection::query(const String &query) {
return Ref<QueryResult>(); return Ref<QueryResult>();
} }
void Database::query_run(const String &query) { void DatabaseConnection::query_run(const String &query) {
} }
Ref<QueryBuilder> Database::get_query_builder() { Ref<QueryBuilder> DatabaseConnection::get_query_builder() {
return Ref<QueryBuilder>(new QueryBuilder()); return Ref<QueryBuilder>(new QueryBuilder());
} }
Ref<TableBuilder> Database::get_table_builder() { Ref<TableBuilder> DatabaseConnection::get_table_builder() {
return Ref<TableBuilder>(new TableBuilder()); return Ref<TableBuilder>(new TableBuilder());
} }
String Database::escape(const String &str) { String DatabaseConnection::escape(const String &str) {
return String(); return String();
} }
void Database::escape_to(const String &str, String *to) { void DatabaseConnection::escape_to(const String &str, String *to) {
}
DatabaseConnection::DatabaseConnection() {
}
DatabaseConnection::~DatabaseConnection() {
}
void DatabaseConnection::_bind_methods() {
ClassDB::bind_method(D_METHOD("connect", "connection_str"), &DatabaseConnection::connect);
ClassDB::bind_method(D_METHOD("query", "query"), &DatabaseConnection::query);
ClassDB::bind_method(D_METHOD("query_run", "query"), &DatabaseConnection::query_run);
ClassDB::bind_method(D_METHOD("get_query_builder"), &DatabaseConnection::get_query_builder);
ClassDB::bind_method(D_METHOD("get_table_builder"), &DatabaseConnection::get_table_builder);
ClassDB::bind_method(D_METHOD("escape", "str"), &DatabaseConnection::escape);
}
String Database::get_connection_string() {
return _connection_string;
}
void Database::set_connection_string(const String &val) {
_connection_string = val;
}
Ref<DatabaseConnection> Database::get_connection() {
return _allocate_connection();
}
Ref<DatabaseConnection> Database::_allocate_connection() {
return Ref<DatabaseConnection>();
} }
Database::Database() { Database::Database() {
@ -35,10 +65,9 @@ Database::~Database() {
} }
void Database::_bind_methods() { void Database::_bind_methods() {
ClassDB::bind_method(D_METHOD("connect", "connection_str"), &Database::connect); ClassDB::bind_method(D_METHOD("get_connection_string"), &Database::get_connection_string);
ClassDB::bind_method(D_METHOD("query", "query"), &Database::query); ClassDB::bind_method(D_METHOD("set_connection_string", "value"), &Database::set_connection_string);
ClassDB::bind_method(D_METHOD("query_run", "query"), &Database::query_run); ADD_PROPERTY(PropertyInfo(Variant::STRING, "connection_string"), "set_connection_string", "get_connection_string");
ClassDB::bind_method(D_METHOD("get_query_builder"), &Database::get_query_builder);
ClassDB::bind_method(D_METHOD("get_table_builder"), &Database::get_table_builder); ClassDB::bind_method(D_METHOD("get_connection"), &Database::get_connection);
ClassDB::bind_method(D_METHOD("escape", "str"), &Database::escape);
} }

View File

@ -9,8 +9,8 @@ class QueryBuilder;
class TableBuilder; class TableBuilder;
class QueryResult; class QueryResult;
class Database : public Reference { class DatabaseConnection : public Reference {
GDCLASS(Database, Reference); GDCLASS(DatabaseConnection, Reference);
public: public:
virtual void connect(const String &connection_str); virtual void connect(const String &connection_str);
@ -23,11 +23,31 @@ public:
virtual String escape(const String &str); virtual String escape(const String &str);
virtual void escape_to(const String &str, String *to); virtual void escape_to(const String &str, String *to);
Database(); DatabaseConnection();
~Database(); ~DatabaseConnection();
protected: protected:
static void _bind_methods(); static void _bind_methods();
}; };
class Database : public Reference {
GDCLASS(Database, Reference);
public:
String get_connection_string();
void set_connection_string(const String &val);
virtual Ref<DatabaseConnection> get_connection();
Database();
~Database();
protected:
virtual Ref<DatabaseConnection> _allocate_connection();
static void _bind_methods();
String _connection_string;
};
#endif #endif