• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

go-sql-driver / mysql / 3601392501

pending completion
3601392501

push

github

GitHub
update changelog for Version 1.7 (#1376)

2932 of 3584 relevant lines covered (81.81%)

1490385.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.7
/connector.go
1
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2
//
3
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved.
4
//
5
// This Source Code Form is subject to the terms of the Mozilla Public
6
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7
// You can obtain one at http://mozilla.org/MPL/2.0/.
8

9
package mysql
10

11
import (
12
        "context"
13
        "database/sql/driver"
14
        "net"
15
)
16

17
type connector struct {
18
        cfg *Config // immutable private copy.
19
}
20

21
// Connect implements driver.Connector interface.
22
// Connect returns a connection to the database.
23
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
10,980✔
24
        var err error
10,980✔
25

10,980✔
26
        // New mysqlConn
10,980✔
27
        mc := &mysqlConn{
10,980✔
28
                maxAllowedPacket: maxPacketSize,
10,980✔
29
                maxWriteSize:     maxPacketSize - 1,
10,980✔
30
                closech:          make(chan struct{}),
10,980✔
31
                cfg:              c.cfg,
10,980✔
32
        }
10,980✔
33
        mc.parseTime = mc.cfg.ParseTime
10,980✔
34

10,980✔
35
        // Connect to Server
10,980✔
36
        dialsLock.RLock()
10,980✔
37
        dial, ok := dials[mc.cfg.Net]
10,980✔
38
        dialsLock.RUnlock()
10,980✔
39
        if ok {
11,169✔
40
                dctx := ctx
189✔
41
                if mc.cfg.Timeout > 0 {
324✔
42
                        var cancel context.CancelFunc
135✔
43
                        dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
135✔
44
                        defer cancel()
135✔
45
                }
135✔
46
                mc.netConn, err = dial(dctx, mc.cfg.Addr)
189✔
47
        } else {
10,791✔
48
                nd := net.Dialer{Timeout: mc.cfg.Timeout}
10,791✔
49
                mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
10,791✔
50
        }
10,791✔
51

52
        if err != nil {
11,106✔
53
                return nil, err
129✔
54
        }
129✔
55

56
        // Enable TCP Keepalives on TCP connections
57
        if tc, ok := mc.netConn.(*net.TCPConn); ok {
21,580✔
58
                if err := tc.SetKeepAlive(true); err != nil {
10,733✔
59
                        // Don't send COM_QUIT before handshake.
×
60
                        mc.netConn.Close()
×
61
                        mc.netConn = nil
×
62
                        return nil, err
×
63
                }
×
64
        }
65

66
        // Call startWatcher for context support (From Go 1.8)
67
        mc.startWatcher()
10,851✔
68
        if err := mc.watchCancel(ctx); err != nil {
10,878✔
69
                mc.cleanup()
27✔
70
                return nil, err
27✔
71
        }
27✔
72
        defer mc.finish()
10,823✔
73

10,823✔
74
        mc.buf = newBuffer(mc.netConn)
10,823✔
75

10,823✔
76
        // Set I/O timeouts
10,823✔
77
        mc.buf.timeout = mc.cfg.ReadTimeout
10,823✔
78
        mc.writeTimeout = mc.cfg.WriteTimeout
10,823✔
79

10,823✔
80
        // Reading Handshake Initialization Packet
10,823✔
81
        authData, plugin, err := mc.readHandshakePacket()
10,823✔
82
        if err != nil {
10,910✔
83
                mc.cleanup()
87✔
84
                return nil, err
87✔
85
        }
87✔
86

87
        if plugin == "" {
10,737✔
88
                plugin = defaultAuthPlugin
×
89
        }
×
90

91
        // Send Client Authentication Packet
92
        authResp, err := mc.auth(authData, plugin)
10,737✔
93
        if err != nil {
10,737✔
94
                // try the default auth plugin, if using the requested plugin failed
×
95
                errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
×
96
                plugin = defaultAuthPlugin
×
97
                authResp, err = mc.auth(authData, plugin)
×
98
                if err != nil {
×
99
                        mc.cleanup()
×
100
                        return nil, err
×
101
                }
×
102
        }
103
        if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
10,737✔
104
                mc.cleanup()
×
105
                return nil, err
×
106
        }
×
107

108
        // Handle response to auth packet, switch methods if possible
109
        if err = mc.handleAuthResult(authData, plugin); err != nil {
10,824✔
110
                // Authentication failed and MySQL has already closed the connection
87✔
111
                // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
87✔
112
                // Do not send COM_QUIT, just cleanup and return the error.
87✔
113
                mc.cleanup()
87✔
114
                return nil, err
87✔
115
        }
87✔
116

117
        if mc.cfg.MaxAllowedPacket > 0 {
21,219✔
118
                mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
10,569✔
119
        } else {
10,650✔
120
                // Get max allowed packet size
81✔
121
                maxap, err := mc.getSystemVar("max_allowed_packet")
81✔
122
                if err != nil {
81✔
123
                        mc.Close()
×
124
                        return nil, err
×
125
                }
×
126
                mc.maxAllowedPacket = stringToInt(maxap) - 1
81✔
127
        }
128
        if mc.maxAllowedPacket < maxPacketSize {
21,218✔
129
                mc.maxWriteSize = mc.maxAllowedPacket
10,568✔
130
        }
10,568✔
131

132
        // Handle DSN Params
133
        err = mc.handleParams()
10,649✔
134
        if err != nil {
10,838✔
135
                mc.Close()
189✔
136
                return nil, err
189✔
137
        }
189✔
138

139
        return mc, nil
10,460✔
140
}
141

142
// Driver implements driver.Connector interface.
143
// Driver returns &MySQLDriver{}.
144
func (c *connector) Driver() driver.Driver {
×
145
        return &MySQLDriver{}
×
146
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc