forked from paulusrobin/leaf-utilities
/
connectionOption.go
72 lines (54 loc) · 1.59 KB
/
connectionOption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package leafSql
import (
leafLogger "github.com/paulusrobin/leaf-utilities/logger/logger"
"time"
)
type (
ConnectionOption interface {
Apply(o *ConnectionOptions)
}
ConnectionOptions struct {
MaxIdleConnection, MaxOpenConnection int
ConnMaxLifetime time.Duration
LogMode bool
logger leafLogger.Logger
}
)
func (o ConnectionOptions) Logger() leafLogger.Logger {
return o.logger
}
type withMaxIdleConnection int
func (w withMaxIdleConnection) Apply(o *ConnectionOptions) {
o.MaxIdleConnection = int(w)
}
func WithMaxIdleConnection(maxIdleConnection int) ConnectionOption {
return withMaxIdleConnection(maxIdleConnection)
}
type withMaxOpenConnection int
func (w withMaxOpenConnection) Apply(o *ConnectionOptions) {
o.MaxOpenConnection = int(w)
}
func WithMaxOpenConnection(maxOpenConnection int) ConnectionOption {
return withMaxOpenConnection(maxOpenConnection)
}
type withLogMode bool
func (w withLogMode) Apply(o *ConnectionOptions) {
o.LogMode = bool(w)
}
func WithLogMode(logMode bool) ConnectionOption {
return withLogMode(logMode)
}
type withConnMaxLifetime time.Duration
func (w withConnMaxLifetime) Apply(o *ConnectionOptions) {
o.ConnMaxLifetime = time.Duration(w)
}
func WithConnMaxLifetime(connMaxLifetime time.Duration) ConnectionOption {
return withConnMaxLifetime(connMaxLifetime)
}
type withLogger struct{ leafLogger.Logger }
func (w withLogger) Apply(o *ConnectionOptions) {
o.logger = w.Logger
}
func WithLogger(logger leafLogger.Logger) ConnectionOption {
return withLogger{logger}
}