From a701ca60badbf5fb7b734d4585ec4de3ed2ece11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandbox=20=F0=9F=A4=96?= Date: Tue, 5 Dec 2023 10:58:33 +0000 Subject: [PATCH] Triggered by direct commit. Origin: https://github.com/neo4j-contrib/sandbox-code-updater/commit/081ae2117c9bbf7972b8f6551e917b9dd796f7cf --- code/csharp/Example.cs | 2 +- code/go/example.go | 59 ++++++++++++++++---------------------- code/java/Example.java | 2 +- code/javascript/example.js | 4 +-- code/python/example.py | 24 +++++++--------- 5 files changed, 40 insertions(+), 51 deletions(-) diff --git a/code/csharp/Example.cs b/code/csharp/Example.cs index 6f56df4..e8077e4 100644 --- a/code/csharp/Example.cs +++ b/code/csharp/Example.cs @@ -13,7 +13,7 @@ namespace dotnet { class Example { static async Task Main() { - var driver = GraphDatabase.Driver("bolt://:", + var driver = GraphDatabase.Driver("neo4j://:", AuthTokens.Basic("", "")); var cypherQuery = diff --git a/code/go/example.go b/code/go/example.go index 6274d0f..2b2c494 100644 --- a/code/go/example.go +++ b/code/go/example.go @@ -3,14 +3,14 @@ package main import ( + "context" "fmt" - "github.com/neo4j/neo4j-go-driver/v4/neo4j" - "io" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" "reflect" ) func main() { - results, err := runQuery("bolt://:", "neo4j", "", "") + results, err := runQuery("neo4j://:", "neo4j", "", "") if err != nil { panic(err) } @@ -19,46 +19,37 @@ 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 (a:Officer {name: $name})-[r:OFFICER_OF|INTERMEDIARY_OF|REGISTERED_ADDRESS*..5]-(b) - RETURN distinct b.name as name LIMIT 20 - `, map[string]interface{}{ - "name": "Stuart Onslow-Smith", - }) + defer func() { err = handleClose(ctx, driver, err) }() + query := " MATCH (a:Officer {name: $name})-[r:OFFICER_OF|INTERMEDIARY_OF|REGISTERED_ADDRESS*..5]-(b) + RETURN distinct b.name as name LIMIT 20 + params := map[string]any{"name": "Stuart Onslow-Smith"} + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, + neo4j.EagerResultTransformer, + neo4j.ExecuteQueryWithDatabase(database), + neo4j.ExecuteQueryWithReadersRouting()) + if err != nil { + return nil, err + } + names := 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, "name") if err != nil { return nil, err } - var arr []string - for result.Next() { - value, found := result.Record().Get("name") - 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 + names[i] = name } - result = results.([]string) - return result, err + return names, 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/java/Example.java b/code/java/Example.java index 9aa8eb6..4e7cd7a 100644 --- a/code/java/Example.java +++ b/code/java/Example.java @@ -11,7 +11,7 @@ public class Example { public static void main(String...args) { - Driver driver = GraphDatabase.driver("bolt://:", + Driver driver = GraphDatabase.driver("neo4j://:", AuthTokens.basic("","")); try (Session session = driver.session(SessionConfig.forDatabase("neo4j"))) { diff --git a/code/javascript/example.js b/code/javascript/example.js index 18b7425..221fecf 100644 --- a/code/javascript/example.js +++ b/code/javascript/example.js @@ -1,9 +1,9 @@ // npm install --save neo4j-driver // node example.js const neo4j = require('neo4j-driver'); -const driver = neo4j.driver('bolt://:', +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 872e98f..eeb17cd 100644 --- a/code/python/example.py +++ b/code/python/example.py @@ -1,22 +1,20 @@ -# pip3 install neo4j-driver +# pip3 install neo4j # python3 example.py from neo4j import GraphDatabase, basic_auth -driver = GraphDatabase.driver( - "bolt://:", - auth=basic_auth("", "")) - cypher_query = ''' MATCH (a:Officer {name: $name})-[r:OFFICER_OF|INTERMEDIARY_OF|REGISTERED_ADDRESS*..5]-(b) RETURN distinct b.name as name LIMIT 20 ''' -with driver.session(database="neo4j") as session: - results = session.read_transaction( - lambda tx: tx.run(cypher_query, - name="Stuart Onslow-Smith").data()) - for record in results: - print(record['name']) - -driver.close() +with GraphDatabase.driver( + "neo4j://:", + auth=("", "") +) as driver: + result = driver.execute_query( + cypher_query, + name="Stuart Onslow-Smith", + database_="neo4j") + for record in result.records: + print(record['name'])