@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
26
26
#include < stdlib.h>
27
27
#include " mysql_uri.h"
28
28
#include " mysql_util.h"
29
+ #include " exception.h"
29
30
30
31
namespace sql
31
32
{
@@ -43,91 +44,95 @@ static const int DEFAULT_TCP_PORT= 3306;
43
44
44
45
/* {{{ MySQL_Uri::MySQL_Uri() -I- */
45
46
MySQL_Uri::MySQL_Uri ()
46
- : protocol(NativeAPI::PROTOCOL_TCP),
47
- port (DEFAULT_TCP_PORT),
48
- /* Perhaps `localhost` has to be default. so w/out any parameter
49
- driver will still connect? */
50
- host (" " ),
51
- schema (" " )
47
+ : protocol(NativeAPI::PROTOCOL_TCP),
48
+ port (DEFAULT_TCP_PORT),
49
+ /* Perhaps `localhost` has to be default. so w/out any parameter
50
+ driver will still connect? */
51
+ host (" " ),
52
+ schema (" " )
52
53
{}
53
54
/* }}} */
54
55
55
56
56
57
/* {{{ MySQL_Uri::Host() -I- */
57
58
const sql::SQLString & MySQL_Uri::Host ()
58
59
{
59
- static const sql::SQLString hostValue4Pipe (" ." );
60
- static const sql::SQLString hostValue4sock (util::LOCALHOST);
61
-
62
- switch (Protocol ())
63
- {
64
- case NativeAPI::PROTOCOL_TCP:
65
- return host;
66
- case NativeAPI::PROTOCOL_PIPE:
67
- return hostValue4Pipe;
68
- case NativeAPI::PROTOCOL_SOCKET:
69
- return hostValue4sock;
70
- }
71
-
72
- // throw smoething maybe?
73
- return host;
60
+ static const sql::SQLString hostValue4Pipe (" ." );
61
+ static const sql::SQLString hostValue4sock (util::LOCALHOST);
62
+
63
+ switch (Protocol ())
64
+ {
65
+ case NativeAPI::PROTOCOL_TCP:
66
+ return host;
67
+ case NativeAPI::PROTOCOL_PIPE:
68
+ return hostValue4Pipe;
69
+ case NativeAPI::PROTOCOL_SOCKET:
70
+ return hostValue4sock;
71
+
72
+ case NativeAPI::PROTOCOL_COUNT:
73
+ throw sql::InvalidArgumentException (" NativeAPI::PROTOCOL_COUNT shouldn't be used." );
74
+ break ;
75
+ }
76
+
77
+ // throw smoething maybe?
78
+ return host;
74
79
}
75
80
/* }}} */
76
81
77
82
78
83
/* {{{ MySQL_Uri::SocketOrPipe() -I- */
79
84
const sql::SQLString & MySQL_Uri::SocketOrPipe ()
80
85
{
81
- if (tcpProtocol (*this ))
82
- {
83
- static const sql::SQLString emptystr (util::EMPTYSTR);
84
- return emptystr;
85
- }
86
+ if (tcpProtocol (*this ))
87
+ {
88
+ static const sql::SQLString emptystr (util::EMPTYSTR);
89
+ return emptystr;
90
+ }
86
91
87
- return host;
92
+ return host;
88
93
}
89
94
/* }}} */
90
95
91
96
92
97
/* {{{ MySQL_Uri::setHost() -I- */
93
98
void MySQL_Uri::setHost (const sql::SQLString &h)
94
99
{
95
- setProtocol (NativeAPI::PROTOCOL_TCP);
96
- host= h.c_str ();
100
+ setProtocol (NativeAPI::PROTOCOL_TCP);
101
+ host= h.c_str ();
97
102
}
98
103
/* }}} */
99
104
100
105
101
106
/* {{{ MySQL_Uri::setSocket() -I- */
102
107
void MySQL_Uri::setSocket (const sql::SQLString &s)
103
108
{
104
- setProtocol (NativeAPI::PROTOCOL_SOCKET);
105
- host= s.c_str ();
109
+ setProtocol (NativeAPI::PROTOCOL_SOCKET);
110
+ host= s.c_str ();
106
111
}
107
112
/* }}} */
108
113
109
114
110
115
/* {{{ MySQL_Uri::setPipe() -I- */
111
116
void MySQL_Uri::setPipe (const sql::SQLString &p)
112
117
{
113
- setProtocol (NativeAPI::PROTOCOL_PIPE);
114
- host= p.c_str ();
118
+ setProtocol (NativeAPI::PROTOCOL_PIPE);
119
+ host= p.c_str ();
115
120
}
116
121
/* }}} */
117
122
118
123
119
124
/* {{{ MySQL_Uri::setPort() -I- */
120
125
void MySQL_Uri::setPort (unsigned int p)
121
126
{
122
- setProtocol (NativeAPI::PROTOCOL_TCP);
123
- port= p;
127
+ setProtocol (NativeAPI::PROTOCOL_TCP);
128
+ port= p;
124
129
}
125
130
126
131
127
132
/* {{{ tcpProtocol() -I- */
128
133
bool tcpProtocol (MySQL_Uri& uri)
129
134
{
130
- return uri.Protocol () == NativeAPI::PROTOCOL_TCP;
135
+ return uri.Protocol () == NativeAPI::PROTOCOL_TCP;
131
136
}
132
137
/* }}} */
133
138
@@ -139,88 +144,88 @@ bool tcpProtocol(MySQL_Uri& uri)
139
144
*/
140
145
bool parseUri (const sql::SQLString & str, MySQL_Uri& uri)
141
146
{
142
- if (!str.compare (0 , sizeof (MYURI_SOCKET_PREFIX) - 1 , MYURI_SOCKET_PREFIX))
143
- {
144
- uri.setSocket (str.substr (sizeof (MYURI_SOCKET_PREFIX) - 1 , sql::SQLString::npos));
145
-
146
- return true ;
147
- }
148
-
149
- if (!str.compare (0 , sizeof (MYURI_PIPE_PREFIX) - 1 , MYURI_PIPE_PREFIX))
150
- {
151
- uri.setPipe (str.substr (sizeof (MYURI_PIPE_PREFIX) - 1 , sql::SQLString::npos));
152
-
153
- return true ;
154
- }
155
-
156
- sql::SQLString host;
157
- size_t start_sep, end_sep;
158
-
159
- /* i wonder how did it work with "- 1"*/
160
- if (!str.compare (0 , sizeof (MYURI_TCP_PREFIX) - 1 , MYURI_TCP_PREFIX) )
161
- {
162
- host= str.substr (sizeof (MYURI_TCP_PREFIX) - 1 , sql::SQLString::npos);
163
- }
164
- else
165
- {
166
- /* allowing to have port and schema specified even w/out protocol
167
- specifier("tcp://") */
168
- host= str.c_str ();
169
- }
170
-
171
- if (host[0 ] == MYURI_HOST_BEGIN)
172
- {
173
- end_sep= host.find (MYURI_HOST_END);
174
-
175
- /* No closing ] after [*/
176
- if (end_sep == sql::SQLString::npos)
177
- {
178
- return false ;
179
- }
180
-
181
- uri.setHost (host.substr (1 , end_sep - 1 ));
182
- /* Cutting host to continue w/ port and schema reading */
183
- host= host.substr (end_sep + 1 );
184
- }
185
-
186
- /* Looking where schema part begins */
187
- start_sep = host.find (' /' );
188
-
189
- if (start_sep != sql::SQLString::npos)
190
- {
191
- if ((host.length () - start_sep) > 1 /* Slash*/ )
192
- {
193
- uri.setSchema (host.substr (start_sep + 1 , host.length () - start_sep - 1 ));
194
- }
195
-
196
- host= host.substr (0 , start_sep);
197
- }
198
- else
199
- {
200
- uri.setSchema (" " );
201
- }
202
-
203
- /* Looking where port part begins*/
204
- start_sep = host.find_last_of (' :' , sql::SQLString::npos);
205
-
206
- if (start_sep != sql::SQLString::npos)
207
- {
208
- uri.setPort (atoi (host.substr (start_sep + 1 , sql::SQLString::npos).c_str ()));
209
- host = host.substr (0 , start_sep);
210
- }
211
- else
212
- {
213
- uri.setPort (DEFAULT_TCP_PORT);
214
- }
215
-
216
- /* If host was enclosed in [], it has been already set, and "host" variable is
217
- empty */
218
- if (host.length () > 0 )
219
- {
220
- uri.setHost (host);
221
- }
222
-
223
- return true ;
147
+ if (!str.compare (0 , sizeof (MYURI_SOCKET_PREFIX) - 1 , MYURI_SOCKET_PREFIX))
148
+ {
149
+ uri.setSocket (str.substr (sizeof (MYURI_SOCKET_PREFIX) - 1 , sql::SQLString::npos));
150
+
151
+ return true ;
152
+ }
153
+
154
+ if (!str.compare (0 , sizeof (MYURI_PIPE_PREFIX) - 1 , MYURI_PIPE_PREFIX))
155
+ {
156
+ uri.setPipe (str.substr (sizeof (MYURI_PIPE_PREFIX) - 1 , sql::SQLString::npos));
157
+
158
+ return true ;
159
+ }
160
+
161
+ sql::SQLString host;
162
+ size_t start_sep, end_sep;
163
+
164
+ /* i wonder how did it work with "- 1"*/
165
+ if (!str.compare (0 , sizeof (MYURI_TCP_PREFIX) - 1 , MYURI_TCP_PREFIX) )
166
+ {
167
+ host= str.substr (sizeof (MYURI_TCP_PREFIX) - 1 , sql::SQLString::npos);
168
+ }
169
+ else
170
+ {
171
+ /* allowing to have port and schema specified even w/out protocol
172
+ specifier("tcp://") */
173
+ host= str.c_str ();
174
+ }
175
+
176
+ if (host[0 ] == MYURI_HOST_BEGIN)
177
+ {
178
+ end_sep= host.find (MYURI_HOST_END);
179
+
180
+ /* No closing ] after [*/
181
+ if (end_sep == sql::SQLString::npos)
182
+ {
183
+ return false ;
184
+ }
185
+
186
+ uri.setHost (host.substr (1 , end_sep - 1 ));
187
+ /* Cutting host to continue w/ port and schema reading */
188
+ host= host.substr (end_sep + 1 );
189
+ }
190
+
191
+ /* Looking where schema part begins */
192
+ start_sep = host.find (' /' );
193
+
194
+ if (start_sep != sql::SQLString::npos)
195
+ {
196
+ if ((host.length () - start_sep) > 1 /* Slash*/ )
197
+ {
198
+ uri.setSchema (host.substr (start_sep + 1 , host.length () - start_sep - 1 ));
199
+ }
200
+
201
+ host= host.substr (0 , start_sep);
202
+ }
203
+ else
204
+ {
205
+ uri.setSchema (" " );
206
+ }
207
+
208
+ /* Looking where port part begins*/
209
+ start_sep = host.find_last_of (' :' , sql::SQLString::npos);
210
+
211
+ if (start_sep != sql::SQLString::npos)
212
+ {
213
+ uri.setPort (atoi (host.substr (start_sep + 1 , sql::SQLString::npos).c_str ()));
214
+ host = host.substr (0 , start_sep);
215
+ }
216
+ else
217
+ {
218
+ uri.setPort (DEFAULT_TCP_PORT);
219
+ }
220
+
221
+ /* If host was enclosed in [], it has been already set, and "host" variable is
222
+ empty */
223
+ if (host.length () > 0 )
224
+ {
225
+ uri.setHost (host);
226
+ }
227
+
228
+ return true ;
224
229
}
225
230
226
231
0 commit comments