@@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
40
40
41
41
#include " mysql_connection_options.h"
42
42
#include " mysql_util.h"
43
+ #include " mysql_uri.h"
43
44
44
45
/*
45
46
* _WIN32 is defined by 64bit compiler too
@@ -106,7 +107,9 @@ MySQL_Connection::MySQL_Connection(Driver * _driver,
106
107
const sql::SQLString& hostName,
107
108
const sql::SQLString& userName,
108
109
const sql::SQLString& password)
109
- : driver(_driver), proxy(&_proxy), intern(NULL )
110
+ : driver (_driver),
111
+ proxy (&_proxy),
112
+ intern (NULL )
110
113
{
111
114
sql::ConnectOptionsMap connection_properties;
112
115
connection_properties[" hostName" ] = hostName;
@@ -203,20 +206,16 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
203
206
CPP_ENTER_WL (intern->logger , " MySQL_Connection::init" );
204
207
205
208
intern->is_valid = true ;
206
- bool protocol_tcp = true ;
207
- sql::SQLString host;
208
- sql::SQLString socket_or_pipe;
209
- unsigned int port = 3306 ;
210
209
211
- sql::SQLString hostName;
210
+ MySQL_Uri uri;
211
+
212
212
sql::SQLString userName;
213
213
sql::SQLString password;
214
- sql::SQLString schema;
215
214
sql::SQLString defaultCharset (" utf8" );
216
215
sql::SQLString characterSetResults (" utf8" );
217
216
218
217
sql::SQLString sslKey, sslCert, sslCA, sslCAPath, sslCipher;
219
- bool ssl_used = false , schema_used = false ;
218
+ bool ssl_used = false ;
220
219
int flags = CLIENT_MULTI_RESULTS;
221
220
222
221
const int * p_i;
@@ -225,15 +224,23 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
225
224
bool opt_reconnect = false ;
226
225
bool opt_reconnect_value = false ;
227
226
228
- sql::ConnectOptionsMap::const_iterator it = properties.begin ();
229
- for (; it != properties.end (); ++it) {
230
- if (!it->first .compare (" hostName" )) {
231
- if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
232
- hostName = *p_s;
233
- } else {
234
- throw sql::InvalidArgumentException (" No string value passed for hostName" );
235
- }
236
- } else if (!it->first .compare (" userName" )) {
227
+
228
+ /* Values set in properties individually should have priority over those
229
+ we restore from Uri */
230
+ sql::ConnectOptionsMap::const_iterator it = properties.find (" hostName" );
231
+
232
+ if (it != properties.end ())
233
+ {
234
+ if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
235
+
236
+ parseUri (*p_s, uri);
237
+ } else {
238
+ throw sql::InvalidArgumentException (" No string value passed for hostName" );
239
+ }
240
+ }
241
+
242
+ for (it = properties.begin (); it != properties.end (); ++it) {
243
+ if (!it->first .compare (" userName" )) {
237
244
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
238
245
userName = *p_s;
239
246
} else {
@@ -247,31 +254,28 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
247
254
}
248
255
} else if (!it->first .compare (" port" )) {
249
256
if ((p_i = boost::get< int >(&it->second ))) {
250
- port = static_cast <unsigned int >(*p_i);
257
+ uri. setPort ( static_cast <unsigned int >(*p_i) );
251
258
} else {
252
259
throw sql::InvalidArgumentException (" No long long value passed for port" );
253
260
}
254
261
} else if (!it->first .compare (" socket" )) {
255
262
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
256
- socket_or_pipe = *p_s;
263
+ uri. setSocket ( *p_s) ;
257
264
} else {
258
265
throw sql::InvalidArgumentException (" No string value passed for socket" );
259
266
}
260
- protocol_tcp = false ;
261
267
} else if (!it->first .compare (" pipe" )) {
262
268
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
263
- socket_or_pipe = *p_s;
269
+ uri. setPipe ( *p_s) ;
264
270
} else {
265
271
throw sql::InvalidArgumentException (" No string value passed for pipe" );
266
272
}
267
- protocol_tcp = false ;
268
273
} else if (!it->first .compare (" schema" )) {
269
274
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
270
- schema = *p_s;
275
+ uri. setSchema ( *p_s) ;
271
276
} else {
272
277
throw sql::InvalidArgumentException (" No string value passed for schema" );
273
278
}
274
- schema_used = true ;
275
279
} else if (!it->first .compare (" characterSetResults" )) {
276
280
if ((p_s = boost::get< sql::SQLString >(&it->second ))) {
277
281
characterSetResults = *p_s;
@@ -420,46 +424,10 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
420
424
}
421
425
}
422
426
423
- #ifndef _WIN32
424
- if (!hostName.compare (0 , sizeof (" unix://" ) - 1 , " unix://" )) {
425
- protocol_tcp = false ;
426
- socket_or_pipe = hostName.substr (sizeof (" unix://" ) - 1 , sql::SQLString::npos);
427
- host = " localhost" ;
428
- int tmp_protocol = MYSQL_PROTOCOL_SOCKET;
429
- proxy->options (MYSQL_OPT_PROTOCOL, (const char *) &tmp_protocol);
430
- } else
431
- #else
432
- if (!hostName.compare (0 , sizeof (" pipe://" ) - 1 , " pipe://" )) {
433
- protocol_tcp = false ;
434
- socket_or_pipe = hostName.substr (sizeof (" pipe://" ) - 1 , sql::SQLString::npos);
435
- host = " ." ;
436
- int tmp_protocol = MYSQL_PROTOCOL_PIPE;
437
- proxy->options (MYSQL_OPT_PROTOCOL, (const char *) &tmp_protocol);
438
- } else
439
- #endif
440
- if (!hostName.compare (0 , sizeof (" tcp://" ) - 1 , " tcp://" ) ) {
441
- size_t port_pos, schema_pos;
442
- host = hostName.substr (sizeof (" tcp://" ) - 1 , sql::SQLString::npos);
443
- schema_pos = host.find (' /' );
444
- if (schema_pos != sql::SQLString::npos) {
445
- ++schema_pos; // skip the slash
446
- /* TODO: tcp://127.0.0.1/
447
- -> host set, schema empty, schema property ignored */
448
- schema = host.substr (schema_pos, host.length () - schema_pos);
449
- schema_used = true ;
450
- host = host.substr (0 , schema_pos - 1 );
451
- }
452
- port_pos = host.find_last_of (' :' , sql::SQLString::npos);
453
- if (port_pos != sql::SQLString::npos) {
454
- port = atoi (host.substr (port_pos + 1 , sql::SQLString::npos).c_str ());
455
- host = host.substr (0 , port_pos);
456
- }
457
- } else {
458
- host = hostName.c_str ();
459
- }
427
+
460
428
/* libmysql shouldn't think it is too smart */
461
- if (protocol_tcp && !host. compare ( 0 , sizeof ( " localhost " ) - 1 , " localhost " )) {
462
- host = " 127.0.0.1" ;
429
+ if (tcpProtocol (uri) && !uri. Host (). compare (util::LOCALHOST )) {
430
+ uri. setHost ( " 127.0.0.1" ) ;
463
431
}
464
432
465
433
it = properties.begin ();
@@ -498,13 +466,24 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
498
466
throw sql::InvalidArgumentException (" No bool value passed for OPT_REPORT_DATA_TRUNCATION" );
499
467
}
500
468
proxy->options (MYSQL_REPORT_DATA_TRUNCATION, (const char *) p_b);
501
- #ifdef _WIN32
502
469
} else if (!it->first .compare (" OPT_NAMED_PIPE" )) {
503
- proxy-> options (MYSQL_OPT_NAMED_PIPE, NULL );
504
- #endif
470
+ /* Not sure it is really needed */
471
+ uri. setProtocol (NativeAPI::PROTOCOL_PIPE);
505
472
}
506
473
}
507
474
475
+ // Throwing in case of wrong protocol
476
+ #ifdef _WIN32
477
+ if (uri.Protocol () == NativeAPI::PROTOCOL_SOCKET) {
478
+ throw sql::InvalidArgumentException (" Invalid for this platform protocol requested(MYSQL_PROTOCOL_SOCKET)" );
479
+ }
480
+ #else
481
+ if (uri.Protocol () == NativeAPI::PROTOCOL_PIPE) {
482
+ throw sql::InvalidArgumentException (" Invalid for this platform protocol requested(MYSQL_PROTOCOL_PIPE)" );
483
+ }
484
+ #endif
485
+ proxy->use_protocol (uri.Protocol ());
486
+
508
487
{
509
488
my_bool tmp_bool = 1 ;
510
489
proxy->options (MYSQL_SECURE_AUTH, (const char *) &tmp_bool);
@@ -516,17 +495,17 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
516
495
/* According to the docs, always returns 0 */
517
496
proxy->ssl_set (sslKey.c_str (), sslCert.c_str (), sslCA.c_str (), sslCAPath.c_str (), sslCipher.c_str ());
518
497
}
519
- CPP_INFO_FMT (" hostName=%s" , hostName .c_str ());
498
+ CPP_INFO_FMT (" hostName=%s" , uri. Host () .c_str ());
520
499
CPP_INFO_FMT (" user=%s" , userName.c_str ());
521
- CPP_INFO_FMT (" port=%d" , port );
522
- CPP_INFO_FMT (" schema=%s" , schema .c_str ());
523
- CPP_INFO_FMT (" socket/pipe=%s" , socket_or_pipe .c_str ());
524
- if (!proxy->connect (host ,
500
+ CPP_INFO_FMT (" port=%d" , uri. Port () );
501
+ CPP_INFO_FMT (" schema=%s" , uri. Schema () .c_str ());
502
+ CPP_INFO_FMT (" socket/pipe=%s" , uri. SocketOrPipe () .c_str ());
503
+ if (!proxy->connect (uri. Host () ,
525
504
userName,
526
505
password,
527
- schema_used && schema. length ()? schema: (schema= " " ) /* schema */ ,
528
- port ,
529
- protocol_tcp == false ? socket_or_pipe : " " /* socket or named pipe */ ,
506
+ uri. Schema ( ) /* schema */ ,
507
+ uri. Port () ,
508
+ uri. SocketOrPipe () /* socket or named pipe */ ,
530
509
flags))
531
510
{
532
511
CPP_ERR_FMT (" Couldn't connect : %d" , proxy->errNo ());
0 commit comments