generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driverpgx.go
98 lines (76 loc) · 1.55 KB
/
driverpgx.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sqltest
import (
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
)
// pgxDriver is the implementation of Driver for the "pgx" driver.
type pgxDriver struct{}
func (pgxDriver) Name() string {
return "pgx"
}
func (pgxDriver) IsAvailable() bool {
return true
}
func (pgxDriver) ParseDSN(dsn string) (DataSource, error) {
cfg, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, err
}
return pgxDataSource{
config: cfg,
dsn: dsn,
}, nil
}
func (d pgxDriver) DataSourceForPostgres(
user, pass,
host, port,
database string,
) (DataSource, error) {
dsn := "sslmode=disable"
if user != "" {
dsn += " user=" + user
}
if pass != "" {
dsn += " password=" + pass
}
if host != "" {
dsn += " host=" + host
}
if port != "" {
dsn += " port=" + port
}
if database != "" {
dsn += " database=" + database
}
return d.ParseDSN(dsn)
}
// pgxDataSource is an implementation of DataSource for "pgx" driver.
type pgxDataSource struct {
config *pgx.ConnConfig
dsn string
release bool
}
func (ds pgxDataSource) DriverName() string {
return "pgx"
}
func (ds pgxDataSource) DSN() string {
return ds.dsn
}
func (ds pgxDataSource) DatabaseName() string {
return ds.config.Database
}
func (ds pgxDataSource) WithDatabaseName(database string) DataSource {
cfg := ds.config.Copy()
cfg.Database = database
return pgxDataSource{
config: cfg,
dsn: stdlib.RegisterConnConfig(cfg),
release: true,
}
}
func (ds pgxDataSource) Close() error {
if ds.release {
stdlib.UnregisterConnConfig(ds.dsn)
}
return nil
}