Files
DarkflameServer/dDatabase/GameDatabase/SQLite/SQLiteDatabase.cpp
David Markowitz 922a9ef5ea feat: convert character ids to 64 bits
remove all usages of the PERSISTENT bit with regards to storing of playerIDs on the server.  the bit does not exist and was a phantom in the first place.
Tested that a full playthrough of ag, ns and gf was still doable.  slash commands work, ugc works, friends works, ignore list works, properties work and have names, teaming works.
migrating an old mysql database works . need to test an old sqlite database
2025-09-18 21:48:28 -07:00

82 lines
2.5 KiB
C++

#include "SQLiteDatabase.h"
#include "Database.h"
#include "Game.h"
#include "dConfig.h"
#include "Logger.h"
#include "dPlatforms.h"
#include "BinaryPathFinder.h"
// Static Variables
// Status Variables
namespace {
CppSQLite3DB* con = nullptr;
bool isConnected = false;
};
void SQLiteDatabase::Connect() {
LOG("Using SQLite database");
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());
}
con->open(path.string().c_str());
isConnected = true;
// Make sure wal is enabled for the database.
con->execQuery("PRAGMA journal_mode = WAL;");
}
void SQLiteDatabase::Destroy(std::string source) {
if (!con) return;
if (source.empty()) LOG("Destroying SQLite connection!");
else LOG("Destroying SQLite connection from %s!", source.c_str());
con->close();
delete con;
con = nullptr;
}
void SQLiteDatabase::ExecuteCustomQuery(const std::string_view query) {
con->compileStatement(query.data()).execDML();
}
CppSQLite3Statement SQLiteDatabase::CreatePreppedStmt(const std::string& query) {
return con->compileStatement(query.c_str());
}
void SQLiteDatabase::Commit() {
if (!con->IsAutoCommitOn()) con->compileStatement("COMMIT;").execDML();
}
bool SQLiteDatabase::GetAutoCommit() {
return con->IsAutoCommitOn();
}
void SQLiteDatabase::SetAutoCommit(bool value) {
if (value) {
if (GetAutoCommit()) con->compileStatement("BEGIN;").execDML();
} else {
if (!GetAutoCommit()) con->compileStatement("COMMIT;").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 charinfo WHERE id=?;", characterId);
}