Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
49 changes: 48 additions & 1 deletion mysql/resultset_helper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package mysql

import (
"bytes"
"encoding/binary"
"math"
"strconv"
"time"

"github.com/pingcap/errors"

Expand Down Expand Up @@ -39,13 +42,53 @@
return v, nil
case string:
return utils.StringToByteSlice(v), nil
case time.Time:
return hack.Slice(v.Format(time.DateTime)), nil

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / Tests with MySQL 8.0.40

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / platforms (amd64, linux)

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.23 on ubuntu-24.04

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / Tests with MySQL 8.4.3

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / platforms (amd64, freebsd)

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.23 on ubuntu-22.04

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / platforms (amd64, darwin)

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / platforms (arm64, linux)

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.22 on ubuntu-24.04

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / Tests Go 1.22 on ubuntu-22.04

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / platforms (arm64, freebsd)

undefined: hack

Check failure on line 46 in mysql/resultset_helper.go

View workflow job for this annotation

GitHub Actions / platforms (arm64, darwin)

undefined: hack
case nil:
return nil, nil
default:
return nil, errors.Errorf("invalid type %T", value)
}
}

func toBinaryDateTime(t time.Time) ([]byte, error) {
var buf bytes.Buffer

if t.IsZero() {
return nil, nil
}

year, month, day := t.Year(), t.Month(), t.Day()
hour, min, sec := t.Hour(), t.Minute(), t.Second()
nanosec := t.Nanosecond()

if nanosec > 0 {
buf.WriteByte(byte(11))
_ = binary.Write(&buf, binary.LittleEndian, uint16(year))
buf.WriteByte(byte(month))
buf.WriteByte(byte(day))
buf.WriteByte(byte(hour))
buf.WriteByte(byte(min))
buf.WriteByte(byte(sec))
_ = binary.Write(&buf, binary.LittleEndian, uint32(nanosec/1000))
} else if hour > 0 || min > 0 || sec > 0 {
buf.WriteByte(byte(7))
_ = binary.Write(&buf, binary.LittleEndian, uint16(year))
buf.WriteByte(byte(month))
buf.WriteByte(byte(day))
buf.WriteByte(byte(hour))
buf.WriteByte(byte(min))
buf.WriteByte(byte(sec))
} else {
buf.WriteByte(byte(4))
_ = binary.Write(&buf, binary.LittleEndian, uint16(year))
buf.WriteByte(byte(month))
buf.WriteByte(byte(day))
}

return buf.Bytes(), nil
}

func formatBinaryValue(value interface{}) ([]byte, error) {
switch v := value.(type) {
case int8:
Expand Down Expand Up @@ -76,6 +119,8 @@
return v, nil
case string:
return utils.StringToByteSlice(v), nil
case time.Time:
return toBinaryDateTime(v)
default:
return nil, errors.Errorf("invalid type %T", value)
}
Expand All @@ -91,6 +136,8 @@
typ = MYSQL_TYPE_DOUBLE
case string, []byte:
typ = MYSQL_TYPE_VAR_STRING
case time.Time:
typ = MYSQL_TYPE_DATETIME
case nil:
typ = MYSQL_TYPE_NULL
default:
Expand All @@ -110,7 +157,7 @@
case float32, float64:
field.Charset = 63
field.Flag = BINARY_FLAG | NOT_NULL_FLAG
case string, []byte:
case string, []byte, time.Time:
field.Charset = 33
case nil:
field.Charset = 33
Expand Down
94 changes: 94 additions & 0 deletions mysql/util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package mysql

import (
"encoding/binary"
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -51,3 +55,93 @@ func TestFormatBinaryTime(t *testing.T) {
require.Equal(t, test.Expect, string(got), "test case %v", test.Data)
}
}

// mysql driver parse binary datetime
func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (time.Time, error) {
switch num {
case 0:
return time.Time{}, nil
case 4:
return time.Date(
int(binary.LittleEndian.Uint16(data[:2])), // year
time.Month(data[2]), // month
int(data[3]), // day
0, 0, 0, 0,
loc,
), nil
case 7:
return time.Date(
int(binary.LittleEndian.Uint16(data[:2])), // year
time.Month(data[2]), // month
int(data[3]), // day
int(data[4]), // hour
int(data[5]), // minutes
int(data[6]), // seconds
0,
loc,
), nil
case 11:
return time.Date(
int(binary.LittleEndian.Uint16(data[:2])), // year
time.Month(data[2]), // month
int(data[3]), // day
int(data[4]), // hour
int(data[5]), // minutes
int(data[6]), // seconds
int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
loc,
), nil
}
return time.Time{}, fmt.Errorf("invalid DATETIME packet length %d", num)
}

func TestToBinaryDateTime(t *testing.T) {
tests := []struct {
name string
input time.Time
expected string
}{
{
name: "Zero time",
input: time.Time{},
expected: "",
},
{
name: "Date with nanoseconds",
input: time.Date(2023, 10, 10, 10, 10, 10, 123456000, time.UTC),
expected: "2023-10-10 10:10:10.123456 +0000 UTC",
},
{
name: "Date with time",
input: time.Date(2023, 10, 10, 10, 10, 10, 0, time.UTC),
expected: "2023-10-10 10:10:10 +0000 UTC",
},
{
name: "Date only",
input: time.Date(2023, 10, 10, 0, 0, 0, 0, time.UTC),
expected: "2023-10-10 00:00:00 +0000 UTC",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := toBinaryDateTime(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
return
}
num := uint64(result[0])
data := result[1:]
date, err := parseBinaryDateTime(num, data, time.UTC)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if !strings.EqualFold(date.String(), tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
Loading