forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
executable file
·183 lines (131 loc) · 3.24 KB
/
query.go
File metadata and controls
executable file
·183 lines (131 loc) · 3.24 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package query
import (
"context"
"fmt"
"strings"
"time"
"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/util"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/samber/lo"
)
type Query struct {
ctx context.Context
// json请求内容
req model.Map
// 节点树根节点
rootNode *Node
// 用于根据path获取节点
pathNodes map[string]*Node
startAt time.Time
endAt time.Time
err error
// 输出过程
PrintProcessLog bool
// 关闭权限验证 , 默认否
NoAccessVerify bool
queryConfig *config.QueryConfig
// 自定义可访问权限的限定, 例如添加用户id的where条件
AccessCondition config.AccessCondition
DbMeta *config.DBMeta
// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
DbFieldStyle config.FieldStyle
// jsonFieldStyle 数据库返回的字段
JsonFieldStyle config.FieldStyle
// Config *config.Config
}
func New(ctx context.Context, qc *config.QueryConfig, req model.Map) *Query {
q := &Query{
queryConfig: qc,
}
q.init(ctx, req)
q.NoAccessVerify = qc.NoVerify()
return q
}
func (q *Query) init(ctx context.Context, req model.Map) {
q.ctx = ctx
q.req = req
q.startAt = time.Now()
q.pathNodes = make(map[string]*Node)
}
func (q *Query) Result() (model.Map, error) {
if q.PrintProcessLog {
g.Log().Debugf(q.ctx, "【query】 ============ [begin]")
g.Log().Debugf(q.ctx, "【query】 ============ [buildNodeTree]")
}
// 构建节点树,并校验结构是否符合, 不符合则返回错误, 结束本次查询
q.rootNode = newNode(q, "", "", q.req)
err := q.rootNode.buildChild()
if err != nil {
return nil, err
}
if q.PrintProcessLog {
q.printNode(q.rootNode, 0)
}
if q.PrintProcessLog {
g.Log().Debugf(q.ctx, "【query】 ============ [parse]")
}
setNodeRole(q.rootNode, "", "")
q.rootNode.parse()
if q.PrintProcessLog {
g.Log().Debugf(q.ctx, "【query】 ============ [fetch]")
}
q.fetch()
if q.err != nil {
return nil, q.err
}
resultMap, err := q.rootNode.Result()
if err != nil {
if q.rootNode.err != nil {
return nil, q.rootNode.err
}
ret := model.Map{}
for k, node := range q.rootNode.children {
ret[k] = node.err
}
return ret, err
}
if q.PrintProcessLog {
g.Log().Debugf(q.ctx, "【query】 ^=======================^")
}
return resultMap.(model.Map), err
}
func (q *Query) fetch() {
// 分析依赖关系
var prerequisites [][]string
analysisRef(q.rootNode, &prerequisites)
fetchQueue, err := util.AnalysisOrder(prerequisites)
if err != nil {
q.err = err
return
}
for k, _ := range q.pathNodes {
if !lo.Contains(fetchQueue, k) {
fetchQueue = append(fetchQueue, k)
}
}
if q.PrintProcessLog {
g.Log().Debugf(q.ctx, "fetch queue: %s", strings.Join(fetchQueue, " > "))
}
for _, path := range fetchQueue {
q.pathNodes[path].fetch()
}
q.rootNode.fetch()
}
// 输出节点信息
func (q *Query) printNode(n *Node, deep int) {
for i := 0; i < deep; i++ {
fmt.Print("|")
}
desc := gconv.String(n.Type)
if n.isList {
desc += "[]"
}
format := fmt.Sprintf("- %%-%ds | %%s\n", 20-deep)
fmt.Printf(format, n.Key, desc)
for _, node := range n.children {
q.printNode(node, deep+1)
}
}