@@ -204,7 +204,7 @@ static const String2IntMap flagsOptions[]=
204
204
/* {{{ readFlag(::sql::SQLString, int= 0) -I- */
205
205
/* * Check if connection option pointed by map iterator defines a connection
206
206
flag */
207
- static bool read_flag (ConnectOptionsMap::const_iterator &cit, int &flags)
207
+ static bool read_connection_flag (ConnectOptionsMap::const_iterator &cit, int &flags)
208
208
{
209
209
const bool * value;
210
210
@@ -225,9 +225,55 @@ static bool read_flag(ConnectOptionsMap::const_iterator &cit, int &flags)
225
225
}
226
226
return false ;
227
227
}
228
-
229
228
/* }}} */
230
229
230
+ /* Array for mapping of boolean connection options to mysql_options call */
231
+ static const String2IntMap booleanOptions[]=
232
+ {
233
+ {" OPT_REPORT_DATA_TRUNCATION" , MYSQL_REPORT_DATA_TRUNCATION},
234
+ {" OPT_ENABLE_CLEARTEXT_PLUGIN" , MYSQL_ENABLE_CLEARTEXT_PLUGIN}
235
+ };
236
+ /* Array for mapping of integer connection options to mysql_options call */
237
+ static const String2IntMap intOptions[]=
238
+ {
239
+ {" OPT_CONNECT_TIMEOUT" , MYSQL_OPT_CONNECT_TIMEOUT},
240
+ {" OPT_READ_TIMEOUT" , MYSQL_OPT_READ_TIMEOUT},
241
+ {" OPT_WRITE_TIMEOUT" , MYSQL_OPT_WRITE_TIMEOUT}
242
+ };
243
+ /* Array for mapping of string connection options to mysql_options call */
244
+ static const String2IntMap stringOptions[]=
245
+ {
246
+ {" preInit" , MYSQL_INIT_COMMAND}
247
+ };
248
+
249
+ template <class T >
250
+ bool process_connection_option (ConnectOptionsMap::const_iterator &option,
251
+ const String2IntMap options_map[],
252
+ size_t map_size,
253
+ boost::shared_ptr< NativeAPI::NativeConnectionWrapper > &proxy)
254
+ {
255
+ const T * value;
256
+
257
+ for (unsigned int i = 0 ; i < map_size; ++i) {
258
+
259
+ if (!option->first .compare (options_map[i].key )) {
260
+
261
+ if (!(value = boost::get<T>(&option->second ))) {
262
+ sql::SQLString err (" Option " );
263
+ err.append (option->first ).append (" is not of expected type" );
264
+ throw sql::InvalidArgumentException (err);
265
+ }
266
+ proxy->options (static_cast <sql::mysql::MySQL_Connection_Options>(options_map[i].value ),
267
+ *value);
268
+
269
+ return true ;
270
+ }
271
+ }
272
+
273
+ return false ;
274
+ }
275
+
276
+
231
277
/*
232
278
We support :
233
279
- hostName
@@ -262,9 +308,17 @@ static bool read_flag(ConnectOptionsMap::const_iterator &cit, int &flags)
262
308
- OPT_RECONNECT
263
309
- OPT_CHARSET_NAME
264
310
- OPT_REPORT_DATA_TRUNCATION
311
+ - OPT_CAN_HANDLE_EXPIRED_PASSWORDS
312
+ - OPT_ENABLE_CLEARTEXT_PLUGIN
313
+ - preInit
314
+ - postInit
315
+
316
+ To add new connection option that maps to a myql_options call, only add its
317
+ mapping to sql::mysql::MySQL_Connection_Options value to one of arrays above
318
+ - booleanOptions, intOptions, stringOptions. You might need to add new member
319
+ to the sql::mysql::MySQL_Connection_Options enum
265
320
*/
266
321
267
-
268
322
/* {{{ MySQL_Connection::init() -I- */
269
323
void MySQL_Connection::init (ConnectOptionsMap & properties)
270
324
{
@@ -305,6 +359,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
305
359
}
306
360
}
307
361
362
+ #define PROCESS_CONN_OPTION (option_type, options_map ) process_connection_option< option_type >(it, options_map, sizeof (options_map)/sizeof (String2IntMap), proxy)
363
+
308
364
for (it = properties.begin (); it != properties.end (); ++it) {
309
365
if (!it->first .compare (" userName" )) {
310
366
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
@@ -431,24 +487,6 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
431
487
} else {
432
488
throw sql::InvalidArgumentException (" No bool value passed for metadataUseInfoSchema" );
433
489
}
434
- }else if (!it->first .compare (" OPT_CONNECT_TIMEOUT" )) {
435
- if (!(p_i = boost::get< int >(&it->second ))) {
436
- throw sql::InvalidArgumentException (" No long long value passed for OPT_CONNECT_TIMEOUT" );
437
- }
438
- long l_tmp = static_cast <long >(*p_i);
439
- proxy->options (MYSQL_OPT_CONNECT_TIMEOUT, (const char *) &l_tmp);
440
- } else if (!it->first .compare (" OPT_READ_TIMEOUT" )) {
441
- if (!(p_i = boost::get< int >(&it->second ))) {
442
- throw sql::InvalidArgumentException (" No long long value passed for OPT_READ_TIMEOUT" );
443
- }
444
- long l_tmp = static_cast <long >(*p_i);
445
- proxy->options (MYSQL_OPT_READ_TIMEOUT, (const char *) &l_tmp);
446
- } else if (!it->first .compare (" OPT_WRITE_TIMEOUT" )) {
447
- if (!(p_i = boost::get< int >(&it->second ))) {
448
- throw sql::InvalidArgumentException (" No long long value passed for OPT_WRITE_TIMEOUT" );
449
- }
450
- long l_tmp = static_cast <long >(*p_i);
451
- proxy->options (MYSQL_OPT_WRITE_TIMEOUT, (const char *) &l_tmp);
452
490
} else if (!it->first .compare (" OPT_RECONNECT" )) {
453
491
if (!(p_b = boost::get<bool >(&it->second ))) {
454
492
throw sql::InvalidArgumentException (" No bool value passed for OPT_RECONNECT" );
@@ -460,11 +498,6 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
460
498
throw sql::InvalidArgumentException (" No SQLString value passed for OPT_CHARSET_NAME" );
461
499
}
462
500
defaultCharset = *p_s;
463
- } else if (!it->first .compare (" OPT_REPORT_DATA_TRUNCATION" )) {
464
- if (!(p_b = boost::get< bool >(&it->second ))) {
465
- throw sql::InvalidArgumentException (" No bool value passed for OPT_REPORT_DATA_TRUNCATION" );
466
- }
467
- proxy->options (MYSQL_REPORT_DATA_TRUNCATION, (const char *) p_b);
468
501
} else if (!it->first .compare (" OPT_NAMED_PIPE" )) {
469
502
/* Not sure it is really needed */
470
503
uri.setProtocol (NativeAPI::PROTOCOL_PIPE);
@@ -484,26 +517,36 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
484
517
/* We do not care here about server version */
485
518
proxy->options (MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, (const char *)p_b);
486
519
}
487
-
488
- } else if (!it->first .compare (" preInit" )) {
489
- if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
490
- proxy->options (MYSQL_INIT_COMMAND, p_s->c_str ());
491
- } else {
492
- throw sql::InvalidArgumentException (" No string value passed for preInit" );
493
- }
494
- }else if (!it->first .compare (" postInit" )) {
520
+ } else if (!it->first .compare (" postInit" )) {
495
521
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
496
522
postInit= *p_s;
497
523
} else {
498
524
throw sql::InvalidArgumentException (" No string value passed for postInit" );
499
525
}
500
- } else if (read_flag (it, flags)) {
526
+
527
+ /* If you need to add new integer connection option that should result in
528
+ calling mysql_optiong - add its mapping to the intOptions array
529
+ */
530
+ } else if (PROCESS_CONN_OPTION (int , intOptions)) {
531
+ // Nothing to do here
532
+
533
+ /* For boolean coonection option - add mapping to booleanOptions array */
534
+ } else if (PROCESS_CONN_OPTION (bool , booleanOptions)) {
535
+ // Nothing to do here
536
+
537
+ /* For string coonection option - add mapping to stringOptions array */
538
+ } else if (PROCESS_CONN_OPTION (sql::SQLString, stringOptions)) {
539
+ // Nothing to do here
540
+ } else if (read_connection_flag (it, flags)) {
501
541
// Nothing to do here
502
542
} else {
503
543
// TODO: Shouldn't we really create a warning here? as soon as we are able to
504
544
// create a warning
505
545
}
506
- }
546
+
547
+ } /* End of cycle on connection options map */
548
+
549
+ #undef PROCESS_CONNSTR_OPTION
507
550
508
551
/* libmysql shouldn't think it is too smart */
509
552
if (tcpProtocol (uri) && !uri.Host ().compare (util::LOCALHOST)) {
@@ -561,13 +604,12 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
561
604
&& client_doesnt_support_exp_pwd) {
562
605
563
606
native_error= deCL_CANT_HANDLE_EXP_PWD;
564
- error_message= " Your password has expired. Your instance of"
607
+ error_message= " Your password has expired, but your instance of"
565
608
" Connector/C++ is not linked against mysql client library that"
566
- " allows to change it. resetting of an expired password. To"
567
- " resolve this, you either need to change the password with"
568
- " mysql client that can do it or rebuild your instance of"
569
- " Connector/C++ against mysql client library that supports"
570
- " resetting of an expired password." ;
609
+ " allows to reset it. To resolve this you either need to change"
610
+ " the password with mysql client that is capable to do that,"
611
+ " or rebuild your instance of Connector/C++ against mysql client"
612
+ " library that supports resetting of an expired password." ;
571
613
} else {
572
614
error_message= proxy->error ();
573
615
}
0 commit comments