-
Notifications
You must be signed in to change notification settings - Fork 22
/
norm.go
73 lines (63 loc) · 1.48 KB
/
norm.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
package norm
import (
"github.com/pkg/errors"
"github.com/zhihu/norm/v3/dialectors"
)
var teardown = func() {}
type DB struct {
dialector dialectors.IDialector
logger Logger
// 一次查询所需的字段
parent *DB
debug bool
sql string
// 一次查询结束后需要执行的操作
teardown func()
}
// Open initialize nebula connect pool
func Open(dialector dialectors.IDialector, cfg Config, opts ...Option) (*DB, error) {
if dialector == nil {
return &DB{}, errors.New("must have dialector")
}
for _, opt := range opts {
opt(&cfg)
}
cfg.LoadDefault()
return &DB{
dialector: dialector,
logger: cfg.logger,
parent: nil,
debug: cfg.DebugMode,
teardown: teardown,
}, nil
}
// MustOpen 语法糖. 如果 error 则 panic, 适用于强依赖 db 的场景
func MustOpen(dialector dialectors.IDialector, cfg Config, opts ...Option) *DB {
db, err := Open(dialector, cfg, opts...)
if err != nil {
panic(err)
}
return db
}
// Close
func (db *DB) Close() {
db.dialector.Close()
}
// DebugMode 调试模式, 会输出任意语句的sql
func (db *DB) DebugMode() {
db.debug = true
}
// getInstance 获取当前调用链真正使用的 db. 专为链式调用设计, 为每条调用链返回一个 db 的拷贝, 从而避免互相影响.
func (db *DB) getInstance() (tx *DB) {
if db.parent == nil {
tx = &DB{
dialector: db.dialector,
logger: db.logger,
parent: db,
debug: db.debug,
teardown: teardown,
}
return tx
}
return db
}