File tree Expand file tree Collapse file tree 2 files changed +31
-7
lines changed Expand file tree Collapse file tree 2 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -71,19 +71,44 @@ type Config struct {
7171
7272 // private fields. new options should be come here
7373
74- pubKey * rsa.PublicKey // Server public key
75- timeTruncate time.Duration // Truncate time.Time values to the specified duration
74+ pubKey * rsa.PublicKey // Server public key
75+ timeTruncate time.Duration // Truncate time.Time values to the specified duration
7676}
7777
78+ // Functional Options Pattern
79+ // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
80+ type option func (* Config ) error
81+
7882// NewConfig creates a new Config and sets default values.
79- func NewConfig () * Config {
80- return & Config {
83+ func NewConfig (opts ... option ) * Config {
84+ cfg := & Config {
8185 Loc : time .UTC ,
8286 MaxAllowedPacket : defaultMaxAllowedPacket ,
8387 Logger : defaultLogger ,
8488 AllowNativePasswords : true ,
8589 CheckConnLiveness : true ,
8690 }
91+
92+ cfg .SetOptions (opts ... )
93+ return cfg
94+ }
95+
96+ func (c * Config ) SetOptions (opts ... option ) error {
97+ for _ , opt := range opts {
98+ err := opt (c )
99+ if err != nil {
100+ return err
101+ }
102+ }
103+ }
104+
105+ // TimeTruncate sets the time duration to truncate time.Time values in
106+ // query parameters.
107+ func TimeTruncate (d time.Duration ) option {
108+ return func (cfg * Config ) error {
109+ cfg .timeTruncate = d
110+ return nil
111+ }
87112}
88113
89114func (cfg * Config ) Clone () * Config {
Original file line number Diff line number Diff line change @@ -15,9 +15,8 @@ import "database/sql/driver"
1515// This is accessible by executing statements using sql.Conn.Raw() and
1616// downcasting the returned result:
1717//
18- // res, err := rawConn.Exec(...)
19- // res.(mysql.Result).AllRowsAffected()
20- //
18+ // res, err := rawConn.Exec(...)
19+ // res.(mysql.Result).AllRowsAffected()
2120type Result interface {
2221 driver.Result
2322 // AllRowsAffected returns a slice containing the affected rows for each
You can’t perform that action at this time.
0 commit comments