Skip to content
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

feat: Added time.Time support for datetime format #957

Merged
merged 10 commits into from
Dec 25, 2024
49 changes: 48 additions & 1 deletion mysql/resultset_helper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
package mysql

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

"github.com/pingcap/errors"
"github.com/siddontang/go/hack"
)

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 FormatTextValue(value interface{}) ([]byte, error) {
switch v := value.(type) {
case int8:
Expand Down Expand Up @@ -38,6 +79,8 @@ func FormatTextValue(value interface{}) ([]byte, error) {
return v, nil
case string:
return hack.Slice(v), nil
case time.Time:
return hack.Slice(v.Format(time.DateTime)), nil
case nil:
return nil, nil
default:
Expand Down Expand Up @@ -75,6 +118,8 @@ func formatBinaryValue(value interface{}) ([]byte, error) {
return v, nil
case string:
return hack.Slice(v), nil
case time.Time:
return toBinaryDateTime(v)
default:
return nil, errors.Errorf("invalid type %T", value)
}
Expand All @@ -90,6 +135,8 @@ func fieldType(value interface{}) (typ uint8, err error) {
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 @@ -109,7 +156,7 @@ func formatField(field *Field, value interface{}) error {
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
Loading