OregonCore  revision 3611e8a-git
Your Favourite TBC server
Field.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #if !defined(FIELD_H)
19 #define FIELD_H
20 
21 #include "Common.h"
22 #include <mysql.h>
23 
24 class Field
25 {
26  public:
27 
28  Field() : mValue(NULL), mType(MYSQL_TYPE_NULL) {}
29 
31  {
32  if (mIsRawValue)
33  free (mRawValue);
34  }
35 
36  enum_field_types GetType() const
37  {
38  return mType;
39  }
40 
41  const char* GetString() const
42  {
43  if (!mIsRawValue)
44  return mValue;
45 
46  if (mType != MYSQL_TYPE_STRING)
47  return NULL;
48 
49  return reinterpret_cast<char*> (mRawValue);
50  }
51 
52  std::string GetCppString() const
53  {
54  if (!mIsRawValue)
55  return mValue ? mValue : ""; // std::string s = 0 have undefine result in C++
56 
57  if (mType != MYSQL_TYPE_STRING || !mRawValue)
58  return "";
59 
60  return reinterpret_cast<char*> (mRawValue);
61  }
62 
63  std::vector<uint8> GetBinary() const
64  {
65  std::vector<uint8> data;
66 
67  if (!mIsRawValue)
68  return data;
69 
70  data.resize(mRawValueSize, 0);
71  std::copy((unsigned char*)mRawValue, ((unsigned char*) mRawValue) + mRawValueSize, data.begin());
72 
73  return data;
74  }
75 
76  #ifdef OREGON_DEBUG
77  # define dbg_help_var 1
78  #else
79  # define dbg_help_var 0
80  #endif
81  #define GetIntegerDataImpl(name, expected_type, ctype, regular_cast) \
82  ctype name() const \
83  { \
84  if (!mValue) \
85  return 0; \
86  \
87  if (!mIsRawValue) \
88  return static_cast<ctype>(regular_cast); \
89  \
90  if (mType != expected_type) \
91  { \
92  /*if (dbg_help_var)*/ \
93  /* error_db_log(__FUNCTION__ " on incorrent column data type.");*/ \
94  \
95  switch (mType) \
96  { \
97  case MYSQL_TYPE_TINY: return static_cast<ctype>(*reinterpret_cast<int8*>(mRawValue)); \
98  case MYSQL_TYPE_SHORT: return static_cast<ctype>(*reinterpret_cast<int16*>(mRawValue)); \
99  case MYSQL_TYPE_INT24: \
100  case MYSQL_TYPE_LONG: return static_cast<ctype>(*reinterpret_cast<int32*>(mRawValue)); \
101  case MYSQL_TYPE_LONGLONG: return static_cast<ctype>(*reinterpret_cast<int64*>(mRawValue)); \
102  case MYSQL_TYPE_FLOAT: return static_cast<ctype>(*reinterpret_cast<float*>(mRawValue)); \
103  case MYSQL_TYPE_DOUBLE: return static_cast<ctype>(*reinterpret_cast<double*>(mRawValue)); \
104  default: return 0; \
105  } \
106  } \
107  \
108  return *reinterpret_cast<ctype*> (mRawValue); \
109  }
110 
111  GetIntegerDataImpl(GetFloat, MYSQL_TYPE_FLOAT, float, STRTOF(mValue, NULL))
112  GetIntegerDataImpl(GetDouble, MYSQL_TYPE_DOUBLE, double, strtod(mValue, NULL))
113  GetIntegerDataImpl(GetInt8, MYSQL_TYPE_TINY, int8, strtol(mValue, NULL, 10))
114  GetIntegerDataImpl(GetInt16, MYSQL_TYPE_SHORT, int16, strtol(mValue, NULL, 10))
115  GetIntegerDataImpl(GetInt32, MYSQL_TYPE_LONG, int32, strtol(mValue, NULL, 10))
116  GetIntegerDataImpl(GetInt64, MYSQL_TYPE_LONGLONG, int64, strtoll(mValue, NULL, 10))
117  GetIntegerDataImpl(GetUInt8, MYSQL_TYPE_TINY, uint8, strtoul(mValue, NULL, 10))
118  GetIntegerDataImpl(GetUInt16, MYSQL_TYPE_SHORT, uint16, strtoul(mValue, NULL, 10))
119  GetIntegerDataImpl(GetUInt32, MYSQL_TYPE_LONG, uint32, strtoul(mValue, NULL, 10))
120  GetIntegerDataImpl(GetUInt64, MYSQL_TYPE_LONGLONG, uint64, strtoull(mValue, NULL, 10))
121 
122  #undef GetIntegerDataImpl
123  #undef dbg_help_var
124 
125  bool GetBool() const
126  {
127  return GetUInt8() == 1;
128  }
129 
131  void SetBinaryValue(void* value, size_t size, enum_field_types type)
132  {
133  mRawValue = value;
134  mRawValueSize = size;
135  mType = type;
136  mIsRawValue = true;
137  }
138 
140  void SetType(enum_field_types type)
141  {
142  mType = type;
143  }
144 
146  void SetValue(const char* value)
147  {
148  mValue = value;
149  mIsRawValue = false;
150  };
151 
152  private:
153  Field(Field const&);
154  Field& operator=(Field const&);
155 
156  union
157  {
160  struct
161  {
162  void* mRawValue;
164  };
165 
168  const char* mValue;
169  };
170 
171  enum_field_types mType;
172  bool mIsRawValue;
173 };
174 #endif
175 
strtoull(mValue, NULL, 10)) bool GetBool() const
Definition: Field.h:120
MYSQL_TYPE_LONG
Definition: Field.h:115
void SetValue(const char *value)
Called by QueryResult, no need free this.
Definition: Field.h:146
Definition: Field.h:24
ACE_INT32 int32
Definition: Define.h:67
strtoul(mValue, NULL, 10)) GetIntegerDataImpl(GetUInt16
void SetType(enum_field_types type)
Called by QueryResult.
Definition: Field.h:140
Field()
Definition: Field.h:28
size_t mRawValueSize
Definition: Field.h:163
const char * GetString() const
Definition: Field.h:41
ACE_INT8 int8
Definition: Define.h:69
std::vector< uint8 > GetBinary() const
Definition: Field.h:63
strtol(mValue, NULL, 10)) GetIntegerDataImpl(GetInt16
MYSQL_TYPE_LONGLONG
Definition: Field.h:116
ACE_UINT8 uint8
Definition: Define.h:73
const char * mValue
Definition: Field.h:168
std::string GetCppString() const
Definition: Field.h:52
etc mysql my cnf *Then change max_allowed_packet to a bigger value
strtoll(mValue, NULL, 10)) GetIntegerDataImpl(GetUInt8
MYSQL_TYPE_DOUBLE
Definition: Field.h:112
GetIntegerDataImpl(GetFloat, MYSQL_TYPE_FLOAT, float, STRTOF(mValue, NULL)) GetIntegerDataImpl(GetDouble
enum_field_types mType
The mysql type of this field.
Definition: Field.h:171
ACE_UINT64 uint64
Definition: Define.h:70
ACE_INT64 int64
Definition: Define.h:66
strtod(mValue, NULL)) GetIntegerDataImpl(GetInt8
bool mIsRawValue
flag whether we have native data (from prepared statements) or not
Definition: Field.h:172
#define STRTOF
Definition: Common.h:137
Field & operator=(Field const &)
void SetBinaryValue(void *value, size_t size, enum_field_types type)
Used by PreparedQueryResult, we need to copy the original data and free it later. ...
Definition: Field.h:131
void * mRawValue
Definition: Field.h:162
ACE_UINT16 uint16
Definition: Define.h:72
~Field()
Definition: Field.h:30
ACE_INT16 int16
Definition: Define.h:68
ACE_UINT32 uint32
Definition: Define.h:71
MYSQL_TYPE_SHORT
Definition: Field.h:114
enum_field_types GetType() const
Definition: Field.h:36
MYSQL_TYPE_TINY
Definition: Field.h:113