mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 02:43:44 +00:00
The NexusDashboard-parity dashboard (dDashboardServer) and everything built on it on the experimental branch: accounts, characters, properties and moderation tools, permissions shared with in-game slash commands, economy reports, World 3D and property 3D views with client scenery, scheduled events (features, vanity changes, live events, announcements, restarts), vanity files and events, the CDClient browser, the message inspector with saved captures, chat filter tools, community challenges, live ops, the AI moderator helper, and the server-side changes they need. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
1.9 KiB
C++
78 lines
1.9 KiB
C++
#include "Database.h"
|
|
#include "Game.h"
|
|
#include "dConfig.h"
|
|
#include "Logger.h"
|
|
#include "DluAssert.h"
|
|
|
|
#include "SQLiteDatabase.h"
|
|
#include "MySQLDatabase.h"
|
|
|
|
#include <ranges>
|
|
|
|
#pragma warning (disable:4251) //Disables SQL warnings
|
|
|
|
namespace {
|
|
GameDatabase* database = nullptr;
|
|
}
|
|
|
|
std::string Database::GetMigrationFolder() {
|
|
const std::set<std::string> validMysqlTypes = { "mysql", "mariadb", "maria" };
|
|
auto databaseType = Game::config->GetValue("database_type");
|
|
std::ranges::transform(databaseType, databaseType.begin(), ::tolower);
|
|
if (databaseType == "sqlite") return "sqlite";
|
|
else if (validMysqlTypes.contains(databaseType)) return "mysql";
|
|
else {
|
|
LOG("No database specified, using MySQL");
|
|
return "mysql";
|
|
}
|
|
}
|
|
|
|
void Database::Connect() {
|
|
if (database) {
|
|
LOG("Tried to connect to database when it's already connected!");
|
|
return;
|
|
}
|
|
|
|
const auto databaseType = GetMigrationFolder();
|
|
|
|
if (databaseType == "sqlite") database = new SQLiteDatabase();
|
|
else if (databaseType == "mysql") database = new MySQLDatabase();
|
|
else {
|
|
LOG("Invalid database type specified in config, using MySQL");
|
|
database = new MySQLDatabase();
|
|
}
|
|
|
|
database->Connect();
|
|
}
|
|
|
|
GameDatabase* Database::Get() {
|
|
if (!database) {
|
|
LOG("Tried to get database when it's not connected!");
|
|
Connect();
|
|
}
|
|
return database;
|
|
}
|
|
|
|
std::unique_ptr<GameDatabase> Database::CreateConnection() {
|
|
std::unique_ptr<GameDatabase> connection;
|
|
if (GetMigrationFolder() == "sqlite") connection = std::make_unique<SQLiteDatabase>();
|
|
else connection = std::make_unique<MySQLDatabase>();
|
|
connection->Connect();
|
|
return connection;
|
|
}
|
|
|
|
void Database::Destroy(std::string source) {
|
|
if (database) {
|
|
database->Destroy(source);
|
|
delete database;
|
|
database = nullptr;
|
|
} else {
|
|
LOG("Trying to destroy database when it's not connected!");
|
|
}
|
|
}
|
|
|
|
void Database::_setDatabase(GameDatabase* const db) {
|
|
if (database) delete database;
|
|
database = db;
|
|
}
|