-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
92 lines (73 loc) · 1.9 KB
/
types.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package db
import (
"net/url"
"strings"
"go.uber.org/dig"
"go.uber.org/zap"
"upper.io/db.v3"
"upper.io/db.v3/lib/sqlbuilder"
)
type (
// MySQL is wrapper over upper/db Database
MySQL interface {
db.Database
sqlbuilder.SQLBuilder
}
mysqlConnection struct {
db.Database
sqlbuilder.SQLBuilder
}
// PG is wrapper over upper/db Database
PG interface {
db.Database
sqlbuilder.SQLBuilder
}
postgresConnection struct {
db.Database
sqlbuilder.SQLBuilder
}
// ConnectionResult for default module
ConnectionResult struct {
dig.Out
MySQL MySQL `optional:"true" name:"default_mysql"`
PG PG `optional:"true" name:"default_pg"`
}
// ConnectionOptions will be passed into NewConnection
ConnectionOptions struct {
Logger *zap.Logger
URL db.ConnectionURL
Debug bool
Adapter string
}
// ConnectionOption will be passed into NewConnection
ConnectionOption func(opts *ConnectionOptions)
// Error is constant errors
Error string
urlWrapper struct {
url *url.URL
adapter string
}
logger struct {
*zap.Logger
}
)
const (
// ErrEmptyConfig throws when db.ConnectionURL not passed into NewConnection
ErrEmptyConfig = Error("empty config")
// ErrEmptyAdapter throws when passed empty adapter into NewConnection
ErrEmptyAdapter = Error("empty adapter")
// ErrUnknownAdapter throws when passed unknown adapter into NewConnection
ErrUnknownAdapter = Error("unknown adapter")
// ErrConfigNotFound throws when config key is empty or not set
ErrConfigNotFound = Error("configuration for key not found")
)
var replacer = strings.NewReplacer("\n", "", "\t", " ", " ", "", " ", " ", " ", " ")
func (e Error) Error() string {
return string(e)
}
func (l logger) Log(state *db.QueryStatus) {
l.Logger.Debug("exec query",
zap.Stringer("spent", state.End.Sub(state.Start)),
zap.String("query", replacer.Replace(state.Query)),
zap.Any("args", state.Args))
}