MCPcopy Index your code
hub / github.com/upper/db / parseURL

Function parseURL

adapter/postgresql/connection.go:269–313  ·  view source on GitHub ↗

ParseURL no longer needs to be used by clients of this library since supplying a URL as a connection string to sql.Open() is now supported: sql.Open("postgres", "postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full") It remains exported here for backwards-compatibility. ParseURL converts

(uri string)

Source from the content-addressed store, hash-verified

267//
268// NOTE: vendored/copied from github.com/lib/pq
269func parseURL(uri string) (string, error) {
270 u, err := url.Parse(uri)
271 if err != nil {
272 return "", err
273 }
274
275 if u.Scheme != "postgres" && u.Scheme != "postgresql" {
276 return "", fmt.Errorf("invalid connection protocol: %s", u.Scheme)
277 }
278
279 var kvs []string
280 escaper := strings.NewReplacer(` `, `\ `, `'`, `\'`, `\`, `\\`)
281 accrue := func(k, v string) {
282 if v != "" {
283 kvs = append(kvs, k+"="+escaper.Replace(v))
284 }
285 }
286
287 if u.User != nil {
288 v := u.User.Username()
289 accrue("user", v)
290
291 v, _ = u.User.Password()
292 accrue("password", v)
293 }
294
295 if host, port, err := net.SplitHostPort(u.Host); err != nil {
296 accrue("host", u.Host)
297 } else {
298 accrue("host", host)
299 accrue("port", port)
300 }
301
302 if u.Path != "" {
303 accrue("dbname", u.Path[1:])
304 }
305
306 q := u.Query()
307 for k := range q {
308 accrue(k, q.Get(k))
309 }
310
311 sort.Strings(kvs) // Makes testing easier (not a performance concern)
312 return strings.Join(kvs, " "), nil
313}

Callers 1

ParseURLFunction · 0.85

Calls 4

ErrorfMethod · 0.65
QueryMethod · 0.65
GetMethod · 0.65
JoinMethod · 0.65

Tested by

no test coverage detected