Skip to content

Commit 445908a

Browse files
committed
DSN: Use an URIs instead of key/value connection string.
It simplifies the code and will help to support other schemes.
1 parent 6e4e7ff commit 445908a

File tree

1 file changed

+26
-108
lines changed

1 file changed

+26
-108
lines changed

postgresql/config.go

Lines changed: 26 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package postgresql
22

33
import (
4-
"bytes"
54
"database/sql"
65
"fmt"
7-
"log"
6+
"net/url"
7+
"strconv"
88
"strings"
99
"sync"
1010
"unicode"
@@ -169,120 +169,38 @@ func (c *Config) featureSupported(name featureName) bool {
169169
func (c *Config) connStr(database string) string {
170170
// NOTE: dbname must come before user otherwise dbname will be set to
171171
// user.
172-
var dsnFmt string
173-
{
174-
dsnFmtParts := []string{
175-
"host=%s",
176-
"port=%d",
177-
"dbname=%s",
178-
"user=%s",
179-
"password=%s",
180-
"sslmode=%s",
181-
"connect_timeout=%d",
182-
}
183-
184-
if c.featureSupported(featureFallbackApplicationName) {
185-
dsnFmtParts = append(dsnFmtParts, "fallback_application_name=%s")
186-
}
187-
if c.SSLClientCert != nil {
188-
dsnFmtParts = append(
189-
dsnFmtParts,
190-
"sslcert=%s",
191-
"sslkey=%s",
192-
)
193-
}
194-
if c.SSLRootCertPath != "" {
195-
dsnFmtParts = append(dsnFmtParts, "sslrootcert=%s")
196-
}
197-
198-
dsnFmt = strings.Join(dsnFmtParts, " ")
172+
params := map[string]string{
173+
"sslmode": c.SSLMode,
174+
"connect_timeout": strconv.Itoa(c.ConnectTimeoutSec),
199175
}
200176

201-
// Quote empty strings or strings that contain whitespace
202-
quote := func(s string) string {
203-
b := bytes.NewBufferString(`'`)
204-
b.Grow(len(s) + 2)
205-
var haveWhitespace bool
206-
for _, r := range s {
207-
if unicode.IsSpace(r) {
208-
haveWhitespace = true
209-
}
210-
211-
switch r {
212-
case '\'':
213-
b.WriteString(`\'`)
214-
case '\\':
215-
b.WriteString(`\\`)
216-
default:
217-
b.WriteRune(r)
218-
}
219-
}
220-
221-
b.WriteString(`'`)
222-
223-
str := b.String()
224-
if haveWhitespace || len(str) == 2 {
225-
return str
226-
}
227-
return str[1 : len(str)-1]
177+
if c.featureSupported(featureFallbackApplicationName) {
178+
params["fallback_application_name"] = c.ApplicationName
228179
}
229-
230-
{
231-
logValues := []interface{}{
232-
quote(c.Host),
233-
c.Port,
234-
quote(database),
235-
quote(c.Username),
236-
quote("<redacted>"),
237-
quote(c.SSLMode),
238-
c.ConnectTimeoutSec,
239-
}
240-
if c.featureSupported(featureFallbackApplicationName) {
241-
logValues = append(logValues, quote(c.ApplicationName))
242-
}
243-
if c.SSLClientCert != nil {
244-
logValues = append(
245-
logValues,
246-
quote(c.SSLClientCert.CertificatePath),
247-
quote(c.SSLClientCert.KeyPath),
248-
)
249-
}
250-
if c.SSLRootCertPath != "" {
251-
logValues = append(logValues, quote(c.SSLRootCertPath))
252-
}
253-
254-
logDSN := fmt.Sprintf(dsnFmt, logValues...)
255-
log.Printf("[INFO] PostgreSQL DSN: `%s`", logDSN)
180+
if c.SSLClientCert != nil {
181+
params["sslcert"] = c.SSLClientCert.CertificatePath
182+
params["sslkey"] = c.SSLClientCert.KeyPath
256183
}
257184

258-
var connStr string
259-
{
260-
connValues := []interface{}{
261-
quote(c.Host),
262-
c.Port,
263-
quote(database),
264-
quote(c.Username),
265-
quote(c.Password),
266-
quote(c.SSLMode),
267-
c.ConnectTimeoutSec,
268-
}
269-
if c.featureSupported(featureFallbackApplicationName) {
270-
connValues = append(connValues, quote(c.ApplicationName))
271-
}
272-
if c.SSLClientCert != nil {
273-
connValues = append(
274-
connValues,
275-
quote(c.SSLClientCert.CertificatePath),
276-
quote(c.SSLClientCert.KeyPath),
277-
)
278-
}
279-
if c.SSLRootCertPath != "" {
280-
connValues = append(connValues, quote(c.SSLRootCertPath))
281-
}
185+
if c.SSLRootCertPath != "" {
186+
params["sslrootcert"] = c.SSLRootCertPath
187+
}
282188

283-
connStr = fmt.Sprintf(dsnFmt, connValues...)
189+
paramsArray := []string{}
190+
for key, value := range params {
191+
paramsArray = append(paramsArray, "%s=%s", key, url.QueryEscape(value))
284192
}
285193

194+
connStr := fmt.Sprintf(
195+
"postgres://%s:%s@%s:%d/%s?%s",
196+
url.QueryEscape(c.Username),
197+
url.QueryEscape(c.Password),
198+
url.QueryEscape(c.Host),
199+
c.Port,
200+
database,
201+
strings.Join(paramsArray, "&"),
202+
)
203+
286204
return connStr
287205
}
288206

0 commit comments

Comments
 (0)