mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-12-23 05:53:34 +00:00
a60865cd19
* simplify leaderboard code, fully abstract database * update exception catching * update exception catching and sql references, remove ugc from gamemessages fix deleting model remove unrelated changes Update GameMessages.cpp * remove ugc from gamemessages * Update GameMessages.cpp * Update Leaderboard.cpp * bug fixes * fix racing leaderboard * remove extra stuff * update * add sqlite * use a default for optimizations * update sqlite * Fix limits on update and delete * fix bugs * use definition to switch between databases * add switch for different backends * fix include guard and includes * always build both * add mysql if block * Update Database.cpp * add new options and add check to prevent overriding mysql * correct config names * Update README.md * Update README.md * merge to 1 sql file for sqlite database * move to sqlite folder * add back mysql migrations * Update README.md * add migration to correct the folder name or mysql * yes aron * updates * Update CMakeLists.txt * dont use paths at all, add where check to only update if folder name still exist check also doesnt check for slashes and assumes one will be there since it will be. * default dont auto create account for releases we can change this flag * default 0 * add times played query * fix leaderboard not incrementing on a not better score * add env vars with defaults for docker * use an "enum" * default to mariadb * Update .env.example
115 lines
4.4 KiB
C++
115 lines
4.4 KiB
C++
#include "MySQLDatabase.h"
|
|
|
|
#include "Database.h"
|
|
#include "Game.h"
|
|
#include "dConfig.h"
|
|
#include "Logger.h"
|
|
#include "dPlatforms.h"
|
|
|
|
namespace {
|
|
std::string databaseName;
|
|
sql::Properties properties;
|
|
sql::Driver* driver = nullptr;
|
|
sql::Connection* con = nullptr;
|
|
};
|
|
|
|
void MySQLDatabase::Connect() {
|
|
LOG("Using MySQL database");
|
|
driver = sql::mariadb::get_driver_instance();
|
|
|
|
// The mariadb connector is *supposed* to handle unix:// and pipe:// prefixes to hostName, but there are bugs where
|
|
// 1) it tries to parse a database from the connection string (like in tcp://localhost:3001/darkflame) based on the
|
|
// presence of a /
|
|
// 2) even avoiding that, the connector still assumes you're connecting with a tcp socket
|
|
// So, what we do in the presence of a unix socket or pipe is to set the hostname to the protocol and localhost,
|
|
// which avoids parsing errors while still ensuring the correct connection type is used, and then setting the appropriate
|
|
// property manually (which the URL parsing fails to do)
|
|
const std::string UNIX_PROTO = "unix://";
|
|
const std::string PIPE_PROTO = "pipe://";
|
|
std::string mysql_host = Game::config->GetValue("mysql_host");
|
|
if (mysql_host.find(UNIX_PROTO) == 0) {
|
|
properties["hostName"] = "unix://localhost";
|
|
properties["localSocket"] = mysql_host.substr(UNIX_PROTO.length()).c_str();
|
|
} else if (mysql_host.find(PIPE_PROTO) == 0) {
|
|
properties["hostName"] = "pipe://localhost";
|
|
properties["pipe"] = mysql_host.substr(PIPE_PROTO.length()).c_str();
|
|
} else {
|
|
properties["hostName"] = mysql_host.c_str();
|
|
}
|
|
properties["user"] = Game::config->GetValue("mysql_username").c_str();
|
|
properties["password"] = Game::config->GetValue("mysql_password").c_str();
|
|
properties["autoReconnect"] = "true";
|
|
|
|
databaseName = Game::config->GetValue("mysql_database").c_str();
|
|
// `connect(const Properties& props)` segfaults in windows debug, but
|
|
// `connect(const SQLString& host, const SQLString& user, const SQLString& pwd)` doesn't handle pipes/unix sockets correctly
|
|
#if defined(DARKFLAME_PLATFORM_WIN32) && defined(_DEBUG)
|
|
con = driver->connect(properties["hostName"].c_str(), properties["user"].c_str(), properties["password"].c_str());
|
|
#else
|
|
con = driver->connect(properties);
|
|
#endif
|
|
con->setSchema(databaseName.c_str());
|
|
}
|
|
|
|
void MySQLDatabase::Destroy(std::string source) {
|
|
if (!con) return;
|
|
|
|
if (source.empty()) LOG("Destroying MySQL connection!");
|
|
else LOG("Destroying MySQL connection from %s!", source.c_str());
|
|
|
|
con->close();
|
|
delete con;
|
|
con = nullptr;
|
|
}
|
|
|
|
void MySQLDatabase::ExecuteCustomQuery(const std::string_view query) {
|
|
std::unique_ptr<sql::Statement>(con->createStatement())->execute(query.data());
|
|
}
|
|
|
|
sql::PreparedStatement* MySQLDatabase::CreatePreppedStmt(const std::string& query) {
|
|
if (!con) {
|
|
Database::Get()->Connect();
|
|
LOG("Trying to reconnect to MySQL");
|
|
}
|
|
|
|
if (!con->isValid() || con->isClosed()) {
|
|
delete con;
|
|
|
|
con = nullptr;
|
|
|
|
Database::Get()->Connect();
|
|
LOG("Trying to reconnect to MySQL from invalid or closed connection");
|
|
}
|
|
|
|
return con->prepareStatement(sql::SQLString(query.c_str(), query.length()));
|
|
}
|
|
|
|
void MySQLDatabase::Commit() {
|
|
con->commit();
|
|
}
|
|
|
|
bool MySQLDatabase::GetAutoCommit() {
|
|
// TODO This should not just access a pointer. A future PR should update this
|
|
// to check for null and throw an error if the connection is not valid.
|
|
return con->getAutoCommit();
|
|
}
|
|
|
|
void MySQLDatabase::SetAutoCommit(bool value) {
|
|
// TODO This should not just access a pointer. A future PR should update this
|
|
// to check for null and throw an error if the connection is not valid.
|
|
con->setAutoCommit(value);
|
|
}
|
|
|
|
void MySQLDatabase::DeleteCharacter(const uint32_t characterId) {
|
|
ExecuteDelete("DELETE FROM charxml WHERE id=? LIMIT 1;", 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=? LIMIT 1;", characterId);
|
|
}
|