/
query.go
310 lines (252 loc) · 8.85 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package gripql
import (
"fmt"
"strings"
"github.com/bmeg/grip/log"
"github.com/bmeg/grip/util/protoutil"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
)
// V starts a new vertex query, short for `NewQuery().V()`.
func V(ids ...string) *Query {
return NewQuery().V(ids...)
}
// E starts a new vertex query, short for `NewQuery().E()`.
func E(ids ...string) *Query {
return NewQuery().E(ids...)
}
// NewQuery creates a new Query instance.
func NewQuery() *Query {
return &Query{}
}
// Query helps build graph queries.
type Query struct {
Statements []*GraphStatement
}
func (q *Query) with(st *GraphStatement) *Query {
nq := &Query{
Statements: make([]*GraphStatement, len(q.Statements)),
}
copy(nq.Statements, q.Statements)
nq.Statements = append(nq.Statements, st)
return nq
}
// V adds a vertex selection step to the query
func (q *Query) V(id ...string) *Query {
vlist := protoutil.NewListFromStrings(id)
return q.with(&GraphStatement{Statement: &GraphStatement_V{vlist}})
}
// E adds a edge selection step to the query
func (q *Query) E(id ...string) *Query {
elist := protoutil.NewListFromStrings(id)
return q.with(&GraphStatement{Statement: &GraphStatement_E{elist}})
}
// In follows incoming edges to adjacent vertex
func (q *Query) In(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_In{vlist}})
}
// InV follows incoming edges to adjacent vertex
func (q *Query) InNull(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_InNull{vlist}})
}
// InE moves to incoming edge
func (q *Query) InE(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_InE{vlist}})
}
// Out follows outgoing edges to adjacent vertex
func (q *Query) Out(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_Out{vlist}})
}
// OutV follows outgoing edges to adjacent vertex
func (q *Query) OutNull(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_OutNull{vlist}})
}
// OutE moves to outgoing edge
func (q *Query) OutE(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_OutE{vlist}})
}
// Both follows both incoming and outgoing edges to adjacent vertex
func (q *Query) Both(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_Both{vlist}})
}
// BothV follows both incoming and outgoing edges to adjacent vertex
func (q *Query) BothV(label ...string) *Query {
return q.Both(label...)
}
// BothE moves to both incoming and outgoing edges
func (q *Query) BothE(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_BothE{vlist}})
}
// Has filters elements based on data properties.
func (q *Query) Has(expression *HasExpression) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Has{expression}})
}
// HasLabel filters elements based on their label.
func (q *Query) HasLabel(label ...string) *Query {
vlist := protoutil.NewListFromStrings(label)
return q.with(&GraphStatement{Statement: &GraphStatement_HasLabel{vlist}})
}
// HasKey filters elements based on whether it has one or more properties.
func (q *Query) HasKey(key ...string) *Query {
vlist := protoutil.NewListFromStrings(key)
return q.with(&GraphStatement{Statement: &GraphStatement_HasKey{vlist}})
}
// HasID filters elements based on their id.
func (q *Query) HasID(id ...string) *Query {
vlist := protoutil.NewListFromStrings(id)
return q.with(&GraphStatement{Statement: &GraphStatement_HasId{vlist}})
}
// Limit limits the number of results returned.
func (q *Query) Limit(n uint32) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Limit{n}})
}
// Skip will drop the first n number of records and return the rest.
func (q *Query) Skip(n uint32) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Skip{n}})
}
// Range will limits which records are returned. When the low-end of the range is
// not met, objects are continued to be iterated. When within the low (inclusive)
// and high (exclusive) range, traversers are emitted. When above the high range,
// the traversal breaks out of iteration. Finally, the use of -1 on the high range
// will emit remaining traversers after the low range begins.
func (q *Query) Range(start, stop int32) *Query {
return q.with(&GraphStatement{
Statement: &GraphStatement_Range{
&Range{
Start: start,
Stop: stop,
},
},
})
}
// As marks current elements with tag
func (q *Query) As(id string) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_As{id}})
}
// Select retreieves previously marked elemets
func (q *Query) Select(id ...string) *Query {
idList := SelectStatement{Marks: id}
return q.with(&GraphStatement{Statement: &GraphStatement_Select{&idList}})
}
// Fields selects which properties are returned in the result.
func (q *Query) Fields(keys ...string) *Query {
klist := protoutil.NewListFromStrings(keys)
return q.with(&GraphStatement{Statement: &GraphStatement_Fields{klist}})
}
// Count adds a count step to the query
func (q *Query) Count() *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Count{}})
}
// Distinct selects records with distinct elements of arg
func (q *Query) Distinct(args ...string) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Distinct{protoutil.NewListFromStrings(args)}})
}
// Render adds a render step to the query
func (q *Query) Render(template interface{}) *Query {
if sList, ok := template.([]string); ok {
t := []any{}
for _, j := range sList {
t = append(t, j)
}
template = t
}
value, err := structpb.NewValue(template)
if err != nil {
log.Errorf("render error: %s", err)
}
return q.with(&GraphStatement{Statement: &GraphStatement_Render{value}})
}
func (q *Query) Aggregate(agg []*Aggregate) *Query {
return q.with(&GraphStatement{Statement: &GraphStatement_Aggregate{Aggregate: &Aggregations{Aggregations: agg}}})
}
func (q *Query) String() string {
parts := []string{}
add := func(name string, x ...string) {
args := strings.Join(x, ", ")
parts = append(parts, fmt.Sprintf("%s(%s)", name, args))
}
for _, gs := range q.Statements {
switch stmt := gs.GetStatement().(type) {
case *GraphStatement_V:
ids := protoutil.AsStringList(stmt.V)
add("V", ids...)
case *GraphStatement_E:
ids := protoutil.AsStringList(stmt.E)
add("E", ids...)
case *GraphStatement_In:
ids := protoutil.AsStringList(stmt.In)
add("In", ids...)
case *GraphStatement_Out:
ids := protoutil.AsStringList(stmt.Out)
add("Out", ids...)
case *GraphStatement_InNull:
ids := protoutil.AsStringList(stmt.InNull)
add("InNull", ids...)
case *GraphStatement_OutNull:
ids := protoutil.AsStringList(stmt.OutNull)
add("OutNull", ids...)
case *GraphStatement_Both:
ids := protoutil.AsStringList(stmt.Both)
add("Both", ids...)
case *GraphStatement_InE:
ids := protoutil.AsStringList(stmt.InE)
add("InE", ids...)
case *GraphStatement_OutE:
ids := protoutil.AsStringList(stmt.OutE)
add("OutE", ids...)
case *GraphStatement_BothE:
ids := protoutil.AsStringList(stmt.BothE)
add("BothE", ids...)
case *GraphStatement_Has:
add("Has", HasExpressionString(stmt.Has))
case *GraphStatement_HasLabel:
labels := protoutil.AsStringList(stmt.HasLabel)
add("HasLabel", labels...)
case *GraphStatement_HasId:
ids := protoutil.AsStringList(stmt.HasId)
add("HasId", ids...)
case *GraphStatement_HasKey:
keys := protoutil.AsStringList(stmt.HasKey)
add("HasKey", keys...)
case *GraphStatement_Limit:
add("Limit", fmt.Sprintf("%d", stmt.Limit))
case *GraphStatement_Skip:
add("Skip", fmt.Sprintf("%d", stmt.Skip))
case *GraphStatement_Range:
add("Range", fmt.Sprintf("%d", stmt.Range.Start), fmt.Sprintf("%d", stmt.Range.Stop))
case *GraphStatement_Count:
add("Count")
case *GraphStatement_As:
add("As", stmt.As)
case *GraphStatement_Select:
add("Select", stmt.Select.Marks...)
case *GraphStatement_Fields:
fields := protoutil.AsStringList(stmt.Fields)
add("Fields", fields...)
case *GraphStatement_Aggregate:
add("Aggregate")
case *GraphStatement_Render:
jtxt, err := protojson.Marshal(stmt.Render)
if err != nil {
log.Errorf("serialization error: %s", err)
}
add("Render", string(jtxt))
}
}
return strings.Join(parts, ".")
}
func HasExpressionString(stmt *HasExpression) string {
if exp := stmt.GetCondition(); exp != nil {
//exp.Condition
return fmt.Sprintf("%s = %s", exp.Key, exp.Value)
}
return ""
}