/
columns.go
242 lines (195 loc) · 5.02 KB
/
columns.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package qcode
import (
"fmt"
"github.com/dosco/graphjin/core/internal/graph"
"github.com/dosco/graphjin/core/internal/sdata"
"github.com/dosco/graphjin/core/internal/util"
)
func (co *Compiler) compileColumns(
field *graph.Field,
op *graph.Operation,
st *util.StackInt32,
qc *QCode,
sel *Select,
tr trval) error {
sel.Cols = make([]Column, 0, len(field.Children))
//if sel.Rel.Type != sdata.RelRemote {
if err := co.compileChildColumns(field, op, st, qc, sel, tr); err != nil {
return err
}
//}
if err := validateSelector(qc, sel, tr); err != nil {
return err
}
if err := co.addRelColumns(qc, sel); err != nil {
return err
}
co.addOrderByColumns(sel)
return nil
}
func (co *Compiler) compileChildColumns(
field *graph.Field,
op *graph.Operation,
st *util.StackInt32,
qc *QCode,
sel *Select,
tr trval) error {
aggExist := false
for _, cid := range field.Children {
var fname string
f := op.Fields[cid]
if f.Alias != "" {
fname = f.Alias
} else {
fname = f.Name
}
if sel.Rel.Type == sdata.RelRemote {
sel.addFieldCol(fname)
continue
}
if len(f.Children) != 0 {
val := f.ID | (sel.ID << 16)
st.Push(val)
continue
}
fn, agg, err := co.isFunction(sel, f.Name)
if err != nil {
return err
}
if fn.skip {
continue
}
// not a function
if fn.Name == "" {
if dbc, err := sel.Ti.GetColumnB(f.Name); err == nil {
sel.addCol(Column{Col: dbc, FieldName: fname})
} else {
return err
}
// is a function
} else {
if agg {
aggExist = true
}
fn.FieldName = fname
sel.Funcs = append(sel.Funcs, fn)
}
}
if aggExist && len(sel.Cols) != 0 {
sel.GroupCols = true
}
return nil
}
func (co *Compiler) addOrderByColumns(sel *Select) {
for _, ob := range sel.OrderBy {
sel.addCol(Column{Col: ob.Col, Base: true})
}
}
func (co *Compiler) addRelColumns(qc *QCode, sel *Select) error {
var psel *Select
rel := sel.Rel
if sel.ParentID != -1 {
psel = &qc.Selects[sel.ParentID]
}
switch rel.Type {
case sdata.RelNone:
return nil
case sdata.RelOneToOne, sdata.RelOneToMany:
psel.addCol(Column{Col: rel.Right.Col, Base: true})
case sdata.RelOneToManyThrough:
psel.addCol(Column{Col: rel.Left.Col, Base: true})
case sdata.RelEmbedded:
psel.addCol(Column{Col: rel.Left.Col, Base: true})
case sdata.RelRemote:
psel.addCol(Column{Col: rel.Left.Col, Base: true})
sel.SkipRender = SkipTypeRemote
case sdata.RelPolymorphic:
psel.addCol(Column{Col: rel.Left.Col, Base: true})
if v, err := rel.Left.Ti.GetColumn(rel.Right.VTable); err == nil {
psel.addCol(Column{Col: v, Base: true})
} else {
return err
}
case sdata.RelRecursive:
sel.addCol(Column{Col: rel.Left.Col, Base: true})
sel.addCol(Column{Col: rel.Right.Col, Base: true})
}
return nil
}
func (co *Compiler) orderByIDCol(sel *Select) error {
idCol := sel.Ti.PrimaryCol
if idCol.Name == "" {
return fmt.Errorf("table requires primary key: %s", sel.Ti.Name)
}
sel.addCol(Column{Col: idCol, Base: true})
for _, ob := range sel.OrderBy {
if ob.Col.Name == idCol.Name {
return nil
}
}
sel.OrderBy = append(sel.OrderBy, OrderBy{Col: idCol, Order: sel.order})
return nil
}
func validateSelector(qc *QCode, sel *Select, tr trval) error {
for _, col := range sel.Cols {
if !tr.columnAllowed(qc, col.Col.Name) {
return fmt.Errorf("column blocked: %s (%s)", col.Col.Name, tr.role)
}
// if _, ok := sel.ColMap[col.FieldName]; ok {
// return fmt.Errorf("duplicate field: %s", col.FieldName)
// }
// sel.ColMap[col.FieldName] = struct{}{}
// if col.FieldName != col.Col.Name {
// if _, ok := sel.ColMap[col.Col.Name]; ok {
// return fmt.Errorf("duplicate column: %s", col.Col.Name)
// }
// sel.ColMap[col.Col.Name] = struct{}{}
// }
}
if len(sel.Funcs) != 0 && tr.isFuncsBlocked() {
return fmt.Errorf("functions blocked: %s (%s)", sel.Funcs[0].Col.Name, tr.role)
}
for _, fn := range sel.Funcs {
var blocked bool
var fnID string
if fn.Col.Name != "" {
blocked = !tr.columnAllowed(qc, fn.Col.Name)
fnID = (fn.Name + fn.Col.Name)
} else {
blocked = !tr.columnAllowed(qc, fn.Name)
fnID = fn.Name
}
if blocked {
return fmt.Errorf("column blocked: %s (%s)", fn.Name, tr.role)
}
if fn.FieldName != "" {
if _, ok := sel.ColMap[fn.FieldName]; ok {
return fmt.Errorf("duplicate field: %s", fn.FieldName)
}
sel.ColMap[fn.FieldName] = -1
}
if fn.FieldName != fnID {
if _, ok := sel.ColMap[fnID]; ok {
return fmt.Errorf("duplicate function: %s(%s)", fn.Name, fn.Col.Name)
}
sel.ColMap[fnID] = -1
}
}
return nil
}
func (sel *Select) addCol(col Column) {
if i, ok := sel.ColMap[col.Col.Name]; ok {
// Replace column if re-added as not base only.
if i != -1 && !col.Base {
sel.Cols[i] = col
}
} else {
// Else its new and just add it to the map and columns
sel.Cols = append(sel.Cols, col)
sel.ColMap[col.Col.Name] = len(sel.Cols) - 1
}
}
func (sel *Select) addFieldCol(name string) {
sel.Cols = append(sel.Cols, Column{FieldName: name})
sel.ColMap[name] = len(sel.Cols) - 1
}