123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package db
- import (
- "time"
- )
- const (
- defaultDriver = "mysql"
- defaultHost = "localhost"
- defaultPort = 3306
- defaultSslMode = "disabled"
- )
- type Config struct {
- driver string
- username string
- password string
- dbname string
- host string
- port uint16
- sslMode string
- maxOpenConns uint16
- maxIdleConns uint16
- maxOpenConnLifetime *time.Duration
- maxIdleConnLifetime *time.Duration
- }
- type ConfigOption func(*Config)
- type ConfigOptions []ConfigOption
- func (s *ConfigOptions) Add(option ConfigOption) {
- *s = append(*s, option)
- }
- func NewConfig(opts ...ConfigOption) Config {
- c := &Config{}
- for _, opt := range opts {
- opt(c)
- }
- return *c
- }
- func WithDriver(driver string) ConfigOption {
- return func(s *Config) {
- s.driver = driver
- }
- }
- func WithUsername(username string) ConfigOption {
- return func(s *Config) {
- s.username = username
- }
- }
- func WithDatabase(dbname string) ConfigOption {
- return func(s *Config) {
- s.dbname = dbname
- }
- }
- func WithPassword(password string) ConfigOption {
- return func(s *Config) {
- s.password = password
- }
- }
- func WithHost(host string) ConfigOption {
- return func(s *Config) {
- s.host = host
- }
- }
- func WithPort(port uint16) ConfigOption {
- return func(s *Config) {
- s.port = port
- }
- }
- func WithSSLMode(sslMode string) ConfigOption {
- return func(s *Config) {
- s.sslMode = sslMode
- }
- }
- func WithMaxOpenConns(maxOpenConns uint16) ConfigOption {
- return func(s *Config) {
- s.maxOpenConns = maxOpenConns
- }
- }
- func WithMaxIdleConns(maxIdleConns uint16) ConfigOption {
- return func(s *Config) {
- s.maxIdleConns = maxIdleConns
- }
- }
- func WithMaxOpenConnLifetime(lifetime time.Duration) ConfigOption {
- return func(s *Config) {
- s.maxOpenConnLifetime = &lifetime
- }
- }
- func WithMaxIdleConnLifetime(lifetime time.Duration) ConfigOption {
- return func(s *Config) {
- s.maxIdleConnLifetime = &lifetime
- }
- }
|