...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package postgres
21
22 import (
23 "database/sql"
24 "database/sql/driver"
25 "fmt"
26 "net"
27 "regexp"
28 "time"
29
30 "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy"
31 "github.com/lib/pq"
32 )
33
34 func init() {
35 sql.Register("cloudsqlpostgres", &Driver{})
36 }
37
38 type Driver struct{}
39
40 type dialer struct{}
41
42
43
44 var instanceRegexp = regexp.MustCompile(`^\[(.+)\]:[0-9]+$`)
45
46 func (d dialer) Dial(ntw, addr string) (net.Conn, error) {
47 matches := instanceRegexp.FindStringSubmatch(addr)
48 if len(matches) != 2 {
49 return nil, fmt.Errorf("failed to parse addr: %q. It should conform to the regular expression %q", addr, instanceRegexp)
50 }
51 instance := matches[1]
52 return proxy.Dial(instance)
53 }
54
55 func (d dialer) DialTimeout(ntw, addr string, timeout time.Duration) (net.Conn, error) {
56 return nil, fmt.Errorf("timeout is not currently supported for cloudsqlpostgres dialer")
57 }
58
59 func (d *Driver) Open(name string) (driver.Conn, error) {
60 return pq.DialOpen(dialer{}, name)
61 }
62
View as plain text