-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerics.go
More file actions
316 lines (267 loc) · 8.01 KB
/
generics.go
File metadata and controls
316 lines (267 loc) · 8.01 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
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
311
312
313
314
315
316
package norm
import (
"context"
"github.com/haysons/norm/clause"
nebula "github.com/vesoft-inc/nebula-go/v3"
)
type ChainInterface[T any] interface {
ExecInterface[T]
Raw(raw string) ChainInterface[T]
Go(step ...int) ChainInterface[T]
From(vid any) ChainInterface[T]
Over(edgeType ...string) ChainInterface[T]
Where(query string, args ...any) ChainInterface[T]
Or(query string, args ...any) ChainInterface[T]
Not(query string, args ...any) ChainInterface[T]
Xor(query string, args ...any) ChainInterface[T]
Sample(sampleList ...int) ChainInterface[T]
Fetch(name string, vid any) ChainInterface[T]
FetchMulti(names []string, vid any) ChainInterface[T]
Lookup(name string) ChainInterface[T]
GroupBy(expr string) ChainInterface[T]
Yield(expr string, distinct ...bool) ChainInterface[T]
OrderBy(expr string) ChainInterface[T]
Limit(limit int) ChainInterface[T]
GetSubgraph(steps int, withProp ...bool) ChainInterface[T]
In(edgeTypes ...string) ChainInterface[T]
Out(edgeTypes ...string) ChainInterface[T]
Both(edgeTypes ...string) ChainInterface[T]
InsertVertex(vertexes any, ifNotExists ...bool) ChainInterface[T]
UpdateVertex(vid any, propsUpdate any, opts ...clause.Option) ChainInterface[T]
UpsertVertex(vid any, propsUpdate any, opts ...clause.Option) ChainInterface[T]
DeleteVertex(vid any, withEdge ...bool) ChainInterface[T]
InsertEdge(edges any, ifNotExists ...bool) ChainInterface[T]
UpdateEdge(edge any, propsUpdate any, opts ...clause.Option) ChainInterface[T]
UpsertEdge(edge any, propsUpdate any, opts ...clause.Option) ChainInterface[T]
DeleteEdge(edgeTypeName string, edge any) ChainInterface[T]
When(query string, args ...any) ChainInterface[T]
Pipe() ChainInterface[T]
}
type ExecInterface[T any] interface {
NGQL(ctx context.Context) (string, error)
RawResult(ctx context.Context) (*nebula.ResultSet, error)
Exec(ctx context.Context) error
Find(ctx context.Context) ([]T, error)
FindCol(ctx context.Context, col string) ([]T, error)
Take(ctx context.Context) (T, error)
TakeCol(ctx context.Context, col string) (T, error)
}
type op func(*DB) *DB
// G constructs ngql statements generically, with the primary advantage being the direct return of results of a specific
// type without requiring externally passed variables, making it comparatively more user-friendly.
// Its overall usage is largely consistent with DB objects.
func G[T any](db *DB) ChainInterface[T] {
v := &g[T]{
db: db,
ops: make([]op, 0, 5),
}
v.chainG = &chainG[T]{
execG: execG[T]{g: v},
}
return v
}
type g[T any] struct {
*chainG[T]
db *DB
ops []op
}
func (g *g[T]) apply(_ context.Context) *DB {
db := g.db.session()
for _, op := range g.ops {
db = op(db)
}
return db
}
type chainG[T any] struct {
execG[T]
}
func (c chainG[T]) with(v op) chainG[T] {
return chainG[T]{
execG: execG[T]{g: &g[T]{
db: c.g.db,
ops: append(append([]op(nil), c.g.ops...), v),
}},
}
}
func (c chainG[T]) Raw(raw string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Raw(raw)
})
}
func (c chainG[T]) Go(step ...int) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Go(step...)
})
}
func (c chainG[T]) From(vid any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.From(vid)
})
}
func (c chainG[T]) Over(edgeType ...string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Over(edgeType...)
})
}
func (c chainG[T]) Where(query string, args ...any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Where(query, args...)
})
}
func (c chainG[T]) Or(query string, args ...any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Or(query, args...)
})
}
func (c chainG[T]) Not(query string, args ...any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Not(query, args...)
})
}
func (c chainG[T]) Xor(query string, args ...any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Xor(query, args...)
})
}
func (c chainG[T]) Sample(sampleList ...int) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Sample(sampleList...)
})
}
func (c chainG[T]) Fetch(name string, vid any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Fetch(name, vid)
})
}
func (c chainG[T]) FetchMulti(names []string, vid any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.FetchMulti(names, vid)
})
}
func (c chainG[T]) Lookup(name string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Lookup(name)
})
}
func (c chainG[T]) GroupBy(expr string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.GroupBy(expr)
})
}
func (c chainG[T]) Yield(expr string, distinct ...bool) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Yield(expr, distinct...)
})
}
func (c chainG[T]) OrderBy(expr string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.OrderBy(expr)
})
}
func (c chainG[T]) Limit(limit int) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Limit(limit)
})
}
func (c chainG[T]) GetSubgraph(steps int, withProp ...bool) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.GetSubgraph(steps, withProp...)
})
}
func (c chainG[T]) In(edgeTypes ...string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.In(edgeTypes...)
})
}
func (c chainG[T]) Out(edgeTypes ...string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Out(edgeTypes...)
})
}
func (c chainG[T]) Both(edgeTypes ...string) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Both(edgeTypes...)
})
}
func (c chainG[T]) InsertVertex(vertexes any, ifNotExists ...bool) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.InsertVertex(vertexes, ifNotExists...)
})
}
func (c chainG[T]) UpdateVertex(vid any, propsUpdate any, opts ...clause.Option) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.UpdateVertex(vid, propsUpdate, opts...)
})
}
func (c chainG[T]) UpsertVertex(vid any, propsUpdate any, opts ...clause.Option) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.UpsertVertex(vid, propsUpdate, opts...)
})
}
func (c chainG[T]) DeleteVertex(vid any, withEdge ...bool) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.DeleteVertex(vid, withEdge...)
})
}
func (c chainG[T]) InsertEdge(edges any, ifNotExists ...bool) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.InsertEdge(edges, ifNotExists...)
})
}
func (c chainG[T]) UpdateEdge(edge any, propsUpdate any, opts ...clause.Option) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.UpdateEdge(edge, propsUpdate, opts...)
})
}
func (c chainG[T]) UpsertEdge(edge any, propsUpdate any, opts ...clause.Option) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.UpsertEdge(edge, propsUpdate, opts...)
})
}
func (c chainG[T]) DeleteEdge(edgeTypeName string, edge any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.DeleteEdge(edgeTypeName, edge)
})
}
func (c chainG[T]) When(query string, args ...any) ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.When(query, args...)
})
}
func (c chainG[T]) Pipe() ChainInterface[T] {
return c.with(func(db *DB) *DB {
return db.Pipe()
})
}
type execG[T any] struct {
g *g[T]
}
func (g execG[T]) NGQL(ctx context.Context) (string, error) {
return g.g.apply(ctx).NGQL()
}
func (g execG[T]) RawResult(ctx context.Context) (*nebula.ResultSet, error) {
return g.g.apply(ctx).RawResult()
}
func (g execG[T]) Exec(ctx context.Context) error {
return g.g.apply(ctx).Exec()
}
func (g execG[T]) Find(ctx context.Context) ([]T, error) {
var r []T
err := g.g.apply(ctx).Find(&r)
return r, err
}
func (g execG[T]) FindCol(ctx context.Context, col string) ([]T, error) {
var r []T
err := g.g.apply(ctx).FindCol(col, &r)
return r, err
}
func (g execG[T]) Take(ctx context.Context) (T, error) {
var r T
err := g.g.apply(ctx).Take(&r)
return r, err
}
func (g execG[T]) TakeCol(ctx context.Context, col string) (T, error) {
var r T
err := g.g.apply(ctx).TakeCol(col, &r)
return r, err
}