MCPcopy Create free account
hub / github.com/go-pg/pg / ToURL

Method ToURL

options.go:279–324  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

277}
278
279func (opts *Options) ToURL() string {
280 dsn := "postgres://"
281
282 if len(opts.User) > 0 {
283 dsn += opts.User
284
285 if len(opts.Password) > 0 {
286 dsn += ":" + opts.Password
287 }
288
289 dsn += "@"
290 }
291
292 if len(opts.Addr) > 0 {
293 dsn += opts.Addr
294 } else {
295 dsn += "localhost:5432"
296 }
297
298 dsn += "/" + opts.Database
299
300 values := url.Values{}
301
302 if opts.DialTimeout > 0 {
303 values.Add("connect_timeout", strconv.Itoa(int(opts.DialTimeout)/int(time.Second)))
304 }
305
306 if len(opts.ApplicationName) > 0 {
307 values.Add("application_name", opts.ApplicationName)
308 }
309
310 if opts.TLSConfig == nil {
311 values.Add("sslmode", "disable")
312 } else if opts.TLSConfig.InsecureSkipVerify {
313 values.Add("sslmode", "allow")
314 } else if !opts.TLSConfig.InsecureSkipVerify {
315 values.Add("sslmode", "verify-ca")
316 }
317
318 encoded := values.Encode()
319 if len(encoded) > 0 {
320 dsn += "?" + encoded
321 }
322
323 return dsn
324}
325
326func (opt *Options) getDialer() func(context.Context) (net.Conn, error) {
327 return func(ctx context.Context) (net.Conn, error) {

Callers 2

TestOptions_ToURLFunction · 0.80

Calls 1

EncodeMethod · 0.80

Tested by 2

TestOptions_ToURLFunction · 0.64