-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathquery.go
66 lines (58 loc) · 1.35 KB
/
query.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
package query
import (
"github.com/yaoapp/gou/connector"
"github.com/yaoapp/gou/model"
"github.com/yaoapp/gou/query"
dsl "github.com/yaoapp/gou/query/gou"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/xun/capsule"
"github.com/yaoapp/yao/config"
)
// Load 加载查询引擎
func Load(cfg config.Config) error {
if _, has := query.Engines["default"]; !has {
registerDefault()
}
// register connector
for id, conn := range connector.Connectors {
if _, has := query.Engines[id]; has {
continue
}
if conn.Is(connector.DATABASE) {
qb, err := conn.Query()
if err != nil {
log.Error("[Query] load connector error %v", err.Error())
continue
}
query.Register(id, &dsl.Query{
Query: qb,
GetTableName: func(s string) string { return s },
AESKey: config.Conf.DB.AESKey,
})
}
}
return nil
}
// Unload Query Engine
func Unload() error {
for id := range query.Engines {
query.Unregister(id)
}
return nil
}
// registerDefaultQuery register the default engine
func registerDefault() {
if capsule.Global != nil {
query.Register("default", &dsl.Query{
Query: capsule.Query(),
GetTableName: func(s string) string {
if mod, has := model.Models[s]; has {
return mod.MetaData.Table.Name
}
log.Error("%s model does not load", s)
return s
},
AESKey: config.Conf.DB.AESKey,
})
}
}