Skip to content

Commit

Permalink
add Charset() option (#1679)
Browse files Browse the repository at this point in the history
Fix #1664.
  • Loading branch information
methane authored Mar 10, 2025
1 parent 58941dd commit c879816
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type Config struct {
DBName string // Database name
Params map[string]string // Connection parameters
ConnectionAttributes string // Connection Attributes, comma-delimited string of user-defined "key:value" pairs
charsets []string // Connection charset. When set, this will be set in SET NAMES <charset> query
Collation string // Connection collation. When set, this will be set in SET NAMES <charset> COLLATE <collation> query
Loc *time.Location // Location for time.Time values
MaxAllowedPacket int // Max packet size allowed
Expand Down Expand Up @@ -81,6 +80,7 @@ type Config struct {
beforeConnect func(context.Context, *Config) error // Invoked before a connection is established
pubKey *rsa.PublicKey // Server public key
timeTruncate time.Duration // Truncate time.Time values to the specified duration
charsets []string // Connection charset. When set, this will be set in SET NAMES <charset> query
}

// Functional Options Pattern
Expand Down Expand Up @@ -135,6 +135,21 @@ func EnableCompression(yes bool) Option {
}
}

// Charset sets the connection charset and collation.
//
// charset is the connection charset.
// collation is the connection collation. It can be null or empty string.
//
// When collation is not specified, `SET NAMES <charset>` command is sent when the connection is established.
// When collation is specified, `SET NAMES <charset> COLLATE <collation>` command is sent when the connection is established.
func Charset(charset, collation string) Option {
return func(cfg *Config) error {
cfg.charsets = []string{charset}
cfg.Collation = collation
return nil
}
}

func (cfg *Config) Clone() *Config {
cp := *cfg
if cp.TLS != nil {
Expand Down

0 comments on commit c879816

Please sign in to comment.