From 3d5a68ad94faa2d87a75513505e94b8405499f49 Mon Sep 17 00:00:00 2001 From: m4ex <4exmail@gmail.com> Date: Tue, 10 Sep 2024 13:24:20 +0300 Subject: [PATCH 1/3] mysqldialect: add time location support --- dialect/mysqldialect/dialect.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/dialect/mysqldialect/dialect.go b/dialect/mysqldialect/dialect.go index 959747d94..865d2aa7a 100644 --- a/dialect/mysqldialect/dialect.go +++ b/dialect/mysqldialect/dialect.go @@ -32,6 +32,7 @@ type Dialect struct { tables *schema.Tables features feature.Feature + loc *time.Location } func New() *Dialect { @@ -50,6 +51,16 @@ func New() *Dialect { return d } +func NewWithLocation(loc string) *Dialect { + d := New() + location, err := time.LoadLocation(loc) + if err != nil { + panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err)) + } + d.loc = location + return d +} + func (d *Dialect) Init(db *sql.DB) { var version string if err := db.QueryRow("SELECT version()").Scan(&version); err != nil { @@ -103,9 +114,13 @@ func (d *Dialect) IdentQuote() byte { return '`' } -func (*Dialect) AppendTime(b []byte, tm time.Time) []byte { +func (d *Dialect) AppendTime(b []byte, tm time.Time) []byte { b = append(b, '\'') - b = tm.AppendFormat(b, "2006-01-02 15:04:05.999999") + if d.loc != nil { + b = tm.In(d.loc).AppendFormat(b, "2006-01-02 15:04:05.999999") + } else { + b = tm.AppendFormat(b, "2006-01-02 15:04:05.999999") + } b = append(b, '\'') return b } From 810f331e6a2f3dde24cd1d19a1515beadb9e7667 Mon Sep 17 00:00:00 2001 From: m4ex <4exmail@gmail.com> Date: Fri, 13 Sep 2024 15:09:19 +0300 Subject: [PATCH 2/3] method chaining setter --- dialect/mysqldialect/dialect.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dialect/mysqldialect/dialect.go b/dialect/mysqldialect/dialect.go index 865d2aa7a..959df66ae 100644 --- a/dialect/mysqldialect/dialect.go +++ b/dialect/mysqldialect/dialect.go @@ -51,8 +51,7 @@ func New() *Dialect { return d } -func NewWithLocation(loc string) *Dialect { - d := New() +func (d *Dialect) WithTimeLocation(loc string) *Dialect { location, err := time.LoadLocation(loc) if err != nil { panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err)) From 7f90f4c50783af94985cffc32685dea829bf46b9 Mon Sep 17 00:00:00 2001 From: m4ex <4exmail@gmail.com> Date: Wed, 25 Sep 2024 00:50:32 +0300 Subject: [PATCH 3/3] use functional Options Pattern --- dialect/mysqldialect/dialect.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/dialect/mysqldialect/dialect.go b/dialect/mysqldialect/dialect.go index 959df66ae..881aa7ebf 100644 --- a/dialect/mysqldialect/dialect.go +++ b/dialect/mysqldialect/dialect.go @@ -27,6 +27,8 @@ func init() { } } +type DialectOption func(d *Dialect) + type Dialect struct { schema.BaseDialect @@ -35,7 +37,7 @@ type Dialect struct { loc *time.Location } -func New() *Dialect { +func New(opts ...DialectOption) *Dialect { d := new(Dialect) d.tables = schema.NewTables(d) d.features = feature.AutoIncrement | @@ -48,16 +50,22 @@ func New() *Dialect { feature.InsertOnDuplicateKey | feature.SelectExists | feature.CompositeIn + + for _, opt := range opts { + opt(d) + } + return d } -func (d *Dialect) WithTimeLocation(loc string) *Dialect { - location, err := time.LoadLocation(loc) - if err != nil { - panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err)) +func WithTimeLocation(loc string) DialectOption { + return func(d *Dialect) { + location, err := time.LoadLocation(loc) + if err != nil { + panic(fmt.Errorf("mysqldialect can't load provided location %s: %s", loc, err)) + } + d.loc = location } - d.loc = location - return d } func (d *Dialect) Init(db *sql.DB) {