@@ -150,6 +150,19 @@ func (cfg *Config) normalize() error {
150150 return nil
151151}
152152
153+ func writeDSNParam (buf * bytes.Buffer , hasParam * bool , name , value string ) {
154+ buf .Grow (1 + len (name ) + 1 + len (value ))
155+ if ! * hasParam {
156+ * hasParam = true
157+ buf .WriteByte ('?' )
158+ } else {
159+ buf .WriteByte ('&' )
160+ }
161+ buf .WriteString (name )
162+ buf .WriteByte ('=' )
163+ buf .WriteString (value )
164+ }
165+
153166// FormatDSN formats the given Config into a DSN string which can be passed to
154167// the driver.
155168func (cfg * Config ) FormatDSN () string {
@@ -188,174 +201,75 @@ func (cfg *Config) FormatDSN() string {
188201 }
189202
190203 if cfg .AllowCleartextPasswords {
191- if hasParam {
192- buf .WriteString ("&allowCleartextPasswords=true" )
193- } else {
194- hasParam = true
195- buf .WriteString ("?allowCleartextPasswords=true" )
196- }
204+ writeDSNParam (& buf , & hasParam , "allowCleartextPasswords" , "true" )
197205 }
198206
199207 if ! cfg .AllowNativePasswords {
200- if hasParam {
201- buf .WriteString ("&allowNativePasswords=false" )
202- } else {
203- hasParam = true
204- buf .WriteString ("?allowNativePasswords=false" )
205- }
208+ writeDSNParam (& buf , & hasParam , "allowNativePasswords" , "false" )
206209 }
207210
208211 if cfg .AllowOldPasswords {
209- if hasParam {
210- buf .WriteString ("&allowOldPasswords=true" )
211- } else {
212- hasParam = true
213- buf .WriteString ("?allowOldPasswords=true" )
214- }
212+ writeDSNParam (& buf , & hasParam , "allowOldPasswords" , "true" )
215213 }
216214
217215 if ! cfg .CheckConnLiveliness {
218- if hasParam {
219- buf .WriteString ("&checkConnLiveliness=false" )
220- } else {
221- hasParam = true
222- buf .WriteString ("?checkConnLiveliness=false" )
223- }
216+ writeDSNParam (& buf , & hasParam , "checkConnLiveliness" , "false" )
224217 }
225218
226219 if cfg .ClientFoundRows {
227- if hasParam {
228- buf .WriteString ("&clientFoundRows=true" )
229- } else {
230- hasParam = true
231- buf .WriteString ("?clientFoundRows=true" )
232- }
220+ writeDSNParam (& buf , & hasParam , "clientFoundRows" , "true" )
233221 }
234222
235223 if col := cfg .Collation ; col != defaultCollation && len (col ) > 0 {
236- if hasParam {
237- buf .WriteString ("&collation=" )
238- } else {
239- hasParam = true
240- buf .WriteString ("?collation=" )
241- }
242- buf .WriteString (col )
224+ writeDSNParam (& buf , & hasParam , "collation" , col )
243225 }
244226
245227 if cfg .ColumnsWithAlias {
246- if hasParam {
247- buf .WriteString ("&columnsWithAlias=true" )
248- } else {
249- hasParam = true
250- buf .WriteString ("?columnsWithAlias=true" )
251- }
228+ writeDSNParam (& buf , & hasParam , "columnsWithAlias" , "true" )
252229 }
253230
254231 if cfg .InterpolateParams {
255- if hasParam {
256- buf .WriteString ("&interpolateParams=true" )
257- } else {
258- hasParam = true
259- buf .WriteString ("?interpolateParams=true" )
260- }
232+ writeDSNParam (& buf , & hasParam , "interpolateParams" , "true" )
261233 }
262234
263235 if cfg .Loc != time .UTC && cfg .Loc != nil {
264- if hasParam {
265- buf .WriteString ("&loc=" )
266- } else {
267- hasParam = true
268- buf .WriteString ("?loc=" )
269- }
270- buf .WriteString (url .QueryEscape (cfg .Loc .String ()))
236+ writeDSNParam (& buf , & hasParam , "loc" , url .QueryEscape (cfg .Loc .String ()))
271237 }
272238
273239 if cfg .MultiStatements {
274- if hasParam {
275- buf .WriteString ("&multiStatements=true" )
276- } else {
277- hasParam = true
278- buf .WriteString ("?multiStatements=true" )
279- }
240+ writeDSNParam (& buf , & hasParam , "multiStatements" , "true" )
280241 }
281242
282243 if cfg .ParseTime {
283- if hasParam {
284- buf .WriteString ("&parseTime=true" )
285- } else {
286- hasParam = true
287- buf .WriteString ("?parseTime=true" )
288- }
244+ writeDSNParam (& buf , & hasParam , "parseTime" , "true" )
289245 }
290246
291247 if cfg .ReadTimeout > 0 {
292- if hasParam {
293- buf .WriteString ("&readTimeout=" )
294- } else {
295- hasParam = true
296- buf .WriteString ("?readTimeout=" )
297- }
298- buf .WriteString (cfg .ReadTimeout .String ())
248+ writeDSNParam (& buf , & hasParam , "readTimeout" , cfg .ReadTimeout .String ())
299249 }
300250
301251 if cfg .RejectReadOnly {
302- if hasParam {
303- buf .WriteString ("&rejectReadOnly=true" )
304- } else {
305- hasParam = true
306- buf .WriteString ("?rejectReadOnly=true" )
307- }
252+ writeDSNParam (& buf , & hasParam , "rejectReadOnly" , "true" )
308253 }
309254
310255 if len (cfg .ServerPubKey ) > 0 {
311- if hasParam {
312- buf .WriteString ("&serverPubKey=" )
313- } else {
314- hasParam = true
315- buf .WriteString ("?serverPubKey=" )
316- }
317- buf .WriteString (url .QueryEscape (cfg .ServerPubKey ))
256+ writeDSNParam (& buf , & hasParam , "serverPubKey" , url .QueryEscape (cfg .ServerPubKey ))
318257 }
319258
320259 if cfg .Timeout > 0 {
321- if hasParam {
322- buf .WriteString ("&timeout=" )
323- } else {
324- hasParam = true
325- buf .WriteString ("?timeout=" )
326- }
327- buf .WriteString (cfg .Timeout .String ())
260+ writeDSNParam (& buf , & hasParam , "timeout" , cfg .Timeout .String ())
328261 }
329262
330263 if len (cfg .TLSConfig ) > 0 {
331- if hasParam {
332- buf .WriteString ("&tls=" )
333- } else {
334- hasParam = true
335- buf .WriteString ("?tls=" )
336- }
337- buf .WriteString (url .QueryEscape (cfg .TLSConfig ))
264+ writeDSNParam (& buf , & hasParam , "tls" , url .QueryEscape (cfg .TLSConfig ))
338265 }
339266
340267 if cfg .WriteTimeout > 0 {
341- if hasParam {
342- buf .WriteString ("&writeTimeout=" )
343- } else {
344- hasParam = true
345- buf .WriteString ("?writeTimeout=" )
346- }
347- buf .WriteString (cfg .WriteTimeout .String ())
268+ writeDSNParam (& buf , & hasParam , "writeTimeout" , cfg .WriteTimeout .String ())
348269 }
349270
350271 if cfg .MaxAllowedPacket != defaultMaxAllowedPacket {
351- if hasParam {
352- buf .WriteString ("&maxAllowedPacket=" )
353- } else {
354- hasParam = true
355- buf .WriteString ("?maxAllowedPacket=" )
356- }
357- buf .WriteString (strconv .Itoa (cfg .MaxAllowedPacket ))
358-
272+ writeDSNParam (& buf , & hasParam , "maxAllowedPacket" , strconv .Itoa (cfg .MaxAllowedPacket ))
359273 }
360274
361275 // other params
@@ -366,16 +280,7 @@ func (cfg *Config) FormatDSN() string {
366280 }
367281 sort .Strings (params )
368282 for _ , param := range params {
369- if hasParam {
370- buf .WriteByte ('&' )
371- } else {
372- hasParam = true
373- buf .WriteByte ('?' )
374- }
375-
376- buf .WriteString (param )
377- buf .WriteByte ('=' )
378- buf .WriteString (url .QueryEscape (cfg .Params [param ]))
283+ writeDSNParam (& buf , & hasParam , param , url .QueryEscape (cfg .Params [param ]))
379284 }
380285 }
381286
0 commit comments