forked from ZbigniewTomanek/pgvertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
148 lines (125 loc) · 3.1 KB
/
conn.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package pgvertica
import (
"database/sql"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"github.com/jackc/pgproto3/v2"
)
type ParsedConnString struct {
url *url.URL
host string
port int
}
func parseConnectionString(connStr string) (*ParsedConnString, error) {
u, err := url.Parse(connStr)
if err != nil {
Logger.Error("Error parsing connection string:", "error", err)
return nil, err
}
var host, port string
if host, port, err = net.SplitHostPort(u.Host); err != nil {
Logger.Error("Error on split host and port", "error", err)
return nil, err
}
portNum, err := strconv.Atoi(port)
if err != nil {
Logger.Error("Error parsing port number", "error", err)
return nil, err
}
return &ParsedConnString{
url: u,
host: host,
port: portNum,
}, nil
}
type DBOpener interface {
Open(driverName, dataSourceName string) (*sql.DB, error)
}
type RealDBOpener struct{}
func (ro RealDBOpener) Open(driverName, dataSourceName string) (*sql.DB, error) {
return sql.Open(driverName, dataSourceName)
}
type Receiver interface {
Receive() (pgproto3.FrontendMessage, error)
ReceiveStartupMessage() (pgproto3.FrontendMessage, error)
}
type BackendWrapper struct {
backend *pgproto3.Backend
}
func (bw *BackendWrapper) Receive() (pgproto3.FrontendMessage, error) {
return bw.backend.Receive()
}
func (bw *BackendWrapper) ReceiveStartupMessage() (pgproto3.FrontendMessage, error) {
return bw.backend.ReceiveStartupMessage()
}
type Conn struct {
net.Conn
receiver Receiver
vdb *sql.DB
pgdb *sql.DB
}
func newConn(conn net.Conn) *Conn {
return &Conn{
Conn: conn,
receiver: &BackendWrapper{
backend: pgproto3.NewBackend(pgproto3.NewChunkReader(conn), conn),
},
}
}
func (c *Conn) Close() (err error) {
if c.vdb != nil {
if e := c.vdb.Close(); err == nil {
err = e
}
}
if c.pgdb != nil {
if e := c.pgdb.Close(); err == nil {
err = e
}
}
if e := c.Conn.Close(); err == nil {
err = e
}
return err
}
func getDBNameFromConnString(connStr string) (string, error) {
parsedConn, err := parseConnectionString(connStr)
if err != nil {
return "", err
}
return strings.ReplaceAll(parsedConn.url.Path, "/", ""), nil
}
func buildConnectionString(scheme string, params map[string]string, host string, port int) string {
user := params["user"]
password := params["password"]
database := params["database"]
var query = url.URL{
Scheme: scheme,
User: url.UserPassword(user, password),
Host: fmt.Sprintf("%s:%d", host, port),
Path: database,
}
return query.String()
}
func connectToDB(opener DBOpener, driverName string, connectionString string, params map[string]string) (*sql.DB, error) {
if params != nil {
parsedConn, err := parseConnectionString(connectionString)
if err != nil {
return nil, err
}
connectionString = buildConnectionString(parsedConn.url.Scheme, params, parsedConn.host, parsedConn.port)
}
Logger.Debug("Connect to DB", "connection", connectionString)
var db *sql.DB
var err error
if db, err = opener.Open(driverName, connectionString); err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
return db, nil
}