iFun Engine API  1.0.0-b2936
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
mariadb.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 //
7 
10 #ifndef INCLUDE_FUNAPI_DATABASE_MARIADB_H_
11 #define INCLUDE_FUNAPI_DATABASE_MARIADB_H_
12 
13 #include <boost/bind.hpp>
14 #include <errmsg.h>
15 #include <funapi/types.h>
16 #include <funapi/time/wall_clock.h>
17 #include <mysql.h>
18 #include <mysqld_error.h>
19 
20 
21 namespace fun {
22 
24 class ResultSets {
25  public:
26  class Impl;
27 
29 
30  explicit ResultSets(const Ptr<Impl> &impl);
31 
34  size_t GetResultSetCount() const;
35 
38  bool SeekNextResultSet();
39 
41  size_t GetAffectedRowCount() const;
42 
45  size_t GetColumnCount() const;
46 
50  string GetColumnName(const size_t &column_index) const;
51 
55  enum_field_types GetColumnType(const size_t &column_index) const;
56 
59  size_t GetRowCount() const;
60 
63  bool SeekNextRow();
64 
67  bool IsNull(size_t column_index) const;
68 
72  bool GetBool(size_t column_index, bool regard_null_as = false) const;
73 
77  int64_t GetInt(size_t column_index, int64_t regard_null_as = 0) const;
78  int8_t GetInt8(size_t column_index, int8_t regard_null_as = 0) const;
79  int16_t GetInt16(size_t column_index, int16_t regard_null_as = 0) const;
80  int32_t GetInt32(size_t column_index, int32_t regard_null_as = 0) const;
81  int64_t GetInt64(size_t column_index, int64_t regard_null_as = 0) const;
82 
86  uint64_t GetUint(size_t column_index, uint64_t regard_null_as = 0) const;
87  uint8_t GetUint8(size_t column_index, uint8_t regard_null_as = 0) const;
88  uint16_t GetUint16(size_t column_index, uint16_t regard_null_as = 0) const;
89  uint32_t GetUint32(size_t column_index, uint32_t regard_null_as = 0) const;
90  uint64_t GetUint64(size_t column_index, uint64_t regard_null_as = 0) const;
91 
95  float GetFloat(size_t column_index, float regard_null_as = 0.0) const;
96 
100  double GetDouble(size_t column_index, double regard_null_as = 0.0) const;
101 
105  const char *GetString(size_t column_index) const;
106 
110  WallClock::Value GetDateTime(
111  size_t column_index,
112  const WallClock::Value &regard_null_as = WallClock::kEpochClock) const;
113 
114  const string GetDateTimeString(
115  size_t column_index,
116  const string &regard_null_as = "") const;
117 
121  WallClock::Duration GetTime(
122  size_t column_index,
123  const WallClock::Duration &regard_null_as =
125 
129  bool GetBool(const string &column_name, bool regard_null_as = false) const;
130 
134  int64_t GetInt(const string &column_name, int64_t regard_null_as = 0) const;
135 
139  uint64_t GetUint(const string &column_name, uint64_t regard_null_as = 0) const;
140 
144  float GetFloat(const string &column_name, float regard_null_as = 0.0) const;
145 
149  double GetDouble(const string &column_name, double regard_null_as = 0.0) const;
150 
154  const char *GetString(const string &column_name) const;
155 
159  WallClock::Value GetDateTime(
160  const string &column_name,
161  const WallClock::Value &regard_null_as = WallClock::kEpochClock) const;
162 
166  WallClock::Duration GetTime(
167  const string &column_name,
168  const WallClock::Duration &regard_null_as =
170 
171  private:
172  Ptr<Impl> impl_;
173 };
174 
175 
177 class Mariadb {
178  public:
179  // Contains mysql errors excluding connection errors.
180  // see files below for details.
181  // - /usr/include/mariadb/errmsg.h
182  // - /usr/include/mariadb/mysqld_error.h
183  struct Error {
184  int code;
185  string desc;
186  string sqlstate;
187  };
188 
189  typedef function<void(const Ptr<ResultSets> &,
190  const Error &)> QueryExecuteHandler;
191 
192  static Ptr<Mariadb> Create(const string &address, const string &id,
193  const string &password, const string &database,
194  size_t connection_count,
195  const string &connection_charset = string(""),
196  bool auto_retry_on_deadlock = false,
197  unsigned long client_flags = 0);
198 
199  void Initialize();
200  void Finalize();
201 
202  void ExecuteQuery(const string &query, const QueryExecuteHandler &handler);
203  Ptr<ResultSets> ExecuteQuerySync(const string &query, Error *error = NULL);
204 
205  const string &address() const;
206  const string &id() const;
207  const string &database() const;
208  size_t connection_count() const;
209 
210  private:
211  class Impl;
212 
213  Mariadb(const string &address, const string &id, const string &password,
214  const string &database, size_t connection_count,
215  const string &connection_charset, bool auto_retry_on_deadlock,
216  unsigned long client_flags);
217 
218  Ptr<Impl> impl_;
219 };
220 
221 } // namespace fun
222 
223 #endif // INCLUDE_FUNAPI_DATABASE_MARIADB_H_
const char * GetString(size_t column_index) const
Gets the specified culumn's value of the current row as string.
Provides query interface for Mariadb and manages connection pool.
Definition: mariadb.h:177
uint64_t GetUint(size_t column_index, uint64_t regard_null_as=0) const
Gets the specified culumn's value of the current row as unsigned int.
double GetDouble(size_t column_index, double regard_null_as=0.0) const
Gets the specified culumn's value of the current row as double.
bool IsNull(size_t column_index) const
Check the specified column's value of the current row is null.
bool SeekNextRow()
Seeks the next row.
float GetFloat(size_t column_index, float regard_null_as=0.0) const
Gets the specified culumn's value of the current row as float.
bool SeekNextResultSet()
Seeks the next result set.
string GetColumnName(const size_t &column_index) const
Gets the column name of specified index.
bool GetBool(size_t column_index, bool regard_null_as=false) const
Gets the specified culumn's value of the current row as bool.
Contains result sets and provides access interface.
Definition: mariadb.h:24
size_t GetRowCount() const
Gets the row count of the current result set.
static const Duration kEmptyDuration
the value of 00:00:00.000000
Definition: wall_clock.h:36
size_t GetAffectedRowCount() const
Gets the affected row count of the current result set.
int64_t GetInt(size_t column_index, int64_t regard_null_as=0) const
Gets the specified culumn's value of the current row as int.
Definition: mariadb.h:183
size_t GetColumnCount() const
Gets the column count of the current result set.
WallClock::Value GetDateTime(size_t column_index, const WallClock::Value &regard_null_as=WallClock::kEpochClock) const
Gets the specified column's value of the current row as datetime.
enum_field_types GetColumnType(const size_t &column_index) const
Gets the column type of specified index.
WallClock::Duration GetTime(size_t column_index, const WallClock::Duration &regard_null_as=WallClock::kEmptyDuration) const
Gets the specified column's value of the current row as time.
static const Value kEpochClock
The value of time_t(0), 1970-01-01 00:00:00.000000 UTC.
Definition: wall_clock.h:33
size_t GetResultSetCount() const
Gets the result set count.
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:72