MySQL Connector/C++ 9.5.0
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
524 SessionSettings(const string &uri)
525 {
526 try {
527 Settings_detail::set_from_uri(uri);
528 }
529 CATCH_AND_WRAP
530 }
531
532
541 const std::string &host, unsigned port,
542 const string &user,
543 const char *pwd = NULL,
544 const string &db = string()
545 )
546 {
547 set(
548 SessionOption::HOST, host,
549 SessionOption::PORT, port,
550 SessionOption::USER, user
551 );
552
553 if (pwd)
554 set(SessionOption::PWD, std::string(pwd));
555
556 if (!db.empty())
557 set(SessionOption::DB, db);
558 }
559
561 const std::string &host, unsigned port,
562 const string &user,
563 const std::string &pwd,
564 const string &db = string()
565 )
566 : SessionSettings(host, port, user, pwd.c_str(), db)
567 {}
568
577 const std::string &host,
578 const string &user,
579 const char *pwd = NULL,
580 const string &db = string()
581 )
582 : SessionSettings(host, DEFAULT_MYSQLX_PORT, user, pwd, db)
583 {}
584
586 const std::string &host,
587 const string &user,
588 const std::string &pwd,
589 const string &db = string()
590 )
591 : SessionSettings(host, DEFAULT_MYSQLX_PORT, user, pwd, db)
592 {}
593
602 unsigned port,
603 const string &user,
604 const char *pwd = NULL,
605 const string &db = string()
606 )
607 : SessionSettings("localhost", port, user, pwd, db)
608 {}
609
611 unsigned port,
612 const string &user,
613 const std::string &pwd,
614 const string &db = string()
615 )
616 : SessionSettings("localhost", port, user, pwd.c_str(), db)
617 {}
618
619 /*
620 Templates below are here to take care of the optional password
621 parameter of type const char* (which can be either 3-rd or 4-th in
622 the parameter list). Without these templates passing
623 NULL as password is ambiguous because NULL is defined as 0,
624 which has type int, and then it could be treated as port value.
625 */
626
627 template <
628 typename HOST,
629 typename PORT,
630 typename USER,
631 typename... T,
632 typename std::enable_if<
633 std::is_constructible<SessionSettings, HOST, PORT, USER, const char*, T...>::value
634 >::type* = nullptr
635 >
636 SessionSettings(HOST h, PORT p, USER u, long, T... args)
637 : SessionSettings(h, p, u, nullptr, args...)
638 {}
639
640
641 template <
642 typename PORT,
643 typename USER,
644 typename... T,
645 typename std::enable_if<
646 std::is_constructible<SessionSettings, PORT, USER, const char*, T...>::value
647 >::type* = nullptr
648 >
649 SessionSettings(PORT p, USER u, long, T... args)
650 : SessionSettings(p, u, nullptr, args...)
651 {}
652
653
673 template <typename... R>
674 SessionSettings(SessionOption::Enum opt, R&&...rest)
675 {
676 try {
677 // set<true> means that only SessionOption can be used
678 Settings_detail::set<true>(opt, std::forward<R>(rest)...);
679 }
680 CATCH_AND_WRAP
681 }
682
683 /*
684 Return an iterator pointing to the first element of the SessionSettings.
685 */
686 using iterator = internal::iterator<SessionOption, Settings_detail::iterator>;
687
688 iterator begin()
689 {
690 try {
691 return Settings_detail::begin();
692 }
693 CATCH_AND_WRAP
694 }
695
696 /*
697 Return an iterator pointing to the last element of the SessionSettings.
698 */
699
700 iterator end()
701 {
702 try {
703 return Settings_detail::end();
704 }
705 CATCH_AND_WRAP
706 }
707
708
718 Value find(SessionOption opt)
719 {
720 try {
721 return Settings_detail::get(opt);
722 }
723 CATCH_AND_WRAP
724 }
725
741 template<typename... R>
742 void set(SessionOption opt, R&&... rest)
743 {
744 try {
745 // set<true> means that only SessionOption can be used
746 Settings_detail::set<true>(opt, std::forward<R>(rest)...);
747 }
748 CATCH_AND_WRAP
749 }
750
755 void clear()
756 {
757 try {
758 Settings_detail::clear();
759 }
760 CATCH_AND_WRAP
761 }
762
770 void erase(SessionOption opt)
771 {
772 try {
773 Settings_detail::erase(static_cast<int>(opt));
774 }
775 CATCH_AND_WRAP
776 }
777
778
783 bool has_option(SessionOption opt)
784 {
785 try {
786 return Settings_detail::has_option(opt);
787 }
788 CATCH_AND_WRAP
789 }
790
791private:
792
793 friend Client;
794 friend Session;
795};
796
797
808class ClientSettings
809 : private internal::Settings_detail<internal::Settings_traits>
810{
811
812public:
813
814 using Base = internal::Settings_detail<internal::Settings_traits>;
815 using Value = mysqlx::Value;
816
817 /*
818 Return an iterator pointing to the first element of the SessionSettings.
819 */
820
821 using iterator = internal::iterator<ClientOption, Settings_detail::iterator>;
822
823 iterator begin()
824 {
825 try {
826 return Settings_detail::begin();
827 }
828 CATCH_AND_WRAP
829 }
830
831 /*
832 Return an iterator pointing to the last element of the SessionSettings.
833 */
834
835 iterator end()
836 {
837 try {
838 return Settings_detail::end();
839 }
840 CATCH_AND_WRAP
841 }
842
849 ClientSettings(const string &uri)
850 {
851 try {
852 Settings_detail::set_from_uri(uri);
853 }
854 CATCH_AND_WRAP
855 }
856
863 ClientSettings(const string &uri, ClientSettings &opts)
864 {
865 try {
866 Settings_detail::set_from_uri(uri);
867 Settings_detail::set_client_opts(opts);
868 }
869 CATCH_AND_WRAP
870 }
871
905 ClientSettings(const string &uri, const DbDoc &options)
906 {
907 try {
908 Settings_detail::set_from_uri(uri);
909 std::stringstream str_opts;
910 str_opts << options;
911 Settings_detail::set_client_opts(str_opts.str());
912 }
913 CATCH_AND_WRAP
914 }
915
949 ClientSettings(const string &uri, const char *options)
950 {
951 try {
952 Settings_detail::set_from_uri(uri);
953 Settings_detail::set_client_opts(options);
954 }
955 CATCH_AND_WRAP
956 }
957
982 template<typename...R>
983 ClientSettings(const string &uri, mysqlx::ClientOption opt, R... rest)
984 try
985 : ClientSettings(uri)
986 {
987 // set<false> means that both SessionOption and ClientOption can be used
988 Settings_detail::set<false>(opt, std::forward<R>(rest)...);
989 }
990 CATCH_AND_WRAP
991
992
993 template <typename... R>
994 ClientSettings(mysqlx::ClientOption opt, R&&...rest)
995 {
996 try {
997 // set<false> means that both SessionOption and ClientOption can be used
998 Settings_detail::set<false>(opt, std::forward<R>(rest)...);
999 }
1000 CATCH_AND_WRAP
1001 }
1002
1010 Value find(mysqlx::ClientOption opt)
1011 {
1012 try {
1013 return Settings_detail::get(opt);
1014 }
1015 CATCH_AND_WRAP
1016 }
1017
1034 template<typename OPT, typename... R>
1035 void set(OPT opt, R&&... rest)
1036 {
1037 try {
1038 // set<false> means that both SessionOption and ClientOption can be used
1039 Settings_detail::set<false>(opt, std::forward<R>(rest)...);
1040 }
1041 CATCH_AND_WRAP
1042 }
1043
1044
1049 void clear()
1050 {
1051 try {
1052 Settings_detail::clear();
1053 }
1054 CATCH_AND_WRAP
1055 }
1056
1061 void erase(mysqlx::ClientOption opt)
1062 {
1063 try {
1064 Settings_detail::erase(static_cast<int>(opt));
1065 }
1066 CATCH_AND_WRAP
1067 }
1068
1069
1075 {
1076 try {
1077 return Settings_detail::has_option(opt);
1078 }
1079 CATCH_AND_WRAP
1080 }
1081
1086 bool has_option(SessionOption::Enum opt)
1087 {
1088 try {
1089 return Settings_detail::has_option(opt);
1090 }
1091 CATCH_AND_WRAP
1092 }
1093
1094private:
1095 friend Client;
1096 friend Session;
1097};
1098
1099
1100MYSQLX_ABI_END(2,0)
1101} // mysqlx
1102
1103#endif
Client creation options.
Definition: settings.h:117
void set(OPT opt, R &&... rest)
Set client and session options.
Definition: settings.h:1032
bool has_option(ClientOption::Enum opt)
Check if option opt was defined.
Definition: settings.h:1071
void erase(mysqlx::ClientOption opt)
Remove the given option opt.
Definition: settings.h:1058
Value find(mysqlx::ClientOption opt)
Find the specified option opt and returns its Value.
Definition: settings.h:1007
ClientSettings(const string &uri)
Create client settings from a connection string.
Definition: settings.h:846
void clear()
Clear all settings specified so far.
Definition: settings.h:1046
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:780
Value find(SessionOption opt)
Find the specified option opt and returns its Value.
Definition: settings.h:715
SessionSettings(const string &uri)
Create session settings from a connection string.
Definition: settings.h:521
void erase(SessionOption opt)
Remove all settings for the given option opt.
Definition: settings.h:767
void set(SessionOption opt, R &&... rest)
Set session options.
Definition: settings.h:739
void clear()
Clear all settings specified so far.
Definition: settings.h:752
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