Skip to content

Commit e93829e

Browse files
committed
query ok
1 parent 58d5cf0 commit e93829e

File tree

13 files changed

+237
-127
lines changed

13 files changed

+237
-127
lines changed

apijson.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package apijson
33
import (
44
"context"
55
"github.com/glennliao/apijson-go/config"
6+
"github.com/glennliao/apijson-go/model"
7+
"github.com/glennliao/apijson-go/query"
68
)
79

810
type Plugin interface {
@@ -52,3 +54,14 @@ func (a *ApiJson) Load() {
5254
func (a *ApiJson) Config() *config.Config {
5355
return a.config
5456
}
57+
58+
func (a *ApiJson) NewQuery(ctx context.Context, req model.Map) *query.Query {
59+
q := query.New(ctx, a.Config().QueryConfig(), req)
60+
61+
q.DbMeta = a.config.DbMeta
62+
q.Functions = a.config.Functions
63+
q.DbFieldStyle = a.config.DbFieldStyle
64+
q.JsonFieldStyle = a.config.JsonFieldStyle
65+
66+
return q
67+
}

config/access_config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ func (a *AccessConfig) GetFieldsGetInByRole(role string) map[string][]string {
5656
return inFieldsMap
5757
}
5858

59-
func (a *Access) GetAccess(tableAlias string, accessVerify bool) (*AccessConfig, error) {
59+
func (a *Access) GetAccess(tableAlias string, noVerify bool) (*AccessConfig, error) {
6060
tableAlias, _ = util.ParseNodeKey(tableAlias)
6161
access, ok := a.accessConfigMap[tableAlias]
6262

6363
if !ok {
64-
if accessVerify {
65-
return nil, gerror.Newf("access[%s]: 404", tableAlias)
64+
if noVerify {
65+
return &AccessConfig{
66+
Debug: 0,
67+
Name: tableAlias,
68+
Alias: tableAlias,
69+
}, nil
6670
}
67-
return &AccessConfig{
68-
Debug: 0,
69-
Name: tableAlias,
70-
Alias: tableAlias,
71-
}, nil
71+
return nil, gerror.Newf("access[%s]: 404", tableAlias)
7272
}
7373

7474
return &access, nil

config/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Config struct {
2121
AccessList []AccessConfig // todo to access
2222

2323
RequestConfig *RequestConfig
24+
25+
queryConfig *QueryConfig
2426
}
2527

2628
func New() *Config {
@@ -48,4 +50,16 @@ func (c *Config) Load() {
4850
for _, access := range c.AccessList {
4951
c.Access.accessConfigMap[access.Alias] = access
5052
}
53+
54+
c.queryConfig = &QueryConfig{
55+
access: c.Access,
56+
functions: c.Functions,
57+
maxTreeDeep: c.MaxTreeDeep,
58+
maxTreeWidth: c.MaxTreeWidth,
59+
defaultRoleFunc: c.Access.DefaultRoleFunc,
60+
}
61+
}
62+
63+
func (c *Config) QueryConfig() *QueryConfig {
64+
return c.queryConfig
5165
}

config/executor/query.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ type QueryExecutor interface {
1616
SetEmptyResult()
1717
}
1818

19-
type queryExecutorBuilder func(ctx context.Context, noAccessVerify bool, role string, access *config.AccessConfig, config *config.Config) (QueryExecutor, error)
19+
type queryExecutorBuilder func(ctx context.Context, config *config.ExecutorConfig) (QueryExecutor, error)
2020

2121
var queryExecutorBuilderMap = map[string]queryExecutorBuilder{}
2222

2323
func RegQueryExecutor(name string, e queryExecutorBuilder) {
2424
queryExecutorBuilderMap[name] = e
2525
}
2626

27-
func NewQueryExecutor(name string, ctx context.Context, noAccessVerify bool, role string, access *config.AccessConfig, config *config.Config) (QueryExecutor, error) {
27+
func NewQueryExecutor(name string, ctx context.Context, config *config.ExecutorConfig) (QueryExecutor, error) {
2828
if v, exists := queryExecutorBuilderMap[name]; exists {
29-
return v(ctx, noAccessVerify, role, access, config)
29+
return v(ctx, config)
3030
}
31-
return queryExecutorBuilderMap["default"](ctx, noAccessVerify, role, access, config)
31+
return queryExecutorBuilderMap["default"](ctx, config)
3232
}
3333

3434
func QueryExecutorList() []string {

config/query_config.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"github.com/glennliao/apijson-go/model"
6+
"github.com/samber/lo"
7+
"net/http"
8+
)
9+
10+
type QueryConfig struct {
11+
access *Access
12+
functions *Functions
13+
maxTreeDeep int
14+
maxTreeWidth int
15+
defaultRoleFunc DefaultRole
16+
}
17+
18+
func (c *QueryConfig) NoVerify() bool {
19+
return c.access.NoVerify
20+
}
21+
22+
func (c *QueryConfig) DefaultRoleFunc() DefaultRole {
23+
return c.defaultRoleFunc
24+
}
25+
26+
func (c *QueryConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig, error) {
27+
return c.access.GetAccess(key, noVerify)
28+
}
29+
30+
func (c *QueryConfig) CallFunc(ctx context.Context, name string, param model.Map) (any, error) {
31+
return c.functions.Call(ctx, name, param)
32+
}
33+
34+
func (c *QueryConfig) MaxTreeDeep() int {
35+
return c.maxTreeDeep
36+
}
37+
func (c *QueryConfig) MaxTreeWidth() int {
38+
return c.maxTreeWidth
39+
}
40+
41+
type ExecutorConfig struct {
42+
NoVerify bool
43+
accessConfig *AccessConfig
44+
method string
45+
role string
46+
DBMeta *DBMeta
47+
DbFieldStyle FieldStyle
48+
JsonFieldStyle FieldStyle
49+
}
50+
51+
func NewExecutorConfig(accessConfig *AccessConfig, method string, noVerify bool) *ExecutorConfig {
52+
return &ExecutorConfig{
53+
accessConfig: accessConfig,
54+
method: method,
55+
NoVerify: noVerify,
56+
}
57+
}
58+
59+
func (c *ExecutorConfig) SetRole(role string) {
60+
c.role = role
61+
}
62+
63+
func (c *ExecutorConfig) TableName() string {
64+
return c.accessConfig.Name
65+
}
66+
67+
func (c *ExecutorConfig) TableColumns() []string {
68+
return c.DBMeta.GetTableColumns(c.accessConfig.Name)
69+
}
70+
71+
func (c *ExecutorConfig) GetFieldsGetOutByRole() []string {
72+
var fieldsMap map[string]string
73+
74+
if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
75+
fieldsMap = val.Out
76+
} else {
77+
fieldsMap = c.accessConfig.FieldsGet["default"].Out
78+
}
79+
return lo.Keys(fieldsMap)
80+
}
81+
82+
func (c *ExecutorConfig) GetFieldsGetInByRole() map[string][]string {
83+
var inFieldsMap map[string][]string
84+
85+
if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
86+
inFieldsMap = val.In
87+
} else {
88+
inFieldsMap = c.accessConfig.FieldsGet["default"].In
89+
}
90+
91+
return inFieldsMap
92+
}
93+
94+
func (c *ExecutorConfig) AccessRoles() []string {
95+
switch c.method {
96+
case http.MethodGet:
97+
return c.accessConfig.Get
98+
case http.MethodHead:
99+
return c.accessConfig.Head
100+
case http.MethodPost:
101+
return c.accessConfig.Post
102+
case http.MethodPut:
103+
return c.accessConfig.Put
104+
case http.MethodDelete:
105+
return c.accessConfig.Delete
106+
}
107+
return []string{}
108+
109+
}
110+
111+
func (c *ExecutorConfig) Executor() string {
112+
return c.accessConfig.Executor
113+
114+
}

config/rowkeygen.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import (
88

99
type RowKeyGenFuncHandler func(ctx context.Context, genParam g.Map, table string, data g.Map) (g.Map, error)
1010

11-
func (a *Config) RowKeyGen(ctx context.Context, genFuncName string, table string, data g.Map) (g.Map, error) {
12-
if f, exists := a.rowKeyGenFuncMap[genFuncName]; exists {
11+
func (c *Config) RowKeyGen(ctx context.Context, genFuncName string, table string, data g.Map) (g.Map, error) {
12+
if f, exists := c.rowKeyGenFuncMap[genFuncName]; exists {
1313
return f(ctx, g.Map{}, table, data)
1414
}
1515

1616
return nil, nil
1717
}
1818

19-
func (a *Config) RowKeyGenFunc(name string, f RowKeyGenFuncHandler) {
20-
a.rowKeyGenFuncMap[name] = f
19+
func (c *Config) RowKeyGenFunc(name string, f RowKeyGenFuncHandler) {
20+
c.rowKeyGenFuncMap[name] = f
2121
}
2222

23-
func (a *Config) RowKeyGenList() []string {
24-
return lo.Keys(a.rowKeyGenFuncMap)
23+
func (c *Config) RowKeyGenList() []string {
24+
return lo.Keys(c.rowKeyGenFuncMap)
2525
}

drivers/executor_goframe/query.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,18 @@ type SqlExecutor struct {
3131
// 是否最终为空结果, 用于node中中断数据获取
3232
WithEmptyResult bool
3333

34-
noAccessVerify bool
35-
36-
access *config.AccessConfig
37-
38-
config *config.Config
34+
config *config.ExecutorConfig
3935
}
4036

41-
func New(ctx context.Context, noAccessVerify bool, role string, access *config.AccessConfig, config *config.Config) (executor.QueryExecutor, error) {
37+
func New(ctx context.Context, config *config.ExecutorConfig) (executor.QueryExecutor, error) {
4238

4339
return &SqlExecutor{
4440
ctx: ctx,
45-
Role: role,
4641
Where: [][]any{},
4742
Columns: nil,
4843
Order: "",
4944
Group: "",
5045
WithEmptyResult: false,
51-
noAccessVerify: noAccessVerify,
52-
access: access,
5346
config: config,
5447
}, nil
5548
}
@@ -81,15 +74,15 @@ func (e *SqlExecutor) ParseCondition(conditions model.MapStrAny, accessVerify bo
8174
return nil
8275
}
8376

84-
if e.noAccessVerify { // 可任意字段搜索
77+
if e.config.NoVerify { // 可任意字段搜索
8578
return nil
8679
}
8780

88-
inFieldsMap := e.access.GetFieldsGetInByRole(e.Role)
81+
inFieldsMap := e.config.GetFieldsGetInByRole()
8982

9083
dbStyle := e.config.DbFieldStyle
9184

92-
tableName := e.access.Name
85+
tableName := e.config.TableName()
9386

9487
for _, where := range e.Where {
9588
k := dbStyle(e.ctx, tableName, where[0].(string))
@@ -169,7 +162,7 @@ var exp = regexp.MustCompile(`^[\s\w][\w()]+`) // 匹配 field, COUNT(field)
169162
func (e *SqlExecutor) ParseCtrl(ctrl model.Map) error {
170163

171164
fieldStyle := e.config.DbFieldStyle
172-
tableName := e.access.Name
165+
tableName := e.config.TableName()
173166
for k, v := range ctrl {
174167
// 使用;分割字段
175168
fieldStr := strings.ReplaceAll(gconv.String(v), ";", ",")
@@ -203,7 +196,7 @@ func (e *SqlExecutor) ParseCtrl(ctrl model.Map) error {
203196
}
204197

205198
func (e *SqlExecutor) build() *gdb.Model {
206-
tableName := e.access.Name
199+
tableName := e.config.TableName()
207200
m := g.DB().Model(tableName).Ctx(e.ctx)
208201

209202
if e.Order != "" {
@@ -272,16 +265,16 @@ func (e *SqlExecutor) build() *gdb.Model {
272265

273266
func (e *SqlExecutor) column() []string {
274267

275-
outFields := e.access.GetFieldsGetOutByRole(e.Role)
268+
outFields := e.config.GetFieldsGetOutByRole()
276269

277-
tableName := e.access.Name
270+
tableName := e.config.TableName()
278271

279272
var columns []string
280273

281274
if e.Columns != nil {
282275
columns = e.Columns
283276
} else {
284-
columns = e.config.DbMeta.GetTableColumns(tableName)
277+
columns = e.config.TableColumns()
285278
}
286279

287280
var fields = make([]string, 0, len(columns))
@@ -304,7 +297,7 @@ func (e *SqlExecutor) column() []string {
304297
}
305298

306299
// 过滤可访问字段
307-
if e.noAccessVerify || lo.Contains(outFields, dbStyle(e.ctx, tableName, fieldName)) ||
300+
if e.config.NoVerify || lo.Contains(outFields, dbStyle(e.ctx, tableName, fieldName)) ||
308301
len(outFields) == 0 /* 数据库中未设置, 则看成全部可访问 */ {
309302
fields = append(fields, column)
310303
}

drivers/framework_goframe/gf.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/glennliao/apijson-go"
77
"github.com/glennliao/apijson-go/action"
88
"github.com/glennliao/apijson-go/model"
9-
"github.com/glennliao/apijson-go/query"
109
"github.com/gogf/gf/v2/container/gmap"
1110
"github.com/gogf/gf/v2/errors/gerror"
1211
"github.com/gogf/gf/v2/frame/g"
@@ -55,18 +54,10 @@ func (gf *GF) Bind(group *ghttp.RouterGroup, mode ...Mode) {
5554
}
5655

5756
func (gf *GF) Get(ctx context.Context, req model.Map) (res model.Map, err error) {
58-
q := query.New(ctx, req)
57+
q := gf.apijson.NewQuery(ctx, req)
5958
q.NoAccessVerify = gf.apijson.Config().Access.NoVerify
60-
q.Access = gf.apijson.Config().Access
6159
q.AccessCondition = gf.apijson.Config().Access.ConditionFunc
62-
63-
q.DbMeta = gf.apijson.Config().DbMeta // todo
64-
q.Functions = gf.apijson.Config().Functions // todo
65-
q.DbFieldStyle = gf.apijson.Config().DbFieldStyle
66-
q.JsonFieldStyle = gf.apijson.Config().JsonFieldStyle
67-
6860
q.Config = gf.apijson.Config()
69-
7061
return q.Result()
7162
}
7263

0 commit comments

Comments
 (0)