TrinityCore
Field Class Reference

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

#include <Field.h>

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 const * GetCString () const
 
std::string GetString () const
 
std::vector< uint8GetBinary () const
 
template<size_t S>
std::array< uint8, S > GetBinary () const
 
bool IsNull () const
 

Protected Member Functions

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

Protected Attributes

struct {
   char const *   value
 
   uint32   length
 
   bool   raw
 
data
 

Private Member Functions

void LogWrongType (char const *getter) const
 
void SetMetadata (QueryResultFieldMetadata const *fieldMeta)
 
void GetBinarySizeChecked (uint8 *buf, size_t size) const
 

Private Attributes

QueryResultFieldMetadata const * meta
 

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::Field ( )
24 {
25  data.value = nullptr;
26  data.length = 0;
27  data.raw = false;
28  meta = nullptr;
29 }
QueryResultFieldMetadata const * meta
Definition: Field.h:138
struct Field::@5 data
+ Here is the call graph for this function:

◆ ~Field()

Field::~Field ( )
default
+ Here is the caller graph for this function:

Member Function Documentation

◆ GetBinary() [1/2]

std::vector< uint8 > Field::GetBinary ( ) const
241 {
242  std::vector<uint8> result;
243  if (!data.value || !data.length)
244  return result;
245 
246  result.resize(data.length);
247  memcpy(result.data(), data.value, data.length);
248  return result;
249 }
struct Field::@5 data
+ Here is the caller graph for this function:

◆ GetBinary() [2/2]

template<size_t S>
std::array<uint8, S> Field::GetBinary ( ) const
inline
110  {
111  std::array<uint8, S> buf;
112  GetBinarySizeChecked(buf.data(), S);
113  return buf;
114  }
void GetBinarySizeChecked(uint8 *buf, size_t size) const
Definition: Field.cpp:251

◆ GetBinarySizeChecked()

void Field::GetBinarySizeChecked ( uint8 buf,
size_t  size 
) const
private
252 {
253  ASSERT(data.value && (data.length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, data.value ? "" : "no ", data.length);
254  memcpy(buf, data.value, length);
255 }
struct Field::@5 data
uint32 length
Definition: Field.h:126
#define ASSERT
Definition: Errors.h:61

◆ GetBool()

bool Field::GetBool ( ) const
inline
91  {
92  return GetUInt8() == 1 ? true : false;
93  }
uint8 GetUInt8() const
Definition: Field.cpp:33
+ Here is the caller graph for this function:

◆ GetCString()

char const * Field::GetCString ( ) const
214 {
215  if (!data.value)
216  return nullptr;
217 
218 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
219  if (IsNumeric() && data.raw)
220  {
221  LogWrongType(__FUNCTION__);
222  return nullptr;
223  }
224 #endif
225  return static_cast<char const*>(data.value);
226 }
bool IsNumeric() const
Definition: Field.cpp:278
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetDouble()

double Field::GetDouble ( ) const
196 {
197  if (!data.value)
198  return 0.0f;
199 
200 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
202  {
203  LogWrongType(__FUNCTION__);
204  return 0.0f;
205  }
206 #endif
207 
209  return *reinterpret_cast<double const*>(data.value);
210  return static_cast<double>(atof(data.value));
211 }
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:

◆ GetFloat()

float Field::GetFloat ( ) const
178 {
179  if (!data.value)
180  return 0.0f;
181 
182 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
184  {
185  LogWrongType(__FUNCTION__);
186  return 0.0f;
187  }
188 #endif
189 
190  if (data.raw)
191  return *reinterpret_cast<float const*>(data.value);
192  return static_cast<float>(atof(data.value));
193 }
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetInt16()

int16 Field::GetInt16 ( ) const
88 {
89  if (!data.value)
90  return 0;
91 
92 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
94  {
95  LogWrongType(__FUNCTION__);
96  return 0;
97  }
98 #endif
99 
100  if (data.raw)
101  return *reinterpret_cast<int16 const*>(data.value);
102  return static_cast<int16>(strtol(data.value, nullptr, 10));
103 }
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
int16_t int16
Definition: Define.h:149
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetInt32()

int32 Field::GetInt32 ( ) const
124 {
125  if (!data.value)
126  return 0;
127 
128 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
130  {
131  LogWrongType(__FUNCTION__);
132  return 0;
133  }
134 #endif
135 
136  if (data.raw)
137  return *reinterpret_cast<int32 const*>(data.value);
138  return static_cast<int32>(strtol(data.value, nullptr, 10));
139 }
int32_t int32
Definition: Define.h:148
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetInt64()

int64 Field::GetInt64 ( ) const
160 {
161  if (!data.value)
162  return 0;
163 
164 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
166  {
167  LogWrongType(__FUNCTION__);
168  return 0;
169  }
170 #endif
171 
172  if (data.raw)
173  return *reinterpret_cast<int64 const*>(data.value);
174  return static_cast<int64>(strtoll(data.value, nullptr, 10));
175 }
int64_t int64
Definition: Define.h:147
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetInt8()

int8 Field::GetInt8 ( ) const
52 {
53  if (!data.value)
54  return 0;
55 
56 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
58  {
59  LogWrongType(__FUNCTION__);
60  return 0;
61  }
62 #endif
63 
64  if (data.raw)
65  return *reinterpret_cast<int8 const*>(data.value);
66  return static_cast<int8>(strtol(data.value, nullptr, 10));
67 }
int8_t int8
Definition: Define.h:150
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetString()

std::string Field::GetString ( ) const
229 {
230  if (!data.value)
231  return "";
232 
233  char const* string = GetCString();
234  if (!string)
235  return "";
236 
237  return std::string(string, data.length);
238 }
char const * GetCString() const
Definition: Field.cpp:213
struct Field::@5 data
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetUInt16()

uint16 Field::GetUInt16 ( ) const
70 {
71  if (!data.value)
72  return 0;
73 
74 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
76  {
77  LogWrongType(__FUNCTION__);
78  return 0;
79  }
80 #endif
81 
82  if (data.raw)
83  return *reinterpret_cast<uint16 const*>(data.value);
84  return static_cast<uint16>(strtoul(data.value, nullptr, 10));
85 }
uint16_t uint16
Definition: Define.h:153
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetUInt32()

uint32 Field::GetUInt32 ( ) const
106 {
107  if (!data.value)
108  return 0;
109 
110 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
112  {
113  LogWrongType(__FUNCTION__);
114  return 0;
115  }
116 #endif
117 
118  if (data.raw)
119  return *reinterpret_cast<uint32 const*>(data.value);
120  return static_cast<uint32>(strtoul(data.value, nullptr, 10));
121 }
uint32_t uint32
Definition: Define.h:152
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:

◆ GetUInt64()

uint64 Field::GetUInt64 ( ) const
142 {
143  if (!data.value)
144  return 0;
145 
146 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
148  {
149  LogWrongType(__FUNCTION__);
150  return 0;
151  }
152 #endif
153 
154  if (data.raw)
155  return *reinterpret_cast<uint64 const*>(data.value);
156  return static_cast<uint64>(strtoull(data.value, nullptr, 10));
157 }
uint64_t uint64
Definition: Define.h:151
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetUInt8()

uint8 Field::GetUInt8 ( ) const
34 {
35  if (!data.value)
36  return 0;
37 
38 #ifdef TRINITY_STRICT_DATABASE_TYPE_CHECKS
40  {
41  LogWrongType(__FUNCTION__);
42  return 0;
43  }
44 #endif
45 
46  if (data.raw)
47  return *reinterpret_cast<uint8 const*>(data.value);
48  return static_cast<uint8>(strtoul(data.value, nullptr, 10));
49 }
bool IsType(DatabaseFieldTypes type) const
Definition: Field.cpp:273
struct Field::@5 data
uint8_t uint8
Definition: Define.h:154
void LogWrongType(char const *getter) const
Definition: Field.cpp:288
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsNull()

bool Field::IsNull ( ) const
inline
118  {
119  return data.value == nullptr;
120  }
struct Field::@5 data
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsNumeric()

bool Field::IsNumeric ( ) const
protected
+ Here is the caller graph for this function:

◆ IsType()

bool Field::IsType ( DatabaseFieldTypes  type) const
protected
274 {
275  return meta->Type == type;
276 }
DatabaseFieldTypes Type
Definition: Field.h:48
QueryResultFieldMetadata const * meta
Definition: Field.h:138
+ Here is the caller graph for this function:

◆ LogWrongType()

void Field::LogWrongType ( char const *  getter) const
private
289 {
290  TC_LOG_WARN("sql.sql", "Warning: %s on %s field %s.%s (%s.%s) at index %u.",
292 }
uint32 Index
Definition: Field.h:47
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:183
char const * TableAlias
Definition: Field.h:43
char const * Name
Definition: Field.h:44
QueryResultFieldMetadata const * meta
Definition: Field.h:138
char const * TableName
Definition: Field.h:42
char const * Alias
Definition: Field.h:45
char const * TypeName
Definition: Field.h:46
+ Here is the caller graph for this function:

◆ SetByteValue()

void Field::SetByteValue ( char const *  newValue,
uint32  length 
)
protected
258 {
259  // This value stores raw bytes that have to be explicitly cast later
260  data.value = newValue;
261  data.length = length;
262  data.raw = true;
263 }
struct Field::@5 data
uint32 length
Definition: Field.h:126

◆ SetMetadata()

void Field::SetMetadata ( QueryResultFieldMetadata const *  fieldMeta)
private
295 {
296  meta = fieldMeta;
297 }
QueryResultFieldMetadata const * meta
Definition: Field.h:138
+ Here is the caller graph for this function:

◆ SetStructuredValue()

void Field::SetStructuredValue ( char const *  newValue,
uint32  length 
)
protected
266 {
267  // This value stores somewhat structured data that needs function style casting
268  data.value = newValue;
269  data.length = length;
270  data.raw = false;
271 }
struct Field::@5 data
uint32 length
Definition: Field.h:126

Friends And Related Function Documentation

◆ PreparedResultSet

friend class PreparedResultSet
friend

◆ ResultSet

friend class ResultSet
friend

Member Data Documentation

◆ data

struct { ... } Field::data

◆ length

uint32 Field::length

◆ meta

QueryResultFieldMetadata const* Field::meta
private

◆ raw

bool Field::raw

◆ value

char const* Field::value

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