TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MySQLPreparedStatement Class Reference

#include <PreparedStatement.h>

Public Member Functions

 MySQLPreparedStatement (MYSQL_STMT *stmt)
 
 ~MySQLPreparedStatement ()
 
void setBool (const uint8 index, const bool value)
 
void setUInt8 (const uint8 index, const uint8 value)
 
void setUInt16 (const uint8 index, const uint16 value)
 
void setUInt32 (const uint8 index, const uint32 value)
 
void setUInt64 (const uint8 index, const uint64 value)
 
void setInt8 (const uint8 index, const int8 value)
 
void setInt16 (const uint8 index, const int16 value)
 
void setInt32 (const uint8 index, const int32 value)
 
void setInt64 (const uint8 index, const int64 value)
 
void setFloat (const uint8 index, const float value)
 
void setDouble (const uint8 index, const double value)
 
void setBinary (const uint8 index, const std::vector< uint8 > &value, bool isString)
 
void setNull (const uint8 index)
 

Protected Member Functions

MYSQL_STMT * GetSTMT ()
 
MYSQL_BIND * GetBind ()
 
void ClearParameters ()
 
bool CheckValidIndex (uint8 index)
 
std::string getQueryString (std::string const &sqlPattern) const
 

Protected Attributes

PreparedStatementm_stmt
 

Private Member Functions

void setValue (MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
 
 MySQLPreparedStatement (MySQLPreparedStatement const &right)=delete
 
MySQLPreparedStatementoperator= (MySQLPreparedStatement const &right)=delete
 

Private Attributes

MYSQL_STMT * m_Mstmt
 
uint32 m_paramCount
 
std::vector< boolm_paramsSet
 
MYSQL_BIND * m_bind
 

Friends

class MySQLConnection
 
class PreparedStatement
 

Constructor & Destructor Documentation

MySQLPreparedStatement::MySQLPreparedStatement ( MYSQL_STMT *  stmt)

Initialize variable parameters

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

214  :
215 m_stmt(NULL),
216 m_Mstmt(stmt),
217 m_bind(NULL)
218 {
220  m_paramCount = mysql_stmt_param_count(stmt);
221  m_paramsSet.assign(m_paramCount, false);
222  m_bind = new MYSQL_BIND[m_paramCount];
223  memset(m_bind, 0, sizeof(MYSQL_BIND)*m_paramCount);
224 
226  my_bool bool_tmp = 1;
227  mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &bool_tmp);
228 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
arena_t NULL
Definition: jemalloc_internal.h:624
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
PreparedStatement * m_stmt
Definition: PreparedStatement.h:140
uint32 m_paramCount
Definition: PreparedStatement.h:150
MYSQL_STMT * m_Mstmt
Definition: PreparedStatement.h:149
MySQLPreparedStatement::~MySQLPreparedStatement ( )
231 {
232  ClearParameters();
233  if (m_Mstmt->bind_result_done)
234  {
235  delete[] m_Mstmt->bind->length;
236  delete[] m_Mstmt->bind->is_null;
237  }
238  mysql_stmt_close(m_Mstmt);
239  delete[] m_bind;
240 }
void ClearParameters()
Definition: PreparedStatement.cpp:242
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
MYSQL_STMT * m_Mstmt
Definition: PreparedStatement.h:149

+ Here is the call graph for this function:

MySQLPreparedStatement::MySQLPreparedStatement ( MySQLPreparedStatement const right)
privatedelete

Member Function Documentation

bool MySQLPreparedStatement::CheckValidIndex ( uint8  index)
protected
262 {
264 
265  if (m_paramsSet[index])
266  TC_LOG_WARN("sql.sql", "[WARNING] Prepared Statement (id: %u) trying to bind value on already bound index (%u).", m_stmt->m_index, index);
267  return true;
268 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
PreparedStatement * m_stmt
Definition: PreparedStatement.h:140
uint32 m_paramCount
Definition: PreparedStatement.h:150
uint32 m_index
Definition: PreparedStatement.h:104
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
#define ASSERT
Definition: Errors.h:55
static bool ParamenterIndexAssertFail(uint32 stmtIndex, uint8 index, uint32 paramCount)
Definition: PreparedStatement.cpp:254

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::ClearParameters ( )
protected
243 {
244  for (uint32 i=0; i < m_paramCount; ++i)
245  {
246  delete m_bind[i].length;
247  m_bind[i].length = NULL;
248  delete[] (char*) m_bind[i].buffer;
249  m_bind[i].buffer = NULL;
250  m_paramsSet[i] = false;
251  }
252 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
arena_t NULL
Definition: jemalloc_internal.h:624
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
uint32_t uint32
Definition: Define.h:150
uint32 m_paramCount
Definition: PreparedStatement.h:150

+ Here is the caller graph for this function:

MYSQL_BIND* MySQLPreparedStatement::GetBind ( )
inlineprotected
139 { return m_bind; }
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
std::string MySQLPreparedStatement::getQueryString ( std::string const sqlPattern) const
protected
405 {
406  std::string queryString = sqlPattern;
407 
408  size_t pos = 0;
409  for (uint32 i = 0; i < m_stmt->statement_data.size(); i++)
410  {
411  pos = queryString.find('?', pos);
412  std::stringstream ss;
413 
414  switch (m_stmt->statement_data[i].type)
415  {
416  case TYPE_BOOL:
417  ss << uint16(m_stmt->statement_data[i].data.boolean);
418  break;
419  case TYPE_UI8:
420  ss << uint16(m_stmt->statement_data[i].data.ui8); // stringstream will append a character with that code instead of numeric representation
421  break;
422  case TYPE_UI16:
423  ss << m_stmt->statement_data[i].data.ui16;
424  break;
425  case TYPE_UI32:
426  ss << m_stmt->statement_data[i].data.ui32;
427  break;
428  case TYPE_I8:
429  ss << int16(m_stmt->statement_data[i].data.i8); // stringstream will append a character with that code instead of numeric representation
430  break;
431  case TYPE_I16:
432  ss << m_stmt->statement_data[i].data.i16;
433  break;
434  case TYPE_I32:
435  ss << m_stmt->statement_data[i].data.i32;
436  break;
437  case TYPE_UI64:
438  ss << m_stmt->statement_data[i].data.ui64;
439  break;
440  case TYPE_I64:
441  ss << m_stmt->statement_data[i].data.i64;
442  break;
443  case TYPE_FLOAT:
444  ss << m_stmt->statement_data[i].data.f;
445  break;
446  case TYPE_DOUBLE:
447  ss << m_stmt->statement_data[i].data.d;
448  break;
449  case TYPE_STRING:
450  ss << '\'' << (char const*)m_stmt->statement_data[i].binary.data() << '\'';
451  break;
452  case TYPE_BINARY:
453  ss << "BINARY";
454  break;
455  case TYPE_NULL:
456  ss << "NULL";
457  break;
458  }
459 
460  std::string replaceStr = ss.str();
461  queryString.replace(pos, 1, replaceStr);
462  pos += replaceStr.length();
463  }
464 
465  return queryString;
466 }
Definition: PreparedStatement.h:55
Definition: PreparedStatement.h:51
int16_t int16
Definition: g3dmath.h:165
Definition: PreparedStatement.h:54
Definition: PreparedStatement.h:47
PreparedStatement * m_stmt
Definition: PreparedStatement.h:140
Definition: PreparedStatement.h:56
Definition: PreparedStatement.h:58
uint16_t uint16
Definition: g3dmath.h:166
Definition: PreparedStatement.h:60
Definition: PreparedStatement.h:52
std::vector< PreparedStatementData > statement_data
Definition: PreparedStatement.h:105
uint32_t uint32
Definition: Define.h:150
Definition: PreparedStatement.h:49
Definition: PreparedStatement.h:53
Definition: PreparedStatement.h:48
Definition: PreparedStatement.h:50
Definition: PreparedStatement.h:57
Definition: PreparedStatement.h:59
MYSQL_STMT* MySQLPreparedStatement::GetSTMT ( )
inlineprotected
138 { return m_Mstmt; }
MYSQL_STMT * m_Mstmt
Definition: PreparedStatement.h:149
MySQLPreparedStatement& MySQLPreparedStatement::operator= ( MySQLPreparedStatement const right)
privatedelete
void MySQLPreparedStatement::setBinary ( const uint8  index,
const std::vector< uint8 > &  value,
bool  isString 
)
356 {
357  CheckValidIndex(index);
358  m_paramsSet[index] = true;
359  MYSQL_BIND* param = &m_bind[index];
360  uint32 len = uint32(value.size());
361  param->buffer_type = MYSQL_TYPE_BLOB;
362  delete [] static_cast<char *>(param->buffer);
363  param->buffer = new char[len];
364  param->buffer_length = len;
365  param->is_null_value = 0;
366  delete param->length;
367  param->length = new unsigned long(len);
368  if (isString)
369  {
370  *param->length -= 1;
371  param->buffer_type = MYSQL_TYPE_VAR_STRING;
372  }
373 
374  memcpy(param->buffer, value.data(), len);
375 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
uint32_t uint32
Definition: Define.h:150
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
uint32_t uint32
Definition: g3dmath.h:168

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setBool ( const uint8  index,
const bool  value 
)
271 {
272  setUInt8(index, value ? 1 : 0);
273 }
void setUInt8(const uint8 index, const uint8 value)
Definition: PreparedStatement.cpp:275
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setDouble ( const uint8  index,
const double  value 
)
348 {
349  CheckValidIndex(index);
350  m_paramsSet[index] = true;
351  MYSQL_BIND* param = &m_bind[index];
352  setValue(param, MYSQL_TYPE_DOUBLE, &value, sizeof(double), (value > 0.0f));
353 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setFloat ( const uint8  index,
const float  value 
)
340 {
341  CheckValidIndex(index);
342  m_paramsSet[index] = true;
343  MYSQL_BIND* param = &m_bind[index];
344  setValue(param, MYSQL_TYPE_FLOAT, &value, sizeof(float), (value > 0.0f));
345 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setInt16 ( const uint8  index,
const int16  value 
)
316 {
317  CheckValidIndex(index);
318  m_paramsSet[index] = true;
319  MYSQL_BIND* param = &m_bind[index];
320  setValue(param, MYSQL_TYPE_SHORT, &value, sizeof(int16), false);
321 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522
int16_t int16
Definition: Define.h:147

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setInt32 ( const uint8  index,
const int32  value 
)
324 {
325  CheckValidIndex(index);
326  m_paramsSet[index] = true;
327  MYSQL_BIND* param = &m_bind[index];
328  setValue(param, MYSQL_TYPE_LONG, &value, sizeof(int32), false);
329 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
int32_t int32
Definition: Define.h:146
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setInt64 ( const uint8  index,
const int64  value 
)
332 {
333  CheckValidIndex(index);
334  m_paramsSet[index] = true;
335  MYSQL_BIND* param = &m_bind[index];
336  setValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(int64), false);
337 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
int64_t int64
Definition: Define.h:145
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setInt8 ( const uint8  index,
const int8  value 
)
308 {
309  CheckValidIndex(index);
310  m_paramsSet[index] = true;
311  MYSQL_BIND* param = &m_bind[index];
312  setValue(param, MYSQL_TYPE_TINY, &value, sizeof(int8), false);
313 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
int8_t int8
Definition: Define.h:148
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setNull ( const uint8  index)
378 {
379  CheckValidIndex(index);
380  m_paramsSet[index] = true;
381  MYSQL_BIND* param = &m_bind[index];
382  param->buffer_type = MYSQL_TYPE_NULL;
383  delete [] static_cast<char *>(param->buffer);
384  param->buffer = NULL;
385  param->buffer_length = 0;
386  param->is_null_value = 1;
387  delete param->length;
388  param->length = NULL;
389 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
arena_t NULL
Definition: jemalloc_internal.h:624
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setUInt16 ( const uint8  index,
const uint16  value 
)
284 {
285  CheckValidIndex(index);
286  m_paramsSet[index] = true;
287  MYSQL_BIND* param = &m_bind[index];
288  setValue(param, MYSQL_TYPE_SHORT, &value, sizeof(uint16), true);
289 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
uint16_t uint16
Definition: Define.h:151
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setUInt32 ( const uint8  index,
const uint32  value 
)
292 {
293  CheckValidIndex(index);
294  m_paramsSet[index] = true;
295  MYSQL_BIND* param = &m_bind[index];
296  setValue(param, MYSQL_TYPE_LONG, &value, sizeof(uint32), true);
297 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
uint32_t uint32
Definition: Define.h:150
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setUInt64 ( const uint8  index,
const uint64  value 
)
300 {
301  CheckValidIndex(index);
302  m_paramsSet[index] = true;
303  MYSQL_BIND* param = &m_bind[index];
304  setValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(uint64), true);
305 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
uint64_t uint64
Definition: Define.h:149
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setUInt8 ( const uint8  index,
const uint8  value 
)
276 {
277  CheckValidIndex(index);
278  m_paramsSet[index] = true;
279  MYSQL_BIND* param = &m_bind[index];
280  setValue(param, MYSQL_TYPE_TINY, &value, sizeof(uint8), true);
281 }
std::vector< bool > m_paramsSet
Definition: PreparedStatement.h:151
MYSQL_BIND * m_bind
Definition: PreparedStatement.h:152
void setValue(MYSQL_BIND *param, enum_field_types type, const void *value, uint32 len, bool isUnsigned)
Definition: PreparedStatement.cpp:391
uint8_t uint8
Definition: Define.h:152
bool CheckValidIndex(uint8 index)
Definition: PreparedStatement.cpp:261
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MySQLPreparedStatement::setValue ( MYSQL_BIND *  param,
enum_field_types  type,
const void *  value,
uint32  len,
bool  isUnsigned 
)
private
392 {
393  param->buffer_type = type;
394  delete [] static_cast<char *>(param->buffer);
395  param->buffer = new char[len];
396  param->buffer_length = 0;
397  param->is_null_value = 0;
398  param->length = NULL; // Only != NULL for strings
399  param->is_unsigned = isUnsigned;
400 
401  memcpy(param->buffer, value, len);
402 }
arena_t NULL
Definition: jemalloc_internal.h:624
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class MySQLConnection
friend
friend class PreparedStatement
friend

Member Data Documentation

MYSQL_BIND* MySQLPreparedStatement::m_bind
private
MYSQL_STMT* MySQLPreparedStatement::m_Mstmt
private
uint32 MySQLPreparedStatement::m_paramCount
private
std::vector<bool> MySQLPreparedStatement::m_paramsSet
private
PreparedStatement* MySQLPreparedStatement::m_stmt
protected

The documentation for this class was generated from the following files: