3
3
package main
4
4
5
5
import (
6
+ "context"
6
7
"fmt"
7
- "github.com/neo4j/neo4j-go-driver/v4/neo4j"
8
- "io"
8
+ "github.com/neo4j/neo4j-go-driver/v5/neo4j"
9
9
"reflect"
10
10
)
11
11
12
12
func main () {
13
- results , err := runQuery ("bolt ://<HOST>:<BOLTPORT>" , "neo4j" , "<USERNAME>" , "<PASSWORD>" )
13
+ results , err := runQuery ("neo4j ://<HOST>:<BOLTPORT>" , "neo4j" , "<USERNAME>" , "<PASSWORD>" )
14
14
if err != nil {
15
15
panic (err )
16
16
}
@@ -19,46 +19,37 @@ func main() {
19
19
}
20
20
}
21
21
22
- func runQuery (uri , database , username , password string ) (result []string , err error ) {
23
- driver , err := neo4j .NewDriver (uri , neo4j .BasicAuth (username , password , "" ))
22
+ func runQuery (uri , database , username , password string ) (_ []string , err error ) {
23
+ ctx := context .Background ()
24
+ driver , err := neo4j .NewDriverWithContext (uri , neo4j .BasicAuth (username , password , "" ))
24
25
if err != nil {
25
26
return nil , err
26
27
}
27
- defer func () {err = handleClose (driver , err )}()
28
- session := driver .NewSession (neo4j.SessionConfig {AccessMode : neo4j .AccessModeRead , DatabaseName : database })
29
- defer func () {err = handleClose (session , err )}()
30
- results , err := session .ReadTransaction (func (transaction neo4j.Transaction ) (interface {}, error ) {
31
- result , err := transaction .Run (
32
- `
33
- MATCH (dc:DataCenter {location: $location})-[:CONTAINS]->(r:Router)-[:ROUTES]->(i:Interface)
34
- RETURN i.ip as ip
35
- ` , map [string ]interface {}{
36
- "location" : "Iceland" ,
37
- })
28
+ defer func () { err = handleClose (ctx , driver , err ) }()
29
+ query := " MATCH (dc:DataCenter {location: $location})-[:CONTAINS]->(r:Router)-[:ROUTES]->(i:Interface)
30
+ RETURN i .ip as ip
31
+ params := map [string ]any {"location" : "Iceland" }
32
+ result , err := neo4j .ExecuteQuery (ctx , driver , query , params ,
33
+ neo4j .EagerResultTransformer ,
34
+ neo4j .ExecuteQueryWithDatabase (database ),
35
+ neo4j .ExecuteQueryWithReadersRouting ())
36
+ if err != nil {
37
+ return nil , err
38
+ }
39
+ ips := make ([]string , len (result .Records ))
40
+ for i , record := range result .Records {
41
+ // this assumes all actors have names, hence ignoring the 2nd returned value
42
+ name , _ , err := neo4j .GetRecordValue [string ](record , "ip" )
38
43
if err != nil {
39
44
return nil , err
40
45
}
41
- var arr []string
42
- for result .Next () {
43
- value , found := result .Record ().Get ("ip" )
44
- if found {
45
- arr = append (arr , value .(string ))
46
- }
47
- }
48
- if err = result .Err (); err != nil {
49
- return nil , err
50
- }
51
- return arr , nil
52
- })
53
- if err != nil {
54
- return nil , err
46
+ ips [i ] = name
55
47
}
56
- result = results .([]string )
57
- return result , err
48
+ return ips , nil
58
49
}
59
50
60
- func handleClose (closer io. Closer , previousError error ) error {
61
- err := closer .Close ()
51
+ func handleClose (ctx context. Context , closer interface { Close (context. Context ) error } , previousError error ) error {
52
+ err := closer .Close (ctx )
62
53
if err == nil {
63
54
return previousError
64
55
}
0 commit comments