From 7e538980a2b4fc6c74bde7bd456633d954272708 Mon Sep 17 00:00:00 2001 From: Shauren Date: Fri, 12 May 2017 18:49:51 +0200 Subject: Core/Database: Include cleanup --- src/server/database/Database/PreparedStatement.cpp | 83 +++++++++++----------- 1 file changed, 43 insertions(+), 40 deletions(-) (limited to 'src/server/database/Database/PreparedStatement.cpp') diff --git a/src/server/database/Database/PreparedStatement.cpp b/src/server/database/Database/PreparedStatement.cpp index 6e00da4e33f..a756ddf6757 100644 --- a/src/server/database/Database/PreparedStatement.cpp +++ b/src/server/database/Database/PreparedStatement.cpp @@ -16,8 +16,12 @@ */ #include "PreparedStatement.h" +#include "Errors.h" #include "MySQLConnection.h" +#include "QueryResult.h" #include "Log.h" +#include +#include PreparedStatement::PreparedStatement(uint32 index) : m_stmt(NULL), @@ -27,7 +31,7 @@ PreparedStatement::~PreparedStatement() { } void PreparedStatement::BindParameters() { - ASSERT (m_stmt); + ASSERT(m_stmt); uint8 i = 0; for (; i < statement_data.size(); i++) @@ -257,14 +261,40 @@ static bool ParamenterIndexAssertFail(uint32 stmtIndex, uint8 index, uint32 para return false; } +static void SetParameterValue(MYSQL_BIND* param, enum_field_types type, const void* value, uint32 len, bool isUnsigned) +{ + param->buffer_type = type; + delete[] static_cast(param->buffer); + param->buffer = new char[len]; + param->buffer_length = 0; + param->is_null_value = 0; + param->length = NULL; // Only != NULL for strings + param->is_unsigned = isUnsigned; + + memcpy(param->buffer, value, len); +} + //- Bind on mysql level -bool MySQLPreparedStatement::CheckValidIndex(uint8 index) +void MySQLPreparedStatement::CheckValidIndex(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); - return true; +} + +void MySQLPreparedStatement::setNull(const uint8 index) +{ + CheckValidIndex(index); + m_paramsSet[index] = true; + MYSQL_BIND* param = &m_bind[index]; + param->buffer_type = MYSQL_TYPE_NULL; + delete[] static_cast(param->buffer); + param->buffer = NULL; + param->buffer_length = 0; + param->is_null_value = 1; + delete param->length; + param->length = NULL; } void MySQLPreparedStatement::setBool(const uint8 index, const bool value) @@ -277,7 +307,7 @@ void MySQLPreparedStatement::setUInt8(const uint8 index, const uint8 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_TINY, &value, sizeof(uint8), true); + SetParameterValue(param, MYSQL_TYPE_TINY, &value, sizeof(uint8), true); } void MySQLPreparedStatement::setUInt16(const uint8 index, const uint16 value) @@ -285,7 +315,7 @@ void MySQLPreparedStatement::setUInt16(const uint8 index, const uint16 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_SHORT, &value, sizeof(uint16), true); + SetParameterValue(param, MYSQL_TYPE_SHORT, &value, sizeof(uint16), true); } void MySQLPreparedStatement::setUInt32(const uint8 index, const uint32 value) @@ -293,7 +323,7 @@ void MySQLPreparedStatement::setUInt32(const uint8 index, const uint32 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_LONG, &value, sizeof(uint32), true); + SetParameterValue(param, MYSQL_TYPE_LONG, &value, sizeof(uint32), true); } void MySQLPreparedStatement::setUInt64(const uint8 index, const uint64 value) @@ -301,7 +331,7 @@ void MySQLPreparedStatement::setUInt64(const uint8 index, const uint64 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(uint64), true); + SetParameterValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(uint64), true); } void MySQLPreparedStatement::setInt8(const uint8 index, const int8 value) @@ -309,7 +339,7 @@ void MySQLPreparedStatement::setInt8(const uint8 index, const int8 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_TINY, &value, sizeof(int8), false); + SetParameterValue(param, MYSQL_TYPE_TINY, &value, sizeof(int8), false); } void MySQLPreparedStatement::setInt16(const uint8 index, const int16 value) @@ -317,7 +347,7 @@ void MySQLPreparedStatement::setInt16(const uint8 index, const int16 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_SHORT, &value, sizeof(int16), false); + SetParameterValue(param, MYSQL_TYPE_SHORT, &value, sizeof(int16), false); } void MySQLPreparedStatement::setInt32(const uint8 index, const int32 value) @@ -325,7 +355,7 @@ void MySQLPreparedStatement::setInt32(const uint8 index, const int32 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_LONG, &value, sizeof(int32), false); + SetParameterValue(param, MYSQL_TYPE_LONG, &value, sizeof(int32), false); } void MySQLPreparedStatement::setInt64(const uint8 index, const int64 value) @@ -333,7 +363,7 @@ void MySQLPreparedStatement::setInt64(const uint8 index, const int64 value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(int64), false); + SetParameterValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(int64), false); } void MySQLPreparedStatement::setFloat(const uint8 index, const float value) @@ -341,7 +371,7 @@ void MySQLPreparedStatement::setFloat(const uint8 index, const float value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_FLOAT, &value, sizeof(float), (value > 0.0f)); + SetParameterValue(param, MYSQL_TYPE_FLOAT, &value, sizeof(float), (value > 0.0f)); } void MySQLPreparedStatement::setDouble(const uint8 index, const double value) @@ -349,7 +379,7 @@ void MySQLPreparedStatement::setDouble(const uint8 index, const double value) CheckValidIndex(index); m_paramsSet[index] = true; MYSQL_BIND* param = &m_bind[index]; - setValue(param, MYSQL_TYPE_DOUBLE, &value, sizeof(double), (value > 0.0f)); + SetParameterValue(param, MYSQL_TYPE_DOUBLE, &value, sizeof(double), (value > 0.0f)); } void MySQLPreparedStatement::setBinary(const uint8 index, const std::vector& value, bool isString) @@ -374,33 +404,6 @@ void MySQLPreparedStatement::setBinary(const uint8 index, const std::vectorbuffer, value.data(), len); } -void MySQLPreparedStatement::setNull(const uint8 index) -{ - CheckValidIndex(index); - m_paramsSet[index] = true; - MYSQL_BIND* param = &m_bind[index]; - param->buffer_type = MYSQL_TYPE_NULL; - delete [] static_cast(param->buffer); - param->buffer = NULL; - param->buffer_length = 0; - param->is_null_value = 1; - delete param->length; - param->length = NULL; -} - -void MySQLPreparedStatement::setValue(MYSQL_BIND* param, enum_field_types type, const void* value, uint32 len, bool isUnsigned) -{ - param->buffer_type = type; - delete [] static_cast(param->buffer); - param->buffer = new char[len]; - param->buffer_length = 0; - param->is_null_value = 0; - param->length = NULL; // Only != NULL for strings - param->is_unsigned = isUnsigned; - - memcpy(param->buffer, value, len); -} - std::string MySQLPreparedStatement::getQueryString(std::string const& sqlPattern) const { std::string queryString = sqlPattern; -- cgit v1.2.3