MCPcopy Create free account
hub / github.com/upper/db / ParseURL

Function ParseURL

adapter/postgresql/connection.go:108–160  ·  view source on GitHub ↗

ParseURL parses the given DSN into a ConnectionURL struct. A typical PostgreSQL connection URL looks like: postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full

(s string)

Source from the content-addressed store, hash-verified

106//
107// postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full
108func ParseURL(s string) (u *ConnectionURL, err error) {
109 o := make(values)
110
111 if strings.HasPrefix(s, "postgres://") || strings.HasPrefix(s, "postgresql://") {
112 s, err = parseURL(s)
113 if err != nil {
114 return u, err
115 }
116 }
117
118 if err := parseOpts(s, o); err != nil {
119 return u, err
120 }
121 u = &ConnectionURL{}
122
123 u.User = o.Get("user")
124 u.Password = o.Get("password")
125
126 h := o.Get("host")
127 p := o.Get("port")
128
129 if strings.HasPrefix(h, "/") {
130 u.Socket = h
131 } else {
132 if p == "" {
133 u.Host = h
134 } else {
135 u.Host = fmt.Sprintf("%s:%s", h, p)
136 }
137 }
138
139 u.Database = o.Get("dbname")
140
141 u.Options = make(map[string]string)
142
143 for k := range o {
144 switch k {
145 case "user", "password", "host", "port", "dbname":
146 // Skip
147 default:
148 u.Options[k] = o[k]
149 }
150 }
151
152 if timezone, ok := u.Options["timezone"]; ok {
153 u.timezone, err = time.LoadLocation(timezone)
154 if err != nil {
155 return nil, err
156 }
157 }
158
159 return u, err
160}
161
162// parseOpts parses the options from name and adds them to the values.
163//

Callers 4

TestParseConnectionURLFunction · 0.70
OpenDSNMethod · 0.70
OpenDSNMethod · 0.70
TestParseConnectionURLFunction · 0.70

Calls 3

parseURLFunction · 0.85
parseOptsFunction · 0.70
GetMethod · 0.65

Tested by 2

TestParseConnectionURLFunction · 0.56
TestParseConnectionURLFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…