forked from pubnative/mysqldriver-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
100 lines (83 loc) · 2.45 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
package mysqldriver
import (
"context"
"net"
"github.com/pubnative/mysqlproto-go"
)
var capabilityFlags = mysqlproto.CLIENT_LONG_PASSWORD |
mysqlproto.CLIENT_FOUND_ROWS |
mysqlproto.CLIENT_LONG_FLAG |
mysqlproto.CLIENT_CONNECT_WITH_DB |
mysqlproto.CLIENT_PLUGIN_AUTH |
mysqlproto.CLIENT_TRANSACTIONS |
mysqlproto.CLIENT_PROTOCOL_41 |
mysqlproto.CLIENT_SECURE_CONNECTION |
mysqlproto.CLIENT_SESSION_TRACK
// Conn represents connection to MySQL server
type Conn struct {
conn mysqlproto.Conn
valid bool
closed bool
}
// Contains connection statistics
type Stats struct {
Syscalls int // number of system calls performed to read all packets
}
// NewConn establishes a connection to the DB. After obtaining the connection,
// it sends "SET NAMES utf8" command to the DB
func NewConn(username, password, protocol, address, database string) (*Conn, error) {
return NewConnContext(context.Background(), username, password, protocol, address, database)
}
// NewConnContext establishes a connection to the DB. After obtaining the connection,
// it sends "SET NAMES utf8" command to the DB
//
// Go Context is only used to establish a TCP connection.
// TODO use Go Context to establish a MySQL connection.
func NewConnContext(ctx context.Context, username, password, protocol, address, database string) (*Conn, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, protocol, address)
if err != nil {
return nil, err
}
stream, err := mysqlproto.ConnectPlainHandshake(
conn, capabilityFlags,
username, password, database, nil,
)
if err != nil {
return &Conn{conn: stream, valid: false, closed: false}, err
}
if err = setUTF8Charset(stream); err != nil {
return &Conn{conn: stream, valid: false, closed: false}, err
}
return &Conn{conn: stream, valid: true, closed: false}, nil
}
// Close closes the connection
func (c *Conn) Close() error {
if !c.closed {
c.closed = true
return c.conn.Close()
}
return nil
}
// Returns statistics about the connection
func (c *Conn) Stats() Stats {
return Stats{
Syscalls: c.conn.Syscalls(),
}
}
// Add sum ups all stats
func (s Stats) Add(stats Stats) Stats {
return Stats{
Syscalls: s.Syscalls + stats.Syscalls,
}
}
func setUTF8Charset(conn mysqlproto.Conn) error {
data := mysqlproto.ComQueryRequest([]byte("SET NAMES utf8"))
if _, err := conn.Write(data); err != nil {
return err
}
packet, err := conn.NextPacket()
if err != nil {
return err
}
return handleOK(packet.Payload, conn.CapabilityFlags)
}