The Battle for Wesnoth  1.15.3+dev
account.hpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright Sylvain Rochette Langlois 2013,
5 // The ViaDuck Project 2016 - 2018.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #ifndef _MARIADB_ACCOUNT_HPP_
11 #define _MARIADB_ACCOUNT_HPP_
12 
13 #include <string>
14 #include <map>
15 #include <mariadb++/types.hpp>
16 #include <mysql.h>
17 
18 namespace mariadb {
19 class account;
20 typedef std::shared_ptr<account> account_ref;
21 
22 class option_arg {
23  public:
24  virtual ~option_arg() = default;
25  virtual const void *value() = 0;
26 };
27 
28 #define MAKE_OPTION_ARG(name, type, return_value) \
29 class option_arg_##name : public option_arg { \
30  public: \
31  explicit option_arg_##name(const type &arg) : m_value(arg) { } \
32  const void *value() override { return return_value; } \
33  protected: \
34  type m_value; \
35 }
36 
37 MAKE_OPTION_ARG(bool, bool, &m_value);
38 MAKE_OPTION_ARG(int, int, &m_value);
39 MAKE_OPTION_ARG(string, std::string, m_value.c_str());
40 
41 /**
42  * Class used to store account and connection information used by mariadb::connection when
43  * connecting.
44  * Note that modifying an account after the connection was established is useless.
45  */
46 class account {
47  public:
48  typedef std::map<std::string, std::string> map_options_t;
49  typedef std::map<mysql_option, std::unique_ptr<option_arg>> map_connect_options_t;
50 
51  public:
52  /**
53  * Destructs the account
54  */
55  virtual ~account() {}
56 
57  /**
58  * Gets the name of the host to connect to
59  */
60  const std::string &host_name() const;
61 
62  /**
63  * Gets the username to log in with
64  */
65  const std::string &user_name() const;
66 
67  /**
68  * Gets the password of the user to log in with
69  */
70  const std::string &password() const;
71 
72  /**
73  * Gets the unix socket path to connect to.
74  * If this option is set, host and port will be ignored
75  */
76  const std::string &unix_socket() const;
77 
78  /**
79  * Gets the path to the key file
80  */
81  const std::string &ssl_key() const;
82 
83  /**
84  * Gets the path to the certificate file
85  */
86  const std::string &ssl_certificate() const;
87 
88  /**
89  * Gets the path to the certificate authority file
90  */
91  const std::string &ssl_ca() const;
92 
93  /**
94  * Gets the path to the directory containing CA files
95  */
96  const std::string &ssl_ca_path() const;
97 
98  /**
99  * Gets the list of allowed SSL ciphers
100  */
101  const std::string &ssl_cipher() const;
102 
103  /**
104  * Gets the port to connect to
105  */
106  u32 port() const;
107 
108  /**
109  * Gets the name of the database to open on connect
110  */
111  const std::string &schema() const;
112 
113  /**
114  * Sets the name of the database to open on connect
115  */
116  void set_schema(const std::string &schema);
117 
118  /**
119  * Set SSL options. All files should be PEM format
120  *
121  * @param key Path to the key file
122  * @param certificate Path to the certificate file
123  * @param ca Path to the certificate authority file
124  * @param ca_path Path to a directory containing CA files
125  * @param cipher List of allowed SSL ciphers. See MariaDB manual for possible values
126  */
127  void set_ssl(const std::string &key, const std::string &certificate, const std::string &ca,
128  const std::string &ca_path, const std::string &cipher);
129 
130  /**
131  * Gets the current state of the auto_commit option. This option is turned on by default.
132  */
133  bool auto_commit() const;
134 
135  /**
136  * Sets the state of the auto_commit option.
137  */
138  void set_auto_commit(bool auto_commit);
139 
140  /**
141  * Gets the current state of the store_result option. When set, the connection uses buffered store
142  * (mysql_store_result) instead of unbuffered store (mysql_use_result). This option is turned on by default.
143  *
144  * Note: Unbuffered store has some restrictions and might lead to unexpected behavior. See the documentation
145  * (https://mariadb.com/kb/en/library/mysql_use_result/) for more information.
146  */
147  bool store_result() const;
148 
149  /**
150  * Sets the state of the store_result option.
151  */
152  void set_store_result(bool store_result);
153 
154  /**
155  * Gets the current value of any named option that was previously set
156  *
157  * @return Value of the found option or empty string if not found
158  */
159  const std::string option(const std::string &name) const;
160 
161  /**
162  * Gets a map of all option key/value pairs previously set
163  */
164  const map_options_t &options() const;
165 
166  /**
167  * Sets a named option key/value pair
168  */
169  void set_option(const std::string &name, const std::string &value);
170 
171  /**
172  * Deletes all stored key/value pairs of named options
173  */
174  void clear_options();
175 
176  /**
177  * Gets a map of all connect option key/value pairs previously set
178  */
179  const map_connect_options_t &connect_options() const;
180 
181  /**
182  * Sets a connect option key/value pair with bool argument
183  */
184  void set_connect_option(mysql_option option, bool arg);
185  /**
186  * Sets a connect option key/value pair with int argument
187  */
188  void set_connect_option(mysql_option option, int arg);
189  /**
190  * Sets a connect option key/value pair with string argument
191  */
192  void set_connect_option(mysql_option option, const std::string &arg);
193 
194  /**
195  * Deletes all stored key/value pairs of named options
196  */
197  void clear_connect_options();
198 
199  /**
200  * Create an account
201  *
202  * @param host_name Hostname to connect to
203  * @param user_name Username to log in with
204  * @param password Password for the user to log in with (may be empty)
205  * @param schema Database name to select on connect. Can also be set after connecting
206  * @param port Port of host to connect to (defaults to 3306)
207  * @param unix_sock Path of unix socket to connect to. If specified, host and port will be
208  * ignored
209  */
210  static account_ref create(const std::string &host_name, const std::string &user_name,
211  const std::string &password, const std::string &schema = "",
212  u32 port = 3306, const std::string &unix_socket = "");
213 
214  private:
215  /**
216  * Private account constructor
217  */
218  account(const std::string &host_name, const std::string &user_name, const std::string &password,
219  const std::string &schema, u32 port, const std::string &unix_sock);
220 
221  bool m_auto_commit = true;
222  bool m_store_result = true;
234  map_options_t m_options;
235  map_connect_options_t m_connect_options;
236 };
237 }
238 
239 #endif
std::string m_ssl_ca
Definition: account.hpp:231
std::string m_schema
Definition: account.hpp:227
std::shared_ptr< account > account_ref
Definition: account.hpp:19
std::string m_password
Definition: account.hpp:226
std::string m_ssl_key
Definition: account.hpp:229
virtual ~option_arg()=default
std::string m_host_name
Definition: account.hpp:224
std::map< mysql_option, std::unique_ptr< option_arg > > map_connect_options_t
Definition: account.hpp:49
Class used to store account and connection information used by mariadb::connection when connecting...
Definition: account.hpp:46
const config & options()
Definition: game.cpp:592
MAKE_OPTION_ARG(bool, bool, &m_value)
std::string m_unix_socket
Definition: account.hpp:228
std::string m_ssl_certificate
Definition: account.hpp:230
map_options_t m_options
Definition: account.hpp:234
map_connect_options_t m_connect_options
Definition: account.hpp:235
std::string m_ssl_ca_path
Definition: account.hpp:232
std::string password(const std::string &server, const std::string &login)
virtual ~account()
Destructs the account.
Definition: account.hpp:55
std::map< std::string, std::string > map_options_t
Definition: account.hpp:48
std::string m_user_name
Definition: account.hpp:225
std::string m_ssl_cipher
Definition: account.hpp:233
virtual const void * value()=0
unsigned int u32
Definition: types.hpp:22