File tree Expand file tree Collapse file tree 5 files changed +157
-0
lines changed Expand file tree Collapse file tree 5 files changed +157
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Neo4j \QueryAPI ;
4
+ require __DIR__ . '/../vendor/autoload.php ' ;
5
+
6
+
7
+ use GuzzleHttp \Client ;
8
+
9
+ class Profile
10
+ {
11
+ private $ neo4jUrl ;
12
+ private $ username ;
13
+ private $ password ;
14
+ private $ client ;
15
+
16
+ public function __construct ($ url , $ username , $ password )
17
+ {
18
+ $ this ->neo4jUrl = $ url ;
19
+ $ this ->username = $ username ;
20
+ $ this ->password = $ password ;
21
+ $ this ->client = new Client ();
22
+ }
23
+
24
+ public function executeQuery ($ query ,$ parameters =[])
25
+ {
26
+ $ response = $ this ->client ->post ($ this ->neo4jUrl , [
27
+ 'auth ' => [$ this ->username , $ this ->password ],
28
+ 'json ' => [
29
+ 'statement ' => $ query ,
30
+ 'parameters ' =>$ parameters
31
+ ]
32
+ ]);
33
+
34
+ return json_decode ($ response ->getBody (), true );
35
+ }
36
+
37
+ public function formatResponse ($ data ): array
38
+ {
39
+ $ output = [
40
+ "data " => [
41
+ "fields " => [],
42
+ "values " => []
43
+ ],
44
+ "profiledQueryPlan " => [],
45
+ "bookmarks " => $ data ['bookmarks ' ] ?? []
46
+ ];
47
+
48
+ if (isset ($ data ['result ' ]['columns ' ]) && isset ($ data ['result ' ]['rows ' ])) {
49
+ $ output ["data " ]["fields " ] = $ data ['result ' ]['columns ' ];
50
+ foreach ($ data ['result ' ]['rows ' ] as $ row ) {
51
+ $ output ["data " ]["values " ][] = $ row ;
52
+ }
53
+ }
54
+
55
+ if (isset ($ data ['profiledQueryPlan ' ])) {
56
+ $ output ["profiledQueryPlan " ] = $ data ['profiledQueryPlan ' ];
57
+ }
58
+
59
+ return $ output ;
60
+ }
61
+ }
62
+
Original file line number Diff line number Diff line change
1
+ <?php
2
+ require __DIR__ . '/../vendor/autoload.php ' ;
3
+
4
+ use GuzzleHttp \Client ;
5
+
6
+ $ neo4jUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2 ' ;
7
+ $ username = 'neo4j ' ;
8
+ $ password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 ' ;
9
+
10
+ $ client = new Client ();
11
+
12
+ $ query = "PROFILE MATCH (n:Person) RETURN n " ;
13
+
14
+ $ response = $ client ->post ($ neo4jUrl , [
15
+ 'auth ' => [$ username , $ password ],
16
+ 'json ' => [
17
+ 'statement ' => $ query
18
+ ]
19
+ ]);
20
+
21
+ $ body = $ response ->getBody ();
22
+ $ data = json_decode ($ body , true );
23
+
24
+ $ output = [
25
+ "data " => [
26
+ "fields " => [],
27
+ "values " => []
28
+ ],
29
+ "profiledQueryPlan " => [],
30
+ "bookmarks " => $ data ['bookmarks ' ] ?? []
31
+ ];
32
+
33
+ if (isset ($ data ['result ' ]['columns ' ]) && isset ($ data ['result ' ]['rows ' ])) {
34
+ $ output ["data " ]["fields " ] = $ data ['result ' ]['columns ' ];
35
+ foreach ($ data ['result ' ]['rows ' ] as $ row ) {
36
+ $ output ["data " ]["values " ][] = $ row ;
37
+ }
38
+ }
39
+
40
+ if (isset ($ data ['profiledQueryPlan ' ])) {
41
+ $ output ["profiledQueryPlan " ] = $ data ['profiledQueryPlan ' ];
42
+ }
43
+
44
+ echo json_encode ($ output , JSON_PRETTY_PRINT );
45
+
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Neo4j \QueryAPI \Profile ;
4
+
5
+ require __DIR__ . '/../vendor/autoload.php ' ;
6
+
7
+ $ neo4jUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2 ' ;
8
+ $ username = 'neo4j ' ;
9
+ $ password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 ' ;
10
+
11
+ $ client = new Profile ($ neo4jUrl , $ username , $ password );
12
+
13
+ $ params = ['name ' => 'Alice ' ];
14
+
15
+ $ query = "PROFILE MATCH (n:Person {name: \$name}) RETURN n.name " ;
16
+
17
+ $ data = $ client ->executeQuery ($ query , $ params );
18
+
19
+
20
+ $ formattedResponse = $ client ->formatResponse ($ data );
21
+
22
+ echo json_encode ($ formattedResponse , JSON_PRETTY_PRINT );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ require __DIR__ . '/../vendor/autoload.php ' ;
4
+
5
+ use Neo4j \QueryAPI \Neo4jQueryAPI ;
6
+
7
+ $ neo4jUrl = 'https://6f72daa1.databases.neo4j.io/ ' ;
8
+ $ username = 'neo4j ' ;
9
+ $ password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0 ' ;
10
+
11
+ $ api = Neo4jQueryAPI::login ($ neo4jUrl , $ username , $ password );
12
+
13
+ $ transaction = $ api ->beginTransaction ();
14
+
15
+ $ query = 'CREATE (n:Person {name: "Bobby"}) RETURN n ' ;
16
+
17
+ $ response = $ transaction ->run ($ query , []);
18
+
19
+ print_r ($ response );
20
+
21
+ $ transaction ->commit ();
Original file line number Diff line number Diff line change @@ -44,6 +44,13 @@ public function testCounters(): void
44
44
$ this ->assertEquals (1 , $ result ->getQueryCounters ()->getNodesCreated ());
45
45
}
46
46
47
+ public function testProfileExistence (): void
48
+ {
49
+ $ result = $ this ->api ->run ('PROFILE MATCH (x) RETURN x ' );
50
+
51
+ $ this ->assertNotNull ($ result ->getProfiledQueryPlan ());
52
+ }
53
+
47
54
public function testTransactionCommit (): void
48
55
{
49
56
// Begin a new transaction
You can’t perform that action at this time.
0 commit comments