• 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

74.36
/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) {
189✔
50
        dialsLock.Lock()
189✔
51
        defer dialsLock.Unlock()
189✔
52
        if dials == nil {
216✔
53
                dials = make(map[string]DialContextFunc)
27✔
54
        }
27✔
55
        dials[net] = dial
189✔
56
}
57

58
// RegisterDial registers a custom dial function. It can then be used by the
59
// network address mynet(addr), where mynet is the registered new network.
60
// addr is passed as a parameter to the dial function.
61
//
62
// Deprecated: users should call RegisterDialContext instead
63
func RegisterDial(network string, dial DialFunc) {
×
64
        RegisterDialContext(network, func(_ context.Context, addr string) (net.Conn, error) {
×
65
                return dial(addr)
×
66
        })
×
67
}
68

69
// Open new Connection.
70
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
71
// the DSN string is formatted
72
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
27✔
73
        cfg, err := ParseDSN(dsn)
27✔
74
        if err != nil {
27✔
75
                return nil, err
×
76
        }
×
77
        c := &connector{
27✔
78
                cfg: cfg,
27✔
79
        }
27✔
80
        return c.Connect(context.Background())
27✔
81
}
82

83
func init() {
27✔
84
        sql.Register("mysql", &MySQLDriver{})
27✔
85
}
27✔
86

87
// NewConnector returns new driver.Connector.
88
func NewConnector(cfg *Config) (driver.Connector, error) {
81✔
89
        cfg = cfg.Clone()
81✔
90
        // normalize the contents of cfg so calls to NewConnector have the same
81✔
91
        // behavior as MySQLDriver.OpenConnector
81✔
92
        if err := cfg.normalize(); err != nil {
81✔
93
                return nil, err
×
94
        }
×
95
        return &connector{cfg: cfg}, nil
81✔
96
}
97

98
// OpenConnector implements driver.DriverContext.
99
func (d MySQLDriver) OpenConnector(dsn string) (driver.Connector, error) {
6,066✔
100
        cfg, err := ParseDSN(dsn)
6,066✔
101
        if err != nil {
6,066✔
102
                return nil, err
×
103
        }
×
104
        return &connector{
6,066✔
105
                cfg: cfg,
6,066✔
106
        }, nil
6,066✔
107
}
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