Skip to content

Commit 0c62bb2

Browse files
authored
Go1.19 is released (#1350)
1 parent ad9fa14 commit 0c62bb2

19 files changed

+226
-176
lines changed

.github/workflows/test.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ jobs:
2222
import json
2323
go = [
2424
# Keep the most recent production release at the top
25-
'1.18',
25+
'1.19',
2626
# Older production releases
27+
'1.18',
2728
'1.17',
2829
'1.16',
2930
'1.15',

atomic_bool.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
2+
//
3+
// Copyright 2022 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+
//go:build go1.19
9+
// +build go1.19
10+
11+
package mysql
12+
13+
import "sync/atomic"
14+
15+
/******************************************************************************
16+
* Sync utils *
17+
******************************************************************************/
18+
19+
type atomicBool = atomic.Bool

atomic_bool_go118.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
2+
//
3+
// Copyright 2022 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+
//go:build !go1.19
9+
// +build !go1.19
10+
11+
package mysql
12+
13+
import "sync/atomic"
14+
15+
/******************************************************************************
16+
* Sync utils *
17+
******************************************************************************/
18+
19+
// atomicBool is an implementation of atomic.Bool for older version of Go.
20+
// it is a wrapper around uint32 for usage as a boolean value with
21+
// atomic access.
22+
type atomicBool struct {
23+
_ noCopy
24+
value uint32
25+
}
26+
27+
// Load returns whether the current boolean value is true
28+
func (ab *atomicBool) Load() bool {
29+
return atomic.LoadUint32(&ab.value) > 0
30+
}
31+
32+
// Store sets the value of the bool regardless of the previous value
33+
func (ab *atomicBool) Store(value bool) {
34+
if value {
35+
atomic.StoreUint32(&ab.value, 1)
36+
} else {
37+
atomic.StoreUint32(&ab.value, 0)
38+
}
39+
}
40+
41+
// Swap sets the value of the bool and returns the old value.
42+
func (ab *atomicBool) Swap(value bool) bool {
43+
if value {
44+
return atomic.SwapUint32(&ab.value, 1) > 0
45+
}
46+
return atomic.SwapUint32(&ab.value, 0) > 0
47+
}

atomic_bool_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package.
2+
//
3+
// Copyright 2022 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+
//go:build !go1.19
9+
// +build !go1.19
10+
11+
package mysql
12+
13+
import (
14+
"testing"
15+
)
16+
17+
func TestAtomicBool(t *testing.T) {
18+
var ab atomicBool
19+
if ab.Load() {
20+
t.Fatal("Expected value to be false")
21+
}
22+
23+
ab.Store(true)
24+
if ab.value != 1 {
25+
t.Fatal("Set(true) did not set value to 1")
26+
}
27+
if !ab.Load() {
28+
t.Fatal("Expected value to be true")
29+
}
30+
31+
ab.Store(true)
32+
if !ab.Load() {
33+
t.Fatal("Expected value to be true")
34+
}
35+
36+
ab.Store(false)
37+
if ab.value != 0 {
38+
t.Fatal("Set(false) did not set value to 0")
39+
}
40+
if ab.Load() {
41+
t.Fatal("Expected value to be false")
42+
}
43+
44+
ab.Store(false)
45+
if ab.Load() {
46+
t.Fatal("Expected value to be false")
47+
}
48+
if ab.Swap(false) {
49+
t.Fatal("Expected the old value to be false")
50+
}
51+
if ab.Swap(true) {
52+
t.Fatal("Expected the old value to be false")
53+
}
54+
if !ab.Load() {
55+
t.Fatal("Expected value to be true")
56+
}
57+
58+
ab.Store(true)
59+
if !ab.Load() {
60+
t.Fatal("Expected value to be true")
61+
}
62+
if !ab.Swap(true) {
63+
t.Fatal("Expected the old value to be true")
64+
}
65+
if !ab.Swap(false) {
66+
t.Fatal("Expected the old value to be true")
67+
}
68+
if ab.Load() {
69+
t.Fatal("Expected value to be false")
70+
}
71+
}

auth.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,26 @@ var (
3333
// Note: The provided rsa.PublicKey instance is exclusively owned by the driver
3434
// after registering it and may not be modified.
3535
//
36-
// data, err := ioutil.ReadFile("mykey.pem")
37-
// if err != nil {
38-
// log.Fatal(err)
39-
// }
36+
// data, err := ioutil.ReadFile("mykey.pem")
37+
// if err != nil {
38+
// log.Fatal(err)
39+
// }
4040
//
41-
// block, _ := pem.Decode(data)
42-
// if block == nil || block.Type != "PUBLIC KEY" {
43-
// log.Fatal("failed to decode PEM block containing public key")
44-
// }
41+
// block, _ := pem.Decode(data)
42+
// if block == nil || block.Type != "PUBLIC KEY" {
43+
// log.Fatal("failed to decode PEM block containing public key")
44+
// }
4545
//
46-
// pub, err := x509.ParsePKIXPublicKey(block.Bytes)
47-
// if err != nil {
48-
// log.Fatal(err)
49-
// }
50-
//
51-
// if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
52-
// mysql.RegisterServerPubKey("mykey", rsaPubKey)
53-
// } else {
54-
// log.Fatal("not a RSA public key")
55-
// }
46+
// pub, err := x509.ParsePKIXPublicKey(block.Bytes)
47+
// if err != nil {
48+
// log.Fatal(err)
49+
// }
5650
//
51+
// if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
52+
// mysql.RegisterServerPubKey("mykey", rsaPubKey)
53+
// } else {
54+
// log.Fatal("not a RSA public key")
55+
// }
5756
func RegisterServerPubKey(name string, pubKey *rsa.PublicKey) {
5857
serverPubKeyLock.Lock()
5958
if serverPubKeyRegistry == nil {

collations.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const binaryCollation = "binary"
1313

1414
// A list of available collations mapped to the internal ID.
1515
// To update this map use the following MySQL query:
16-
// SELECT COLLATION_NAME, ID FROM information_schema.COLLATIONS WHERE ID<256 ORDER BY ID
16+
//
17+
// SELECT COLLATION_NAME, ID FROM information_schema.COLLATIONS WHERE ID<256 ORDER BY ID
1718
//
1819
// Handshake packet have only 1 byte for collation_id. So we can't use collations with ID > 255.
1920
//

conncheck.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
77
// You can obtain one at http://mozilla.org/MPL/2.0/.
88

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

1112
package mysql

conncheck_dummy.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
77
// You can obtain one at http://mozilla.org/MPL/2.0/.
88

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

1112
package mysql

conncheck_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
77
// You can obtain one at http://mozilla.org/MPL/2.0/.
88

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

1112
package mysql

connection.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
104104
}
105105

106106
func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
107-
if mc.closed.IsSet() {
107+
if mc.closed.Load() {
108108
errLog.Print(ErrInvalidConn)
109109
return nil, driver.ErrBadConn
110110
}
@@ -123,7 +123,7 @@ func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
123123

124124
func (mc *mysqlConn) Close() (err error) {
125125
// Makes Close idempotent
126-
if !mc.closed.IsSet() {
126+
if !mc.closed.Load() {
127127
err = mc.writeCommandPacket(comQuit)
128128
}
129129

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

@@ -152,7 +152,7 @@ func (mc *mysqlConn) cleanup() {
152152
}
153153

154154
func (mc *mysqlConn) error() error {
155-
if mc.closed.IsSet() {
155+
if mc.closed.Load() {
156156
if err := mc.canceled.Value(); err != nil {
157157
return err
158158
}
@@ -162,7 +162,7 @@ func (mc *mysqlConn) error() error {
162162
}
163163

164164
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
165-
if mc.closed.IsSet() {
165+
if mc.closed.Load() {
166166
errLog.Print(ErrInvalidConn)
167167
return nil, driver.ErrBadConn
168168
}
@@ -295,7 +295,7 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
295295
}
296296

297297
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
298-
if mc.closed.IsSet() {
298+
if mc.closed.Load() {
299299
errLog.Print(ErrInvalidConn)
300300
return nil, driver.ErrBadConn
301301
}
@@ -356,7 +356,7 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
356356
}
357357

358358
func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
359-
if mc.closed.IsSet() {
359+
if mc.closed.Load() {
360360
errLog.Print(ErrInvalidConn)
361361
return nil, driver.ErrBadConn
362362
}
@@ -450,7 +450,7 @@ func (mc *mysqlConn) finish() {
450450

451451
// Ping implements driver.Pinger interface
452452
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
453-
if mc.closed.IsSet() {
453+
if mc.closed.Load() {
454454
errLog.Print(ErrInvalidConn)
455455
return driver.ErrBadConn
456456
}
@@ -469,7 +469,7 @@ func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
469469

470470
// BeginTx implements driver.ConnBeginTx interface
471471
func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
472-
if mc.closed.IsSet() {
472+
if mc.closed.Load() {
473473
return nil, driver.ErrBadConn
474474
}
475475

@@ -636,7 +636,7 @@ func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) {
636636
// ResetSession implements driver.SessionResetter.
637637
// (From Go 1.10)
638638
func (mc *mysqlConn) ResetSession(ctx context.Context) error {
639-
if mc.closed.IsSet() {
639+
if mc.closed.Load() {
640640
return driver.ErrBadConn
641641
}
642642
mc.reset = true
@@ -646,5 +646,5 @@ func (mc *mysqlConn) ResetSession(ctx context.Context) error {
646646
// IsValid implements driver.Validator interface
647647
// (From Go 1.15)
648648
func (mc *mysqlConn) IsValid() bool {
649-
return !mc.closed.IsSet()
649+
return !mc.closed.Load()
650650
}

connection_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestCleanCancel(t *testing.T) {
147147
t.Errorf("expected context.Canceled, got %#v", err)
148148
}
149149

150-
if mc.closed.IsSet() {
150+
if mc.closed.Load() {
151151
t.Error("expected mc is not closed, closed actually")
152152
}
153153

driver.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
//
99
// The driver should be used via the database/sql package:
1010
//
11-
// import "database/sql"
12-
// import _ "github.com/go-sql-driver/mysql"
11+
// import "database/sql"
12+
// import _ "github.com/go-sql-driver/mysql"
1313
//
14-
// db, err := sql.Open("mysql", "user:password@/dbname")
14+
// db, err := sql.Open("mysql", "user:password@/dbname")
1515
//
1616
// See https://github.com/go-sql-driver/mysql#usage for details
1717
package mysql

fuzz.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
77
// You can obtain one at http://mozilla.org/MPL/2.0/.
88

9+
//go:build gofuzz
910
// +build gofuzz
1011

1112
package mysql

infile.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ var (
2828
// Alternatively you can allow the use of all local files with
2929
// the DSN parameter 'allowAllFiles=true'
3030
//
31-
// filePath := "/home/gopher/data.csv"
32-
// mysql.RegisterLocalFile(filePath)
33-
// err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
34-
// if err != nil {
35-
// ...
36-
//
31+
// filePath := "/home/gopher/data.csv"
32+
// mysql.RegisterLocalFile(filePath)
33+
// err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
34+
// if err != nil {
35+
// ...
3736
func RegisterLocalFile(filePath string) {
3837
fileRegisterLock.Lock()
3938
// lazy map init
@@ -58,15 +57,14 @@ func DeregisterLocalFile(filePath string) {
5857
// If the handler returns a io.ReadCloser Close() is called when the
5958
// request is finished.
6059
//
61-
// mysql.RegisterReaderHandler("data", func() io.Reader {
62-
// var csvReader io.Reader // Some Reader that returns CSV data
63-
// ... // Open Reader here
64-
// return csvReader
65-
// })
66-
// err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
67-
// if err != nil {
68-
// ...
69-
//
60+
// mysql.RegisterReaderHandler("data", func() io.Reader {
61+
// var csvReader io.Reader // Some Reader that returns CSV data
62+
// ... // Open Reader here
63+
// return csvReader
64+
// })
65+
// err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
66+
// if err != nil {
67+
// ...
7068
func RegisterReaderHandler(name string, handler func() io.Reader) {
7169
readerRegisterLock.Lock()
7270
// lazy map init

0 commit comments

Comments
 (0)