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

Class used to access individual fields of database query result. More...

#include <Field.h>

Classes

struct  Metadata
 

Public Member Functions

 Field ()
 
 ~Field ()
 
bool GetBool () const
 
uint8 GetUInt8 () const
 
int8 GetInt8 () const
 
uint16 GetUInt16 () const
 
int16 GetInt16 () const
 
uint32 GetUInt32 () const
 
int32 GetInt32 () const
 
uint64 GetUInt64 () const
 
int64 GetInt64 () const
 
float GetFloat () const
 
double GetDouble () const
 
char constGetCString () const
 
std::string GetString () const
 
std::vector< uint8GetBinary () const
 
bool IsNull () const
 

Protected Member Functions

void SetByteValue (void *newValue, enum_field_types newType, uint32 length)
 
void SetStructuredValue (char *newValue, enum_field_types newType, uint32 length)
 
void CleanUp ()
 
bool IsType (enum_field_types type) const
 
bool IsNumeric () const
 

Static Protected Member Functions

static uint32 SizeForType (MYSQL_FIELD *field)
 

Protected Attributes

struct {
   uint32   length
 
   void *   value
 
   enum_field_types   type
 
   bool   raw
 
data
 

Friends

class ResultSet
 
class PreparedResultSet
 

Detailed Description

Class used to access individual fields of database query result.

Guideline on field type matching:

MySQL type method to use
TINYINT GetBool, GetInt8, GetUInt8
SMALLINT GetInt16, GetUInt16
MEDIUMINT, INT GetInt32, GetUInt32
BIGINT GetInt64, GetUInt64
FLOAT GetFloat
DOUBLE, DECIMAL GetDouble
CHAR, VARCHAR, GetCString, GetString
TINYTEXT, MEDIUMTEXT, GetCString, GetString
TEXT, LONGTEXT GetCString, GetString
TINYBLOB, MEDIUMBLOB, GetBinary, GetString
BLOB, LONGBLOB GetBinary, GetString
BINARY, VARBINARY GetBinary

Return types of aggregate functions:

Function Type
MIN, MAX Same as the field
SUM, AVG DECIMAL
COUNT BIGINT

Constructor & Destructor Documentation

Field::Field ( )
21 {
22  data.value = NULL;
23  data.type = MYSQL_TYPE_NULL;
24  data.length = 0;
25  data.raw = false;
26 }
arena_t NULL
Definition: jemalloc_internal.h:624
struct Field::@57 data
Field::~Field ( )
29 {
30  CleanUp();
31 }
void CleanUp()
Definition: Field.h:328

+ Here is the call graph for this function:

Member Function Documentation

void Field::CleanUp ( )
inlineprotected
329  {
330  // Field does not own the data if fetched with prepared statement
331  if (!data.raw)
332  delete[] ((char*)data.value);
333  data.value = NULL;
334  }
arena_t NULL
Definition: jemalloc_internal.h:624
struct Field::@57 data

+ Here is the caller graph for this function:

std::vector<uint8> Field::GetBinary ( ) const
inline
289  {
290  std::vector<uint8> result;
291  if (!data.value || !data.length)
292  return result;
293 
294  result.resize(data.length);
295  memcpy(result.data(), data.value, data.length);
296  return result;
297  }
struct Field::@57 data
bool Field::GetBool ( ) const
inline
66  {
67  return GetUInt8() == 1 ? true : false;
68  }
uint8 GetUInt8() const
Definition: Field.h:70

+ Here is the caller graph for this function:

char const* Field::GetCString ( ) const
inline
261  {
262  if (!data.value)
263  return NULL;
264 
265  #ifdef TRINITY_DEBUG
266  if (IsNumeric())
267  {
268  TC_LOG_WARN("sql.sql", "Error: GetCString() on numeric field %s.%s (%s.%s) at index %u. Using type: %s.",
269  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
270  return NULL;
271  }
272  #endif
273  return static_cast<char const*>(data.value);
274  }
arena_t NULL
Definition: jemalloc_internal.h:624
struct Field::@57 data
bool IsNumeric() const
Definition: Field.h:391
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
#define const
Definition: zconf.h:217

+ Here is the caller graph for this function:

double Field::GetDouble ( ) const
inline
242  {
243  if (!data.value)
244  return 0.0f;
245 
246  #ifdef TRINITY_DEBUG
247  if (!IsType(MYSQL_TYPE_DOUBLE) && !IsType(MYSQL_TYPE_NEWDECIMAL))
248  {
249  TC_LOG_WARN("sql.sql", "Warning: GetDouble() on non-double/non-decimal field %s.%s (%s.%s) at index %u. Using type: %s.",
250  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
251  return 0.0f;
252  }
253  #endif
254 
255  if (data.raw && !IsType(MYSQL_TYPE_NEWDECIMAL))
256  return *reinterpret_cast<double*>(data.value);
257  return static_cast<double>(atof((char*)data.value));
258  }
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
float Field::GetFloat ( ) const
inline
223  {
224  if (!data.value)
225  return 0.0f;
226 
227  #ifdef TRINITY_DEBUG
228  if (!IsType(MYSQL_TYPE_FLOAT))
229  {
230  TC_LOG_WARN("sql.sql", "Warning: GetFloat() on non-float field %s.%s (%s.%s) at index %u. Using type: %s.",
231  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
232  return 0.0f;
233  }
234  #endif
235 
236  if (data.raw)
237  return *reinterpret_cast<float*>(data.value);
238  return static_cast<float>(atof((char*)data.value));
239  }
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
int16 Field::GetInt16 ( ) const
inline
128  {
129  if (!data.value)
130  return 0;
131 
132  #ifdef TRINITY_DEBUG
133  if (!IsType(MYSQL_TYPE_SHORT) && !IsType(MYSQL_TYPE_YEAR))
134  {
135  TC_LOG_WARN("sql.sql", "Warning: GetInt16() on non-smallint field %s.%s (%s.%s) at index %u. Using type: %s.",
136  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
137  return 0;
138  }
139  #endif
140 
141  if (data.raw)
142  return *reinterpret_cast<int16*>(data.value);
143  return static_cast<int16>(strtol((char*)data.value, NULL, 10));
144  }
arena_t NULL
Definition: jemalloc_internal.h:624
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
int16_t int16
Definition: Define.h:147

+ Here is the caller graph for this function:

int32 Field::GetInt32 ( ) const
inline
166  {
167  if (!data.value)
168  return 0;
169 
170  #ifdef TRINITY_DEBUG
171  if (!IsType(MYSQL_TYPE_INT24) && !IsType(MYSQL_TYPE_LONG))
172  {
173  TC_LOG_WARN("sql.sql", "Warning: GetInt32() on non-(medium)int field %s.%s (%s.%s) at index %u. Using type: %s.",
174  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
175  return 0;
176  }
177  #endif
178 
179  if (data.raw)
180  return *reinterpret_cast<int32*>(data.value);
181  return static_cast<int32>(strtol((char*)data.value, NULL, 10));
182  }
arena_t NULL
Definition: jemalloc_internal.h:624
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
int32_t int32
Definition: Define.h:146
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204

+ Here is the caller graph for this function:

int64 Field::GetInt64 ( ) const
inline
204  {
205  if (!data.value)
206  return 0;
207 
208  #ifdef TRINITY_DEBUG
209  if (!IsType(MYSQL_TYPE_LONGLONG) && !IsType(MYSQL_TYPE_BIT))
210  {
211  TC_LOG_WARN("sql.sql", "Warning: GetInt64() on non-bigint field %s.%s (%s.%s) at index %u. Using type: %s.",
212  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
213  return 0;
214  }
215  #endif
216 
217  if (data.raw)
218  return *reinterpret_cast<int64*>(data.value);
219  return static_cast<int64>(strtoll((char*)data.value, NULL, 10));
220  }
int64_t int64
Definition: Define.h:145
arena_t NULL
Definition: jemalloc_internal.h:624
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204

+ Here is the caller graph for this function:

int8 Field::GetInt8 ( ) const
inline
90  {
91  if (!data.value)
92  return 0;
93 
94  #ifdef TRINITY_DEBUG
95  if (!IsType(MYSQL_TYPE_TINY))
96  {
97  TC_LOG_WARN("sql.sql", "Warning: GetInt8() on non-tinyint field %s.%s (%s.%s) at index %u. Using type: %s.",
98  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
99  return 0;
100  }
101  #endif
102 
103  if (data.raw)
104  return *reinterpret_cast<int8*>(data.value);
105  return static_cast<int8>(strtol((char*)data.value, NULL, 10));
106  }
int8_t int8
Definition: Define.h:148
arena_t NULL
Definition: jemalloc_internal.h:624
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204

+ Here is the caller graph for this function:

std::string Field::GetString ( ) const
inline
277  {
278  if (!data.value)
279  return "";
280 
281  char const* string = GetCString();
282  if (!string)
283  return "";
284 
285  return std::string(string, data.length);
286  }
char const * GetCString() const
Definition: Field.h:260
struct Field::@57 data
uint16 Field::GetUInt16 ( ) const
inline
109  {
110  if (!data.value)
111  return 0;
112 
113  #ifdef TRINITY_DEBUG
114  if (!IsType(MYSQL_TYPE_SHORT) && !IsType(MYSQL_TYPE_YEAR))
115  {
116  TC_LOG_WARN("sql.sql", "Warning: GetUInt16() on non-smallint field %s.%s (%s.%s) at index %u. Using type: %s.",
117  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
118  return 0;
119  }
120  #endif
121 
122  if (data.raw)
123  return *reinterpret_cast<uint16*>(data.value);
124  return static_cast<uint16>(strtoul((char*)data.value, nullptr, 10));
125  }
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
uint16_t uint16
Definition: Define.h:151
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
uint32 Field::GetUInt32 ( ) const
inline
147  {
148  if (!data.value)
149  return 0;
150 
151  #ifdef TRINITY_DEBUG
152  if (!IsType(MYSQL_TYPE_INT24) && !IsType(MYSQL_TYPE_LONG))
153  {
154  TC_LOG_WARN("sql.sql", "Warning: GetUInt32() on non-(medium)int field %s.%s (%s.%s) at index %u. Using type: %s.",
155  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
156  return 0;
157  }
158  #endif
159 
160  if (data.raw)
161  return *reinterpret_cast<uint32*>(data.value);
162  return static_cast<uint32>(strtoul((char*)data.value, nullptr, 10));
163  }
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
uint32_t uint32
Definition: Define.h:150
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
uint64 Field::GetUInt64 ( ) const
inline
185  {
186  if (!data.value)
187  return 0;
188 
189  #ifdef TRINITY_DEBUG
190  if (!IsType(MYSQL_TYPE_LONGLONG) && !IsType(MYSQL_TYPE_BIT))
191  {
192  TC_LOG_WARN("sql.sql", "Warning: GetUInt64() on non-bigint field %s.%s (%s.%s) at index %u. Using type: %s.",
193  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
194  return 0;
195  }
196  #endif
197 
198  if (data.raw)
199  return *reinterpret_cast<uint64*>(data.value);
200  return static_cast<uint64>(strtoull((char*)data.value, nullptr, 10));
201  }
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
uint64_t uint64
Definition: Define.h:149
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
uint8 Field::GetUInt8 ( ) const
inline
71  {
72  if (!data.value)
73  return 0;
74 
75  #ifdef TRINITY_DEBUG
76  if (!IsType(MYSQL_TYPE_TINY))
77  {
78  TC_LOG_WARN("sql.sql", "Warning: GetUInt8() on non-tinyint field %s.%s (%s.%s) at index %u. Using type: %s.",
79  meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
80  return 0;
81  }
82  #endif
83 
84  if (data.raw)
85  return *reinterpret_cast<uint8*>(data.value);
86  return static_cast<uint8>(strtoul((char*)data.value, nullptr, 10));
87  }
bool IsType(enum_field_types type) const
Definition: Field.h:386
struct Field::@57 data
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
uint8_t uint8
Definition: Define.h:152
bool Field::IsNull ( ) const
inline
300  {
301  return data.value == NULL;
302  }
arena_t NULL
Definition: jemalloc_internal.h:624
struct Field::@57 data
bool Field::IsNumeric ( ) const
inlineprotected
392  {
393  return (data.type == MYSQL_TYPE_TINY ||
394  data.type == MYSQL_TYPE_SHORT ||
395  data.type == MYSQL_TYPE_INT24 ||
396  data.type == MYSQL_TYPE_LONG ||
397  data.type == MYSQL_TYPE_FLOAT ||
398  data.type == MYSQL_TYPE_DOUBLE ||
399  data.type == MYSQL_TYPE_LONGLONG );
400  }
struct Field::@57 data
bool Field::IsType ( enum_field_types  type) const
inlineprotected
387  {
388  return data.type == type;
389  }
struct Field::@57 data
enum_field_types type
Definition: Field.h:320
void Field::SetByteValue ( void *  newValue,
enum_field_types  newType,
uint32  length 
)
protected
34 {
35  // This value stores raw bytes that have to be explicitly cast later
36  data.value = newValue;
37  data.length = length;
38  data.type = newType;
39  data.raw = true;
40 }
struct Field::@57 data
uint32 length
Definition: Field.h:318
void Field::SetStructuredValue ( char *  newValue,
enum_field_types  newType,
uint32  length 
)
protected
43 {
44  if (data.value)
45  CleanUp();
46 
47  // This value stores somewhat structured data that needs function style casting
48  if (newValue)
49  {
50  data.value = new char[length + 1];
51  memcpy(data.value, newValue, length);
52  *(reinterpret_cast<char*>(data.value) + length) = '\0';
53  data.length = length;
54  }
55 
56  data.type = newType;
57  data.raw = false;
58 }
struct Field::@57 data
void CleanUp()
Definition: Field.h:328
uint32 length
Definition: Field.h:318

+ Here is the call graph for this function:

static uint32 Field::SizeForType ( MYSQL_FIELD *  field)
inlinestaticprotected
337  {
338  switch (field->type)
339  {
340  case MYSQL_TYPE_NULL:
341  return 0;
342  case MYSQL_TYPE_TINY:
343  return 1;
344  case MYSQL_TYPE_YEAR:
345  case MYSQL_TYPE_SHORT:
346  return 2;
347  case MYSQL_TYPE_INT24:
348  case MYSQL_TYPE_LONG:
349  case MYSQL_TYPE_FLOAT:
350  return 4;
351  case MYSQL_TYPE_DOUBLE:
352  case MYSQL_TYPE_LONGLONG:
353  case MYSQL_TYPE_BIT:
354  return 8;
355 
356  case MYSQL_TYPE_TIMESTAMP:
357  case MYSQL_TYPE_DATE:
358  case MYSQL_TYPE_TIME:
359  case MYSQL_TYPE_DATETIME:
360  return sizeof(MYSQL_TIME);
361 
362  case MYSQL_TYPE_TINY_BLOB:
363  case MYSQL_TYPE_MEDIUM_BLOB:
364  case MYSQL_TYPE_LONG_BLOB:
365  case MYSQL_TYPE_BLOB:
366  case MYSQL_TYPE_STRING:
367  case MYSQL_TYPE_VAR_STRING:
368  return field->max_length + 1;
369 
370  case MYSQL_TYPE_DECIMAL:
371  case MYSQL_TYPE_NEWDECIMAL:
372  return 64;
373 
374  case MYSQL_TYPE_GEOMETRY:
375  /*
376  Following types are not sent over the wire:
377  MYSQL_TYPE_ENUM:
378  MYSQL_TYPE_SET:
379  */
380  default:
381  TC_LOG_WARN("sql.sql", "SQL::SizeForType(): invalid field type %u", uint32(field->type));
382  return 0;
383  }
384  }
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
uint32_t uint32
Definition: g3dmath.h:168

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class PreparedResultSet
friend
friend class ResultSet
friend

Member Data Documentation

struct { ... } Field::data
uint32 Field::length
bool Field::raw
enum_field_types Field::type
void* Field::value

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