settings.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  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 also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://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. #ifndef MYSQLX_DEVAPI_SETTINGS_H
  31. #define MYSQLX_DEVAPI_SETTINGS_H
  32. /**
  33. @file
  34. TODO
  35. */
  36. #include "common.h"
  37. #include "detail/settings.h"
  38. namespace mysqlx {
  39. /**
  40. Client creation options
  41. */
  42. enum_class ClientOption
  43. {
  44. #define CLIENT_OPT_ENUM_any(X,N) X = N,
  45. #define CLIENT_OPT_ENUM_num(X,N) X = N,
  46. #define CLIENT_OPT_ENUM_str(X,N) X = N,
  47. #define CLIENT_OPT_ENUM_end(X,N) X = N,
  48. CLIENT_OPTION_LIST(CLIENT_OPT_ENUM)
  49. };
  50. /// @cond DISABLED
  51. // Note: Doxygen gets confused here and renders docs incorrectly.
  52. inline
  53. std::string ClientOptionName(ClientOption opt)
  54. {
  55. #define CLT_OPT_NAME_any(X,N) case ClientOption::X: return #X;
  56. #define CLT_OPT_NAME_bool(X,N) CLT_OPT_NAME_any(X,N)
  57. #define CLT_OPT_NAME_num(X,N) CLT_OPT_NAME_any(X,N)
  58. #define CLT_OPT_NAME_str(X,N) CLT_OPT_NAME_any(X,N)
  59. #define CLT_OPT_NAME_end(X,N) // let it use the default
  60. switch(opt)
  61. {
  62. CLIENT_OPTION_LIST(CLT_OPT_NAME)
  63. default:
  64. {
  65. std::ostringstream buf;
  66. buf << "<UKNOWN (" << unsigned(opt) << ")>" << std::ends;
  67. return buf.str();
  68. }
  69. };
  70. }
  71. /// @endcond
  72. /*
  73. TODO: Cross-references to session options inside Doxygen docs do not work.
  74. */
  75. /**
  76. Session creation options
  77. @note `PRIORITY` should be defined after a `HOST` (`PORT`) to which
  78. it applies.
  79. @note Specifying `SSL_CA` option requires `SSL_MODE` value of `VERIFY_CA`
  80. or `VERIFY_IDENTITY`. If `SSL_MODE` is not explicitly given then
  81. setting `SSL_CA` implies `VERIFY_CA`.
  82. */
  83. enum_class SessionOption
  84. {
  85. #define SESS_OPT_ENUM_any(X,N) X = N,
  86. #define SESS_OPT_ENUM_num(X,N) X = N,
  87. #define SESS_OPT_ENUM_str(X,N) X = N,
  88. SESSION_OPTION_LIST(SESS_OPT_ENUM)
  89. LAST
  90. };
  91. /// @cond DISABLED
  92. // Note: Doxygen gets confused here and renders docs incorrectly.
  93. inline
  94. std::string SessionOptionName(SessionOption opt)
  95. {
  96. #define SESS_OPT_NAME_any(X,N) case SessionOption::X: return #X;
  97. #define SESS_OPT_NAME_num(X,N) SESS_OPT_NAME_any(X,N)
  98. #define SESS_OPT_NAME_str(X,N) SESS_OPT_NAME_any(X,N)
  99. switch(opt)
  100. {
  101. SESSION_OPTION_LIST(SESS_OPT_NAME)
  102. default:
  103. {
  104. std::ostringstream buf;
  105. buf << "<UKNOWN (" << unsigned(opt) << ")>" << std::ends;
  106. return buf.str();
  107. }
  108. };
  109. }
  110. /// @endcond
  111. /**
  112. Modes to be used with `SSL_MODE` option
  113. */
  114. enum_class SSLMode
  115. {
  116. #define SSL_ENUM(X,N) X = N,
  117. SSL_MODE_LIST(SSL_ENUM)
  118. };
  119. /// @cond DISABLED
  120. inline
  121. std::string SSLModeName(SSLMode m)
  122. {
  123. #define MODE_NAME(X,N) case SSLMode::X: return #X;
  124. switch(m)
  125. {
  126. SSL_MODE_LIST(MODE_NAME)
  127. default:
  128. {
  129. std::ostringstream buf;
  130. buf << "<UKNOWN (" << unsigned(m) << ")>" << std::ends;
  131. return buf.str();
  132. }
  133. };
  134. }
  135. /// @endcond
  136. /**
  137. Authentication methods to be used with `AUTH` option.
  138. */
  139. enum_class AuthMethod
  140. {
  141. #define AUTH_ENUM(X,N) X=N,
  142. AUTH_METHOD_LIST(AUTH_ENUM)
  143. };
  144. /// @cond DISABLED
  145. inline
  146. std::string AuthMethodName(AuthMethod m)
  147. {
  148. #define AUTH_NAME(X,N) case AuthMethod::X: return #X;
  149. switch(m)
  150. {
  151. AUTH_METHOD_LIST(AUTH_NAME)
  152. default:
  153. {
  154. std::ostringstream buf;
  155. buf << "<UKNOWN (" << unsigned(m) << ")>" << std::ends;
  156. return buf.str();
  157. }
  158. };
  159. }
  160. /// @endcond
  161. namespace internal {
  162. /*
  163. Encapsulate public enumerations in the Settings_traits class to be used
  164. by Settings_detail<> template.
  165. */
  166. struct Settings_traits
  167. {
  168. using Options = mysqlx::SessionOption;
  169. using CliOptions = mysqlx::ClientOption;
  170. using SSLMode = mysqlx::SSLMode;
  171. using AuthMethod = mysqlx::AuthMethod;
  172. static std::string get_mode_name(SSLMode mode)
  173. {
  174. return SSLModeName(mode);
  175. }
  176. static std::string get_option_name(Options opt)
  177. {
  178. return SessionOptionName(opt);
  179. }
  180. static std::string get_auth_name(AuthMethod m)
  181. {
  182. return AuthMethodName(m);
  183. }
  184. };
  185. template<>
  186. PUBLIC_API
  187. void
  188. internal::Settings_detail<internal::Settings_traits>::
  189. do_set(session_opt_list_t &&opts);
  190. } // internal namespace
  191. class Client;
  192. class Session;
  193. /**
  194. Represents session options to be passed at session creation time.
  195. SessionSettings can be constructed using a connection string, common
  196. connect options (host, port, user, password, database) or with a list
  197. of `SessionOption` constants, each followed by the option value.
  198. Examples:
  199. ~~~~~~
  200. SessionSettings from_url("mysqlx://user:pwd@host:port/db?ssl-mode=required");
  201. SessionSettings from_options("host", port, "user", "pwd", "db");
  202. SessionSettings from_option_list(
  203. SessionOption::USER, "user",
  204. SessionOption::PWD, "pwd",
  205. SessionOption::HOST, "host",
  206. SessionOption::PORT, port,
  207. SessionOption::DB, "db",
  208. SessionOption::SSL_MODE, SSLMode::REQUIRED
  209. );
  210. ~~~~~~
  211. The HOST, PORT and SOCKET settings can be repeated to build a list of hosts
  212. to be used by the connection fail-over logic when creating a session (see
  213. description of `Session` class). In that case each host can be assigned
  214. a priority by setting the `PRIORITY` option. If priorities are not explicitly
  215. assigned, hosts are tried in the order in which they are specified in session
  216. settings. If priorities are used, they must be assigned to all hosts
  217. specified in the settings.
  218. @ingroup devapi
  219. */
  220. class SessionSettings
  221. : private internal::Settings_detail<internal::Settings_traits>
  222. {
  223. using Value = mysqlx::Value;
  224. public:
  225. /**
  226. Create session settings from a connection string.
  227. Connection sting has the form
  228. "user:pass@connection-data/db?option&option"
  229. with optional `mysqlx://` prefix.
  230. The `connetction-data` part is either a single host address or a coma
  231. separated list of hosts in square brackets: `[host1, host2, ..., hostN]`.
  232. In the latter case the connection fail-over logic will be used when
  233. creating the session.
  234. A single host address is either a DNS host name, an IPv4 address of
  235. the form "nn.nn.nn.nn" or an IPv6 address of the form "[nn:nn:nn:...]".
  236. On Unix systems a host can be specified as a path to a Unix domain
  237. socket - this path must start with `/` or `.`.
  238. Characters like `/` in the connection data, which otherwise have a special
  239. meaning inside a connection string, must be represented using percent
  240. encoding (e.g., `%2F` for `/`). Another option is to enclose a host name or
  241. a socket path in round braces. For example, one can write
  242. "mysqlx://(./path/to/socket)/db"
  243. instead of
  244. "mysqlx://.%2Fpath%2Fto%2Fsocket/db"
  245. To specify priorities for hosts in a multi-host settings, use list of pairs
  246. of the form `(address=host,priority=N)`. If priorities are specified, they
  247. must be given to all hosts in the list.
  248. The optional `db` part of the connection string defines the default schema
  249. of the session.
  250. Possible connection options are:
  251. - `ssl-mode` : define `SSLMode` option to be used
  252. - `ssl-ca=`path : path to a PEM file specifying trusted root certificates
  253. */
  254. SessionSettings(const string &uri)
  255. {
  256. try {
  257. Settings_detail::set_from_uri(uri);
  258. }
  259. CATCH_AND_WRAP
  260. }
  261. /**
  262. Explicitly specify basic connection settings.
  263. @note Session settings constructed this way request an SSL connection
  264. by default.
  265. */
  266. SessionSettings(const std::string &host, unsigned port,
  267. const string &user,
  268. const char *pwd = NULL,
  269. const string &db = string())
  270. {
  271. set(
  272. SessionOption::HOST, host,
  273. SessionOption::PORT, port,
  274. SessionOption::USER, user
  275. );
  276. if (pwd)
  277. set(SessionOption::PWD, std::string(pwd));
  278. if (!db.empty())
  279. set(SessionOption::DB, db);
  280. }
  281. SessionSettings(const std::string &host, unsigned port,
  282. const string &user,
  283. const std::string &pwd,
  284. const string &db = string())
  285. : SessionSettings(host, port, user, pwd.c_str(), db)
  286. {}
  287. /**
  288. Basic settings with the default port
  289. @note Session settings constructed this way request an SSL connection
  290. by default.
  291. */
  292. SessionSettings(const std::string &host,
  293. const string &user,
  294. const char *pwd = NULL,
  295. const string &db = string())
  296. : SessionSettings(host, DEFAULT_MYSQLX_PORT, user, pwd, db)
  297. {}
  298. SessionSettings(const std::string &host,
  299. const string &user,
  300. const std::string &pwd,
  301. const string &db = string())
  302. : SessionSettings(host, DEFAULT_MYSQLX_PORT, user, pwd, db)
  303. {}
  304. /**
  305. Basic settings for a session on the localhost.
  306. @note Session settings constructed this way request an SSL connection
  307. by default.
  308. */
  309. SessionSettings(unsigned port,
  310. const string &user,
  311. const char *pwd = NULL,
  312. const string &db = string())
  313. : SessionSettings("localhost", port, user, pwd, db)
  314. {}
  315. SessionSettings(unsigned port,
  316. const string &user,
  317. const std::string &pwd,
  318. const string &db = string())
  319. : SessionSettings("localhost", port, user, pwd.c_str(), db)
  320. {}
  321. /*
  322. Templates below are here to take care of the optional password
  323. parameter of type const char* (which can be either 3-rd or 4-th in
  324. the parameter list). Without these templates passing
  325. NULL as password is ambiguous because NULL is defined as 0,
  326. which has type int, and then it could be treated as port value.
  327. */
  328. template <
  329. typename HOST,
  330. typename PORT,
  331. typename USER,
  332. typename... T,
  333. typename std::enable_if<
  334. std::is_constructible<SessionSettings, HOST, PORT, USER, const char*, T...>::value
  335. >::type* = nullptr
  336. >
  337. SessionSettings(HOST h, PORT p, USER u ,long , T... args)
  338. : SessionSettings(h, p, u, nullptr, args...)
  339. {}
  340. template <
  341. typename PORT,
  342. typename USER,
  343. typename... T,
  344. typename std::enable_if<
  345. std::is_constructible<SessionSettings, PORT, USER, const char*, T...>::value
  346. >::type* = nullptr
  347. >
  348. SessionSettings(PORT p, USER u ,long , T... args)
  349. : SessionSettings(p, u, nullptr, args...)
  350. {}
  351. /**
  352. Specify settings as a list of session options.
  353. The list of options consist of a SessionOption constant,
  354. identifying the option to set, followed by the value of the option.
  355. Example:
  356. ~~~~~~
  357. SessionSettings from_option_list(
  358. SessionOption::USER, "user",
  359. SessionOption::PWD, "pwd",
  360. SessionOption::HOST, "host",
  361. SessionOption::PORT, port,
  362. SessionOption::DB, "db",
  363. SessionOption::SSL_MODE, SessionSettings::SSLMode::REQUIRED
  364. );
  365. ~~~~~~
  366. */
  367. template <typename... R>
  368. SessionSettings(SessionOption opt, R&&...rest)
  369. {
  370. try {
  371. // set<true> means that only SessionOption can be used
  372. Settings_detail::set<true>(opt, std::forward<R>(rest)...);
  373. }
  374. CATCH_AND_WRAP
  375. }
  376. /*
  377. Return an iterator pointing to the first element of the SessionSettings.
  378. */
  379. using Settings_detail::iterator;
  380. iterator begin()
  381. {
  382. try {
  383. return Settings_detail::begin();
  384. }
  385. CATCH_AND_WRAP
  386. }
  387. /*
  388. Return an iterator pointing to the last element of the SessionSettings.
  389. */
  390. iterator end()
  391. {
  392. try {
  393. return Settings_detail::end();
  394. }
  395. CATCH_AND_WRAP
  396. }
  397. /**
  398. Find the specified option @p opt and returns its Value.
  399. Returns NULL Value if not found.
  400. @note For option such as `HOST`, which can repeat several times in
  401. the settings, only the last value is reported.
  402. */
  403. Value find(SessionOption opt)
  404. {
  405. try {
  406. return Settings_detail::get(opt);
  407. }
  408. CATCH_AND_WRAP
  409. }
  410. /**
  411. Set session options.
  412. Accepts a list of one or more `SessionOption` constants, each followed by
  413. the option value. Options specified here are added to the current settings.
  414. Repeated `HOST`, `PORT`, `SOCKET` and `PRIORITY` options build a list of
  415. hosts to be used by the fail-over logic. For other options, if they are set
  416. again, the new value overrides the previous setting.
  417. @note
  418. When using `HOST`, `PORT` and `PRIORITY` options to specify a single
  419. host, all have to be specified in the same `set()` call.
  420. */
  421. template<typename... R>
  422. void set(SessionOption opt, R&&... rest)
  423. {
  424. try {
  425. // set<true> means that only SessionOption can be used
  426. Settings_detail::set<true>(opt, std::forward<R>(rest)...);
  427. }
  428. CATCH_AND_WRAP
  429. }
  430. /**
  431. Clear all settings specified so far.
  432. */
  433. void clear()
  434. {
  435. try {
  436. Settings_detail::clear();
  437. }
  438. CATCH_AND_WRAP
  439. }
  440. /**
  441. Remove all settings for the given option @p opt.
  442. @note For option such as `HOST`, which can repeat several times in
  443. the settings, all occurrences are erased.
  444. */
  445. void erase(SessionOption opt)
  446. {
  447. try {
  448. Settings_detail::erase(opt);
  449. }
  450. CATCH_AND_WRAP
  451. }
  452. /**
  453. Check if option @p opt was defined.
  454. */
  455. bool has_option(SessionOption opt)
  456. {
  457. try {
  458. return Settings_detail::has_option(opt);
  459. }
  460. CATCH_AND_WRAP
  461. }
  462. private:
  463. friend Client;
  464. friend Session;
  465. };
  466. /**
  467. ClientSettings are used to construct Client objects.
  468. It can be constructed using a connection string plus a JSON with client
  469. options, or by setting each ClientOption and SessionOption with its
  470. correspondant value.
  471. @ingroup devapi
  472. */
  473. class ClientSettings
  474. : private internal::Settings_detail<internal::Settings_traits>
  475. {
  476. public:
  477. using Value = mysqlx::Value;
  478. /**
  479. Create client settings from a connection string.
  480. @see SessionSettings
  481. */
  482. ClientSettings(const string &uri)
  483. {
  484. try {
  485. Settings_detail::set_from_uri(uri);
  486. }
  487. CATCH_AND_WRAP
  488. }
  489. /**
  490. Create client settings from a connection string and a ClientSettings object
  491. @see SessionSettings
  492. */
  493. ClientSettings(const string &uri, ClientSettings &opts)
  494. {
  495. try {
  496. Settings_detail::set_from_uri(uri);
  497. Settings_detail::set_client_opts(opts);
  498. }
  499. CATCH_AND_WRAP
  500. }
  501. /**
  502. Create client settings from a connection string and. Client options are
  503. expressed in JSON format. Here is an example:
  504. ~~~~~~
  505. { "pooling": {
  506. "enabled": true,
  507. "maxSize": 25,
  508. "queueTimeout": 1000,
  509. "maxIdleTime": 5000}
  510. }
  511. ~~~~~~
  512. All options are defined under a document with key vale "pooling". Inside the
  513. document, the available options are these:
  514. - `enabled` : boolean value that enable or disable connection pooling. If
  515. disabled, session created from pool are the same as created
  516. directly without client handle.
  517. Enabled by default.
  518. - `mazSize` : integer that defines the max pooling sessions possible. If
  519. uses tries to get session from pool when maximum sessions are
  520. used, it will wait for an available session untill
  521. `queueTimeout`.
  522. Defaults to 25.
  523. - `queueTimeout` : integer value that defines the time, in milliseconds,
  524. that client will wait to get an available session.
  525. By default it doesn't timeouts.
  526. - `maxIdleTime` : integer value that defines the time, in milliseconds, that
  527. an available session will wait in the pool before it is
  528. removed.
  529. By default it doesn't cleans sessions.
  530. */
  531. ClientSettings(const string &uri, const DbDoc &options)
  532. {
  533. try {
  534. Settings_detail::set_from_uri(uri);
  535. std::stringstream str_opts;
  536. str_opts << options;
  537. Settings_detail::set_client_opts(str_opts.str());
  538. }
  539. CATCH_AND_WRAP
  540. }
  541. /**
  542. Create client settings from a connection string and. Client options are
  543. expressed in JSON format. Here is an example:
  544. ~~~~~~
  545. { "pooling": {
  546. "enabled": true,
  547. "maxSize": 25,
  548. "queueTimeout": 1000,
  549. "maxIdleTime": 5000}
  550. }
  551. ~~~~~~
  552. All options are defined under a document with key vale "pooling". Inside the
  553. document, the available options are these:
  554. - `enabled` : boolean value that enable or disable connection pooling. If
  555. disabled, session created from pool are the same as created
  556. directly without client handle.
  557. Enabled by default.
  558. - `mazSize` : integer that defines the max pooling sessions possible. If
  559. uses tries to get session from pool when maximum sessions are
  560. used, it will wait for an available session untill
  561. `queueTimeout`.
  562. Defaults to 25.
  563. - `queueTimeout` : integer value that defines the time, in milliseconds,
  564. that client will wait to get an available session.
  565. By default it doesn't timeouts.
  566. - `maxIdleTime` : integer value that defines the time, in milliseconds, that
  567. an available session will wait in the pool before it is
  568. removed.
  569. By default it doesn't cleans sessions.
  570. */
  571. ClientSettings(const string &uri, const char *options)
  572. {
  573. try {
  574. Settings_detail::set_from_uri(uri);
  575. Settings_detail::set_client_opts(options);
  576. }
  577. CATCH_AND_WRAP
  578. }
  579. /**
  580. Create client settings from a connection string and client settings as a
  581. list of client options.
  582. The list of options consist of a ClientOption constant,
  583. identifying the option to set, followed by the value of the option.
  584. Example:
  585. ~~~~~~
  586. ClientSettings from_option_list( "mysqlx://root@localhost",
  587. ClientOption::POOLING, true,
  588. ClientOption::POOL_MAX_SIZE, max_connections,
  589. ClientOption::POOL_QUEUE_TIMEOUT, std::chrono::seconds(100),
  590. ClientOption::POOL_MAX_IDLE_TIME, std::chrono::microseconds(1)
  591. );
  592. ~~~~~~
  593. ClientOption::POOL_QUEUE_TIMEOUT and ClientOption::POOL_MAX_IDLE_TIME can
  594. be specified using std::chrono::duration objects, or by integer values, with
  595. the latest to be specified in milliseconds.
  596. @see SessionSettings
  597. */
  598. template<typename...R>
  599. ClientSettings(const string &uri, mysqlx::ClientOption opt, R... rest)
  600. try
  601. : ClientSettings(uri)
  602. {
  603. // set<false> means that both SessionOption and ClientOption can be used
  604. Settings_detail::set<false>(opt, std::forward<R>(rest)...);
  605. }
  606. CATCH_AND_WRAP
  607. template <typename... R>
  608. ClientSettings(mysqlx::SessionOption opt, R&&...rest)
  609. {
  610. try {
  611. // set<false> means that both SessionOption and ClientOption can be used
  612. Settings_detail::set<false>(opt, std::forward<R>(rest)...);
  613. }
  614. CATCH_AND_WRAP
  615. }
  616. template <typename... R>
  617. ClientSettings(mysqlx::ClientOption opt, R&&...rest)
  618. {
  619. try {
  620. // set<false> means that both SessionOption and ClientOption can be used
  621. Settings_detail::set<false>(opt, std::forward<R>(rest)...);
  622. }
  623. CATCH_AND_WRAP
  624. }
  625. /**
  626. Find the specified option @p opt and returns its Value.
  627. Returns NULL Value if not found.
  628. */
  629. Value find(mysqlx::ClientOption opt)
  630. {
  631. try {
  632. return Settings_detail::get(opt);
  633. }
  634. CATCH_AND_WRAP
  635. }
  636. /**
  637. Find the specified option @p opt and returns its Value.
  638. Returns NULL Value if not found.
  639. @note For option such as `HOST`, which can repeat several times in
  640. the settings, only the last value is reported.
  641. */
  642. Value find(mysqlx::SessionOption opt)
  643. {
  644. try {
  645. return Settings_detail::get(opt);
  646. }
  647. CATCH_AND_WRAP
  648. }
  649. /**
  650. Set client and session options.
  651. Accepts a list of one or more `ClientOption` or `SessionOption` constants,
  652. each followed by the option value. Options specified here are added to the
  653. current settings.
  654. Repeated `HOST`, `PORT`, `SOCKET` and `PRIORITY` options build a list of
  655. hosts to be used by the fail-over logic. For other options, if they are set
  656. again, the new value overrides the previous setting.
  657. @note
  658. When using `HOST`, `PORT` and `PRIORITY` options to specify a single
  659. host, all have to be specified in the same `set()` call.
  660. */
  661. template<typename OPT,typename... R>
  662. void set(OPT opt, R&&... rest)
  663. {
  664. try {
  665. // set<false> means that both SessionOption and ClientOption can be used
  666. Settings_detail::set<false>(opt, std::forward<R>(rest)...);
  667. }
  668. CATCH_AND_WRAP
  669. }
  670. /**
  671. Clear all settings specified so far.
  672. */
  673. void clear()
  674. {
  675. try {
  676. Settings_detail::clear();
  677. }
  678. CATCH_AND_WRAP
  679. }
  680. /**
  681. Remove the given option @p opt.
  682. */
  683. void erase(mysqlx::ClientOption opt)
  684. {
  685. try {
  686. Settings_detail::erase(opt);
  687. }
  688. CATCH_AND_WRAP
  689. }
  690. /**
  691. Remove all settings for the given option @p opt.
  692. @note For option such as `HOST`, which can repeat several times in
  693. the settings, all occurrences are erased.
  694. */
  695. void erase(mysqlx::SessionOption opt)
  696. {
  697. try {
  698. Settings_detail::erase(opt);
  699. }
  700. CATCH_AND_WRAP
  701. }
  702. /**
  703. Check if option @p opt was defined.
  704. */
  705. bool has_option(mysqlx::ClientOption opt)
  706. {
  707. try {
  708. return Settings_detail::has_option(opt);
  709. }
  710. CATCH_AND_WRAP
  711. }
  712. /**
  713. Check if option @p opt was defined.
  714. */
  715. bool has_option(SessionOption opt)
  716. {
  717. try {
  718. return Settings_detail::has_option(opt);
  719. }
  720. CATCH_AND_WRAP
  721. }
  722. private:
  723. friend Client;
  724. friend Session;
  725. };
  726. } // mysqlx
  727. #endif