Skip to content

Commit

Permalink
mysqldialect: add time location support
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ex committed Sep 10, 2024
1 parent 6c83b05 commit 3d5a68a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions dialect/mysqldialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Dialect struct {

tables *schema.Tables
features feature.Feature
loc *time.Location
}

func New() *Dialect {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 3d5a68a

Please sign in to comment.