forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
79 lines (60 loc) · 1.34 KB
/
Copy pathdriver.go
File metadata and controls
79 lines (60 loc) · 1.34 KB
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
package dbtestutil
import (
"context"
"database/sql/driver"
"github.com/lib/pq"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database"
)
var _ database.DialerConnector = &Connector{}
type Connector struct {
name string
driver *Driver
dialer pq.Dialer
}
func (c *Connector) Connect(_ context.Context) (driver.Conn, error) {
if c.dialer != nil {
conn, err := pq.DialOpen(c.dialer, c.name)
if err != nil {
return nil, xerrors.Errorf("failed to dial open connection: %w", err)
}
c.driver.Connections <- conn
return conn, nil
}
conn, err := pq.Driver{}.Open(c.name)
if err != nil {
return nil, xerrors.Errorf("failed to open connection: %w", err)
}
c.driver.Connections <- conn
return conn, nil
}
func (c *Connector) Driver() driver.Driver {
return c.driver
}
func (c *Connector) Dialer(dialer pq.Dialer) {
c.dialer = dialer
}
type Driver struct {
Connections chan driver.Conn
}
func NewDriver() *Driver {
return &Driver{
Connections: make(chan driver.Conn, 1),
}
}
func (d *Driver) Connector(name string) (driver.Connector, error) {
return &Connector{
name: name,
driver: d,
}, nil
}
func (d *Driver) Open(name string) (driver.Conn, error) {
c, err := d.Connector(name)
if err != nil {
return nil, err
}
return c.Connect(context.Background())
}
func (d *Driver) Close() {
close(d.Connections)
}