Skip to content

Commit

Permalink
Merge branch 'master' into connect_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed May 5, 2023
2 parents de51e0d + 191a7c4 commit 40aed26
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 71 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/test.yml
Expand Up @@ -23,19 +23,16 @@ jobs:
import os
go = [
# Keep the most recent production release at the top
'1.19',
'1.20',
# Older production releases
'1.19',
'1.18',
'1.17',
'1.16',
'1.15',
'1.14',
'1.13',
]
mysql = [
'8.0',
'5.7',
'5.6',
'mariadb-10.11',
'mariadb-10.6',
'mariadb-10.5',
'mariadb-10.4',
Expand All @@ -45,7 +42,7 @@ jobs:
includes = []
# Go versions compatibility check
for v in go[1:]:
includes.append({'os': 'ubuntu-latest', 'go': v, 'mysql': mysql[0]})
includes.append({'os': 'ubuntu-latest', 'go': v, 'mysql': mysql[0]})
matrix = {
# OS vs MySQL versions
Expand All @@ -67,10 +64,10 @@ jobs:
matrix: ${{ fromJSON(needs.list.outputs.matrix) }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- uses: shogo82148/actions-setup-mysql@v1
- uses: shogo82148/actions-setup-mysql@v1.16.0
with:
mysql-version: ${{ matrix.mysql }}
user: ${{ env.MYSQL_TEST_USER }}
Expand Down
4 changes: 4 additions & 0 deletions AUTHORS
Expand Up @@ -47,6 +47,7 @@ INADA Naoki <songofacandy at gmail.com>
Jacek Szwec <szwec.jacek at gmail.com>
James Harr <james.harr at gmail.com>
Janek Vedock <janekvedock at comcast.net>
Jean-Yves Pellé <jy at pelle.link>
Jeff Hodges <jeff at somethingsimilar.com>
Jeffrey Charles <jeffreycharles at gmail.com>
Jerome Meyer <jxmeyer at gmail.com>
Expand Down Expand Up @@ -78,11 +79,13 @@ Olivier Mengué <dolmen at cpan.org>
oscarzhao <oscarzhaosl at gmail.com>
Paul Bonser <misterpib at gmail.com>
Peter Schultz <peter.schultz at classmarkets.com>
Phil Porada <philporada at gmail.com>
Rebecca Chin <rchin at pivotal.io>
Reed Allman <rdallman10 at gmail.com>
Richard Wilkes <wilkes at me.com>
Robert Russell <robert at rrbrussell.com>
Runrioter Wung <runrioter at gmail.com>
Samantha Frank <hello at entropy.cat>
Santhosh Kumar Tekuri <santhosh.tekuri at gmail.com>
Sho Iizuka <sho.i518 at gmail.com>
Sho Ikeda <suicaicoca at gmail.com>
Expand All @@ -93,6 +96,7 @@ Stan Putrya <root.vagner at gmail.com>
Stanley Gunawan <gunawan.stanley at gmail.com>
Steven Hartland <steven.hartland at multiplay.co.uk>
Tan Jinhua <312841925 at qq.com>
Tetsuro Aoki <t.aoki1130 at gmail.com>
Thomas Wodarek <wodarekwebpage at gmail.com>
Tim Ruffles <timruffles at gmail.com>
Tom Jenkinson <tom at tjenkinson.me>
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,16 @@
## Version 1.7.1 (2023-04-25)

Changes:

- bump actions/checkout@v3 and actions/setup-go@v3 (#1375)
- Add go1.20 and mariadb10.11 to the testing matrix (#1403)
- Increase default maxAllowedPacket size. (#1411)

Bugfixes:

- Use SET syntax as specified in the MySQL documentation (#1402)


## Version 1.7 (2022-11-29)

Changes:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -40,8 +40,8 @@ A MySQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) pac
* Optional placeholder interpolation

## Requirements
* Go 1.13 or higher. We aim to support the 3 latest versions of Go.
* MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
* Go 1.18 or higher. We aim to support the 3 latest versions of Go.
* MySQL (5.6+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)

---------------------------------------

Expand Down Expand Up @@ -282,10 +282,10 @@ Please keep in mind, that param values must be [url.QueryEscape](https://golang.
##### `maxAllowedPacket`
```
Type: decimal number
Default: 4194304
Default: 64*1024*1024
```

Max packet size allowed in bytes. The default value is 4 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
Max packet size allowed in bytes. The default value is 64 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.

##### `multiStatements`

Expand Down
2 changes: 1 addition & 1 deletion auth.go
Expand Up @@ -291,7 +291,7 @@ func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
return enc, err

default:
errLog.Print("unknown auth plugin:", plugin)
mc.cfg.Logger.Print("unknown auth plugin:", plugin)
return nil, ErrUnknownPlugin
}
}
Expand Down
22 changes: 11 additions & 11 deletions connection.go
Expand Up @@ -68,13 +68,13 @@ func (mc *mysqlConn) handleParams() (err error) {
default:
if cmdSet.Len() == 0 {
// Heuristic: 29 chars for each other key=value to reduce reallocations
cmdSet.Grow(4 + len(param) + 1 + len(val) + 30*(len(mc.cfg.Params)-1))
cmdSet.Grow(4 + len(param) + 3 + len(val) + 30*(len(mc.cfg.Params)-1))
cmdSet.WriteString("SET ")
} else {
cmdSet.WriteByte(',')
cmdSet.WriteString(", ")
}
cmdSet.WriteString(param)
cmdSet.WriteByte('=')
cmdSet.WriteString(" = ")
cmdSet.WriteString(val)
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {

func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
mc.cfg.Logger.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
var q string
Expand Down Expand Up @@ -147,7 +147,7 @@ func (mc *mysqlConn) cleanup() {
return
}
if err := mc.netConn.Close(); err != nil {
errLog.Print(err)
mc.cfg.Logger.Print(err)
}
}

Expand All @@ -163,14 +163,14 @@ func (mc *mysqlConn) error() error {

func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
mc.cfg.Logger.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
// Send command
err := mc.writeCommandPacketStr(comStmtPrepare, query)
if err != nil {
// STMT_PREPARE is safe to retry. So we can return ErrBadConn here.
errLog.Print(err)
mc.cfg.Logger.Print(err)
return nil, driver.ErrBadConn
}

Expand Down Expand Up @@ -204,7 +204,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
buf, err := mc.buf.takeCompleteBuffer()
if err != nil {
// can not take the buffer. Something must be wrong with the connection
errLog.Print(err)
mc.cfg.Logger.Print(err)
return "", ErrInvalidConn
}
buf = buf[:0]
Expand Down Expand Up @@ -296,7 +296,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin

func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
mc.cfg.Logger.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
if len(args) != 0 {
Expand Down Expand Up @@ -357,7 +357,7 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro

func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
mc.cfg.Logger.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
if len(args) != 0 {
Expand Down Expand Up @@ -451,7 +451,7 @@ func (mc *mysqlConn) finish() {
// Ping implements driver.Pinger interface
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
mc.cfg.Logger.Print(ErrInvalidConn)
return driver.ErrBadConn
}

Expand Down
1 change: 1 addition & 0 deletions connection_test.go
Expand Up @@ -179,6 +179,7 @@ func TestPingErrInvalidConn(t *testing.T) {
buf: newBuffer(nc),
maxAllowedPacket: defaultMaxAllowedPacket,
closech: make(chan struct{}),
cfg: NewConfig(),
}

err := ms.Ping(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion connector.go
Expand Up @@ -92,7 +92,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
authResp, err := mc.auth(authData, plugin)
if err != nil {
// try the default auth plugin, if using the requested plugin failed
errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
c.cfg.Logger.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
plugin = defaultAuthPlugin
authResp, err = mc.auth(authData, plugin)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion const.go
Expand Up @@ -12,7 +12,7 @@ import "runtime"

const (
defaultAuthPlugin = "mysql_native_password"
defaultMaxAllowedPacket = 4 << 20 // 4 MiB
defaultMaxAllowedPacket = 64 << 20 // 64 MiB. See https://github.com/go-sql-driver/mysql/issues/1355
minProtocolVersion = 10
maxPacketSize = 1<<24 - 1
timeFormat = "2006-01-02 15:04:05.999999"
Expand Down
11 changes: 11 additions & 0 deletions driver.go
Expand Up @@ -55,6 +55,17 @@ func RegisterDialContext(net string, dial DialContextFunc) {
dials[net] = dial
}

// DeregisterDialContext removes the custom dial function registered with the given net.
func DeregisterDialContext(net string) {
dialsLock.Lock()
defer dialsLock.Unlock()
if dials != nil {
if _, ok := dials[net]; ok {
delete(dials, net)
}
}
}

// RegisterDial registers a custom dial function. It can then be used by the
// network address mynet(addr), where mynet is the registered new network.
// addr is passed as a parameter to the dial function.
Expand Down
6 changes: 3 additions & 3 deletions driver_test.go
Expand Up @@ -1995,7 +1995,7 @@ func TestInsertRetrieveEscapedData(t *testing.T) {
func TestUnixSocketAuthFail(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
// Save the current logger so we can restore it.
oldLogger := errLog
oldLogger := defaultLogger

// Set a new logger so we can capture its output.
buffer := bytes.NewBuffer(make([]byte, 0, 64))
Expand Down Expand Up @@ -2703,7 +2703,7 @@ func TestContextBeginIsolationLevel(t *testing.T) {
if err := row.Scan(&v); err != nil {
dbt.Fatal(err)
}
// Because writer transaction wasn't commited yet, it should be available
// Because writer transaction wasn't committed yet, it should be available
if v != 0 {
dbt.Errorf("expected val to be 0, got %d", v)
}
Expand All @@ -2717,7 +2717,7 @@ func TestContextBeginIsolationLevel(t *testing.T) {
if err := row.Scan(&v); err != nil {
dbt.Fatal(err)
}
// Data written by writer transaction is already commited, it should be selectable
// Data written by writer transaction is already committed, it should be selectable
if v != 1 {
dbt.Errorf("expected val to be 1, got %d", v)
}
Expand Down
8 changes: 7 additions & 1 deletion dsn.go
Expand Up @@ -40,6 +40,7 @@ type Config struct {
Addr string // Network address (requires Net)
DBName string // Database name
Params map[string]string // Connection parameters
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
Collation string // Connection collation
Loc *time.Location // Location for time.Time values
MaxAllowedPacket int // Max packet size allowed
Expand All @@ -50,7 +51,7 @@ type Config struct {
Timeout time.Duration // Dial timeout
ReadTimeout time.Duration // I/O read timeout
WriteTimeout time.Duration // I/O write timeout
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
Logger Logger // Logger

AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
AllowCleartextPasswords bool // Allows the cleartext client side plugin
Expand All @@ -72,6 +73,7 @@ func NewConfig() *Config {
Collation: defaultCollation,
Loc: time.UTC,
MaxAllowedPacket: defaultMaxAllowedPacket,
Logger: defaultLogger,
AllowNativePasswords: true,
CheckConnLiveness: true,
}
Expand Down Expand Up @@ -154,6 +156,10 @@ func (cfg *Config) normalize() error {
}
}

if cfg.Logger == nil {
cfg.Logger = defaultLogger
}

return nil
}

Expand Down

0 comments on commit 40aed26

Please sign in to comment.