-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
build.go
126 lines (100 loc) · 2.4 KB
/
build.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
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
package core
import (
"bytes"
"encoding/json"
"fmt"
"sync"
"github.com/dosco/graphjin/core/internal/psql"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/go-playground/validator/v10"
)
type queryComp struct {
sync.Once
qr queryReq
st stmt
}
type stmt struct {
role *Role
qc *qcode.QCode
md psql.Metadata
va *validator.Validate
sql string
}
func (gj *graphjin) compileQuery(qr queryReq, role string) (*queryComp, error) {
var userVars map[string]json.RawMessage
var qc *queryComp
var err error
if len(qr.vars) != 0 {
if err := json.Unmarshal(qr.vars, &userVars); err != nil {
return nil, fmt.Errorf("variables: %w", err)
}
}
if !gj.prod || gj.conf.DisableAllowList {
if len(qr.query) == 0 {
item, err := gj.allowList.GetByName(qr.name)
if err != nil {
return nil, err
}
qr.query = []byte(item.Query)
}
st, err := gj.compileQueryForRole(qr, userVars, role)
if err != nil {
return nil, err
}
qc = &queryComp{qr: qr, st: st}
} else {
// In production mode enforce the allow list and
// compile and cache the result else compile each time
if qc, err = gj.getQuery(qr, role, userVars); err != nil {
return nil, err
}
if qc, err = gj.compileQueryForRoleOnce(qc, role); err != nil {
return nil, err
}
// Overwrite allow list vars with user vars
qc.qr.vars = qr.vars
qc.qr.ns = qr.ns
}
return qc, err
}
func (gj *graphjin) compileQueryForRoleOnce(qc *queryComp, role string) (*queryComp, error) {
var err error
if qc.st.sql != "" {
return qc, nil
}
qc.Do(func() {
var vars1 map[string]json.RawMessage
if len(qc.qr.vars) != 0 {
err = json.Unmarshal(qc.qr.vars, &vars1)
}
if err == nil {
qc.st, err = gj.compileQueryForRole(qc.qr, vars1, role)
}
})
if err != nil {
return nil, err
}
return qc, nil
}
func (gj *graphjin) compileQueryForRole(
qr queryReq, vm map[string]json.RawMessage, role string) (stmt, error) {
var st stmt
var err error
var ok bool
if st.role, ok = gj.roles[role]; !ok {
return st, fmt.Errorf(`roles '%s' not defined in c.gj.config`, role)
}
if qr.order[0] != "" {
vm[qr.order[0]] = json.RawMessage(qr.order[1])
}
if st.qc, err = gj.qc.Compile(qr.query, vm, st.role.Name, qr.ns); err != nil {
return st, err
}
var w bytes.Buffer
if st.md, err = gj.pc.Compile(&w, st.qc); err != nil {
return st, err
}
st.va = validator.New()
st.sql = w.String()
return st, nil
}