-
Notifications
You must be signed in to change notification settings - Fork 0
/
neo4j_run.go
130 lines (118 loc) · 3.21 KB
/
neo4j_run.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
package neo4j_extended
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/neo4j/neo4j-go-driver/neo4j"
)
var NDriver *neo4j.Driver
var Neo4jLog *log.Logger
func init() {
l1, err := os.OpenFile("/home/workerferd/go/src/github.com/FerdinandWittmann/neo4j.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
Neo4jLog = log.New(l1, "", log.Ldate|log.Ltime|log.Lshortfile)
}
//SetDriver sets the driver variable should be locally initialized
func SetDriver(_driver *neo4j.Driver) {
NDriver = _driver
}
//CreateSession creates neo3j Session from driver
func CreateSession(accessMode neo4j.AccessMode) (session *neo4j.Session, err error) {
driver := (*NDriver)
_session, err := driver.NewSession(neo4j.SessionConfig{
AccessMode: accessMode,
DatabaseName: "coli",
})
if err != nil {
return nil, err
}
session = (&_session)
return session, nil
}
//SendSimple takes a singler cypher instruction and returns a single value
func SendSimple(session neo4j.Session, cypher string) (res interface{}, err error) {
result, err := session.Run(cypher, nil)
if err != nil {
return nil, err
}
Neo4jLog.Println(cypher)
if result.Next() {
return result.Record().Values()[0], nil
}
return nil, result.Err()
}
//Send Sends a new Cypher
func (n *NeoRequest) SendNew(accessMode neo4j.AccessMode) (result *neo4j.Result, err error) {
session, err := CreateSession(accessMode)
defer (*session).Close()
if err != nil {
return nil, err
}
cypherList := append(n.multiCypher)
cypher := MultiCypherToCypher(cypherList)
logCypher(cypher, &n.params)
_result, err := (*session).Run(cypher, n.params)
if err != nil {
return nil, err
}
if err = _result.Err(); err != nil {
return nil, err
}
result = &_result
return result, err
}
//Send Sends a new Cypher
func (n *NeoRequest) Send(session *neo4j.Session) (result *neo4j.Result, err error) {
cypherList := append(n.multiCypher)
cypher := MultiCypherToCypher(cypherList)
logCypher(cypher, &n.params)
_result, err := (*session).Run(cypher, n.params)
if err != nil {
return nil, err
}
result = (&_result)
return result, err
}
//PrettyPrintValues prints the map of a neo4j Node result takes as input rec.values()
func PrettyPrintValues(res *neo4j.Result) {
for (*res).Next() {
rec := (*res).Record()
for _, value := range rec.Keys() {
_values, ok := rec.Get(value)
if !ok {
fmt.Println("what")
}
switch o := _values.(type) {
case neo4j.Node:
values_map := o.Props()
b, err := json.MarshalIndent(values_map, "", " ")
if err != nil {
fmt.Println("@PrettyPrintValues: ", err)
}
fmt.Print(string(b))
case neo4j.Relationship:
values_map := o.Props()
b, err := json.MarshalIndent(values_map, "", " ")
if err != nil {
fmt.Println("@PrettyPrintValues: ", err)
}
fmt.Print(string(b))
}
}
}
}
//logCypher logs a cypher line and adds the correct values into the string
func logCypher(cypher string, fields *map[string]interface{}) {
for key, value := range *fields {
valueString := func(_value interface{}) (ret string) {
return fmt.Sprintf("%v", _value)
}(value)
cypher = strings.Replace(cypher, "$"+key, valueString, -1)
//fmt.Println(cypher)
}
Neo4jLog.Println(cypher)
}