@@ -576,6 +576,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
576
576
intern->is_valid = true ;
577
577
578
578
MySQL_Uri uri;
579
+ MySQL_Uri::Host_data host;
580
+
579
581
580
582
sql::SQLString userName;
581
583
sql::SQLString password;
@@ -654,7 +656,10 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
654
656
throw sql::InvalidArgumentException (" Wrong type passed for port expected int" );
655
657
}
656
658
if (p_i) {
657
- uri.setPort (static_cast <unsigned int >(*p_i));
659
+ for (auto &h : uri)
660
+ {
661
+ h.setPort (*p_i);
662
+ }
658
663
} else {
659
664
throw sql::InvalidArgumentException (" No long long value passed for port" );
660
665
}
@@ -665,7 +670,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
665
670
throw sql::InvalidArgumentException (" Wrong type passed for socket expected sql::SQLString" );
666
671
}
667
672
if (p_s) {
668
- uri.setSocket (*p_s);
673
+ host.setSocket (*p_s);
674
+ uri.setHost (host);
669
675
} else {
670
676
throw sql::InvalidArgumentException (" No string value passed for socket" );
671
677
}
@@ -676,7 +682,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
676
682
throw sql::InvalidArgumentException (" Wrong type passed for pipe expected sql::SQLString" );
677
683
}
678
684
if (p_s) {
679
- uri.setPipe (*p_s);
685
+ host.setPipe (*p_s);
686
+ uri.setHost (host);
680
687
} else {
681
688
throw sql::InvalidArgumentException (" No string value passed for pipe" );
682
689
}
@@ -868,7 +875,8 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
868
875
defaultCharset = *p_s;
869
876
} else if (!it->first .compare (OPT_NAMED_PIPE)) {
870
877
/* Not sure it is really needed */
871
- uri.setProtocol (NativeAPI::PROTOCOL_PIPE);
878
+ host.setProtocol (NativeAPI::PROTOCOL_PIPE);
879
+ uri.setHost (host);
872
880
} else if (!it->first .compare (OPT_CAN_HANDLE_EXPIRED_PASSWORDS)) {
873
881
try {
874
882
p_b = (it->second ).get <bool >();
@@ -987,18 +995,21 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
987
995
988
996
#undef PROCESS_CONNSTR_OPTION
989
997
990
- // Throwing in case of wrong protocol
998
+ for (auto h : uri)
999
+ {
1000
+
1001
+ // Throwing in case of wrong protocol
991
1002
#ifdef _WIN32
992
- if (uri .Protocol () == NativeAPI::PROTOCOL_SOCKET) {
993
- throw sql::InvalidArgumentException (" Invalid for this platform protocol requested(MYSQL_PROTOCOL_SOCKET)" );
994
- }
1003
+ if (h .Protocol () == NativeAPI::PROTOCOL_SOCKET) {
1004
+ throw sql::InvalidArgumentException (" Invalid for this platform protocol requested(MYSQL_PROTOCOL_SOCKET)" );
1005
+ }
995
1006
#else
996
- if (uri .Protocol () == NativeAPI::PROTOCOL_PIPE) {
997
- throw sql::InvalidArgumentException (" Invalid for this platform protocol requested(MYSQL_PROTOCOL_PIPE)" );
998
- }
1007
+ if (h .Protocol () == NativeAPI::PROTOCOL_PIPE) {
1008
+ throw sql::InvalidArgumentException (" Invalid for this platform protocol requested(MYSQL_PROTOCOL_PIPE)" );
1009
+ }
999
1010
#endif
1000
1011
1001
-
1012
+ }
1002
1013
1003
1014
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID < 80000
1004
1015
try {
@@ -1100,34 +1111,49 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
1100
1111
1101
1112
if (opt_dns_srv)
1102
1113
{
1103
- if (uri.Protocol () != NativeAPI::PROTOCOL_TCP)
1114
+ if (uri.size () > 1 )
1115
+ {
1116
+ throw sql::InvalidArgumentException (" Using more than one host with DNS SRV lookup is not allowed." );
1117
+ }
1118
+
1119
+ if (uri.size () ==0 )
1120
+ {
1121
+ throw sql::InvalidArgumentException (" No hostname specified for DNS SRV lookup." );
1122
+ }
1123
+
1124
+ host = *uri.begin ();
1125
+
1126
+ if (host.Protocol () != NativeAPI::PROTOCOL_TCP)
1104
1127
{
1105
1128
throw sql::InvalidArgumentException (" Using Unix domain sockets with DNS SRV lookup is not allowed." );
1106
1129
}
1107
1130
1108
- if (uri .hasPort ())
1131
+ if (host .hasPort ())
1109
1132
{
1110
1133
throw sql::InvalidArgumentException (" Specifying a port number with DNS SRV lookup is not allowed." );
1111
1134
}
1112
1135
1113
- host_list = srv_list (uri .Host (),total_weight);
1136
+ host_list = srv_list (host .Host (),total_weight);
1114
1137
1115
1138
if (host_list.empty ())
1116
1139
{
1117
1140
std::stringstream err;
1118
- err << " Unable to locate any hosts for " << uri .Host ();
1141
+ err << " Unable to locate any hosts for " << host .Host ();
1119
1142
throw sql::InvalidArgumentException (err.str ());
1120
1143
}
1121
1144
1122
-
1123
-
1124
1145
}
1125
1146
else
1126
1147
{
1127
1148
for (auto host : uri)
1128
1149
{
1129
1150
host_list.insert (std::make_pair (Prio ({0 , 0 }), host));
1130
1151
}
1152
+ if (host_list.empty ())
1153
+ {
1154
+ // Adding default host
1155
+ host_list.insert (std::make_pair (Prio ({0 , 0 }), Host_data ()));
1156
+ }
1131
1157
}
1132
1158
1133
1159
// Connect loop
@@ -1142,7 +1168,6 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
1142
1168
std::vector<uint16_t > weights;
1143
1169
1144
1170
1145
-
1146
1171
for (auto it = same_range.first ; it != same_range.second ; ++it)
1147
1172
{
1148
1173
// if weight is not used, we should put all weights = 1 so that
@@ -1167,14 +1192,14 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
1167
1192
std::advance (weight_el, pos);
1168
1193
}
1169
1194
1170
- proxy->use_protocol (uri. Protocol ());
1195
+ proxy->use_protocol ((*el)-> Protocol ());
1171
1196
1172
1197
try {
1173
- connect ((*el)->name , userName,
1198
+ connect ((*el)->Host () , userName,
1174
1199
password,
1175
1200
uri.Schema () /* schema */ ,
1176
- (*el)->port ,
1177
- uri. SocketOrPipe ());
1201
+ (*el)->Port () ,
1202
+ (*el)-> SocketOrPipe ());
1178
1203
connected = true ;
1179
1204
break ;
1180
1205
}
@@ -1213,12 +1238,21 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
1213
1238
{
1214
1239
std::stringstream err;
1215
1240
if (opt_dns_srv)
1216
- err << " Unable to connect to any of the hosts of " << uri .Host () << " SRV" ;
1241
+ err << " Unable to connect to any of the hosts of " << host .Host () << " SRV" ;
1217
1242
else if (uri.size () >1 ) {
1218
1243
err << " Unable to connect to any of the hosts" ;
1219
1244
}
1220
1245
else {
1221
- err << " Unable to connect to " << uri.Host () << " :" << uri.Port ();
1246
+ switch (host.Protocol ())
1247
+ {
1248
+ case NativeAPI::PROTOCOL_SOCKET:
1249
+ case NativeAPI::PROTOCOL_PIPE:
1250
+ err << " Unable to connect to " << host.SocketOrPipe () ;
1251
+ break ;
1252
+ default :
1253
+ err << " Unable to connect to " << host.Host () << " :" << host.Port ();
1254
+ break ;
1255
+ }
1222
1256
}
1223
1257
proxy.reset ();
1224
1258
throw sql::InvalidArgumentException (err.str ());
0 commit comments