File tree 2 files changed +31
-7
lines changed
2 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -71,19 +71,44 @@ type Config struct {
71
71
72
72
// private fields. new options should be come here
73
73
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
76
76
}
77
77
78
+ // Functional Options Pattern
79
+ // https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
80
+ type option func (* Config ) error
81
+
78
82
// 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 {
81
85
Loc : time .UTC ,
82
86
MaxAllowedPacket : defaultMaxAllowedPacket ,
83
87
Logger : defaultLogger ,
84
88
AllowNativePasswords : true ,
85
89
CheckConnLiveness : true ,
86
90
}
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
+ }
87
112
}
88
113
89
114
func (cfg * Config ) Clone () * Config {
Original file line number Diff line number Diff line change @@ -15,9 +15,8 @@ import "database/sql/driver"
15
15
// This is accessible by executing statements using sql.Conn.Raw() and
16
16
// downcasting the returned result:
17
17
//
18
- // res, err := rawConn.Exec(...)
19
- // res.(mysql.Result).AllRowsAffected()
20
- //
18
+ // res, err := rawConn.Exec(...)
19
+ // res.(mysql.Result).AllRowsAffected()
21
20
type Result interface {
22
21
driver.Result
23
22
// AllRowsAffected returns a slice containing the affected rows for each
You can’t perform that action at this time.
0 commit comments