Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to initialize MySQLDialect with server timezone #1021

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions dialect/mysqldialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ func init() {
}
}

type DialectOption func(d *Dialect)

type Dialect struct {
schema.BaseDialect

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

func New() *Dialect {
func New(opts ...DialectOption) *Dialect {
d := new(Dialect)
d.tables = schema.NewTables(d)
d.features = feature.AutoIncrement |
Expand All @@ -47,9 +50,24 @@ func New() *Dialect {
feature.InsertOnDuplicateKey |
feature.SelectExists |
feature.CompositeIn

for _, opt := range opts {
opt(d)
}

return d
}

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
}
}

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 +121,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
Loading