forked from johnnadratowski/golang-neo4j-bolt-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
74 lines (61 loc) · 2.08 KB
/
result.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
package golangNeo4jBoltDriver
import "github.com/johnnadratowski/golang-neo4j-bolt-driver/errors"
// Result represents a result from a query that returns no data
type Result interface {
// LastInsertId Always returns -1. This is necessary
// to meet the sql.driver interface
LastInsertId() (int64, error)
// RowsAffected returns the number of rows affected
// This doesn't currently support updates, only
// inserts/deletions
RowsAffected() (int64, error)
// Metadata returns the metadata response from neo4j
Metadata() map[string]interface{}
}
type boltResult struct {
metadata map[string]interface{}
}
func newResult(metadata map[string]interface{}) boltResult {
return boltResult{metadata: metadata}
}
// Returns the response metadata from the bolt success message
func (r boltResult) Metadata() map[string]interface{} {
return r.metadata
}
// LastInsertId gets the last inserted id. This will always return -1.
func (r boltResult) LastInsertId() (int64, error) {
// TODO: Is this possible?
return -1, nil
}
// RowsAffected returns the number of nodes+rels created/deleted. For reasons of limitations
// on the API, we cannot tell how many nodes+rels were updated, only how many properties were
// updated. If this changes in the future, number updated will be added to the output of this
// interface.
func (r boltResult) RowsAffected() (int64, error) {
// metadata omits stats when rowsAffected == 0, check existence to prevent panic
if _, ok := r.metadata["stats"]; !ok {
return 0, nil
}
stats, ok := r.metadata["stats"].(map[string]interface{})
if !ok {
return -1, errors.New("Unrecognized type for stats metadata: %#v", r.metadata)
}
var rowsAffected int64
nodesCreated, ok := stats["nodes-created"]
if ok {
rowsAffected += nodesCreated.(int64)
}
relsCreated, ok := stats["relationships-created"]
if ok {
rowsAffected += relsCreated.(int64)
}
nodesDeleted, ok := stats["nodes-deleted"]
if ok {
rowsAffected += nodesDeleted.(int64)
}
relsDeleted, ok := stats["relationships-deleted"]
if ok {
rowsAffected += relsDeleted.(int64)
}
return rowsAffected, nil
}