mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 19:03:43 +00:00
Four tables (MySQL migration 97, SQLite 80) and the IGuilds interface with MySQL, SQLite and TestSQL implementations, for the guild system (docs/Guilds.md). A guild has a name (unique without regard to case), a name status like pet_names (1 waiting for moderation, 2 approved), its founder and when it was made. A character is in at most one guild (guild_members, with its rank: 1 leader, 2 officer, 3 veteran, 4 recruit, the client's names, and when it joined) and has at most one pending invite (guild_invites). guild_events is each guild's history and outlives the guild. Deleting a character removes its membership and the invites to and from it. Nothing uses the tables yet. Check: both migrations run on a fresh and an existing database; DatabaseParityTests Guilds passes against MariaDB (DLU_TEST_MYSQL_HOST) and SQLite gives the same results. Nothing to check in game. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
3.1 KiB
C++
84 lines
3.1 KiB
C++
#include "SQLiteDatabase.h"
|
|
|
|
#include "Database.h"
|
|
#include "Game.h"
|
|
#include "dConfig.h"
|
|
#include "Logger.h"
|
|
#include "dPlatforms.h"
|
|
#include "BinaryPathFinder.h"
|
|
|
|
void SQLiteDatabase::Connect() {
|
|
LOG("Using SQLite database");
|
|
m_Con = new CppSQLite3DB();
|
|
const auto path = BinaryPathFinder::GetBinaryDir() / Game::config->GetValue("sqlite_database_path");
|
|
|
|
if (!std::filesystem::exists(path)) {
|
|
LOG("Creating sqlite path %s", path.string().c_str());
|
|
std::filesystem::create_directories(path.parent_path());
|
|
}
|
|
|
|
m_Con->open(path.string().c_str());
|
|
|
|
// Make sure wal is enabled for the database.
|
|
m_Con->execQuery("PRAGMA journal_mode = WAL;");
|
|
}
|
|
|
|
void SQLiteDatabase::Destroy(std::string source) {
|
|
if (!m_Con) return;
|
|
|
|
if (source.empty()) LOG("Destroying SQLite connection!");
|
|
else LOG("Destroying SQLite connection from %s!", source.c_str());
|
|
|
|
m_Con->close();
|
|
delete m_Con;
|
|
m_Con = nullptr;
|
|
}
|
|
|
|
void SQLiteDatabase::ExecuteCustomQuery(const std::string_view query) {
|
|
m_Con->compileStatement(query.data()).execDML();
|
|
}
|
|
|
|
CppSQLite3Statement SQLiteDatabase::CreatePreppedStmt(const std::string& query) {
|
|
return m_Con->compileStatement(query.c_str());
|
|
}
|
|
|
|
void SQLiteDatabase::Commit() {
|
|
if (!m_Con->IsAutoCommitOn()) m_Con->compileStatement("COMMIT;").execDML();
|
|
}
|
|
|
|
bool SQLiteDatabase::GetAutoCommit() {
|
|
return m_Con->IsAutoCommitOn();
|
|
}
|
|
|
|
void SQLiteDatabase::SetAutoCommit(bool value) {
|
|
if (value) {
|
|
if (!GetAutoCommit()) m_Con->compileStatement("COMMIT;").execDML();
|
|
} else {
|
|
if (GetAutoCommit()) m_Con->compileStatement("BEGIN;").execDML();
|
|
}
|
|
}
|
|
|
|
void SQLiteDatabase::Rollback() {
|
|
// A failed statement can already have ended the transaction; only roll back one that is open
|
|
if (!m_Con->IsAutoCommitOn()) m_Con->compileStatement("ROLLBACK;").execDML();
|
|
}
|
|
|
|
void SQLiteDatabase::DeleteCharacter(const LWOOBJID characterId) {
|
|
ExecuteDelete("DELETE FROM charxml WHERE id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM command_log WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM friends WHERE player_id=? OR friend_id=?;", characterId, characterId);
|
|
ExecuteDelete("DELETE FROM leaderboard WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM properties_contents WHERE property_id IN (SELECT id FROM properties WHERE owner_id=?);", characterId);
|
|
ExecuteDelete("DELETE FROM properties WHERE owner_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM ugc WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM activity_log WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM mail WHERE receiver_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM ignore_list WHERE player_id=? OR ignored_player_id=?;", characterId, characterId);
|
|
ExecuteDelete("DELETE FROM ugc_modular_build WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM pet_names WHERE owner_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM player_positions WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM guild_members WHERE character_id=?;", characterId);
|
|
ExecuteDelete("DELETE FROM guild_invites WHERE character_id=? OR inviter_id=?;", characterId, characterId);
|
|
ExecuteDelete("DELETE FROM charinfo WHERE id=?;", characterId);
|
|
}
|