-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.go
More file actions
201 lines (161 loc) · 4.43 KB
/
Copy pathnode.go
File metadata and controls
201 lines (161 loc) · 4.43 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
package gopherneo
import (
"bytes"
"encoding/json"
"fmt"
)
func (c *Connection) FindNode(label string, key string, val interface{}, result interface{}) (found bool, err error) {
if len(label) == 0 {
err = fmt.Errorf("a label is required to find nodes")
return
}
cr, err := c.FindNodesPaginated(label, key, val, "", 0, 0)
if err != nil || len(cr.Rows) == 0 {
return
}
if len(cr.Rows) > 1 {
err = fmt.Errorf("found more than one %v node where '%v'='%v'", label, key, val)
return
}
if len(cr.Rows) == 1 && len(cr.Rows[0]) > 0 {
err = json.Unmarshal(*cr.Rows[0][0], &result)
found = true
}
return
}
func (c *Connection) FindNodesPaginated(label string, key string, val interface{}, orderClause string, pg int, pgSize int) (cr CypherResult, err error) {
if len(label) == 0 {
err = fmt.Errorf("a label is required to find nodes")
return
}
// determine where part
params := &map[string]interface{}{}
whereCypher, whereParams := cypherForWhere("n", key, val, true)
if len(whereParams) > 0 {
*params = whereParams
}
pagPart := cypherForPagination(pg, pgSize)
// TODO ghetto. pass in order object instead of strings
orderPart := orderClause
parts := []string{
fmt.Sprintf("MATCH (n:%v)", label),
whereCypher,
"RETURN n",
orderPart,
pagPart,
}
cypher := joinUsing(parts, " ")
cr, err = c.ExecuteCypher(cypher, params)
if err != nil {
return
}
return
}
func (c *Connection) CreateNode(label string, props *map[string]interface{}, node interface{}) (err error) {
if len(label) == 0 {
err = fmt.Errorf("a label is required to create a node")
return
}
cypher := fmt.Sprintf(`CREATE (n:%v {p}) RETURN n`, label)
// add my cypher props to a map[string]interface{}
params := &map[string]interface{}{
"p": props,
}
cr, err := c.ExecuteCypher(cypher, params)
if err != nil {
return
}
if node != nil && len(cr.Rows) == 1 && len(cr.Rows[0]) > 0 {
err = json.Unmarshal(*cr.Rows[0][0], &node)
}
return
}
func (c *Connection) UpdateNode(label string, key string, val interface{}, props *map[string]interface{}, node interface{}) (err error) {
if len(label) == 0 {
err = fmt.Errorf("a label is required to update a node")
return
}
params := make(map[string]interface{})
// normally we'd use 'SET {props}' but that replaces _all_ the node's props
// and here we want to do it with only the props the user provides
setCypher, setParams := cypherForSetProps("n", props)
// copy params
for k, v := range setParams {
params[k] = v
}
whereCypher, whereParams := cypherForWhere("n", key, val, true)
// copy params
for k, v := range whereParams {
params[k] = v
}
parts := []string{
fmt.Sprintf("MATCH (n:%v)", label),
whereCypher,
setCypher,
"RETURN n",
}
cypher := joinUsing(parts, " ")
cr, err := c.ExecuteCypher(cypher, ¶ms)
if err != nil {
return
}
if node != nil && len(cr.Rows) == 1 && len(cr.Rows[0]) > 0 {
err = json.Unmarshal(*cr.Rows[0][0], &node)
}
return
}
func (c *Connection) DeleteNodes(label string, key string, val interface{}) (err error) {
if len(label) == 0 {
err = fmt.Errorf("a label is required to delete nodes")
return
}
params := &map[string]interface{}{}
whereCypher, whereParams := cypherForWhere("n", key, val, true)
if len(whereParams) > 0 {
*params = whereParams
}
parts := []string{
fmt.Sprintf("MATCH (n:%v)", label),
whereCypher,
"OPTIONAL MATCH (n)-[r]-() DELETE n, r",
}
cypher := joinUsing(parts, " ")
_, err = c.ExecuteCypher(cypher, params)
return
}
func cypherForSetProps(alias string, props *map[string]interface{}) (cypher string, params map[string]interface{}) {
params = make(map[string]interface{})
parts := make([]string, len(*props))
i := 0
for key, val := range *props {
paramKey := fmt.Sprintf("setval%d", i)
parts[i] = fmt.Sprintf("n.%v={%v}", key, paramKey)
params[paramKey] = val
i++
}
cypher = "SET " + joinUsing(parts, ", ")
return
}
func cypherForWhere(alias string, key string, val interface{}, inclKeyword bool) (cypher string, params map[string]interface{}) {
var b bytes.Buffer
if len(key) > 0 {
if inclKeyword {
b.WriteString("WHERE ")
}
b.WriteString(fmt.Sprintf("%v.%v={whereval}", alias, key))
params = make(map[string]interface{})
params["whereval"] = val
}
cypher = b.String()
return
}
func cypherForPagination(pg int, pgSize int) (cypher string) {
if pg < 0 {
pg = 0
}
if pgSize > 0 {
skip := pg * pgSize
cypher = fmt.Sprintf("SKIP %d LIMIT %d", skip, pgSize)
}
return
}