@@ -44,7 +44,6 @@ type Config struct {
44
44
DBName string // Database name
45
45
Params map [string ]string // Connection parameters
46
46
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
47
- charsets []string // Connection charset. When set, this will be set in SET NAMES <charset> query
48
47
Collation string // Connection collation. When set, this will be set in SET NAMES <charset> COLLATE <collation> query
49
48
Loc * time.Location // Location for time.Time values
50
49
MaxAllowedPacket int // Max packet size allowed
@@ -81,6 +80,7 @@ type Config struct {
81
80
beforeConnect func (context.Context , * Config ) error // Invoked before a connection is established
82
81
pubKey * rsa.PublicKey // Server public key
83
82
timeTruncate time.Duration // Truncate time.Time values to the specified duration
83
+ charsets []string // Connection charset. When set, this will be set in SET NAMES <charset> query
84
84
}
85
85
86
86
// Functional Options Pattern
@@ -135,6 +135,21 @@ func EnableCompression(yes bool) Option {
135
135
}
136
136
}
137
137
138
+ // Charset sets the connection charset and collation.
139
+ //
140
+ // charset is the connection charset.
141
+ // collation is the connection collation. It can be null or empty string.
142
+ //
143
+ // When collation is not specified, `SET NAMES <charset>` command is sent when the connection is established.
144
+ // When collation is specified, `SET NAMES <charset> COLLATE <collation>` command is sent when the connection is established.
145
+ func Charset (charset , collation string ) Option {
146
+ return func (cfg * Config ) error {
147
+ cfg .charsets = []string {charset }
148
+ cfg .Collation = collation
149
+ return nil
150
+ }
151
+ }
152
+
138
153
func (cfg * Config ) Clone () * Config {
139
154
cp := * cfg
140
155
if cp .TLS != nil {
@@ -307,6 +322,10 @@ func (cfg *Config) FormatDSN() string {
307
322
writeDSNParam (& buf , & hasParam , "columnsWithAlias" , "true" )
308
323
}
309
324
325
+ if cfg .ConnectionAttributes != "" {
326
+ writeDSNParam (& buf , & hasParam , "connectionAttributes" , url .QueryEscape (cfg .ConnectionAttributes ))
327
+ }
328
+
310
329
if cfg .compress {
311
330
writeDSNParam (& buf , & hasParam , "compress" , "true" )
312
331
}
0 commit comments