Skip to content

modernize for Go 1.22 #1695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func benchmarkQueryHelper(b *testing.B, compr bool) {
defer wg.Wait()
b.StartTimer()

for i := 0; i < concurrencyLevel; i++ {
for range concurrencyLevel {
go func() {
for {
if atomic.AddInt64(&remain, -1) < 0 {
Expand Down Expand Up @@ -130,7 +130,7 @@ func BenchmarkExec(b *testing.B) {
defer wg.Wait()
b.StartTimer()

for i := 0; i < concurrencyLevel; i++ {
for range concurrencyLevel {
go func() {
for {
if atomic.AddInt64(&remain, -1) < 0 {
Expand Down Expand Up @@ -345,7 +345,7 @@ func BenchmarkQueryRawBytes(b *testing.B) {
for i := range blob {
blob[i] = 42
}
for i := 0; i < 100; i++ {
for i := range 100 {
_, err := db.Exec("INSERT INTO bench_rawbytes VALUES (?, ?)", i, blob)
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -401,7 +401,7 @@ func BenchmarkReceiveMassiveRows(b *testing.B) {
}
for i := 0; i < 10000; i += 100 {
args := make([]any, 200)
for j := 0; j < 100; j++ {
for j := range 100 {
args[j*2] = i + j
args[j*2+1] = sval
}
Expand Down
2 changes: 1 addition & 1 deletion connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestCleanCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

for i := 0; i < 3; i++ { // Repeat same behavior
for range 3 { // Repeat same behavior
err := mc.Ping(ctx)
if err != context.Canceled {
t.Errorf("expected context.Canceled, got %#v", err)
Expand Down
21 changes: 11 additions & 10 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"reflect"
"runtime"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -1926,7 +1927,7 @@ func TestPreparedManyCols(t *testing.T) {
rows.Close()

// Create 0byte string which we can't send via STMT_LONG_DATA.
for i := 0; i < numParams; i++ {
for i := range numParams {
params[i] = ""
}
rows, err = stmt.Query(params...)
Expand Down Expand Up @@ -1971,7 +1972,7 @@ func TestConcurrent(t *testing.T) {
})
}

for i := 0; i < max; i++ {
for i := range max {
go func(id int) {
defer wg.Done()

Expand Down Expand Up @@ -2355,7 +2356,7 @@ func TestPing(t *testing.T) {
q.Close()

// Verify that Ping() clears both fields.
for i := 0; i < 2; i++ {
for range 2 {
if err := c.Ping(ctx); err != nil {
dbt.fail("Pinger", "Ping", err)
}
Expand Down Expand Up @@ -2558,7 +2559,7 @@ func TestMultiResultSet(t *testing.T) {
}
defer stmt.Close()

for j := 0; j < 2; j++ {
for j := range 2 {
rows, err := stmt.Query()
if err != nil {
dbt.Fatalf("%v (i=%d) (j=%d)", err, i, j)
Expand Down Expand Up @@ -2665,7 +2666,7 @@ func TestQueryMultipleResults(t *testing.T) {
c := conn.(*mysqlConn)

// Demonstrate that repeated queries reset the affectedRows
for i := 0; i < 2; i++ {
for range 2 {
_, err := qr.Query(`
INSERT INTO test (value) VALUES ('a'), ('b');
INSERT INTO test (value) VALUES ('c'), ('d'), ('e');
Expand Down Expand Up @@ -3293,11 +3294,11 @@ func TestRawBytesAreNotModified(t *testing.T) {

runTests(t, dsn, func(dbt *DBTest) {
dbt.mustExec("CREATE TABLE test (id int, value BLOB) CHARACTER SET utf8")
for i := 0; i < insertRows; i++ {
for i := range insertRows {
dbt.mustExec("INSERT INTO test VALUES (?, ?)", i+1, sqlBlobs[i&1])
}

for i := 0; i < contextRaceIterations; i++ {
for i := range contextRaceIterations {
func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -3552,8 +3553,8 @@ func TestConnectionAttributes(t *testing.T) {
rowsMap[attrName] = attrValue
}

connAttrs := append(append([]string{}, defaultAttrs...), customAttrs...)
expectedAttrValues := append(append([]string{}, defaultAttrValues...), customAttrValues...)
connAttrs := slices.Concat(defaultAttrs, customAttrs)
expectedAttrValues := slices.Concat(defaultAttrValues, customAttrValues)
for i := range connAttrs {
if gotValue := rowsMap[connAttrs[i]]; gotValue != expectedAttrValues[i] {
dbt.Errorf("expected %q, got %q", expectedAttrValues[i], gotValue)
Expand Down Expand Up @@ -3637,7 +3638,7 @@ func TestIssue1567(t *testing.T) {
count = max
}

for i := 0; i < count; i++ {
for range count {
timeout := time.Duration(mrand.Int63n(int64(rtt)))
ctx, cancel := context.WithTimeout(context.Background(), timeout)
dbt.db.PingContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func ParseDSN(dsn string) (cfg *Config, err error) {
if dsn[j] == '@' {
// username[:password]
// Find the first ':' in dsn[:j]
for k = 0; k < j; k++ {
for k = 0; k < j; k++ { // We cannot use k = range j here, because we use dsn[:k] below
if dsn[k] == ':' {
cfg.Passwd = dsn[k+1 : j]
break
Expand Down
5 changes: 1 addition & 4 deletions infile.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ const defaultPacketSize = 16 * 1024 // 16KB is small enough for disk readahead a

func (mc *okHandler) handleInFileRequest(name string) (err error) {
var rdr io.Reader
packetSize := defaultPacketSize
if mc.maxWriteSize < packetSize {
packetSize = mc.maxWriteSize
}
packetSize := min(mc.maxWriteSize, defaultPacketSize)

if idx := strings.Index(name, "Reader::"); idx == 0 || (idx > 0 && name[idx-1] == '/') { // io.Reader
// The server might return an an absolute path. See issue #355.
Expand Down
5 changes: 1 addition & 4 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,10 +984,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
mc := stmt.mc

// Determine threshold dynamically to avoid packet size shortage.
longDataSize := mc.maxAllowedPacket / (stmt.paramCount + 1)
if longDataSize < 64 {
longDataSize = 64
}
longDataSize := max(mc.maxAllowedPacket/(stmt.paramCount+1), 64)

// Reset packet-sequence
mc.resetSequence()
Expand Down
6 changes: 4 additions & 2 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package mysql

import "slices"

import "database/sql/driver"

// Result exposes data not available through *connection.Result.
Expand Down Expand Up @@ -42,9 +44,9 @@ func (res *mysqlResult) RowsAffected() (int64, error) {
}

func (res *mysqlResult) AllLastInsertIds() []int64 {
return append([]int64{}, res.insertIds...) // defensive copy
return slices.Clone(res.insertIds) // defensive copy
}

func (res *mysqlResult) AllRowsAffected() []int64 {
return append([]int64{}, res.affectedRows...) // defensive copy
return slices.Clone(res.affectedRows) // defensive copy
}
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {

func parseByteYear(b []byte) (int, error) {
year, n := 0, 1000
for i := 0; i < 4; i++ {
for i := range 4 {
v, err := bToi(b[i])
if err != nil {
return 0, err
Expand All @@ -207,7 +207,7 @@ func parseByte2Digits(b1, b2 byte) (int, error) {

func parseByteNanoSec(b []byte) (int, error) {
ns, digit := 0, 100000 // max is 6-digits
for i := 0; i < len(b); i++ {
for i := range b {
v, err := bToi(b[i])
if err != nil {
return 0, err
Expand Down Expand Up @@ -678,7 +678,7 @@ func escapeStringBackslash(buf []byte, v string) []byte {
pos := len(buf)
buf = reserveBuffer(buf, len(v)*2)

for i := 0; i < len(v); i++ {
for i := range len(v) {
c := v[i]
switch c {
case '\x00':
Expand Down Expand Up @@ -746,7 +746,7 @@ func escapeStringQuotes(buf []byte, v string) []byte {
pos := len(buf)
buf = reserveBuffer(buf, len(v)*2)

for i := 0; i < len(v); i++ {
for i := range len(v) {
c := v[i]
if c == '\'' {
buf[pos+1] = '\''
Expand Down