aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/server/database/Database/DatabaseWorkerPool.cpp26
-rw-r--r--src/server/database/Database/DatabaseWorkerPool.h1
-rw-r--r--src/server/database/Database/MySQLConnection.cpp141
-rw-r--r--src/server/database/Database/MySQLConnection.h9
-rw-r--r--src/server/database/Database/PreparedStatement.cpp120
-rw-r--r--src/server/database/Database/PreparedStatement.h19
6 files changed, 160 insertions, 156 deletions
diff --git a/src/server/database/Database/DatabaseWorkerPool.cpp b/src/server/database/Database/DatabaseWorkerPool.cpp
index 77beac8f277..dce61ed891b 100644
--- a/src/server/database/Database/DatabaseWorkerPool.cpp
+++ b/src/server/database/Database/DatabaseWorkerPool.cpp
@@ -128,6 +128,7 @@ template <class T>
bool DatabaseWorkerPool<T>::PrepareStatements()
{
for (auto& connections : _connections)
+ {
for (auto& connection : connections)
{
connection->LockIfReady();
@@ -139,7 +140,30 @@ bool DatabaseWorkerPool<T>::PrepareStatements()
}
else
connection->Unlock();
+
+ size_t const preparedSize = connection->m_stmts.size();
+ if (_preparedStatementSize.size() < preparedSize)
+ _preparedStatementSize.resize(preparedSize);
+
+ for (size_t i = 0; i < preparedSize; ++i)
+ {
+ // already set by another connection
+ // (each connection only has prepared statements of it's own type sync/async)
+ if (_preparedStatementSize[i] > 0)
+ continue;
+
+ if (MySQLPreparedStatement * stmt = connection->m_stmts[i].get())
+ {
+ uint32 const paramCount = stmt->GetParameterCount();
+
+ // TC only supports uint8 indices.
+ ASSERT(paramCount < std::numeric_limits<uint8>::max());
+
+ _preparedStatementSize[i] = static_cast<uint8>(paramCount);
+ }
+ }
}
+ }
return true;
}
@@ -271,7 +295,7 @@ void DatabaseWorkerPool<T>::DirectCommitTransaction(SQLTransaction<T>& transacti
template <class T>
PreparedStatement<T>* DatabaseWorkerPool<T>::GetPreparedStatement(PreparedStatementIndex index)
{
- return new PreparedStatement<T>(index);
+ return new PreparedStatement<T>(index, _preparedStatementSize[index]);
}
template <class T>
diff --git a/src/server/database/Database/DatabaseWorkerPool.h b/src/server/database/Database/DatabaseWorkerPool.h
index a679dca14b8..40678bbc283 100644
--- a/src/server/database/Database/DatabaseWorkerPool.h
+++ b/src/server/database/Database/DatabaseWorkerPool.h
@@ -219,6 +219,7 @@ class DatabaseWorkerPool
std::unique_ptr<ProducerConsumerQueue<SQLOperation*>> _queue;
std::array<std::vector<std::unique_ptr<T>>, IDX_SIZE> _connections;
std::unique_ptr<MySQLConnectionInfo> _connectionInfo;
+ std::vector<uint8> _preparedStatementSize;
uint8 _async_threads, _synch_threads;
};
diff --git a/src/server/database/Database/MySQLConnection.cpp b/src/server/database/Database/MySQLConnection.cpp
index 229081a3ce0..cf953015d19 100644
--- a/src/server/database/Database/MySQLConnection.cpp
+++ b/src/server/database/Database/MySQLConnection.cpp
@@ -199,105 +199,100 @@ bool MySQLConnection::Execute(PreparedStatementBase* stmt)
return false;
uint32 index = stmt->m_index;
- {
- MySQLPreparedStatement* m_mStmt = GetPreparedStatement(index);
- ASSERT(m_mStmt); // Can only be null if preparation failed, server side error or bad query
- m_mStmt->m_stmt = stmt; // Cross reference them for debug output
- stmt->m_stmt = m_mStmt; /// @todo Cleaner way
-
- stmt->BindParameters();
- MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT();
- MYSQL_BIND* msql_BIND = m_mStmt->GetBind();
+ MySQLPreparedStatement* m_mStmt = GetPreparedStatement(index);
+ ASSERT(m_mStmt); // Can only be null if preparation failed, server side error or bad query
+ m_mStmt->m_stmt = stmt; // Cross reference them for debug output
- uint32 _s = getMSTime();
+ stmt->BindParameters(m_mStmt);
- if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
- {
- uint32 lErrno = mysql_errno(m_Mysql);
- TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString(m_queries[index].first).c_str(), lErrno, mysql_stmt_error(msql_STMT));
+ MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT();
+ MYSQL_BIND* msql_BIND = m_mStmt->GetBind();
- if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
- return Execute(stmt); // Try again
+ uint32 _s = getMSTime();
- m_mStmt->ClearParameters();
- return false;
- }
+ if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
+ {
+ uint32 lErrno = mysql_errno(m_Mysql);
+ TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString().c_str(), lErrno, mysql_stmt_error(msql_STMT));
- if (mysql_stmt_execute(msql_STMT))
- {
- uint32 lErrno = mysql_errno(m_Mysql);
- TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString(m_queries[index].first).c_str(), lErrno, mysql_stmt_error(msql_STMT));
+ if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
+ return Execute(stmt); // Try again
- if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
- return Execute(stmt); // Try again
+ m_mStmt->ClearParameters();
+ return false;
+ }
- m_mStmt->ClearParameters();
- return false;
- }
+ if (mysql_stmt_execute(msql_STMT))
+ {
+ uint32 lErrno = mysql_errno(m_Mysql);
+ TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString().c_str(), lErrno, mysql_stmt_error(msql_STMT));
- TC_LOG_DEBUG("sql.sql", "[%u ms] SQL(p): %s", getMSTimeDiff(_s, getMSTime()), m_mStmt->getQueryString(m_queries[index].first).c_str());
+ if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
+ return Execute(stmt); // Try again
m_mStmt->ClearParameters();
- return true;
+ return false;
}
+
+ TC_LOG_DEBUG("sql.sql", "[%u ms] SQL(p): %s", getMSTimeDiff(_s, getMSTime()), m_mStmt->getQueryString().c_str());
+
+ m_mStmt->ClearParameters();
+ return true;
}
-bool MySQLConnection::_Query(PreparedStatementBase* stmt, MYSQL_RES **pResult, uint64* pRowCount, uint32* pFieldCount)
+bool MySQLConnection::_Query(PreparedStatementBase* stmt, MYSQL_RES** pResult, uint64* pRowCount, uint32* pFieldCount)
{
if (!m_Mysql)
return false;
uint32 index = stmt->m_index;
- {
- MySQLPreparedStatement* m_mStmt = GetPreparedStatement(index);
- ASSERT(m_mStmt); // Can only be null if preparation failed, server side error or bad query
- m_mStmt->m_stmt = stmt; // Cross reference them for debug output
- stmt->m_stmt = m_mStmt; /// @todo Cleaner way
-
- stmt->BindParameters();
- MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT();
- MYSQL_BIND* msql_BIND = m_mStmt->GetBind();
+ MySQLPreparedStatement* m_mStmt = GetPreparedStatement(index);
+ ASSERT(m_mStmt); // Can only be null if preparation failed, server side error or bad query
+ m_mStmt->m_stmt = stmt; // Cross reference them for debug output
- uint32 _s = getMSTime();
+ stmt->BindParameters(m_mStmt);
- if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
- {
- uint32 lErrno = mysql_errno(m_Mysql);
- TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString(m_queries[index].first).c_str(), lErrno, mysql_stmt_error(msql_STMT));
+ MYSQL_STMT* msql_STMT = m_mStmt->GetSTMT();
+ MYSQL_BIND* msql_BIND = m_mStmt->GetBind();
- if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
- return _Query(stmt, pResult, pRowCount, pFieldCount); // Try again
+ uint32 _s = getMSTime();
- m_mStmt->ClearParameters();
- return false;
- }
+ if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
+ {
+ uint32 lErrno = mysql_errno(m_Mysql);
+ TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s", m_mStmt->getQueryString().c_str(), lErrno, mysql_stmt_error(msql_STMT));
- if (mysql_stmt_execute(msql_STMT))
- {
- uint32 lErrno = mysql_errno(m_Mysql);
- TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s",
- m_mStmt->getQueryString(m_queries[index].first).c_str(), lErrno, mysql_stmt_error(msql_STMT));
+ if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
+ return _Query(stmt, pResult, pRowCount, pFieldCount); // Try again
- if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
- return _Query(stmt, pResult, pRowCount, pFieldCount); // Try again
+ m_mStmt->ClearParameters();
+ return false;
+ }
- m_mStmt->ClearParameters();
- return false;
- }
+ if (mysql_stmt_execute(msql_STMT))
+ {
+ uint32 lErrno = mysql_errno(m_Mysql);
+ TC_LOG_ERROR("sql.sql", "SQL(p): %s\n [ERROR]: [%u] %s",
+ m_mStmt->getQueryString().c_str(), lErrno, mysql_stmt_error(msql_STMT));
- TC_LOG_DEBUG("sql.sql", "[%u ms] SQL(p): %s", getMSTimeDiff(_s, getMSTime()), m_mStmt->getQueryString(m_queries[index].first).c_str());
+ if (_HandleMySQLErrno(lErrno)) // If it returns true, an error was handled successfully (i.e. reconnection)
+ return _Query(stmt, pResult, pRowCount, pFieldCount); // Try again
m_mStmt->ClearParameters();
+ return false;
+ }
- *pResult = mysql_stmt_result_metadata(msql_STMT);
- *pRowCount = mysql_stmt_num_rows(msql_STMT);
- *pFieldCount = mysql_stmt_field_count(msql_STMT);
+ TC_LOG_DEBUG("sql.sql", "[%u ms] SQL(p): %s", getMSTimeDiff(_s, getMSTime()), m_mStmt->getQueryString().c_str());
- return true;
+ m_mStmt->ClearParameters();
- }
+ *pResult = mysql_stmt_result_metadata(msql_STMT);
+ *pRowCount = mysql_stmt_num_rows(msql_STMT);
+ *pFieldCount = mysql_stmt_field_count(msql_STMT);
+
+ return true;
}
ResultSet* MySQLConnection::Query(const char* sql)
@@ -454,10 +449,8 @@ MySQLPreparedStatement* MySQLConnection::GetPreparedStatement(uint32 index)
return ret;
}
-void MySQLConnection::PrepareStatement(uint32 index, const char* sql, ConnectionFlags flags)
+void MySQLConnection::PrepareStatement(uint32 index, std::string const& sql, ConnectionFlags flags)
{
- m_queries.insert(PreparedStatementMap::value_type(index, std::make_pair(sql, flags)));
-
// Check if specified query should be prepared on this connection
// i.e. don't prepare async statements on synchronous connections
// to save memory that will not be used.
@@ -470,23 +463,21 @@ void MySQLConnection::PrepareStatement(uint32 index, const char* sql, Connection
MYSQL_STMT* stmt = mysql_stmt_init(m_Mysql);
if (!stmt)
{
- TC_LOG_ERROR("sql.sql", "In mysql_stmt_init() id: %u, sql: \"%s\"", index, sql);
+ TC_LOG_ERROR("sql.sql", "In mysql_stmt_init() id: %u, sql: \"%s\"", index, sql.c_str());
TC_LOG_ERROR("sql.sql", "%s", mysql_error(m_Mysql));
m_prepareError = true;
}
else
{
- if (mysql_stmt_prepare(stmt, sql, static_cast<unsigned long>(strlen(sql))))
+ if (mysql_stmt_prepare(stmt, sql.c_str(), static_cast<unsigned long>(sql.size())))
{
- TC_LOG_ERROR("sql.sql", "In mysql_stmt_prepare() id: %u, sql: \"%s\"", index, sql);
+ TC_LOG_ERROR("sql.sql", "In mysql_stmt_prepare() id: %u, sql: \"%s\"", index, sql.c_str());
TC_LOG_ERROR("sql.sql", "%s", mysql_stmt_error(stmt));
mysql_stmt_close(stmt);
m_prepareError = true;
}
else
- {
- m_stmts[index] = Trinity::make_unique<MySQLPreparedStatement>(stmt);
- }
+ m_stmts[index] = Trinity::make_unique<MySQLPreparedStatement>(stmt, sql);
}
}
diff --git a/src/server/database/Database/MySQLConnection.h b/src/server/database/Database/MySQLConnection.h
index 1e0c65cb27c..ea9ce7eb46f 100644
--- a/src/server/database/Database/MySQLConnection.h
+++ b/src/server/database/Database/MySQLConnection.h
@@ -51,8 +51,6 @@ struct TC_DATABASE_API MySQLConnectionInfo
std::string port_or_socket;
};
-typedef std::map<uint32 /*index*/, std::pair<std::string /*query*/, ConnectionFlags /*sync/async*/> > PreparedStatementMap;
-
class TC_DATABASE_API MySQLConnection
{
template <class T> friend class DatabaseWorkerPool;
@@ -95,13 +93,14 @@ class TC_DATABASE_API MySQLConnection
MYSQL* GetHandle() { return m_Mysql; }
MySQLPreparedStatement* GetPreparedStatement(uint32 index);
- void PrepareStatement(uint32 index, const char* sql, ConnectionFlags flags);
+ void PrepareStatement(uint32 index, std::string const& sql, ConnectionFlags flags);
virtual void DoPrepareStatements() = 0;
protected:
- std::vector<std::unique_ptr<MySQLPreparedStatement>> m_stmts; //! PreparedStatements storage
- PreparedStatementMap m_queries; //! Query storage
+ typedef std::vector<std::unique_ptr<MySQLPreparedStatement>> PreparedStatementContainer;
+
+ PreparedStatementContainer m_stmts; //! PreparedStatements storage
bool m_reconnecting; //! Are we reconnecting?
bool m_prepareError; //! Was there any error while preparing statements?
diff --git a/src/server/database/Database/PreparedStatement.cpp b/src/server/database/Database/PreparedStatement.cpp
index 371543453da..e410df84464 100644
--- a/src/server/database/Database/PreparedStatement.cpp
+++ b/src/server/database/Database/PreparedStatement.cpp
@@ -26,15 +26,15 @@
#include <mysql.h>
#include <sstream>
-PreparedStatementBase::PreparedStatementBase(uint32 index) :
-m_stmt(NULL),
-m_index(index) { }
+PreparedStatementBase::PreparedStatementBase(uint32 index, uint8 capacity) :
+ m_stmt(nullptr), m_index(index), statement_data(capacity) { }
PreparedStatementBase::~PreparedStatementBase() { }
-void PreparedStatementBase::BindParameters()
+void PreparedStatementBase::BindParameters(MySQLPreparedStatement* stmt)
{
- ASSERT(m_stmt);
+ ASSERT(stmt);
+ m_stmt = stmt;
uint8 i = 0;
for (; i < statement_data.size(); i++)
@@ -42,51 +42,51 @@ void PreparedStatementBase::BindParameters()
switch (statement_data[i].type)
{
case TYPE_BOOL:
- m_stmt->setBool(i, statement_data[i].data.boolean);
+ stmt->setBool(i, statement_data[i].data.boolean);
break;
case TYPE_UI8:
- m_stmt->setUInt8(i, statement_data[i].data.ui8);
+ stmt->setUInt8(i, statement_data[i].data.ui8);
break;
case TYPE_UI16:
- m_stmt->setUInt16(i, statement_data[i].data.ui16);
+ stmt->setUInt16(i, statement_data[i].data.ui16);
break;
case TYPE_UI32:
- m_stmt->setUInt32(i, statement_data[i].data.ui32);
+ stmt->setUInt32(i, statement_data[i].data.ui32);
break;
case TYPE_I8:
- m_stmt->setInt8(i, statement_data[i].data.i8);
+ stmt->setInt8(i, statement_data[i].data.i8);
break;
case TYPE_I16:
- m_stmt->setInt16(i, statement_data[i].data.i16);
+ stmt->setInt16(i, statement_data[i].data.i16);
break;
case TYPE_I32:
- m_stmt->setInt32(i, statement_data[i].data.i32);
+ stmt->setInt32(i, statement_data[i].data.i32);
break;
case TYPE_UI64:
- m_stmt->setUInt64(i, statement_data[i].data.ui64);
+ stmt->setUInt64(i, statement_data[i].data.ui64);
break;
case TYPE_I64:
- m_stmt->setInt64(i, statement_data[i].data.i64);
+ stmt->setInt64(i, statement_data[i].data.i64);
break;
case TYPE_FLOAT:
- m_stmt->setFloat(i, statement_data[i].data.f);
+ stmt->setFloat(i, statement_data[i].data.f);
break;
case TYPE_DOUBLE:
- m_stmt->setDouble(i, statement_data[i].data.d);
+ stmt->setDouble(i, statement_data[i].data.d);
break;
case TYPE_STRING:
- m_stmt->setBinary(i, statement_data[i].binary, true);
+ stmt->setBinary(i, statement_data[i].binary, true);
break;
case TYPE_BINARY:
- m_stmt->setBinary(i, statement_data[i].binary, false);
+ stmt->setBinary(i, statement_data[i].binary, false);
break;
case TYPE_NULL:
- m_stmt->setNull(i);
+ stmt->setNull(i);
break;
}
}
#ifdef _DEBUG
- if (i < m_stmt->m_paramCount)
+ if (i < stmt->m_paramCount)
TC_LOG_WARN("sql.sql", "[WARNING]: BindParameters() for statement %u did not bind all allocated parameters", m_index);
#endif
}
@@ -94,8 +94,7 @@ void PreparedStatementBase::BindParameters()
//- Bind to buffer
void PreparedStatementBase::setBool(const uint8 index, const bool value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.boolean = value;
statement_data[index].type = TYPE_BOOL;
@@ -103,8 +102,7 @@ void PreparedStatementBase::setBool(const uint8 index, const bool value)
void PreparedStatementBase::setUInt8(const uint8 index, const uint8 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.ui8 = value;
statement_data[index].type = TYPE_UI8;
@@ -112,8 +110,7 @@ void PreparedStatementBase::setUInt8(const uint8 index, const uint8 value)
void PreparedStatementBase::setUInt16(const uint8 index, const uint16 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.ui16 = value;
statement_data[index].type = TYPE_UI16;
@@ -121,8 +118,7 @@ void PreparedStatementBase::setUInt16(const uint8 index, const uint16 value)
void PreparedStatementBase::setUInt32(const uint8 index, const uint32 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.ui32 = value;
statement_data[index].type = TYPE_UI32;
@@ -130,8 +126,7 @@ void PreparedStatementBase::setUInt32(const uint8 index, const uint32 value)
void PreparedStatementBase::setUInt64(const uint8 index, const uint64 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.ui64 = value;
statement_data[index].type = TYPE_UI64;
@@ -139,8 +134,7 @@ void PreparedStatementBase::setUInt64(const uint8 index, const uint64 value)
void PreparedStatementBase::setInt8(const uint8 index, const int8 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.i8 = value;
statement_data[index].type = TYPE_I8;
@@ -148,8 +142,7 @@ void PreparedStatementBase::setInt8(const uint8 index, const int8 value)
void PreparedStatementBase::setInt16(const uint8 index, const int16 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.i16 = value;
statement_data[index].type = TYPE_I16;
@@ -157,8 +150,7 @@ void PreparedStatementBase::setInt16(const uint8 index, const int16 value)
void PreparedStatementBase::setInt32(const uint8 index, const int32 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.i32 = value;
statement_data[index].type = TYPE_I32;
@@ -166,8 +158,7 @@ void PreparedStatementBase::setInt32(const uint8 index, const int32 value)
void PreparedStatementBase::setInt64(const uint8 index, const int64 value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.i64 = value;
statement_data[index].type = TYPE_I64;
@@ -175,8 +166,7 @@ void PreparedStatementBase::setInt64(const uint8 index, const int64 value)
void PreparedStatementBase::setFloat(const uint8 index, const float value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.f = value;
statement_data[index].type = TYPE_FLOAT;
@@ -184,8 +174,7 @@ void PreparedStatementBase::setFloat(const uint8 index, const float value)
void PreparedStatementBase::setDouble(const uint8 index, const double value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].data.d = value;
statement_data[index].type = TYPE_DOUBLE;
@@ -193,8 +182,7 @@ void PreparedStatementBase::setDouble(const uint8 index, const double value)
void PreparedStatementBase::setString(const uint8 index, const std::string& value)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].binary.resize(value.length() + 1);
memcpy(statement_data[index].binary.data(), value.c_str(), value.length() + 1);
@@ -203,8 +191,7 @@ void PreparedStatementBase::setString(const uint8 index, const std::string& valu
void PreparedStatementBase::setBinary(const uint8 index, const std::vector<uint8>& value)
{
- if (index >= statement_data.size())
- statement_data.resize(index + 1);
+ ASSERT(index < statement_data.size());
statement_data[index].binary = value;
statement_data[index].type = TYPE_BINARY;
@@ -212,16 +199,13 @@ void PreparedStatementBase::setBinary(const uint8 index, const std::vector<uint8
void PreparedStatementBase::setNull(const uint8 index)
{
- if (index >= statement_data.size())
- statement_data.resize(index+1);
+ ASSERT(index < statement_data.size());
statement_data[index].type = TYPE_NULL;
}
-MySQLPreparedStatement::MySQLPreparedStatement(MYSQL_STMT* stmt) :
-m_stmt(NULL),
-m_Mstmt(stmt),
-m_bind(NULL)
+MySQLPreparedStatement::MySQLPreparedStatement(MYSQL_STMT* stmt, std::string queryString) :
+ m_stmt(nullptr), m_Mstmt(stmt), m_bind(nullptr), m_queryString(std::move(queryString))
{
/// Initialize variable parameters
m_paramCount = mysql_stmt_param_count(stmt);
@@ -278,17 +262,17 @@ static void SetParameterValue(MYSQL_BIND* param, enum_field_types type, const vo
}
//- Bind on mysql level
-void MySQLPreparedStatement::CheckValidIndex(uint8 index)
+void MySQLPreparedStatement::AssertValidIndex(uint8 index)
{
ASSERT(index < m_paramCount || ParamenterIndexAssertFail(m_stmt->m_index, index, m_paramCount));
if (m_paramsSet[index])
- TC_LOG_WARN("sql.sql", "[WARNING] Prepared Statement (id: %u) trying to bind value on already bound index (%u).", m_stmt->m_index, index);
+ TC_LOG_ERROR("sql.sql", "[ERROR] Prepared Statement (id: %u) trying to bind value on already bound index (%u).", m_stmt->m_index, index);
}
void MySQLPreparedStatement::setNull(const uint8 index)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
param->buffer_type = MYSQL_TYPE_NULL;
@@ -307,7 +291,7 @@ void MySQLPreparedStatement::setBool(const uint8 index, const bool value)
void MySQLPreparedStatement::setUInt8(const uint8 index, const uint8 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_TINY, &value, sizeof(uint8), true);
@@ -315,7 +299,7 @@ void MySQLPreparedStatement::setUInt8(const uint8 index, const uint8 value)
void MySQLPreparedStatement::setUInt16(const uint8 index, const uint16 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_SHORT, &value, sizeof(uint16), true);
@@ -323,7 +307,7 @@ void MySQLPreparedStatement::setUInt16(const uint8 index, const uint16 value)
void MySQLPreparedStatement::setUInt32(const uint8 index, const uint32 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_LONG, &value, sizeof(uint32), true);
@@ -331,7 +315,7 @@ void MySQLPreparedStatement::setUInt32(const uint8 index, const uint32 value)
void MySQLPreparedStatement::setUInt64(const uint8 index, const uint64 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(uint64), true);
@@ -339,7 +323,7 @@ void MySQLPreparedStatement::setUInt64(const uint8 index, const uint64 value)
void MySQLPreparedStatement::setInt8(const uint8 index, const int8 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_TINY, &value, sizeof(int8), false);
@@ -347,7 +331,7 @@ void MySQLPreparedStatement::setInt8(const uint8 index, const int8 value)
void MySQLPreparedStatement::setInt16(const uint8 index, const int16 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_SHORT, &value, sizeof(int16), false);
@@ -355,7 +339,7 @@ void MySQLPreparedStatement::setInt16(const uint8 index, const int16 value)
void MySQLPreparedStatement::setInt32(const uint8 index, const int32 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_LONG, &value, sizeof(int32), false);
@@ -363,7 +347,7 @@ void MySQLPreparedStatement::setInt32(const uint8 index, const int32 value)
void MySQLPreparedStatement::setInt64(const uint8 index, const int64 value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(int64), false);
@@ -371,7 +355,7 @@ void MySQLPreparedStatement::setInt64(const uint8 index, const int64 value)
void MySQLPreparedStatement::setFloat(const uint8 index, const float value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_FLOAT, &value, sizeof(float), (value > 0.0f));
@@ -379,7 +363,7 @@ void MySQLPreparedStatement::setFloat(const uint8 index, const float value)
void MySQLPreparedStatement::setDouble(const uint8 index, const double value)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
SetParameterValue(param, MYSQL_TYPE_DOUBLE, &value, sizeof(double), (value > 0.0f));
@@ -387,7 +371,7 @@ void MySQLPreparedStatement::setDouble(const uint8 index, const double value)
void MySQLPreparedStatement::setBinary(const uint8 index, const std::vector<uint8>& value, bool isString)
{
- CheckValidIndex(index);
+ AssertValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
uint32 len = uint32(value.size());
@@ -407,9 +391,9 @@ void MySQLPreparedStatement::setBinary(const uint8 index, const std::vector<uint
memcpy(param->buffer, value.data(), len);
}
-std::string MySQLPreparedStatement::getQueryString(std::string const& sqlPattern) const
+std::string MySQLPreparedStatement::getQueryString() const
{
- std::string queryString = sqlPattern;
+ std::string queryString(m_queryString);
size_t pos = 0;
for (uint32 i = 0; i < m_stmt->statement_data.size(); i++)
diff --git a/src/server/database/Database/PreparedStatement.h b/src/server/database/Database/PreparedStatement.h
index df56bebca32..3495c5aee67 100644
--- a/src/server/database/Database/PreparedStatement.h
+++ b/src/server/database/Database/PreparedStatement.h
@@ -80,7 +80,7 @@ class TC_DATABASE_API PreparedStatementBase
friend class MySQLConnection;
public:
- explicit PreparedStatementBase(uint32 index);
+ explicit PreparedStatementBase(uint32 index, uint8 capacity);
virtual ~PreparedStatementBase();
void setBool(const uint8 index, const bool value);
@@ -99,12 +99,14 @@ class TC_DATABASE_API PreparedStatementBase
void setNull(const uint8 index);
protected:
- void BindParameters();
+ void BindParameters(MySQLPreparedStatement* stmt);
protected:
MySQLPreparedStatement* m_stmt;
uint32 m_index;
- std::vector<PreparedStatementData> statement_data; //- Buffer of parameters, not tied to MySQL in any way yet
+
+ //- Buffer of parameters, not tied to MySQL in any way yet
+ std::vector<PreparedStatementData> statement_data;
PreparedStatementBase(PreparedStatementBase const& right) = delete;
PreparedStatementBase& operator=(PreparedStatementBase const& right) = delete;
@@ -114,7 +116,7 @@ template<typename T>
class PreparedStatement : public PreparedStatementBase
{
public:
- explicit PreparedStatement(uint32 index) : PreparedStatementBase(index)
+ explicit PreparedStatement(uint32 index, uint8 capacity) : PreparedStatementBase(index, capacity)
{
}
@@ -132,7 +134,7 @@ class TC_DATABASE_API MySQLPreparedStatement
friend class PreparedStatementBase;
public:
- MySQLPreparedStatement(MYSQL_STMT* stmt);
+ MySQLPreparedStatement(MYSQL_STMT* stmt, std::string queryString);
~MySQLPreparedStatement();
void setNull(const uint8 index);
@@ -149,19 +151,22 @@ class TC_DATABASE_API MySQLPreparedStatement
void setDouble(const uint8 index, const double value);
void setBinary(const uint8 index, const std::vector<uint8>& value, bool isString);
+ uint32 GetParameterCount() const { return m_paramCount; }
+
protected:
MYSQL_STMT* GetSTMT() { return m_Mstmt; }
MYSQL_BIND* GetBind() { return m_bind; }
PreparedStatementBase* m_stmt;
void ClearParameters();
- void CheckValidIndex(uint8 index);
- std::string getQueryString(std::string const& sqlPattern) const;
+ void AssertValidIndex(uint8 index);
+ std::string getQueryString() const;
private:
MYSQL_STMT* m_Mstmt;
uint32 m_paramCount;
std::vector<bool> m_paramsSet;
MYSQL_BIND* m_bind;
+ std::string const m_queryString;
MySQLPreparedStatement(MySQLPreparedStatement const& right) = delete;
MySQLPreparedStatement& operator=(MySQLPreparedStatement const& right) = delete;