MySQL Connector/C++
MySQL connector library for C and C++ applications
settings.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2024, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0, as
6 * published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms, as
10 * designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * https://oss.oracle.com/licenses/universal-foss-exception.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License, version 2.0, for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 */
30
31#ifndef MYSQLX_DEVAPI_SETTINGS_H
32#define MYSQLX_DEVAPI_SETTINGS_H
33
39#include "common.h"
40#include "detail/settings.h"
41
42
43namespace mysqlx {
44MYSQLX_ABI_BEGIN(2,0)
45
46
47/*
48 TODO: Cross-references to session options inside Doxygen docs do not work.
49*/
50
51
63{
64#define SESS_OPT_ENUM_any(X,N) X = N,
65#define SESS_OPT_ENUM_bool(X,N) X = N,
66#define SESS_OPT_ENUM_num(X,N) X = N,
67#define SESS_OPT_ENUM_str(X,N) X = N,
68#define SESS_OPT_ENUM_bool(X,N) X = N,
69
70public:
71
73
74 enum Enum {
75 SESSION_OPTION_LIST(SESS_OPT_ENUM)
76 LAST
77 };
78
79 SessionOption(Enum opt)
80 : m_opt(opt)
81 {}
82
84 {}
85
86 bool operator==(const SessionOption &other) const
87 {
88 return m_opt == other.m_opt;
89 }
90
91 bool operator==(Enum opt) const
92 {
93 return m_opt == opt;
94 }
95
96 bool operator!=(Enum opt) const
97 {
98 return m_opt != opt;
99 }
100
101 operator int()
102 {
103 return m_opt;
104 }
105
106protected:
107 int m_opt = LAST;
108};
109
110
116 : protected SessionOption
117{
118
119#define CLIENT_OPT_ENUM_any(X,N) X = -N,
120#define CLIENT_OPT_ENUM_bool(X,N) X = -N,
121#define CLIENT_OPT_ENUM_num(X,N) X = -N,
122#define CLIENT_OPT_ENUM_str(X,N) X = -N,
123
124
125public:
126
127 using SessionEnum = SessionOption::Enum;
128
130
131 enum Enum {
132 CLIENT_OPTION_LIST(CLIENT_OPT_ENUM)
133 };
134
136 {}
137
139 : SessionOption(opt)
140 {}
141
142 ClientOption(Enum opt)
143 {
144 m_opt = opt;
145 }
146
147 ClientOption(SessionEnum opt)
148 : SessionOption(opt)
149 {}
150
151 bool operator==(Enum opt) const
152 {
153 return m_opt == opt;
154 }
155
156 bool operator==(SessionEnum opt) const
157 {
158 return SessionOption::operator==(opt);
159 }
160
161 bool operator!=(Enum opt) const
162 {
163 return m_opt != opt;
164 }
165
166 bool operator!=(SessionEnum opt) const
167 {
168 return SessionOption::operator!=(opt);
169 }
170
171 inline operator int()
172 {
173 return SessionOption::operator int();
174 }
175
176};
177
178
180// Note: Doxygen gets confused here and renders docs incorrectly.
181
182inline
183std::string OptionName(ClientOption opt)
184{
185#define CLT_OPT_NAME_any(X,N) case ClientOption::X: return #X;
186#define CLT_OPT_NAME_bool(X,N) CLT_OPT_NAME_any(X,N)
187#define CLT_OPT_NAME_num(X,N) CLT_OPT_NAME_any(X,N)
188#define CLT_OPT_NAME_str(X,N) CLT_OPT_NAME_any(X,N)
189
190#define SESS_OPT_NAME_any(X,N) case SessionOption::X: return #X;
191#define SESS_OPT_NAME_bool(X,N) SESS_OPT_NAME_any(X,N)
192#define SESS_OPT_NAME_num(X,N) SESS_OPT_NAME_any(X,N)
193#define SESS_OPT_NAME_str(X,N) SESS_OPT_NAME_any(X,N)
194#define SESS_OPT_NAME_bool(X,N) SESS_OPT_NAME_any(X,N)
195
196
197 switch (opt)
198 {
199 CLIENT_OPTION_LIST(CLT_OPT_NAME)
200 SESSION_OPTION_LIST(SESS_OPT_NAME)
201
202 default:
203 throw_error("Unexpected Option"); return "";
204 };
205}
206
208
209
210inline std::string ClientOptionName(ClientOption opt)
211{
212 return OptionName(opt);
213}
214
215inline std::string SessionOptionName(SessionOption opt)
216{
217 return OptionName(opt);
218}
219
220
226enum_class SSLMode
227{
228#define SSL_ENUM(X,N) X = N,
229
230 SSL_MODE_LIST(SSL_ENUM)
231};
232
233
235
236inline
237std::string SSLModeName(SSLMode m)
238{
239#define MODE_NAME(X,N) case SSLMode::X: return #X;
240
241 switch (m)
242 {
243 SSL_MODE_LIST(MODE_NAME)
244 default:
245 {
246 std::ostringstream buf;
247 buf << "<UKNOWN (" << unsigned(m) << ")>" << std::ends;
248 return buf.str();
249 }
250 };
251}
252
254
255
261enum_class AuthMethod
262{
263#define AUTH_ENUM(X,N) X=N,
265 AUTH_METHOD_LIST(AUTH_ENUM)
266};
267
268
270
271inline
272std::string AuthMethodName(AuthMethod m)
273{
274#define AUTH_NAME(X,N) case AuthMethod::X: return #X;
275
276 switch (m)
277 {
278 AUTH_METHOD_LIST(AUTH_NAME)
279 default:
280 {
281 std::ostringstream buf;
282 buf << "<UKNOWN (" << unsigned(m) << ")>" << std::ends;
283 return buf.str();
284 }
285 };
286}
287
289
295enum_class CompressionMode
296{
297#define COMPRESSION_ENUM(X,N) X = N,
298
299 COMPRESSION_MODE_LIST(COMPRESSION_ENUM)
300};
301
303
304inline
305std::string CompressionModeName(CompressionMode m)
306{
307#define COMPRESSION_NAME(X,N) case CompressionMode::X: return #X;
308
309 switch (m)
310 {
311 COMPRESSION_MODE_LIST(COMPRESSION_NAME)
312 default:
313 {
314 std::ostringstream buf;
315 buf << "<UKNOWN (" << unsigned(m) << ")>" << std::ends;
316 return buf.str();
317 }
318 };
319}
320
322
323
324namespace internal {
325
326
327/*
328 Encapsulate public enumerations in the Settings_traits class to be used
329 by Settings_detail<> template.
330*/
331
332struct Settings_traits
333{
334 using Options = mysqlx::SessionOption;
335 using COptions = mysqlx::ClientOption;
336 using SSLMode = mysqlx::SSLMode;
337 using AuthMethod = mysqlx::AuthMethod;
338 using CompressionMode = mysqlx::CompressionMode;
339
340 static std::string get_mode_name(SSLMode mode)
341 {
342 return SSLModeName(mode);
343 }
344
345 static std::string get_option_name(COptions opt)
346 {
347 return ClientOptionName(opt);
348 }
349
350 static std::string get_auth_name(AuthMethod m)
351 {
352 return AuthMethodName(m);
353 }
354};
355
356
357template<>
358PUBLIC_API
359void
360internal::Settings_detail<internal::Settings_traits>::
361do_set(session_opt_list_t &&opts);
362
363
364template<typename Option, typename base_iterator>
365class iterator
366{
367 base_iterator m_it;
368 std::pair<Option, mysqlx::Value> m_pair;
369
370public:
371 using iterator_category = std::input_iterator_tag;
372 using value_type = std::pair<Option, mysqlx::Value>;
373
374 iterator(const base_iterator &init) : m_it(init) {}
375
376 iterator(const iterator &init) : m_it(init.m_it) {}
377
378 std::pair<Option, mysqlx::Value> &operator*() {
379 auto &el = *m_it;
380 m_pair.first = static_cast<typename Option::Enum>(el.first);
381 m_pair.second = el.second;
382 return m_pair;
383 }
384
385 std::pair<ClientOption, mysqlx::Value>* operator->()
386 {
387 return &operator*();
388 }
389
390 iterator& operator++()
391 {
392 ++m_it;
393 return *this;
394 }
395
396 iterator& operator--()
397 {
398 --m_it;
399 return *this;
400 }
401
402 bool operator !=(const iterator &other)
403 {
404 return m_it != other.m_it;
405 }
406
407};
408
409
410} // internal namespace
411
412
413class Client;
414class Session;
415
451class SessionSettings
452 : private internal::Settings_detail<internal::Settings_traits>
453{
454 using Value = mysqlx::Value;
455
456public:
457
522 SessionSettings(const string &uri)
523 {
524 try {
525 Settings_detail::set_from_uri(uri);
526 }
527 CATCH_AND_WRAP
528 }
529
530
539 const std::string &host, unsigned port,
540 const string &user,
541 const char *pwd = NULL,
542 const string &db = string()
543 )
544 {
545 set(
546 SessionOption::HOST, host,
547 SessionOption::PORT, port,
548 SessionOption::USER, user
549 );
550
551 if (pwd)
552 set(SessionOption::PWD, std::string(pwd));
553
554 if (!db.empty())
555 set(SessionOption::DB, db);
556 }
557
559 const std::string &host, unsigned port,
560 const string &user,
561 const std::string &pwd,
562 const string &db = string()
563 )
564 : SessionSettings(host, port, user, pwd.c_str(), db)
565 {}
566
575 const std::string &host,
576 const string &user,
577 const char *pwd = NULL,
578 const string &db = string()
579 )
580 : SessionSettings(host, DEFAULT_MYSQLX_PORT, user, pwd, db)
581 {}
582
584 const std::string &host,
585 const string &user,
586 const std::string &pwd,
587 const string &db = string()
588 )
589 : SessionSettings(host, DEFAULT_MYSQLX_PORT, user, pwd, db)
590 {}
591
600 unsigned port,
601 const string &user,
602 const char *pwd = NULL,
603 const string &db = string()
604 )
605 : SessionSettings("localhost", port, user, pwd, db)
606 {}
607
609 unsigned port,
610 const string &user,
611 const std::string &pwd,
612 const string &db = string()
613 )
614 : SessionSettings("localhost", port, user, pwd.c_str(), db)
615 {}
616
617 /*
618 Templates below are here to take care of the optional password
619 parameter of type const char* (which can be either 3-rd or 4-th in
620 the parameter list). Without these templates passing
621 NULL as password is ambiguous because NULL is defined as 0,
622 which has type int, and then it could be treated as port value.
623 */
624
625 template <
626 typename HOST,
627 typename PORT,
628 typename USER,
629 typename... T,
630 typename std::enable_if<
631 std::is_constructible<SessionSettings, HOST, PORT, USER, const char*, T...>::value
632 >::type* = nullptr
633 >
634 SessionSettings(HOST h, PORT p, USER u, long, T... args)
635 : SessionSettings(h, p, u, nullptr, args...)
636 {}
637
638
639 template <
640 typename PORT,
641 typename USER,
642 typename... T,
643 typename std::enable_if<
644 std::is_constructible<SessionSettings, PORT, USER, const char*, T...>::value
645 >::type* = nullptr
646 >
647 SessionSettings(PORT p, USER u, long, T... args)
648 : SessionSettings(p, u, nullptr, args...)
649 {}
650
651
671 template <typename... R>
672 SessionSettings(SessionOption::Enum opt, R&&...rest)
673 {
674 try {
675 // set<true> means that only SessionOption can be used
676 Settings_detail::set<true>(opt, std::forward<R>(rest)...);
677 }
678 CATCH_AND_WRAP
679 }
680
681 /*
682 Return an iterator pointing to the first element of the SessionSettings.
683 */
684 using iterator = internal::iterator<SessionOption, Settings_detail::iterator>;
685
686 iterator begin()
687 {
688 try {
689 return Settings_detail::begin();
690 }
691 CATCH_AND_WRAP
692 }
693
694 /*
695 Return an iterator pointing to the last element of the SessionSettings.
696 */
697
698 iterator end()
699 {
700 try {
701 return Settings_detail::end();
702 }
703 CATCH_AND_WRAP
704 }
705
706
716 Value find(SessionOption opt)
717 {
718 try {
719 return Settings_detail::get(opt);
720 }
721 CATCH_AND_WRAP
722 }
723
739 template<typename... R>
740 void set(SessionOption opt, R&&... rest)
741 {
742 try {
743 // set<true> means that only SessionOption can be used
744 Settings_detail::set<true>(opt, std::forward<R>(rest)...);
745 }
746 CATCH_AND_WRAP
747 }
748
753 void clear()
754 {
755 try {
756 Settings_detail::clear();
757 }
758 CATCH_AND_WRAP
759 }
760
768 void erase(SessionOption opt)
769 {
770 try {
771 Settings_detail::erase(static_cast<int>(opt));
772 }
773 CATCH_AND_WRAP
774 }
775
776
781 bool has_option(SessionOption opt)
782 {
783 try {
784 return Settings_detail::has_option(opt);
785 }
786 CATCH_AND_WRAP
787 }
788
789private:
790
791 friend Client;
792 friend Session;
793};
794
795
806class ClientSettings
807 : private internal::Settings_detail<internal::Settings_traits>
808{
809
810public:
811
812 using Base = internal::Settings_detail<internal::Settings_traits>;
813 using Value = mysqlx::Value;
814
815 /*
816 Return an iterator pointing to the first element of the SessionSettings.
817 */
818
819 using iterator = internal::iterator<ClientOption, Settings_detail::iterator>;
820
821 iterator begin()
822 {
823 try {
824 return Settings_detail::begin();
825 }
826 CATCH_AND_WRAP
827 }
828
829 /*
830 Return an iterator pointing to the last element of the SessionSettings.
831 */
832
833 iterator end()
834 {
835 try {
836 return Settings_detail::end();
837 }
838 CATCH_AND_WRAP
839 }
840
847 ClientSettings(const string &uri)
848 {
849 try {
850 Settings_detail::set_from_uri(uri);
851 }
852 CATCH_AND_WRAP
853 }
854
861 ClientSettings(const string &uri, ClientSettings &opts)
862 {
863 try {
864 Settings_detail::set_from_uri(uri);
865 Settings_detail::set_client_opts(opts);
866 }
867 CATCH_AND_WRAP
868 }
869
903 ClientSettings(const string &uri, const DbDoc &options)
904 {
905 try {
906 Settings_detail::set_from_uri(uri);
907 std::stringstream str_opts;
908 str_opts << options;
909 Settings_detail::set_client_opts(str_opts.str());
910 }
911 CATCH_AND_WRAP
912 }
913
947 ClientSettings(const string &uri, const char *options)
948 {
949 try {
950 Settings_detail::set_from_uri(uri);
951 Settings_detail::set_client_opts(options);
952 }
953 CATCH_AND_WRAP
954 }
955
980 template<typename...R>
981 ClientSettings(const string &uri, mysqlx::ClientOption opt, R... rest)
982 try
983 : ClientSettings(uri)
984 {
985 // set<false> means that both SessionOption and ClientOption can be used
986 Settings_detail::set<false>(opt, std::forward<R>(rest)...);
987 }
988 CATCH_AND_WRAP
989
990
991 template <typename... R>
992 ClientSettings(mysqlx::ClientOption opt, R&&...rest)
993 {
994 try {
995 // set<false> means that both SessionOption and ClientOption can be used
996 Settings_detail::set<false>(opt, std::forward<R>(rest)...);
997 }
998 CATCH_AND_WRAP
999 }
1000
1008 Value find(mysqlx::ClientOption opt)
1009 {
1010 try {
1011 return Settings_detail::get(opt);
1012 }
1013 CATCH_AND_WRAP
1014 }
1015
1032 template<typename OPT, typename... R>
1033 void set(OPT opt, R&&... rest)
1034 {
1035 try {
1036 // set<false> means that both SessionOption and ClientOption can be used
1037 Settings_detail::set<false>(opt, std::forward<R>(rest)...);
1038 }
1039 CATCH_AND_WRAP
1040 }
1041
1042
1047 void clear()
1048 {
1049 try {
1050 Settings_detail::clear();
1051 }
1052 CATCH_AND_WRAP
1053 }
1054
1059 void erase(mysqlx::ClientOption opt)
1060 {
1061 try {
1062 Settings_detail::erase(static_cast<int>(opt));
1063 }
1064 CATCH_AND_WRAP
1065 }
1066
1067
1073 {
1074 try {
1075 return Settings_detail::has_option(opt);
1076 }
1077 CATCH_AND_WRAP
1078 }
1079
1084 bool has_option(SessionOption::Enum opt)
1085 {
1086 try {
1087 return Settings_detail::has_option(opt);
1088 }
1089 CATCH_AND_WRAP
1090 }
1091
1092private:
1093 friend Client;
1094 friend Session;
1095};
1096
1097
1098MYSQLX_ABI_END(2,0)
1099} // mysqlx
1100
1101#endif
Client creation options.
Definition: settings.h:117
void set(OPT opt, R &&... rest)
Set client and session options.
Definition: settings.h:1030
bool has_option(ClientOption::Enum opt)
Check if option opt was defined.
Definition: settings.h:1069
void erase(mysqlx::ClientOption opt)
Remove the given option opt.
Definition: settings.h:1056
Value find(mysqlx::ClientOption opt)
Find the specified option opt and returns its Value.
Definition: settings.h:1005
ClientSettings(const string &uri)
Create client settings from a connection string.
Definition: settings.h:844
void clear()
Clear all settings specified so far.
Definition: settings.h:1044
Session creation options.
Definition: settings.h:63
Enum
Possible session creation options.
Definition: settings.h:74
bool has_option(SessionOption opt)
Check if option opt was defined.
Definition: settings.h:778
Value find(SessionOption opt)
Find the specified option opt and returns its Value.
Definition: settings.h:713
SessionSettings(const string &uri)
Create session settings from a connection string.
Definition: settings.h:519
void erase(SessionOption opt)
Remove all settings for the given option opt.
Definition: settings.h:765
void set(SessionOption opt, R &&... rest)
Set session options.
Definition: settings.h:737
void clear()
Clear all settings specified so far.
Definition: settings.h:750
Value object can store value of scalar type, string, array or document.
Definition: document.h:230
SSLMode
Modes to be used with SSL_MODE option .
Definition: settings.h:227