-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsn.go
76 lines (64 loc) · 1.83 KB
/
dsn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package dbkit
import (
"fmt"
"net/url"
"github.com/go-sql-driver/mysql"
)
// MakeMSSQLDSN makes DSN for opening MSSQL database.
func MakeMSSQLDSN(cfg *MSSQLConfig) string {
query := url.Values{}
query.Add("database", cfg.Database)
u := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword(cfg.User, cfg.Password),
Host: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
RawQuery: query.Encode(),
}
return u.String()
}
// MakeMySQLDSN makes DSN for opening MySQL database.
func MakeMySQLDSN(cfg *MySQLConfig) string {
c := mysql.NewConfig()
c.Net = "tcp"
c.Addr = fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
c.User = cfg.User
c.Passwd = cfg.Password
c.DBName = cfg.Database
c.ParseTime = true
c.MultiStatements = true
c.Params = make(map[string]string)
c.Params["autocommit"] = "false"
return c.FormatDSN()
}
// MakePostgresDSN makes DSN for opening Postgres database.
func MakePostgresDSN(cfg *PostgresConfig) string {
sslMode := cfg.SSLMode
if sslMode == "" {
sslMode = PostgresDefaultSSLMode
}
connURI := url.URL{
Scheme: "postgres",
User: url.UserPassword(cfg.User, cfg.Password),
Host: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Path: cfg.Database,
RawQuery: fmt.Sprintf("sslmode=%s", url.QueryEscape(string(sslMode))),
}
if cfg.SearchPath != "" {
connURI.RawQuery += fmt.Sprintf("&search_path=%s", url.QueryEscape(cfg.SearchPath))
}
if len(cfg.AdditionalParameters) != 0 {
for _, p := range cfg.AdditionalParameters {
connURI.RawQuery += fmt.Sprintf("&%s=%s", p.Name, url.QueryEscape(p.Value))
}
}
return connURI.String()
}
// MakeSQLiteDSN makes DSN for opening SQLite database.
func MakeSQLiteDSN(cfg *SQLiteConfig) string {
// Connection params will be used here in the future.
return cfg.Path
}