Skip to content

Commit

Permalink
Go1.19 is released (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Aug 19, 2022
1 parent ad9fa14 commit 0c62bb2
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 176 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Expand Up @@ -22,8 +22,9 @@ jobs:
import json
go = [
# Keep the most recent production release at the top
'1.18',
'1.19',
# Older production releases
'1.18',
'1.17',
'1.16',
'1.15',
Expand Down
19 changes: 19 additions & 0 deletions atomic_bool.go
@@ -0,0 +1,19 @@
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
//
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build go1.19
// +build go1.19

package mysql

import "sync/atomic"

/******************************************************************************
* Sync utils *
******************************************************************************/

type atomicBool = atomic.Bool
47 changes: 47 additions & 0 deletions atomic_bool_go118.go
@@ -0,0 +1,47 @@
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
//
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build !go1.19
// +build !go1.19

package mysql

import "sync/atomic"

/******************************************************************************
* Sync utils *
******************************************************************************/

// atomicBool is an implementation of atomic.Bool for older version of Go.
// it is a wrapper around uint32 for usage as a boolean value with
// atomic access.
type atomicBool struct {
_ noCopy
value uint32
}

// Load returns whether the current boolean value is true
func (ab *atomicBool) Load() bool {
return atomic.LoadUint32(&ab.value) > 0
}

// Store sets the value of the bool regardless of the previous value
func (ab *atomicBool) Store(value bool) {
if value {
atomic.StoreUint32(&ab.value, 1)
} else {
atomic.StoreUint32(&ab.value, 0)
}
}

// Swap sets the value of the bool and returns the old value.
func (ab *atomicBool) Swap(value bool) bool {
if value {
return atomic.SwapUint32(&ab.value, 1) > 0
}
return atomic.SwapUint32(&ab.value, 0) > 0
}
71 changes: 71 additions & 0 deletions atomic_bool_test.go
@@ -0,0 +1,71 @@
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
//
// Copyright 2022 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build !go1.19
// +build !go1.19

package mysql

import (
"testing"
)

func TestAtomicBool(t *testing.T) {
var ab atomicBool
if ab.Load() {
t.Fatal("Expected value to be false")
}

ab.Store(true)
if ab.value != 1 {
t.Fatal("Set(true) did not set value to 1")
}
if !ab.Load() {
t.Fatal("Expected value to be true")
}

ab.Store(true)
if !ab.Load() {
t.Fatal("Expected value to be true")
}

ab.Store(false)
if ab.value != 0 {
t.Fatal("Set(false) did not set value to 0")
}
if ab.Load() {
t.Fatal("Expected value to be false")
}

ab.Store(false)
if ab.Load() {
t.Fatal("Expected value to be false")
}
if ab.Swap(false) {
t.Fatal("Expected the old value to be false")
}
if ab.Swap(true) {
t.Fatal("Expected the old value to be false")
}
if !ab.Load() {
t.Fatal("Expected value to be true")
}

ab.Store(true)
if !ab.Load() {
t.Fatal("Expected value to be true")
}
if !ab.Swap(true) {
t.Fatal("Expected the old value to be true")
}
if !ab.Swap(false) {
t.Fatal("Expected the old value to be true")
}
if ab.Load() {
t.Fatal("Expected value to be false")
}
}
35 changes: 17 additions & 18 deletions auth.go
Expand Up @@ -33,27 +33,26 @@ var (
// Note: The provided rsa.PublicKey instance is exclusively owned by the driver
// after registering it and may not be modified.
//
// data, err := ioutil.ReadFile("mykey.pem")
// if err != nil {
// log.Fatal(err)
// }
// data, err := ioutil.ReadFile("mykey.pem")
// if err != nil {
// log.Fatal(err)
// }
//
// block, _ := pem.Decode(data)
// if block == nil || block.Type != "PUBLIC KEY" {
// log.Fatal("failed to decode PEM block containing public key")
// }
// block, _ := pem.Decode(data)
// if block == nil || block.Type != "PUBLIC KEY" {
// log.Fatal("failed to decode PEM block containing public key")
// }
//
// pub, err := x509.ParsePKIXPublicKey(block.Bytes)
// if err != nil {
// log.Fatal(err)
// }
//
// if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
// mysql.RegisterServerPubKey("mykey", rsaPubKey)
// } else {
// log.Fatal("not a RSA public key")
// }
// pub, err := x509.ParsePKIXPublicKey(block.Bytes)
// if err != nil {
// log.Fatal(err)
// }
//
// if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
// mysql.RegisterServerPubKey("mykey", rsaPubKey)
// } else {
// log.Fatal("not a RSA public key")
// }
func RegisterServerPubKey(name string, pubKey *rsa.PublicKey) {
serverPubKeyLock.Lock()
if serverPubKeyRegistry == nil {
Expand Down
3 changes: 2 additions & 1 deletion collations.go
Expand Up @@ -13,7 +13,8 @@ const binaryCollation = "binary"

// A list of available collations mapped to the internal ID.
// To update this map use the following MySQL query:
// SELECT COLLATION_NAME, ID FROM information_schema.COLLATIONS WHERE ID<256 ORDER BY ID
//
// SELECT COLLATION_NAME, ID FROM information_schema.COLLATIONS WHERE ID<256 ORDER BY ID
//
// Handshake packet have only 1 byte for collation_id. So we can't use collations with ID > 255.
//
Expand Down
1 change: 1 addition & 0 deletions conncheck.go
Expand Up @@ -6,6 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos

package mysql
Expand Down
1 change: 1 addition & 0 deletions conncheck_dummy.go
Expand Up @@ -6,6 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

//go:build !linux && !darwin && !dragonfly && !freebsd && !netbsd && !openbsd && !solaris && !illumos
// +build !linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!solaris,!illumos

package mysql
Expand Down
1 change: 1 addition & 0 deletions conncheck_test.go
Expand Up @@ -6,6 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos

package mysql
Expand Down
22 changes: 11 additions & 11 deletions connection.go
Expand Up @@ -104,7 +104,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
}

func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
if mc.closed.IsSet() {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
Expand All @@ -123,7 +123,7 @@ func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {

func (mc *mysqlConn) Close() (err error) {
// Makes Close idempotent
if !mc.closed.IsSet() {
if !mc.closed.Load() {
err = mc.writeCommandPacket(comQuit)
}

Expand All @@ -137,7 +137,7 @@ func (mc *mysqlConn) Close() (err error) {
// is called before auth or on auth failure because MySQL will have already
// closed the network connection.
func (mc *mysqlConn) cleanup() {
if !mc.closed.TrySet(true) {
if mc.closed.Swap(true) {
return
}

Expand All @@ -152,7 +152,7 @@ func (mc *mysqlConn) cleanup() {
}

func (mc *mysqlConn) error() error {
if mc.closed.IsSet() {
if mc.closed.Load() {
if err := mc.canceled.Value(); err != nil {
return err
}
Expand All @@ -162,7 +162,7 @@ func (mc *mysqlConn) error() error {
}

func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
if mc.closed.IsSet() {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
Expand Down Expand Up @@ -295,7 +295,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.IsSet() {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
Expand Down Expand Up @@ -356,7 +356,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.IsSet() {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
return nil, driver.ErrBadConn
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func (mc *mysqlConn) finish() {

// Ping implements driver.Pinger interface
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
if mc.closed.IsSet() {
if mc.closed.Load() {
errLog.Print(ErrInvalidConn)
return driver.ErrBadConn
}
Expand All @@ -469,7 +469,7 @@ func (mc *mysqlConn) Ping(ctx context.Context) (err error) {

// BeginTx implements driver.ConnBeginTx interface
func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if mc.closed.IsSet() {
if mc.closed.Load() {
return nil, driver.ErrBadConn
}

Expand Down Expand Up @@ -636,7 +636,7 @@ func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) {
// ResetSession implements driver.SessionResetter.
// (From Go 1.10)
func (mc *mysqlConn) ResetSession(ctx context.Context) error {
if mc.closed.IsSet() {
if mc.closed.Load() {
return driver.ErrBadConn
}
mc.reset = true
Expand All @@ -646,5 +646,5 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
// IsValid implements driver.Validator interface
// (From Go 1.15)
func (mc *mysqlConn) IsValid() bool {
return !mc.closed.IsSet()
return !mc.closed.Load()
}
2 changes: 1 addition & 1 deletion connection_test.go
Expand Up @@ -147,7 +147,7 @@ func TestCleanCancel(t *testing.T) {
t.Errorf("expected context.Canceled, got %#v", err)
}

if mc.closed.IsSet() {
if mc.closed.Load() {
t.Error("expected mc is not closed, closed actually")
}

Expand Down
6 changes: 3 additions & 3 deletions driver.go
Expand Up @@ -8,10 +8,10 @@
//
// The driver should be used via the database/sql package:
//
// import "database/sql"
// import _ "github.com/go-sql-driver/mysql"
// import "database/sql"
// import _ "github.com/go-sql-driver/mysql"
//
// db, err := sql.Open("mysql", "user:password@/dbname")
// db, err := sql.Open("mysql", "user:password@/dbname")
//
// See https://github.com/go-sql-driver/mysql#usage for details
package mysql
Expand Down
1 change: 1 addition & 0 deletions fuzz.go
Expand Up @@ -6,6 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

//go:build gofuzz
// +build gofuzz

package mysql
Expand Down
28 changes: 13 additions & 15 deletions infile.go
Expand Up @@ -28,12 +28,11 @@ var (
// Alternatively you can allow the use of all local files with
// the DSN parameter 'allowAllFiles=true'
//
// filePath := "/home/gopher/data.csv"
// mysql.RegisterLocalFile(filePath)
// err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
// if err != nil {
// ...
//
// filePath := "/home/gopher/data.csv"
// mysql.RegisterLocalFile(filePath)
// err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
// if err != nil {
// ...
func RegisterLocalFile(filePath string) {
fileRegisterLock.Lock()
// lazy map init
Expand All @@ -58,15 +57,14 @@ func DeregisterLocalFile(filePath string) {
// If the handler returns a io.ReadCloser Close() is called when the
// request is finished.
//
// mysql.RegisterReaderHandler("data", func() io.Reader {
// var csvReader io.Reader // Some Reader that returns CSV data
// ... // Open Reader here
// return csvReader
// })
// err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
// if err != nil {
// ...
//
// mysql.RegisterReaderHandler("data", func() io.Reader {
// var csvReader io.Reader // Some Reader that returns CSV data
// ... // Open Reader here
// return csvReader
// })
// err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
// if err != nil {
// ...
func RegisterReaderHandler(name string, handler func() io.Reader) {
readerRegisterLock.Lock()
// lazy map init
Expand Down

0 comments on commit 0c62bb2

Please sign in to comment.