AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
MySQLPreparedStatement Class Reference

#include "MySQLPreparedStatement.h"

Public Member Functions

 MySQLPreparedStatement (MySQLStmt *stmt, std::string_view queryString)
 
 ~MySQLPreparedStatement ()
 
void BindParameters (PreparedStatementBase *stmt)
 
uint32 GetParameterCount () const
 

Protected Member Functions

void SetParameter (const uint8 index, bool value)
 
void SetParameter (const uint8 index, std::nullptr_t)
 
void SetParameter (const uint8 index, std::string const &value)
 
void SetParameter (const uint8 index, std::vector< uint8 > const &value)
 
template<typename T >
void SetParameter (const uint8 index, T value)
 
MySQLStmtGetSTMT ()
 
MySQLBindGetBind ()
 
void ClearParameters ()
 
void AssertValidIndex (const uint8 index)
 
std::string getQueryString () const
 

Protected Attributes

PreparedStatementBasem_stmt
 

Private Member Functions

 MySQLPreparedStatement (MySQLPreparedStatement const &right)=delete
 
MySQLPreparedStatementoperator= (MySQLPreparedStatement const &right)=delete
 

Private Attributes

MySQLStmtm_Mstmt
 
uint32 m_paramCount
 
std::vector< bool > m_paramsSet
 
MySQLBindm_bind
 
std::string m_queryString {}
 

Friends

class MySQLConnection
 
class PreparedStatementBase
 

Detailed Description

Constructor & Destructor Documentation

◆ MySQLPreparedStatement() [1/2]

MySQLPreparedStatement::MySQLPreparedStatement ( MySQLStmt stmt,
std::string_view  queryString 
)

Initialize variable parameters

"If set to 1, causes mysql_stmt_store_result() to update the metadata MYSQL_FIELD->max_length value."

38 :
39 m_stmt(nullptr),
40 m_Mstmt(stmt),
41 m_bind(nullptr),
42 m_queryString(std::string(queryString))
43{
45 m_paramCount = mysql_stmt_param_count(stmt);
46 m_paramsSet.assign(m_paramCount, false);
48 memset(m_bind, 0, sizeof(MySQLBind) * m_paramCount);
49
51 MySQLBool bool_tmp = MySQLBool(1);
52 mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &bool_tmp);
53}
std::remove_pointer_t< decltype(std::declval< MYSQL_BIND >().is_null)> MySQLBool
Definition: MySQLHacks.h:32
Definition: MySQLHacks.h:27
MySQLBind * m_bind
Definition: MySQLPreparedStatement.h:66
PreparedStatementBase * m_stmt
Definition: MySQLPreparedStatement.h:57
MySQLStmt * m_Mstmt
Definition: MySQLPreparedStatement.h:63
uint32 m_paramCount
Definition: MySQLPreparedStatement.h:64
std::string m_queryString
Definition: MySQLPreparedStatement.h:67
std::vector< bool > m_paramsSet
Definition: MySQLPreparedStatement.h:65

References m_bind, m_paramCount, and m_paramsSet.

◆ ~MySQLPreparedStatement()

MySQLPreparedStatement::~MySQLPreparedStatement ( )
56{
58 if (m_Mstmt->bind_result_done)
59 {
60 delete[] m_Mstmt->bind->length;
61 delete[] m_Mstmt->bind->is_null;
62 }
63
64 mysql_stmt_close(m_Mstmt);
65 delete[] m_bind;
66}
void ClearParameters()
Definition: MySQLPreparedStatement.cpp:89

References ClearParameters(), m_bind, and m_Mstmt.

◆ MySQLPreparedStatement() [2/2]

MySQLPreparedStatement::MySQLPreparedStatement ( MySQLPreparedStatement const &  right)
privatedelete

Member Function Documentation

◆ AssertValidIndex()

void MySQLPreparedStatement::AssertValidIndex ( const uint8  index)
protected
111{
113
114 if (m_paramsSet[index])
115 LOG_ERROR("sql.sql", "[ERROR] Prepared Statement (id: {}) trying to bind value on already bound index ({}).", m_stmt->GetIndex(), index);
116}
#define ASSERT
Definition: Errors.h:68
#define LOG_ERROR(filterType__,...)
Definition: Log.h:159
static bool ParamenterIndexAssertFail(uint32 stmtIndex, uint8 index, uint32 paramCount)
Definition: MySQLPreparedStatement.cpp:101
uint32 GetIndex() const
Definition: PreparedStatement.h:124

References ASSERT, PreparedStatementBase::GetIndex(), LOG_ERROR, m_paramCount, m_paramsSet, m_stmt, and ParamenterIndexAssertFail().

Referenced by SetParameter().

◆ BindParameters()

void MySQLPreparedStatement::BindParameters ( PreparedStatementBase stmt)
69{
70 m_stmt = stmt; // Cross reference them for debug output
71
72 uint8 pos = 0;
73 for (PreparedStatementData const& data : stmt->GetParameters())
74 {
75 std::visit([&](auto&& param)
76 {
77 SetParameter(pos, param);
78 }, data.data);
79
80 ++pos;
81 }
82
83#ifdef _DEBUG
84 if (pos < m_paramCount)
85 LOG_WARN("sql.sql", "[WARNING]: BindParameters() for statement {} did not bind all allocated parameters", stmt->GetIndex());
86#endif
87}
std::uint8_t uint8
Definition: Define.h:110
#define LOG_WARN(filterType__,...)
Definition: Log.h:163
void SetParameter(const uint8 index, bool value)
Definition: MySQLPreparedStatement.cpp:136
Definition: PreparedStatement.h:43
std::vector< PreparedStatementData > const & GetParameters() const
Definition: PreparedStatement.h:125

References PreparedStatementBase::GetIndex(), PreparedStatementBase::GetParameters(), LOG_WARN, m_paramCount, m_stmt, and SetParameter().

Referenced by MySQLConnection::_Query(), and MySQLConnection::Execute().

◆ ClearParameters()

void MySQLPreparedStatement::ClearParameters ( )
protected
90{
91 for (uint32 i=0; i < m_paramCount; ++i)
92 {
93 delete m_bind[i].length;
94 m_bind[i].length = nullptr;
95 delete[] (char*)m_bind[i].buffer;
96 m_bind[i].buffer = nullptr;
97 m_paramsSet[i] = false;
98 }
99}
std::uint32_t uint32
Definition: Define.h:108

References m_bind, m_paramCount, and m_paramsSet.

Referenced by MySQLConnection::_Query(), MySQLConnection::Execute(), and ~MySQLPreparedStatement().

◆ GetBind()

MySQLBind * MySQLPreparedStatement::GetBind ( )
inlineprotected
56{ return m_bind; }

Referenced by MySQLConnection::_Query(), and MySQLConnection::Execute().

◆ GetParameterCount()

uint32 MySQLPreparedStatement::GetParameterCount ( ) const
inline
44{ return m_paramCount; }

◆ getQueryString()

std::string MySQLPreparedStatement::getQueryString ( ) const
protected
190{
191 std::string queryString(m_queryString);
192
193 size_t pos = 0;
194
195 for (PreparedStatementData const& data : m_stmt->GetParameters())
196 {
197 pos = queryString.find('?', pos);
198
199 std::string replaceStr = std::visit([&](auto&& data)
200 {
202 }, data.data);
203
204 queryString.replace(pos, 1, replaceStr);
205 pos += replaceStr.length();
206 }
207
208 return queryString;
209}
static std::string ToString(T value)
Definition: PreparedStatement.cpp:105

References PreparedStatementBase::GetParameters(), m_queryString, m_stmt, and PreparedStatementData::ToString().

Referenced by MySQLConnection::_Query(), and MySQLConnection::Execute().

◆ GetSTMT()

MySQLStmt * MySQLPreparedStatement::GetSTMT ( )
inlineprotected

◆ operator=()

MySQLPreparedStatement & MySQLPreparedStatement::operator= ( MySQLPreparedStatement const &  right)
privatedelete

◆ SetParameter() [1/5]

void MySQLPreparedStatement::SetParameter ( const uint8  index,
bool  value 
)
protected
137{
138 SetParameter(index, uint8(value ? 1 : 0));
139}

References SetParameter().

Referenced by BindParameters(), and SetParameter().

◆ SetParameter() [2/5]

void MySQLPreparedStatement::SetParameter ( const uint8  index,
std::nullptr_t   
)
protected
142{
143 AssertValidIndex(index);
144 m_paramsSet[index] = true;
145 MYSQL_BIND* param = &m_bind[index];
146 param->buffer_type = MYSQL_TYPE_NULL;
147 delete[] static_cast<char*>(param->buffer);
148 param->buffer = nullptr;
149 param->buffer_length = 0;
150 param->is_null_value = 1;
151 delete param->length;
152 param->length = nullptr;
153}
void AssertValidIndex(const uint8 index)
Definition: MySQLPreparedStatement.cpp:110

References AssertValidIndex(), m_bind, and m_paramsSet.

◆ SetParameter() [3/5]

void MySQLPreparedStatement::SetParameter ( const uint8  index,
std::string const &  value 
)
protected
156{
157 AssertValidIndex(index);
158 m_paramsSet[index] = true;
159 MYSQL_BIND* param = &m_bind[index];
160 uint32 len = uint32(value.size());
161 param->buffer_type = MYSQL_TYPE_VAR_STRING;
162 delete[] static_cast<char*>(param->buffer);
163 param->buffer = new char[len];
164 param->buffer_length = len;
165 param->is_null_value = 0;
166 delete param->length;
167 param->length = new unsigned long(len);
168
169 memcpy(param->buffer, value.c_str(), len);
170}

References AssertValidIndex(), m_bind, and m_paramsSet.

◆ SetParameter() [4/5]

void MySQLPreparedStatement::SetParameter ( const uint8  index,
std::vector< uint8 > const &  value 
)
protected
173{
174 AssertValidIndex(index);
175 m_paramsSet[index] = true;
176 MYSQL_BIND* param = &m_bind[index];
177 uint32 len = uint32(value.size());
178 param->buffer_type = MYSQL_TYPE_BLOB;
179 delete[] static_cast<char*>(param->buffer);
180 param->buffer = new char[len];
181 param->buffer_length = len;
182 param->is_null_value = 0;
183 delete param->length;
184 param->length = new unsigned long(len);
185
186 memcpy(param->buffer, value.data(), len);
187}

References AssertValidIndex(), m_bind, and m_paramsSet.

◆ SetParameter() [5/5]

template<typename T >
void MySQLPreparedStatement::SetParameter ( const uint8  index,
value 
)
protected
120{
121 AssertValidIndex(index);
122 m_paramsSet[index] = true;
123 MYSQL_BIND* param = &m_bind[index];
124 uint32 len = uint32(sizeof(T));
125 param->buffer_type = MySQLType<T>::value;
126 delete[] static_cast<char*>(param->buffer);
127 param->buffer = new char[len];
128 param->buffer_length = 0;
129 param->is_null_value = 0;
130 param->length = nullptr; // Only != NULL for strings
131 param->is_unsigned = std::is_unsigned_v<T>;
132
133 memcpy(param->buffer, &value, len);
134}
Definition: MySQLPreparedStatement.cpp:25

References AssertValidIndex(), m_bind, and m_paramsSet.

Friends And Related Function Documentation

◆ MySQLConnection

friend class MySQLConnection
friend

◆ PreparedStatementBase

friend class PreparedStatementBase
friend

Member Data Documentation

◆ m_bind

MySQLBind* MySQLPreparedStatement::m_bind
private

◆ m_Mstmt

MySQLStmt* MySQLPreparedStatement::m_Mstmt
private

Referenced by ~MySQLPreparedStatement().

◆ m_paramCount

uint32 MySQLPreparedStatement::m_paramCount
private

◆ m_paramsSet

std::vector<bool> MySQLPreparedStatement::m_paramsSet
private

◆ m_queryString

std::string MySQLPreparedStatement::m_queryString {}
private

Referenced by getQueryString().

◆ m_stmt

PreparedStatementBase* MySQLPreparedStatement::m_stmt
protected