-
Notifications
You must be signed in to change notification settings - Fork 55
/
cypher.go
178 lines (165 loc) · 4.95 KB
/
cypher.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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package neoism
import (
"encoding/json"
"errors"
)
// A CypherQuery is a statement in the Cypher query language, with optional
// parameters and result. If Result value is supplied, result data will be
// unmarshalled into it when the query is executed. Result must be a pointer
// to a slice of structs - e.g. &[]someStruct{}.
type CypherQuery struct {
Statement string `json:"statement"`
Parameters map[string]interface{} `json:"parameters"`
Result interface{} `json:"-"`
cr cypherResult
IncludeStats bool `json:"includeStats"`
stats *Stats
}
// Columns returns the names, in order, of the columns returned for this query.
// Empty if query has not been executed.
func (cq *CypherQuery) Columns() []string {
return cq.cr.Columns
}
// Unmarshal decodes result data into v, which must be a pointer to a slice of
// structs - e.g. &[]someStruct{}. Struct fields are matched up with fields
// returned by the cypher query using the `json:"fieldName"` tag.
func (cq *CypherQuery) Unmarshal(v interface{}) error {
// We do a round-trip thru the JSON marshaller. A fairly simple way to
// do type-safe unmarshalling, but perhaps not the most efficient solution.
rs := make([]map[string]*json.RawMessage, len(cq.cr.Data))
for rowNum, row := range cq.cr.Data {
m := map[string]*json.RawMessage{}
for colNum, col := range row {
name := cq.cr.Columns[colNum]
m[name] = col
}
rs[rowNum] = m
}
b, err := json.Marshal(rs)
if err != nil {
logPretty(err)
return err
}
return json.Unmarshal(b, v)
}
func (cq *CypherQuery) Stats() (*Stats, error) {
if cq.stats == nil {
return nil, errors.New("stats were not requested at query time")
}
return cq.stats, nil
}
type cypherRequest struct {
Query string `json:"query"`
Parameters map[string]interface{} `json:"params"`
}
type Stats struct {
ConstraintsAdded int `json:"constraints_added"`
ConstraintsRemoved int `json:"constraints_removed"`
ContainsUpdates bool `json:"contains_updates"`
IndexesAdded int `json:"indexes_added"`
IndexesRemoved int `json:"indexes_removed"`
LabelsAdded int `json:"labels_added"`
LabelsRemoved int `json:"labels_removed"`
NodesCreated int `json:"nodes_created"`
NodesDeleted int `json:"nodes_deleted"`
PropertiesSet int `json:"properties_set"`
RelationshipDeleted int `json:"relationship_deleted"`
RelationshipsCreated int `json:"relationships_created"`
}
type cypherResult struct {
Columns []string
Data [][]*json.RawMessage
Stats *Stats
}
// Cypher executes a db query written in the Cypher language. Data returned
// from the db is used to populate `result`, which should be a pointer to a
// slice of structs. TODO: Or a pointer to a two-dimensional array of structs?
func (db *Database) Cypher(q *CypherQuery) error {
result := cypherResult{}
payload := cypherRequest{
Query: q.Statement,
Parameters: q.Parameters,
}
ne := NeoError{}
url := db.HrefCypher
if q.IncludeStats {
url = db.HrefCypher + "?includeStats=true"
}
// Method: "POST"
// Data: &cReq
// Result: &cRes
// Error: ne
// }
resp, err := db.Session.Post(url, &payload, &result, &ne)
if err != nil {
return err
}
if resp.Status() != 200 {
return ne
}
q.cr = result
if q.Result != nil {
q.Unmarshal(q.Result)
}
q.stats = q.cr.Stats
return nil
}
type batchCypherQuery struct {
Method string `json:"method"`
To string `json:"to"`
Id int `json:"id"`
Body cypherRequest `json:"body"`
}
type batchCypherResponse struct {
Id int
Location string
Body cypherResult
}
// CypherBatch executes a set of cypher queries as a batch. When using the
// {[JOB ID]} special syntax to inject URIs from created resources into JSON
// strings in subsequent job descriptions, CypherQuery's batch id will be its
// index in the slice.
func (db *Database) CypherBatch(qs []*CypherQuery) error {
payload := make([]batchCypherQuery, len(qs))
for i, q := range qs {
payload[i] = batchCypherQuery{
Method: "POST",
To: "/cypher",
Id: i,
Body: cypherRequest{
Query: q.Statement,
Parameters: q.Parameters,
},
}
if q.IncludeStats {
payload[i].To = "/cypher?includeStats=true"
}
}
res := []batchCypherResponse{}
ne := NeoError{}
url := db.HrefBatch
resp, err := db.Session.Post(url, payload, &res, &ne)
if err != nil {
return err
}
if resp.Status() != 200 {
return ne
}
if len(res) != len(qs) {
return errors.New("Result count does not match query count")
}
for i, s := range qs {
s.cr = res[i].Body
if s.Result != nil {
err := s.Unmarshal(s.Result)
if err != nil {
return err
}
}
s.stats = s.cr.Stats
}
return nil
}