TrinityCore
Loading...
Searching...
No Matches
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::string_view GetStringView () 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}
struct Field::@5 data
QueryResultFieldMetadata const * meta
Definition: Field.h:140

◆ ~Field()

Field::~Field ( )
default

Member Function Documentation

◆ GetBinary() [1/2]

std::vector< uint8 > Field::GetBinary ( ) const
253{
254 std::vector<uint8> result;
255 if (!data.value || !data.length)
256 return result;
257
258 result.resize(data.length);
259 memcpy(result.data(), data.value, data.length);
260 return result;
261}
+ Here is the caller graph for this function:

◆ GetBinary() [2/2]

template<size_t S>
std::array< uint8, S > Field::GetBinary ( ) const
inline
113 {
114 std::array<uint8, S> buf;
115 GetBinarySizeChecked(buf.data(), S);
116 return buf;
117 }
void GetBinarySizeChecked(uint8 *buf, size_t size) const
Definition: Field.cpp:263

◆ GetBinarySizeChecked()

void Field::GetBinarySizeChecked ( uint8 buf,
size_t  size 
) const
private
264{
265 ASSERT(data.value && (data.length == length), "Expected %zu-byte binary blob, got %sdata (%u bytes) instead", length, data.value ? "" : "no ", data.length);
266 memcpy(buf, data.value, length);
267}
#define ASSERT
Definition: Errors.h:68
uint32 length
Definition: Field.h:128

◆ GetBool()

bool Field::GetBool ( ) const
inline
93 {
94 return GetUInt8() == 1 ? true : false;
95 }
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}
void LogWrongType(char const *getter) const
Definition: Field.cpp:300
bool IsNumeric() const
Definition: Field.cpp:290
+ 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
201 if (!IsType(DatabaseFieldTypes::Double) && !IsType(DatabaseFieldTypes::Decimal))
202 {
203 LogWrongType(__FUNCTION__);
204 return 0.0f;
205 }
206#endif
207
208 if (data.raw && !IsType(DatabaseFieldTypes::Decimal))
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:285
+ 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
183 if (!IsType(DatabaseFieldTypes::Float))
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}
+ 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
93 if (!IsType(DatabaseFieldTypes::Int16))
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}
int16_t int16
Definition: Define.h:140
+ 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
129 if (!IsType(DatabaseFieldTypes::Int32))
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:139
+ 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
165 if (!IsType(DatabaseFieldTypes::Int64))
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:138
+ 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
57 if (!IsType(DatabaseFieldTypes::Int8))
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:141
+ 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
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetStringView()

std::string_view Field::GetStringView ( ) const
241{
242 if (!data.value)
243 return {};
244
245 char const* const string = GetCString();
246 if (!string)
247 return {};
248
249 return { string, data.length };
250}
+ 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
75 if (!IsType(DatabaseFieldTypes::Int16))
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:144
+ 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
111 if (!IsType(DatabaseFieldTypes::Int32))
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:143
+ 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
147 if (!IsType(DatabaseFieldTypes::Int64))
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:142
+ 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
39 if (!IsType(DatabaseFieldTypes::Int8))
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}
uint8_t uint8
Definition: Define.h:145
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsNull()

bool Field::IsNull ( ) const
inline
120 {
121 return data.value == nullptr;
122 }
+ Here is the caller graph for this function:

◆ IsNumeric()

bool Field::IsNumeric ( ) const
protected
291{
292 return (meta->Type == DatabaseFieldTypes::Int8 ||
293 meta->Type == DatabaseFieldTypes::Int16 ||
294 meta->Type == DatabaseFieldTypes::Int32 ||
295 meta->Type == DatabaseFieldTypes::Int64 ||
296 meta->Type == DatabaseFieldTypes::Float ||
297 meta->Type == DatabaseFieldTypes::Double);
298}
DatabaseFieldTypes Type
Definition: Field.h:50
+ Here is the caller graph for this function:

◆ IsType()

bool Field::IsType ( DatabaseFieldTypes  type) const
protected
286{
287 return meta->Type == type;
288}
+ Here is the caller graph for this function:

◆ LogWrongType()

void Field::LogWrongType ( char const *  getter) const
private
301{
302 TC_LOG_WARN("sql.sql", "Warning: {} on {} field {}.{} ({}.{}) at index {}.",
304}
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:162
char const * Alias
Definition: Field.h:47
char const * TableAlias
Definition: Field.h:45
char const * TableName
Definition: Field.h:44
char const * TypeName
Definition: Field.h:48
uint32 Index
Definition: Field.h:49
char const * Name
Definition: Field.h:46
+ Here is the caller graph for this function:

◆ SetByteValue()

void Field::SetByteValue ( char const *  newValue,
uint32  length 
)
protected
270{
271 // This value stores raw bytes that have to be explicitly cast later
272 data.value = newValue;
273 data.length = length;
274 data.raw = true;
275}

◆ SetMetadata()

void Field::SetMetadata ( QueryResultFieldMetadata const *  fieldMeta)
private
307{
308 meta = fieldMeta;
309}
+ Here is the caller graph for this function:

◆ SetStructuredValue()

void Field::SetStructuredValue ( char const *  newValue,
uint32  length 
)
protected
278{
279 // This value stores somewhat structured data that needs function style casting
280 data.value = newValue;
281 data.length = length;
282 data.raw = false;
283}

Friends And Related Function Documentation

◆ PreparedResultSet

friend class PreparedResultSet
friend

◆ ResultSet

friend class ResultSet
friend

Member Data Documentation

◆ 

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: