Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DB): add support warhead db #7

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/ChatTransmitterDatabaseConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
class MySQLPreparedStatement {};

ChatTransmitterDatabaseConnection::ChatTransmitterDatabaseConnection(MySQLConnectionInfo& connInfo)
: MySQLConnection(connInfo)
: MySQLConnection(connInfo, nullptr, false)
{
}

ChatTransmitterDatabaseConnection::ChatTransmitterDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo)
: MySQLConnection(q, connInfo)
ChatTransmitterDatabaseConnection::ChatTransmitterDatabaseConnection(MySQLConnectionInfo& connInfo, ProducerConsumerQueue<AsyncOperation*>* dbQueue)
: MySQLConnection(connInfo, dbQueue, false)
{
}

ChatTransmitterDatabaseConnection::~ChatTransmitterDatabaseConnection()
{
}

void ChatTransmitterDatabaseConnection::DoPrepareStatements()
{
}
ChatTransmitterDatabaseConnection::~ChatTransmitterDatabaseConnection() = default;

bool ChatTransmitterDatabaseConnection::_HandleMySQLErrno(uint32 errNo, uint8 attempts)
bool ChatTransmitterDatabaseConnection::HandleMySQLError(uint32 errNo, uint8 attempts)
{
switch (errNo)
{
Expand All @@ -31,17 +25,17 @@ bool ChatTransmitterDatabaseConnection::_HandleMySQLErrno(uint32 errNo, uint8 at
case ER_PARSE_ERROR:
return false;
default:
return MySQLConnection::_HandleMySQLErrno(errNo, attempts);
return MySQLConnection::HandleMySQLError(errNo, attempts);
}
}

std::string ChatTransmitterDatabaseConnection::GetLastErrorString()
{
const char* err = mysql_error(reinterpret_cast<MYSQL*>(m_Mysql));
const char* err = mysql_error(reinterpret_cast<MYSQL*>(_mysqlHandle));
return std::string(err);
}

uint64 ChatTransmitterDatabaseConnection::GetAffectedRows()
{
return mysql_affected_rows(reinterpret_cast<MYSQL*>(m_Mysql));
return mysql_affected_rows(reinterpret_cast<MYSQL*>(_mysqlHandle));
}
11 changes: 3 additions & 8 deletions src/ChatTransmitterDatabaseConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
class ChatTransmitterDatabaseConnection : public MySQLConnection
{
public:
enum Statements : uint32
{ };

ChatTransmitterDatabaseConnection(MySQLConnectionInfo& connInfo);
ChatTransmitterDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo);
explicit ChatTransmitterDatabaseConnection(MySQLConnectionInfo& connInfo);
ChatTransmitterDatabaseConnection(MySQLConnectionInfo& connInfo, ProducerConsumerQueue<AsyncOperation*>* dbQueue);

~ChatTransmitterDatabaseConnection() override;

void DoPrepareStatements() override;

std::string GetLastErrorString();
uint64 GetAffectedRows();

protected:
bool _HandleMySQLErrno(uint32 errNo, uint8 attempts = 5) override;
bool HandleMySQLError(uint32 errNo, uint8 attempts = 5) override;
};

#endif
19 changes: 12 additions & 7 deletions src/DatabaseManager.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/*
*
* */

#include "Config.h"
#include "Common.h"
#include "DatabaseEnv.h"
#include "Time/GameTime.h"
#include "DatabaseManager.h"
#include "ChatTransmitter.h"
#include "DatabaseAsyncOperation.h"
#include "Requests/RequestQueryResult.h"

namespace ModChatTransmitter
Expand Down Expand Up @@ -84,7 +89,7 @@ namespace ModChatTransmitter
return;
}

ResultSet* result = db->Query(query);
auto result = db->Query(query);
if (!result)
{
if (db->GetLastError())
Expand Down Expand Up @@ -164,11 +169,11 @@ namespace ModChatTransmitter
workQueue.Cancel();
}

void DatabaseManager::InitializeDatabase(const MySQLConnectionInfo* srcConnInfo, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<SQLOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database)
void DatabaseManager::InitializeDatabase(const MySQLConnectionInfo* srcConnInfo, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<AsyncOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database)
{
*connInfo = new MySQLConnectionInfo(*srcConnInfo);
*sqlQueue = new ProducerConsumerQueue<SQLOperation*>();
*database = new ChatTransmitterDatabaseConnection(*sqlQueue, **connInfo);
*sqlQueue = new ProducerConsumerQueue<AsyncOperation*>();
*database = new ChatTransmitterDatabaseConnection(**connInfo, *sqlQueue);

uint32 res = (*database)->Open();
if (res)
Expand All @@ -178,14 +183,14 @@ namespace ModChatTransmitter
}
}

void DatabaseManager::InitializeDatabase(const std::string& connectionString, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<SQLOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database)
void DatabaseManager::InitializeDatabase(const std::string& connectionString, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<AsyncOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database)
{
MySQLConnectionInfo* srcConnInfo = new MySQLConnectionInfo(connectionString);
auto srcConnInfo = new MySQLConnectionInfo(connectionString);
InitializeDatabase(srcConnInfo, connInfo, sqlQueue, database);
delete srcConnInfo;
}

void DatabaseManager::CleanupDatabase(MySQLConnectionInfo* connInfo, ProducerConsumerQueue<SQLOperation*>* sqlQueue, ChatTransmitterDatabaseConnection** database)
void DatabaseManager::CleanupDatabase(MySQLConnectionInfo* connInfo, ProducerConsumerQueue<AsyncOperation*>* sqlQueue, ChatTransmitterDatabaseConnection** database)
{
(*database)->Close();
sqlQueue->Cancel();
Expand Down
25 changes: 14 additions & 11 deletions src/DatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "PCQueue.h"
#include "Requests/RequestQueryResult.h"
#include "ChatTransmitterDatabaseConnection.h"
#include <utility>

class AsyncOperation;

namespace ModChatTransmitter
{
Expand All @@ -26,9 +29,9 @@ namespace ModChatTransmitter

private:
void HandleQuery(const std::string& id, const std::string& query, QueryDatabase dbType);
void InitializeDatabase(const std::string& connectionString, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<SQLOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database);
void InitializeDatabase(const MySQLConnectionInfo* srcConnInfo, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<SQLOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database);
void CleanupDatabase(MySQLConnectionInfo* connInfo, ProducerConsumerQueue<SQLOperation*>* sqlQueue, ChatTransmitterDatabaseConnection** database);
void InitializeDatabase(const std::string& connectionString, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<AsyncOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database);
void InitializeDatabase(const MySQLConnectionInfo* srcConnInfo, MySQLConnectionInfo** connInfo, ProducerConsumerQueue<AsyncOperation*>** sqlQueue, ChatTransmitterDatabaseConnection** database);
void CleanupDatabase(MySQLConnectionInfo* connInfo, ProducerConsumerQueue<AsyncOperation*>* sqlQueue, ChatTransmitterDatabaseConnection** database);
bool IsValidUtf8(const Binary& data);
void Cleanup();

Expand All @@ -38,9 +41,9 @@ namespace ModChatTransmitter
std::string query;
QueryDatabase dbType;

WorkItem(const std::string& id, const std::string& query, QueryDatabase dbType)
: id(id),
query(query),
WorkItem(std::string id, std::string query, QueryDatabase dbType)
: id(std::move(id)),
query(std::move(query)),
dbType(dbType)
{ }
};
Expand All @@ -52,22 +55,22 @@ namespace ModChatTransmitter
long long nextDbPingTime;

MySQLConnectionInfo* authConnInfo;
ProducerConsumerQueue<SQLOperation*>* authSqlQueue;
ProducerConsumerQueue<AsyncOperation*>* authSqlQueue;
ChatTransmitterDatabaseConnection* authDatabase;

MySQLConnectionInfo* charsConnInfo;
ProducerConsumerQueue<SQLOperation*>* charsSqlQueue;
ProducerConsumerQueue<AsyncOperation*>* charsSqlQueue;
ChatTransmitterDatabaseConnection* charsDatabase;

MySQLConnectionInfo* worldConnInfo;
ProducerConsumerQueue<SQLOperation*>* worldSqlQueue;
ProducerConsumerQueue<AsyncOperation*>* worldSqlQueue;
ChatTransmitterDatabaseConnection* worldDatabase;

MySQLConnectionInfo* elunaConnInfo;
ProducerConsumerQueue<SQLOperation*>* elunaSqlQueue;
ProducerConsumerQueue<AsyncOperation*>* elunaSqlQueue;
ChatTransmitterDatabaseConnection* elunaDatabase;

std::atomic_bool stop;
std::atomic<bool> stop;
};
}

Expand Down