Core/DBLayer: Support using mysql 8

This commit is contained in:
Shauren
2019-11-01 16:21:14 +01:00
parent 2072258ef4
commit c3a9d56b56
23 changed files with 509 additions and 375 deletions

View File

@@ -19,16 +19,15 @@
#include "Common.h"
#include "DatabaseWorker.h"
#include "Log.h"
#include "MySQLHacks.h"
#include "MySQLPreparedStatement.h"
#include "PreparedStatement.h"
#include "QueryResult.h"
#include "Timer.h"
#include "Transaction.h"
#include "Util.h"
#ifdef _WIN32 // hack for broken mysql.h not including the correct winsock header for SOCKET definition, fixed in 5.7
#include <winsock2.h>
#endif
#include <errmsg.h>
#include <mysql.h>
#include "MySQLWorkaround.h"
#include <mysqld_error.h>
MySQLConnectionInfo::MySQLConnectionInfo(std::string const& infoString)
@@ -130,8 +129,8 @@ uint32 MySQLConnection::Open()
}
#endif
m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(),
m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0);
m_Mysql = reinterpret_cast<MySQLHandle*>(mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(),
m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0));
if (m_Mysql)
{
@@ -241,7 +240,7 @@ bool MySQLConnection::Execute(PreparedStatementBase* stmt)
return true;
}
bool MySQLConnection::_Query(PreparedStatementBase* stmt, MYSQL_RES** pResult, uint64* pRowCount, uint32* pFieldCount)
bool MySQLConnection::_Query(PreparedStatementBase* stmt, MySQLResult** pResult, uint64* pRowCount, uint32* pFieldCount)
{
if (!m_Mysql)
return false;
@@ -288,7 +287,7 @@ bool MySQLConnection::_Query(PreparedStatementBase* stmt, MYSQL_RES** pResult, u
m_mStmt->ClearParameters();
*pResult = mysql_stmt_result_metadata(msql_STMT);
*pResult = reinterpret_cast<MySQLResult*>(mysql_stmt_result_metadata(msql_STMT));
*pRowCount = mysql_stmt_num_rows(msql_STMT);
*pFieldCount = mysql_stmt_field_count(msql_STMT);
@@ -300,8 +299,8 @@ ResultSet* MySQLConnection::Query(const char* sql)
if (!sql)
return NULL;
MYSQL_RES *result = NULL;
MYSQL_FIELD *fields = NULL;
MySQLResult* result = NULL;
MySQLField* fields = NULL;
uint64 rowCount = 0;
uint32 fieldCount = 0;
@@ -311,7 +310,7 @@ ResultSet* MySQLConnection::Query(const char* sql)
return new ResultSet(result, fields, rowCount, fieldCount);
}
bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD **pFields, uint64* pRowCount, uint32* pFieldCount)
bool MySQLConnection::_Query(const char* sql, MySQLResult** pResult, MySQLField** pFields, uint64* pRowCount, uint32* pFieldCount)
{
if (!m_Mysql)
return false;
@@ -333,7 +332,7 @@ bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD *
else
TC_LOG_DEBUG("sql.sql", "[%u ms] SQL: %s", getMSTimeDiff(_s, getMSTime()), sql);
*pResult = mysql_store_result(m_Mysql);
*pResult = reinterpret_cast<MySQLResult*>(mysql_store_result(m_Mysql));
*pRowCount = mysql_affected_rows(m_Mysql);
*pFieldCount = mysql_field_count(m_Mysql);
}
@@ -347,7 +346,7 @@ bool MySQLConnection::_Query(const char *sql, MYSQL_RES **pResult, MYSQL_FIELD *
return false;
}
*pFields = mysql_fetch_fields(*pResult);
*pFields = reinterpret_cast<MySQLField*>(mysql_fetch_fields(*pResult));
return true;
}
@@ -418,6 +417,11 @@ int MySQLConnection::ExecuteTransaction(std::shared_ptr<TransactionBase> transac
return 0;
}
size_t MySQLConnection::EscapeString(char* to, const char* from, size_t length)
{
return mysql_real_escape_string(m_Mysql, to, from, length);
}
void MySQLConnection::Ping()
{
mysql_ping(m_Mysql);
@@ -438,6 +442,11 @@ void MySQLConnection::Unlock()
m_Mutex.unlock();
}
uint32 MySQLConnection::GetServerVersion() const
{
return mysql_get_server_version(m_Mysql);
}
MySQLPreparedStatement* MySQLConnection::GetPreparedStatement(uint32 index)
{
ASSERT(index < m_stmts.size());
@@ -477,13 +486,13 @@ void MySQLConnection::PrepareStatement(uint32 index, std::string const& sql, Con
m_prepareError = true;
}
else
m_stmts[index] = Trinity::make_unique<MySQLPreparedStatement>(stmt, sql);
m_stmts[index] = Trinity::make_unique<MySQLPreparedStatement>(reinterpret_cast<MySQLStmt*>(stmt), sql);
}
}
PreparedResultSet* MySQLConnection::Query(PreparedStatementBase* stmt)
{
MYSQL_RES *result = NULL;
MySQLResult* result = NULL;
uint64 rowCount = 0;
uint32 fieldCount = 0;
@@ -509,7 +518,7 @@ bool MySQLConnection::_HandleMySQLErrno(uint32 errNo, uint8 attempts /*= 5*/)
{
TC_LOG_ERROR("sql.sql", "Lost the connection to the MySQL server!");
mysql_close(GetHandle());
mysql_close(m_Mysql);
m_Mysql = nullptr;
}