mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-01-24 18:17:21 +01:00
Added multi threaded and single threaded Database base classes.
This commit is contained in:
parent
42501c339f
commit
6389a5714d
@ -20,6 +20,7 @@ Ref<DatabaseConnection> Database::_allocate_connection() {
|
||||
//Ref<DatabaseConnection> dbc;
|
||||
//dbc.instance();
|
||||
//dbc->set_owner(this); //if needed
|
||||
//dbc->connect(_connection_string);
|
||||
//return dbc;
|
||||
|
||||
return Ref<DatabaseConnection>();
|
||||
|
60
modules/database/database_multi_threaded.cpp
Normal file
60
modules/database/database_multi_threaded.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "database_multi_threaded.h"
|
||||
|
||||
#include "database_connection.h"
|
||||
#include "query_builder.h"
|
||||
#include "query_result.h"
|
||||
#include "table_builder.h"
|
||||
|
||||
Ref<DatabaseConnection> DatabaseMultiThreaded::get_connection() {
|
||||
_connection_map_lock.read_lock();
|
||||
|
||||
Thread::ID tid = Thread::get_caller_id();
|
||||
Map<Thread::ID, Ref<DatabaseConnection>>::Element *e;
|
||||
e = _connections.find(tid);
|
||||
|
||||
if (!e) {
|
||||
_connection_map_lock.read_unlock();
|
||||
|
||||
_connection_map_lock.write_lock();
|
||||
Ref<DatabaseConnection> dbc = _allocate_connection();
|
||||
_connections.insert(tid, dbc);
|
||||
_connection_map_lock.write_unlock();
|
||||
|
||||
return dbc;
|
||||
}
|
||||
|
||||
Ref<DatabaseConnection> dbc = e->get();
|
||||
_connection_map_lock.read_unlock();
|
||||
|
||||
return dbc;
|
||||
}
|
||||
|
||||
Ref<DatabaseConnection> DatabaseMultiThreaded::_allocate_connection() {
|
||||
Ref<DatabaseConnection> dbc;
|
||||
dbc.instance();
|
||||
dbc->set_owner(this);
|
||||
dbc->connect(_connection_string);
|
||||
return dbc;
|
||||
}
|
||||
|
||||
DatabaseMultiThreaded::DatabaseMultiThreaded() {
|
||||
}
|
||||
|
||||
DatabaseMultiThreaded::~DatabaseMultiThreaded() {
|
||||
_connection_map_lock.write_lock();
|
||||
|
||||
for (Map<Thread::ID, Ref<DatabaseConnection>>::Element *e = _connections.front(); e; e = e->next()) {
|
||||
Ref<DatabaseConnection> dbc = e->get();
|
||||
|
||||
if (dbc.is_valid()) {
|
||||
dbc->set_owner(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
_connections.clear();
|
||||
|
||||
_connection_map_lock.write_unlock();
|
||||
}
|
||||
|
||||
void DatabaseMultiThreaded::_bind_methods() {
|
||||
}
|
33
modules/database/database_multi_threaded.h
Normal file
33
modules/database/database_multi_threaded.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef DATABASE_MULTI_THREADED_H
|
||||
#define DATABASE_MULTI_THREADED_H
|
||||
|
||||
#include "core/map.h"
|
||||
#include "core/os/rw_lock.h"
|
||||
#include "core/os/thread.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
class QueryBuilder;
|
||||
class TableBuilder;
|
||||
class QueryResult;
|
||||
class DatabaseConnection;
|
||||
|
||||
class DatabaseMultiThreaded : public Database {
|
||||
GDCLASS(DatabaseMultiThreaded, Database);
|
||||
|
||||
public:
|
||||
Ref<DatabaseConnection> get_connection();
|
||||
|
||||
DatabaseMultiThreaded();
|
||||
~DatabaseMultiThreaded();
|
||||
|
||||
protected:
|
||||
Ref<DatabaseConnection> _allocate_connection();
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
RWLock _connection_map_lock;
|
||||
Map<Thread::ID, Ref<DatabaseConnection>> _connections;
|
||||
};
|
||||
|
||||
#endif
|
36
modules/database/database_single_threaded.cpp
Normal file
36
modules/database/database_single_threaded.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "database_single_threaded.h"
|
||||
|
||||
#include "database_connection.h"
|
||||
#include "query_builder.h"
|
||||
#include "query_result.h"
|
||||
#include "table_builder.h"
|
||||
|
||||
Ref<DatabaseConnection> DatabaseSingleThreaded::get_connection() {
|
||||
if (!_connection.is_valid()) {
|
||||
_connection = _allocate_connection();
|
||||
}
|
||||
|
||||
return _connection;
|
||||
}
|
||||
|
||||
Ref<DatabaseConnection> DatabaseSingleThreaded::_allocate_connection() {
|
||||
Ref<DatabaseConnection> dbc;
|
||||
dbc.instance();
|
||||
dbc->set_owner(this);
|
||||
dbc->connect(_connection_string);
|
||||
return dbc;
|
||||
}
|
||||
|
||||
DatabaseSingleThreaded::DatabaseSingleThreaded() {
|
||||
}
|
||||
|
||||
DatabaseSingleThreaded::~DatabaseSingleThreaded() {
|
||||
if (_connection.is_valid()) {
|
||||
_connection->set_owner(nullptr);
|
||||
}
|
||||
|
||||
_connection.unref();
|
||||
}
|
||||
|
||||
void DatabaseSingleThreaded::_bind_methods() {
|
||||
}
|
33
modules/database/database_single_threaded.h
Normal file
33
modules/database/database_single_threaded.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef DATABASE_SINGLE_THREADED_H
|
||||
#define DATABASE_SINGLE_THREADED_H
|
||||
|
||||
#include "core/ustring.h"
|
||||
|
||||
#include "database.h"
|
||||
|
||||
class QueryBuilder;
|
||||
class TableBuilder;
|
||||
class QueryResult;
|
||||
class DatabaseConnection;
|
||||
|
||||
class DatabaseSingleThreaded : public Database {
|
||||
GDCLASS(DatabaseSingleThreaded, Database);
|
||||
|
||||
public:
|
||||
String get_connection_string();
|
||||
void set_connection_string(const String &val);
|
||||
|
||||
Ref<DatabaseConnection> get_connection();
|
||||
|
||||
DatabaseSingleThreaded();
|
||||
~DatabaseSingleThreaded();
|
||||
|
||||
protected:
|
||||
Ref<DatabaseConnection> _allocate_connection();
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
Ref<DatabaseConnection> _connection;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user