diff --git a/code/go/example.go b/code/go/example.go index a1f8775..06fdcd0 100644 --- a/code/go/example.go +++ b/code/go/example.go @@ -3,9 +3,9 @@ package main import ( + "context" "fmt" - "github.com/neo4j/neo4j-go-driver/v4/neo4j" - "io" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" "reflect" ) @@ -19,47 +19,38 @@ func main() { } } -func runQuery(uri, database, username, password string) (result []string, err error) { - driver, err := neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, "")) +func runQuery(uri, database, username, password string) (_ []string, err error) { + ctx := context.Background() + driver, err := neo4j.NewDriverWithContext(uri, neo4j.BasicAuth(username, password, "")) if err != nil { return nil, err } - defer func() {err = handleClose(driver, err)}() - session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeRead, DatabaseName: database}) - defer func() {err = handleClose(session, err)}() - results, err := session.ReadTransaction(func(transaction neo4j.Transaction) (interface{}, error) { - result, err := transaction.Run( - ` - MATCH (p:Person {healthstatus:$status})-[v:VISITS]->(pl:Place) - WHERE p.confirmedtime < v.starttime - RETURN distinct pl.name as place LIMIT 20 - `, map[string]interface{}{ - "status": "Sick", - }) + defer func() { err = handleClose(ctx, driver, err) }() + query := " MATCH (p:Person {healthstatus:$status})-[v:VISITS]->(pl:Place) + WHERE p.confirmedtime < v.starttime + RETURN distinct pl.name as place LIMIT 20 + params := map[string]any{"status": "Sick"} + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, + neo4j.EagerResultTransformer, + neo4j.ExecuteQueryWithDatabase(database), + neo4j.ExecuteQueryWithReadersRouting()) + if err != nil { + return nil, err + } + places := make([]string, len(result.Records)) + for i, record := range result.Records { + // this assumes all actors have names, hence ignoring the 2nd returned value + name, _, err := neo4j.GetRecordValue[string](record, "place") if err != nil { return nil, err } - var arr []string - for result.Next() { - value, found := result.Record().Get("place") - if found { - arr = append(arr, value.(string)) - } - } - if err = result.Err(); err != nil { - return nil, err - } - return arr, nil - }) - if err != nil { - return nil, err + places[i] = name } - result = results.([]string) - return result, err + return places, nil } -func handleClose(closer io.Closer, previousError error) error { - err := closer.Close() +func handleClose(ctx context.Context, closer interface{ Close(context.Context) error }, previousError error) error { + err := closer.Close(ctx) if err == nil { return previousError } diff --git a/code/javascript/example.js b/code/javascript/example.js index 1ae306f..9726e4e 100644 --- a/code/javascript/example.js +++ b/code/javascript/example.js @@ -3,7 +3,7 @@ const neo4j = require('neo4j-driver'); const driver = neo4j.driver('neo4j://:', neo4j.auth.basic('', ''), - {/* encrypted: 'ENCRYPTION_OFF' */}); + {}); const query = ` diff --git a/code/python/example.py b/code/python/example.py index d6507e0..fe54160 100644 --- a/code/python/example.py +++ b/code/python/example.py @@ -1,23 +1,21 @@ -# pip3 install neo4j-driver +# pip3 install neo4j # python3 example.py from neo4j import GraphDatabase, basic_auth -driver = GraphDatabase.driver( - "neo4j://:", - auth=basic_auth("", "")) - cypher_query = ''' MATCH (p:Person {healthstatus:$status})-[v:VISITS]->(pl:Place) WHERE p.confirmedtime < v.starttime RETURN distinct pl.name as place LIMIT 20 ''' -with driver.session(database="neo4j") as session: - results = session.read_transaction( - lambda tx: tx.run(cypher_query, - status="Sick").data()) - for record in results: - print(record['place']) - -driver.close() +with GraphDatabase.driver( + "neo4j://:", + auth=("", "") +) as driver: + result = driver.execute_query( + cypher_query, + status="Sick", + database_="neo4j") + for record in result.records: + print(record['place'])