Skip to content

Commit

Permalink
feat: Added support for datetime format
Browse files Browse the repository at this point in the history
  • Loading branch information
inoth committed Dec 6, 2024
1 parent 506416d commit fe07a17
Showing 1 changed file with 48 additions and 1 deletion.
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

0 comments on commit fe07a17

Please sign in to comment.