iFun Engine API  1.0.0-b2601
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 
28  explicit ResultSets(const Ptr<Impl> &impl);
29 
32  size_t GetResultSetCount() const;
33 
36  bool SeekNextResultSet();
37 
39  size_t GetAffectedRowCount() const;
40 
43  size_t GetColumnCount() const;
44 
48  string GetColumnName(const size_t &column_index) const;
49 
53  enum_field_types GetColumnType(const size_t &column_index) const;
54 
57  size_t GetRowCount() const;
58 
61  bool SeekNextRow();
62 
65  bool IsNull(size_t column_index) const;
66 
70  bool GetBool(size_t column_index, bool regard_null_as = false) const;
71 
75  int64_t GetInt(size_t column_index, int64_t regard_null_as = 0) const;
76  int8_t GetInt8(size_t column_index, int8_t regard_null_as = 0) const;
77  int16_t GetInt16(size_t column_index, int16_t regard_null_as = 0) const;
78  int32_t GetInt32(size_t column_index, int32_t regard_null_as = 0) const;
79  int64_t GetInt64(size_t column_index, int64_t regard_null_as = 0) const;
80 
84  uint64_t GetUint(size_t column_index, uint64_t regard_null_as = 0) const;
85  uint8_t GetUint8(size_t column_index, uint8_t regard_null_as = 0) const;
86  uint16_t GetUint16(size_t column_index, uint16_t regard_null_as = 0) const;
87  uint32_t GetUint32(size_t column_index, uint32_t regard_null_as = 0) const;
88  uint64_t GetUint64(size_t column_index, uint64_t regard_null_as = 0) const;
89 
93  float GetFloat(size_t column_index, float regard_null_as = 0.0) const;
94 
98  double GetDouble(size_t column_index, double regard_null_as = 0.0) const;
99 
103  const char *GetString(size_t column_index) const;
104 
108  WallClock::Value GetDateTime(
109  size_t column_index,
110  const WallClock::Value &regard_null_as = WallClock::kEpochClock) const;
111 
112  const string GetDateTimeString(
113  size_t column_index,
114  const string &regard_null_as = "") const;
115 
119  WallClock::Duration GetTime(
120  size_t column_index,
121  const WallClock::Duration &regard_null_as =
123 
127  bool GetBool(const string &column_name, bool regard_null_as = false) const;
128 
132  int64_t GetInt(const string &column_name, int64_t regard_null_as = 0) const;
133 
137  uint64_t GetUint(const string &column_name, uint64_t regard_null_as = 0) const;
138 
142  float GetFloat(const string &column_name, float regard_null_as = 0.0) const;
143 
147  double GetDouble(const string &column_name, double regard_null_as = 0.0) const;
148 
152  const char *GetString(const string &column_name) const;
153 
157  WallClock::Value GetDateTime(
158  const string &column_name,
159  const WallClock::Value &regard_null_as = WallClock::kEpochClock) const;
160 
164  WallClock::Duration GetTime(
165  const string &column_name,
166  const WallClock::Duration &regard_null_as =
168 
169  private:
170  Ptr<Impl> impl_;
171 };
172 
173 
175 class Mariadb {
176  public:
177  // Contains mysql errors excluding connection errors.
178  // see files below for details.
179  // - /usr/include/mariadb/errmsg.h
180  // - /usr/include/mariadb/mysqld_error.h
181  struct Error {
182  int code;
183  string desc;
184  string sqlstate;
185  };
186 
187  typedef function<void(const Ptr<ResultSets> &,
188  const Error &)> QueryExecuteHandler;
189 
190  static Ptr<Mariadb> Create(const string &address, const string &id,
191  const string &password, const string &database,
192  size_t connection_count,
193  const string &connection_charset = string(""),
194  bool auto_retry_on_deadlock = false);
195 
196  void Initialize();
197  void Finalize();
198 
199  void ExecuteQuery(const string &query, const QueryExecuteHandler &handler);
200  Ptr<ResultSets> ExecuteQuerySync(const string &query, Error *error = NULL);
201 
202  const string &address() const;
203  const string &id() const;
204  const string &database() const;
205  size_t connection_count() const;
206 
207  private:
208  class Impl;
209 
210  Mariadb(const string &address, const string &id, const string &password,
211  const string &database, size_t connection_count,
212  const string &connection_charset, bool auto_retry_on_deadlock);
213 
214  Ptr<Impl> impl_;
215 };
216 
217 } // namespace fun
218 
219 #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:175
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:181
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.