Skip to content

Commit

Permalink
Fixed Auto Updater
Browse files Browse the repository at this point in the history
Fixed auto updater
  • Loading branch information
talamortis committed Oct 31, 2020
1 parent f904f21 commit b648fde
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/server/database/Database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "DatabaseEnv.h"
#include "Configuration/Config.h"
#include "Database/SqlOperations.h"
#include "DatabaseMysql.h"

#include <ctime>
#include <iostream>
Expand Down Expand Up @@ -360,8 +361,8 @@ bool Database::PExecuteLog(char const* format,...)

bool Database::ExecuteFile(const char* file)
{
//TBD
return true;
SqlConnection::Lock _guard(getAsyncConnection());
return _guard->ExecuteFile(file);
}

QueryResult* Database::PQuery(char const* format,...)
Expand Down
9 changes: 7 additions & 2 deletions src/server/database/Database/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class SqlConnection
virtual bool CommitTransaction() { return true; }
// can't rollback without transaction support
virtual bool RollbackTransaction() { return true; }
virtual bool ExecuteFile(char const* file) { return true; }

//methods to work with prepared statements
bool ExecuteStmt(int nIndex, SqlStmtParameters const& id);
Expand Down Expand Up @@ -155,6 +156,7 @@ class Database

bool DirectPExecute(char const* format,...) ATTR_PRINTF(2,3);


/// Async queries and query holders, implemented in DatabaseImpl.h

// Query / member
Expand Down Expand Up @@ -240,7 +242,8 @@ class Database
//get prepared statement format string
std::string GetStmtString(int const stmtId) const;

operator bool () const { return !m_pQueryConnections.empty() && m_pAsyncConn != 0; }
operator bool() const { return mMysql != NULL && !m_pQueryConnections.empty() && m_pAsyncConn != 0; }


//escape string generation
void escape_string(std::string& str);
Expand Down Expand Up @@ -269,6 +272,7 @@ class Database
inline void AddToSerialDelayQueue(int workerId, SqlOperation* op) { m_serialDelayQueue[workerId]->add(op); }
bool NextSerialDelayedOperation(int workerId, SqlOperation*& op);


bool HasAsyncQuery();

void AddToSerialDelayQueue(SqlOperation* op);
Expand Down Expand Up @@ -356,9 +360,10 @@ class Database
int m_iStmtIndex;

private:

bool m_logSQL;
std::string m_logsDir;
uint32 m_pingIntervallms;

MYSQL* mMysql;
};
#endif
96 changes: 96 additions & 0 deletions src/server/database/Database/DatabaseMysql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,102 @@ MySQLConnection::~MySQLConnection()
mysql_close(mMysql);
}

bool MySQLConnection::ExecuteFile(const char* file)
{
if (!mMysql)
return false;

if (mysql_set_server_option(mMysql, MYSQL_OPTION_MULTI_STATEMENTS_ON))
{
sLog.outErrorDb("Cannot turn multi-statements on: %s", mysql_error(mMysql));
return false;
}

mysql_autocommit(mMysql, 0);
if (mysql_real_query(mMysql, "START TRANSACTION", sizeof("START TRANSACTION") - 1))
{
sLog.outErrorDb("Couldn't start transaction for db update file: %s", file);
return false;
}

bool in_transaction = true;
bool success = false;

if (FILE* fp = ACE_OS::fopen(file, "rb"))
{
#if PLATFORM == PLATFORM_UNIX
flock(fileno(fp), LOCK_SH);
#endif
//------

struct stat info;
fstat(fileno(fp), &info);

// if less than 1MB allocate on stack, else on heap
char* contents = (info.st_size > 1024 * 1024) ? new char[info.st_size] : (char*)alloca(info.st_size);

if (ACE_OS::fread(contents, info.st_size, 1, fp) == 1)
{
if (mysql_real_query(mMysql, contents, info.st_size))
{
sLog.outErrorDb("Cannot execute file %s, size: %lu: %s", file, info.st_size, mysql_error(mMysql));
}
else
{
do
{
if (mysql_field_count(mMysql))
if (MYSQL_RES* result = mysql_use_result(mMysql))
mysql_free_result(result);
} while (0 == mysql_next_result(mMysql));

// check whether the last mysql_next_result ended with an error
if (*mysql_error(mMysql))
{
success = false;
sLog.outErrorDb("Cannot execute file %s, size: %lu: %s", file, info.st_size, mysql_error(mMysql));
if (mysql_rollback(mMysql))
sLog.outErrorDb("ExecuteFile(): Rollback ended with an error!");
else
in_transaction = false;
}
else
{
if (mysql_commit(mMysql))
sLog.outErrorDb("mysql_commit() failed. Update %s will not be applied!", file);
else
in_transaction = false;
success = true;
}
}
}
else
{
sLog.outErrorDb("Couldn't read file %s, size: %lu", file, info.st_size);
return false;
}

// if allocated on heap, free memory
if (info.st_size > 1024 * 1024)
delete[] contents;

//------
#if PLATFORM == PLATFORM_UNIX
flock(fileno(fp), LOCK_UN);
#endif
ACE_OS::fclose(fp);
}

mysql_set_server_option(mMysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
mysql_autocommit(mMysql, 1);
if (in_transaction)
mysql_rollback(mMysql);

sLog.outErrorDb("Successfully applied file %s", file);

return success;
}

bool MySQLConnection::OpenConnection(bool reconnect)
{
MYSQL* mysqlInit = mysql_init(nullptr);
Expand Down
5 changes: 5 additions & 0 deletions src/server/database/Database/DatabaseMysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "Policies/Singleton.h"
#include "ace/Thread_Mutex.h"
#include "ace/Guard_T.h"
#include "ace/Singleton.h"

#ifdef WIN32
#include <winsock2.h>
Expand Down Expand Up @@ -83,6 +84,8 @@ class MySQLConnection : public SqlConnection
MySQLConnection(Database& db) : SqlConnection(db), mMysql(nullptr) {}
~MySQLConnection() override;


bool ExecuteFile(char const* file);
bool OpenConnection(bool reconnect) override;
bool Reconnect();
bool HandleMySQLError(uint32 errNo);
Expand Down Expand Up @@ -131,5 +134,7 @@ class DatabaseMysql : public Database
MYSQL* mMysql;
};

#define sMySQLConnection Oregon::Singleton<MySQLConnection>::Instance()

#endif
#endif

0 comments on commit b648fde

Please sign in to comment.