forked from go-mysql-org/go-mysql
/
conn.go
139 lines (111 loc) · 2.86 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
package server
import (
"net"
"sync/atomic"
. "github.com/Beeketing/go-mysql/mysql"
"github.com/Beeketing/go-mysql/packet"
"github.com/siddontang/go/sync2"
)
/*
Conn acts like a MySQL server connection, you can use MySQL client to communicate with it.
*/
type Conn struct {
*packet.Conn
serverConf *Server
capability uint32
authPluginName string
connectionID uint32
status uint16
salt []byte // should be 8 + 12 for auth-plugin-data-part-1 and auth-plugin-data-part-2
credentialProvider CredentialProvider
user string
password string
cachingSha2FullAuth bool
h Handler
stmts map[uint32]*Stmt
stmtID uint32
closed sync2.AtomicBool
}
var baseConnID uint32 = 10000
// NewConn: create connection with default server settings
func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) {
p := NewInMemoryProvider()
p.AddUser(user, password)
salt, _ := RandomBuf(20)
c := &Conn{
Conn: packet.NewConn(conn),
serverConf: defaultServer,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
stmts: make(map[uint32]*Stmt),
salt: salt,
}
c.closed.Set(false)
if err := c.handshake(); err != nil {
c.Close()
return nil, err
}
return c, nil
}
// NewCustomizedConn: create connection with customized server settings
func NewCustomizedConn(conn net.Conn, serverConf *Server, p CredentialProvider, h Handler) (*Conn, error) {
salt, _ := RandomBuf(20)
c := &Conn{
Conn: packet.NewConn(conn),
serverConf: serverConf,
credentialProvider: p,
h: h,
connectionID: atomic.AddUint32(&baseConnID, 1),
stmts: make(map[uint32]*Stmt),
salt: salt,
}
c.closed.Set(false)
if err := c.handshake(); err != nil {
c.Close()
return nil, err
}
return c, nil
}
func (c *Conn) handshake() error {
if err := c.writeInitialHandshake(); err != nil {
return err
}
if err := c.readHandshakeResponse(); err != nil {
if err == ErrAccessDenied {
err = NewDefaultError(ER_ACCESS_DENIED_ERROR, c.user, c.LocalAddr().String(), "Yes")
}
c.writeError(err)
return err
}
if err := c.writeOK(nil); err != nil {
return err
}
c.ResetSequence()
return nil
}
func (c *Conn) Close() {
c.closed.Set(true)
c.Conn.Close()
}
func (c *Conn) Closed() bool {
return c.closed.Get()
}
func (c *Conn) GetUser() string {
return c.user
}
func (c *Conn) ConnectionID() uint32 {
return c.connectionID
}
func (c *Conn) IsAutoCommit() bool {
return c.status&SERVER_STATUS_AUTOCOMMIT > 0
}
func (c *Conn) IsInTransaction() bool {
return c.status&SERVER_STATUS_IN_TRANS > 0
}
func (c *Conn) SetInTransaction() {
c.status |= SERVER_STATUS_IN_TRANS
}
func (c *Conn) ClearInTransaction() {
c.status &= ^SERVER_STATUS_IN_TRANS
}