Skip to content

Commit

Permalink
Merge pull request #231 from uptrace/fix/mysql-append-time-in-local-tz
Browse files Browse the repository at this point in the history
fix(mysqldialect): append time in local timezone
  • Loading branch information
vmihailenco authored Oct 3, 2021
2 parents 5d7e5c9 + e763cc8 commit 479e526
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 13 deletions.
8 changes: 0 additions & 8 deletions dialect/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"math"
"strconv"
"time"
"unicode/utf8"

"github.com/uptrace/bun/internal"
Expand Down Expand Up @@ -94,13 +93,6 @@ func AppendBytes(b []byte, bytes []byte) []byte {
return b
}

func AppendTime(b []byte, tm time.Time) []byte {
b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'')
return b
}

func AppendJSON(b, jsonb []byte) []byte {
b = append(b, '\'')

Expand Down
7 changes: 7 additions & 0 deletions dialect/mysqldialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func (d *Dialect) IdentQuote() byte {
return '`'
}

func (d *Dialect) AppendTime(b []byte, tm time.Time) []byte {
b = append(b, '\'')
b = tm.AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'')
return b
}

func (d *Dialect) Append(fmter schema.Formatter, b []byte, v interface{}) []byte {
switch v := v.(type) {
case time.Time:
Expand Down
9 changes: 8 additions & 1 deletion dialect/pgdialect/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func customAppender(typ reflect.Type) schema.AppenderFunc {
return nil
}

func appendTime(b []byte, tm time.Time) []byte {
b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'')
return b
}

func appendUint32ValueAsInt(fmter schema.Formatter, b []byte, v reflect.Value) []byte {
return strconv.AppendInt(b, int64(int32(v.Uint())), 10)
}
Expand All @@ -61,7 +68,7 @@ func arrayAppend(fmter schema.Formatter, b []byte, v interface{}) []byte {
case string:
return arrayAppendString(b, v)
case time.Time:
return dialect.AppendTime(b, v)
return appendTime(b, v)
default:
err := fmt.Errorf("pgdialect: can't append %T", v)
return dialect.AppendError(b, err)
Expand Down
6 changes: 5 additions & 1 deletion dialect/pgdialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (d *Dialect) IdentQuote() byte {
return '"'
}

func (d *Dialect) AppendTime(b []byte, tm time.Time) []byte {
return appendTime(b, tm)
}

func (d *Dialect) Append(fmter schema.Formatter, b []byte, v interface{}) []byte {
switch v := v.(type) {
case nil:
Expand All @@ -105,7 +109,7 @@ func (d *Dialect) Append(fmter schema.Formatter, b []byte, v interface{}) []byte
case string:
return dialect.AppendString(b, v)
case time.Time:
return dialect.AppendTime(b, v)
return appendTime(b, v)
case []byte:
return dialect.AppendBytes(b, v)
case schema.QueryAppender:
Expand Down
8 changes: 8 additions & 0 deletions dialect/sqlitedialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"reflect"
"sync"
"time"

"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/feature"
Expand Down Expand Up @@ -62,6 +63,13 @@ func (d *Dialect) IdentQuote() byte {
return '"'
}

func (d *Dialect) AppendTime(b []byte, tm time.Time) []byte {
b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'')
return b
}

func (d *Dialect) Append(fmter schema.Formatter, b []byte, v interface{}) []byte {
return schema.Append(fmter, b, v, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Append(fmter Formatter, b []byte, v interface{}, custom CustomAppender) []b
case string:
return dialect.AppendString(b, v)
case time.Time:
return dialect.AppendTime(b, v)
return fmter.Dialect().AppendTime(b, v)
case []byte:
return dialect.AppendBytes(b, v)
case QueryAppender:
Expand Down
2 changes: 1 addition & 1 deletion schema/append_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func AppendJSONValue(fmter Formatter, b []byte, v reflect.Value) []byte {

func appendTimeValue(fmter Formatter, b []byte, v reflect.Value) []byte {
tm := v.Interface().(time.Time)
return dialect.AppendTime(b, tm)
return fmter.Dialect().AppendTime(b, tm)
}

func appendIPValue(fmter Formatter, b []byte, v reflect.Value) []byte {
Expand Down
11 changes: 11 additions & 0 deletions schema/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"reflect"
"sync"
"time"

"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/feature"
Expand All @@ -19,8 +20,11 @@ type Dialect interface {
OnTable(table *Table)

IdentQuote() byte

AppendTime(b []byte, tm time.Time) []byte
Append(fmter Formatter, b []byte, v interface{}) []byte
Appender(typ reflect.Type) AppenderFunc

FieldAppender(field *Field) AppenderFunc
Scanner(typ reflect.Type) ScannerFunc
}
Expand Down Expand Up @@ -64,6 +68,13 @@ func (d *nopDialect) IdentQuote() byte {
return '"'
}

func (d *nopDialect) AppendTime(b []byte, tm time.Time) []byte {
b = append(b, '\'')
b = tm.UTC().AppendFormat(b, "2006-01-02 15:04:05.999999-07:00")
b = append(b, '\'')
return b
}

func (d *nopDialect) Append(fmter Formatter, b []byte, v interface{}) []byte {
return Append(fmter, b, v, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/sqltype.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (tm NullTime) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
if tm.IsZero() {
return dialect.AppendNull(b), nil
}
return dialect.AppendTime(b, tm.Time), nil
return fmter.Dialect().AppendTime(b, tm.Time), nil
}

func (tm *NullTime) Scan(src interface{}) error {
Expand Down

0 comments on commit 479e526

Please sign in to comment.