iFun Engine API  1.0.0-b6053
Great Technology for Great Games
mariadb.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 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 <boost/enable_shared_from_this.hpp>
15 
16 #include <errmsg.h>
17 
18 #include <funapi/types.h>
19 #include <funapi/time/wall_clock.h>
20 
21 
22 #include <mysql.h>
23 #include <mysqld_error.h>
24 
25 
26 namespace fun {
27 
29 class FUNAPI_DLL_VISIBILITY ResultSets {
30  public:
31  class Impl;
32 
34 
35  explicit ResultSets(const Ptr<Impl> &impl);
36 
39  size_t GetResultSetCount() const;
40 
43  bool SeekNextResultSet();
44 
46  size_t GetCountOfAffectedRows() const;
47 
49  const std::vector<size_t> &GetCountsOfAffectedRows() const;
50 
53  size_t GetColumnCount() const;
54 
58  string GetColumnName(const size_t &column_index) const;
59 
63  enum_field_types GetColumnType(const size_t &column_index) const;
64 
67  size_t GetRowCount() const;
68 
71  bool SeekNextRow();
72 
75  bool IsNull(size_t column_index) const;
76 
80  bool GetBool(size_t column_index, bool regard_null_as = false) const;
81 
85  int64_t GetInt(size_t column_index, int64_t regard_null_as = 0) const;
86  int8_t GetInt8(size_t column_index, int8_t regard_null_as = 0) const;
87  int16_t GetInt16(size_t column_index, int16_t regard_null_as = 0) const;
88  int32_t GetInt32(size_t column_index, int32_t regard_null_as = 0) const;
89  int64_t GetInt64(size_t column_index, int64_t regard_null_as = 0) const;
90 
94  uint64_t GetUint(size_t column_index, uint64_t regard_null_as = 0) const;
95  uint8_t GetUint8(size_t column_index, uint8_t regard_null_as = 0) const;
96  uint16_t GetUint16(size_t column_index, uint16_t regard_null_as = 0) const;
97  uint32_t GetUint32(size_t column_index, uint32_t regard_null_as = 0) const;
98  uint64_t GetUint64(size_t column_index, uint64_t regard_null_as = 0) const;
99 
103  float GetFloat(size_t column_index, float regard_null_as = 0.0) const;
104 
108  double GetDouble(size_t column_index, double regard_null_as = 0.0) const;
109 
113  const char *GetString(size_t column_index) const;
114 
118  const char *GetBinary(size_t column_index) const;
119 
123  uint64_t GetFieldLength(size_t column_index) const;
124 
128  WallClock::Value GetDateTime(
129  size_t column_index,
130  const WallClock::Value &regard_null_as = WallClock::kEpochClock) const;
131 
132  const string GetDateTimeString(
133  size_t column_index,
134  const string &regard_null_as = "") const;
135 
139  WallClock::Duration GetTime(
140  size_t column_index,
141  const WallClock::Duration &regard_null_as =
143 
147  bool GetBool(const string &column_name, bool regard_null_as = false) const;
148 
152  int64_t GetInt(const string &column_name, int64_t regard_null_as = 0) const;
153 
157  uint64_t GetUint(const string &column_name, uint64_t regard_null_as = 0) const;
158 
162  float GetFloat(const string &column_name, float regard_null_as = 0.0) const;
163 
167  double GetDouble(const string &column_name, double regard_null_as = 0.0) const;
168 
172  const char *GetString(const string &column_name) const;
173 
177  const char *GetBinary(const string &column_name) const;
178 
182  uint64_t GetFieldLength(const string &column_name) const;
183 
187  WallClock::Value GetDateTime(
188  const string &column_name,
189  const WallClock::Value &regard_null_as = WallClock::kEpochClock) const;
190 
194  WallClock::Duration GetTime(
195  const string &column_name,
196  const WallClock::Duration &regard_null_as =
198 
199  private:
200  Ptr<Impl> impl_;
201 };
202 
203 
204 class MariadbConnection;
205 
206 
208 class FUNAPI_DLL_VISIBILITY Mariadb {
209  public:
210  class Impl;
211 
212  // Contains mysql errors excluding connection errors.
213  // see files below for details.
214  // - /usr/include/mariadb/errmsg.h
215  // - /usr/include/mariadb/mysqld_error.h
216  struct FUNAPI_DLL_VISIBILITY Error {
217  Error();
218 
219  int code;
220  string desc;
221  string sqlstate;
222  };
223 
224  typedef function<
225  void(const Ptr<ResultSets> &/*result_sets*/,
226  const Error &/*error*/)> QueryExecuteHandler;
227  typedef function<
228  void(size_t /*total*/, size_t /*connected*/,
229  const WallClock::Value &/*failure_time*/)> ConnectionFailureHandler;
230 
231  static const string kDefaultConnectionCharset; // ""
232  static const bool kDefaultAutoRetryOnDeadlock; // false
233  static const unsigned long kDefaultClientFlags; // 0
234  static const int64_t kDefaultSlowQueryLoggingTime; // 5000
235  static const ConnectionFailureHandler kDefaultConnectionFailureHandler;
236 
237  static Ptr<Mariadb> Create(
238  const string &address, const string &id, const string &password,
239  const string &database, size_t connection_count,
240  const string &connection_charset = kDefaultConnectionCharset,
241  bool auto_retry_on_deadlock = kDefaultAutoRetryOnDeadlock,
242  unsigned long client_flags = kDefaultClientFlags,
243  int64_t slow_query_logging_time_in_ms = kDefaultSlowQueryLoggingTime,
244  const ConnectionFailureHandler &connection_failure_handler =
245  kDefaultConnectionFailureHandler);
246 
247  void Initialize();
248  void Finalize();
249 
250  void ExecuteQuery(const string &query, const QueryExecuteHandler &handler);
251  Ptr<ResultSets> ExecuteQuerySync(const string &query, Error *error = NULL);
252 
253  Ptr<MariadbConnection> GetExclusiveConnection();
254 
255  const string &address() const;
256  const string &id() const;
257  const string &database() const;
258 
259  size_t connection_count() const;
260  size_t idle_connection_count() const;
261  size_t query_queue_size() const;
262 
263  private:
264  Mariadb(const string &address, const string &id, const string &password,
265  const string &database, size_t connection_count,
266  const string &connection_charset, bool auto_retry_on_deadlock,
267  unsigned long client_flags, int64_t slow_query_logging_time_in_ms,
268  const ConnectionFailureHandler &connection_failure_handler);
269 
270  Ptr<Impl> impl_;
271 };
272 
273 
274 class FUNAPI_DLL_VISIBILITY MariadbConnection
275  : public boost::enable_shared_from_this<MariadbConnection> {
276  public:
278 
279  typedef Mariadb::QueryExecuteHandler QueryExecuteHandler;
280  typedef Mariadb::Error Error;
281 
282  void ExecuteQuery(const string &query, const QueryExecuteHandler &handler);
283  Ptr<ResultSets> ExecuteQuerySync(const string &query, Error *error = NULL);
284 
285  void Release();
286 
287  private:
288  class Impl;
289 
290  friend class Mariadb::Impl;
291 
292  MariadbConnection(const Ptr<Mariadb::Impl> &origin, size_t connection_index);
293  Ptr<Impl> GetImpl();
294 
295  Ptr<Impl> impl_;
296 };
297 
298 
299 FUNAPI_DLL_VISIBILITY
300 std::ostream &operator<<(std::ostream &stream, const Mariadb::Error &error);
301 
302 } // namespace fun
303 
304 #endif // INCLUDE_FUNAPI_DATABASE_MARIADB_H_
Provides query interface for Mariadb and manages connection pool.
Definition: mariadb.h:208
Definition: mariadb.h:274
Contains result sets and provides access interface.
Definition: mariadb.h:29
static const Duration kEmptyDuration
the value of 00:00:00.000000
Definition: wall_clock.h:36
Definition: json.h:18
Definition: mariadb.h:216
static const Value kEpochClock
The value of time_t(0), 1970-01-01 00:00:00.000000 UTC.
Definition: wall_clock.h:33
#define DECLARE_CLASS_PTR(CLS)
Utility macro to forward-declare smart pointer types for a given class.
Definition: types.h:89