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

go-sql-driver / mysql / 14570841909

21 Apr 2025 09:10AM UTC coverage: 83.0% (-0.01%) from 83.01%
14570841909

Pull #1692

github

web-flow
Merge branch 'master' into clientDeprecateEOF
Pull Request #1692: MariaDB metadata skipping

170 of 203 new or added lines in 6 files covered. (83.74%)

3 existing lines in 1 file now uncovered.

3320 of 4000 relevant lines covered (83.0%)

2184086.06 hits per line

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

61.9
/driver.go
1
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
// You can obtain one at http://mozilla.org/MPL/2.0/.
6

7
// Package mysql provides a MySQL driver for Go's database/sql package.
8
//
9
// The driver should be used via the database/sql package:
10
//
11
//        import "database/sql"
12
//        import _ "github.com/go-sql-driver/mysql"
13
//
14
//        db, err := sql.Open("mysql", "user:password@/dbname")
15
//
16
// See https://github.com/go-sql-driver/mysql#usage for details
17
package mysql
18

19
import (
20
        "context"
21
        "database/sql"
22
        "database/sql/driver"
23
        "net"
24
        "sync"
25
)
26

27
// MySQLDriver is exported to make the driver directly accessible.
28
// In general the driver is used via the database/sql package.
29
type MySQLDriver struct{}
30

31
// DialFunc is a function which can be used to establish the network connection.
32
// Custom dial functions must be registered with RegisterDial
33
//
34
// Deprecated: users should register a DialContextFunc instead
35
type DialFunc func(addr string) (net.Conn, error)
36

37
// DialContextFunc is a function which can be used to establish the network connection.
38
// Custom dial functions must be registered with RegisterDialContext
39
type DialContextFunc func(ctx context.Context, addr string) (net.Conn, error)
40

41
var (
42
        dialsLock sync.RWMutex
43
        dials     map[string]DialContextFunc
44
)
45

46
// RegisterDialContext registers a custom dial function. It can then be used by the
47
// network address mynet(addr), where mynet is the registered new network.
48
// The current context for the connection and its address is passed to the dial function.
49
func RegisterDialContext(net string, dial DialContextFunc) {
224✔
50
        dialsLock.Lock()
224✔
51
        defer dialsLock.Unlock()
224✔
52
        if dials == nil {
256✔
53
                dials = make(map[string]DialContextFunc)
32✔
54
        }
32✔
55
        dials[net] = dial
224✔
56
}
57

58
// DeregisterDialContext removes the custom dial function registered with the given net.
59
func DeregisterDialContext(net string) {
×
60
        dialsLock.Lock()
×
61
        defer dialsLock.Unlock()
×
62
        if dials != nil {
×
63
                delete(dials, net)
×
64
        }
×
65
}
66

67
// RegisterDial registers a custom dial function. It can then be used by the
68
// network address mynet(addr), where mynet is the registered new network.
69
// addr is passed as a parameter to the dial function.
70
//
71
// Deprecated: users should call RegisterDialContext instead
72
func RegisterDial(network string, dial DialFunc) {
×
73
        RegisterDialContext(network, func(_ context.Context, addr string) (net.Conn, error) {
×
74
                return dial(addr)
×
75
        })
×
76
}
77

78
// Open new Connection.
79
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
80
// the DSN string is formatted
81
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
32✔
82
        cfg, err := ParseDSN(dsn)
32✔
83
        if err != nil {
32✔
84
                return nil, err
×
85
        }
×
86
        c := newConnector(cfg)
32✔
87
        return c.Connect(context.Background())
32✔
88
}
89

90
// This variable can be replaced with -ldflags like below:
91
// go build "-ldflags=-X github.com/go-sql-driver/mysql.driverName=custom"
92
var driverName = "mysql"
93

94
func init() {
32✔
95
        if driverName != "" {
64✔
96
                sql.Register(driverName, &MySQLDriver{})
32✔
97
        }
32✔
98
}
99

100
// NewConnector returns new driver.Connector.
101
func NewConnector(cfg *Config) (driver.Connector, error) {
128✔
102
        cfg = cfg.Clone()
128✔
103
        // normalize the contents of cfg so calls to NewConnector have the same
128✔
104
        // behavior as MySQLDriver.OpenConnector
128✔
105
        if err := cfg.normalize(); err != nil {
128✔
106
                return nil, err
×
107
        }
×
108
        return newConnector(cfg), nil
128✔
109
}
110

111
// OpenConnector implements driver.DriverContext.
112
func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
5,154✔
113
        cfg, err := ParseDSN(dsn)
5,154✔
114
        if err != nil {
5,154✔
115
                return nil, err
×
116
        }
×
117
        return newConnector(cfg), nil
5,154✔
118
}
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