From e763cc81eac4b11fff4e074ad3ff6cd970a71697 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 3 Oct 2021 16:59:39 +0300 Subject: [PATCH] fix(mysqldialect): append time in local timezone --- dialect/append.go | 8 -------- dialect/mysqldialect/dialect.go | 7 +++++++ dialect/pgdialect/append.go | 9 ++++++++- dialect/pgdialect/dialect.go | 6 +++++- dialect/sqlitedialect/dialect.go | 8 ++++++++ schema/append.go | 2 +- schema/append_value.go | 2 +- schema/dialect.go | 11 +++++++++++ schema/sqltype.go | 2 +- 9 files changed, 42 insertions(+), 13 deletions(-) diff --git a/dialect/append.go b/dialect/append.go index 01e80745a..9fe4c00a6 100644 --- a/dialect/append.go +++ b/dialect/append.go @@ -4,7 +4,6 @@ import ( "encoding/hex" "math" "strconv" - "time" "unicode/utf8" "github.com/uptrace/bun/internal" @@ -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, '\'') diff --git a/dialect/mysqldialect/dialect.go b/dialect/mysqldialect/dialect.go index 46ef5b649..2cc09cfbe 100644 --- a/dialect/mysqldialect/dialect.go +++ b/dialect/mysqldialect/dialect.go @@ -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: diff --git a/dialect/pgdialect/append.go b/dialect/pgdialect/append.go index e8480b6be..430522b09 100644 --- a/dialect/pgdialect/append.go +++ b/dialect/pgdialect/append.go @@ -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) } @@ -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) diff --git a/dialect/pgdialect/dialect.go b/dialect/pgdialect/dialect.go index 837c2d8ab..8e3fbe50a 100644 --- a/dialect/pgdialect/dialect.go +++ b/dialect/pgdialect/dialect.go @@ -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: @@ -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: diff --git a/dialect/sqlitedialect/dialect.go b/dialect/sqlitedialect/dialect.go index 32fd8d3fa..251ac4a36 100644 --- a/dialect/sqlitedialect/dialect.go +++ b/dialect/sqlitedialect/dialect.go @@ -4,6 +4,7 @@ import ( "database/sql" "reflect" "sync" + "time" "github.com/uptrace/bun/dialect" "github.com/uptrace/bun/dialect/feature" @@ -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) } diff --git a/schema/append.go b/schema/append.go index 57d292bfe..9dfc56c37 100644 --- a/schema/append.go +++ b/schema/append.go @@ -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: diff --git a/schema/append_value.go b/schema/append_value.go index 8fe8a13b2..3d0f37f8d 100644 --- a/schema/append_value.go +++ b/schema/append_value.go @@ -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 { diff --git a/schema/dialect.go b/schema/dialect.go index c50de715a..3ce530d96 100644 --- a/schema/dialect.go +++ b/schema/dialect.go @@ -4,6 +4,7 @@ import ( "database/sql" "reflect" "sync" + "time" "github.com/uptrace/bun/dialect" "github.com/uptrace/bun/dialect/feature" @@ -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 } @@ -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) } diff --git a/schema/sqltype.go b/schema/sqltype.go index 72609cf7a..76259a67b 100644 --- a/schema/sqltype.go +++ b/schema/sqltype.go @@ -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 {