-
Notifications
You must be signed in to change notification settings - Fork 22
/
api_chainable.go
94 lines (77 loc) · 1.73 KB
/
api_chainable.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
package norm
import (
"fmt"
"strings"
"github.com/zhihu/norm/v3/constants"
)
// Debug will print the nGql when it exec
func (db *DB) Debug() (tx *DB) {
tx = db.getInstance()
tx.debug = true
return
}
func (db *DB) Go(step int) (tx *DB) {
tx = db.getInstance()
if step > 1 {
tx.sql += fmt.Sprintf("go %d step ", step)
}
return
}
func (db *DB) From(vs ...IVertex) (tx *DB) {
tx = db.getInstance()
vids := make([]string, len(vs))
for i, v := range vs {
vids[i] = GetVidWithPolicy(v.GetVid(), v.GetPolicy())
}
if tx.sql == "" {
tx.sql += "go "
}
tx.sql += fmt.Sprintf("from %s ", strings.Join(vids, ","))
return
}
func (db *DB) Over(edges ...IEdge) (tx *DB) {
tx = db.getInstance()
names := make([]string, len(edges))
for i, edge := range edges {
names[i] = edge.EdgeName()
}
sql := strings.Join(names, ",")
tx.sql += fmt.Sprintf("over %s ", sql)
return
}
// Reversely over egde reversely
func (db *DB) Reversely() (tx *DB) {
tx = db.getInstance()
tx.sql += constants.DirectionReversely + " "
return
}
// Bidirect over egde bidirect
func (db *DB) Bidirect() (tx *DB) {
tx = db.getInstance()
tx.sql += constants.DirectionBidirect + " "
return
}
func (db *DB) Limit(limit int) (tx *DB) {
tx = db.getInstance()
tx.sql += fmt.Sprintf("|limit %d ", limit)
return
}
func (db *DB) Where(sql string) (tx *DB) {
tx = db.getInstance()
tx.sql += fmt.Sprintf("where %s ", sql)
return
}
func (db *DB) Yield(sql string) (tx *DB) {
tx = db.getInstance()
tx.sql += fmt.Sprintf("yield %s ", sql)
return
}
func (db *DB) Group(fields ...string) (tx *DB) {
tx = db.getInstance()
for i := range fields {
fields[i] = "$-." + fields[i]
}
sql := strings.Join(fields, ",")
tx.sql += fmt.Sprintf("|group by %s ", sql)
return
}