Skip to content

Commit 4624051

Browse files
nsaukklobuczek
andauthored
Add examples for features not yet implemented (#253)
* Add examples for features not yet implemented * Skip AuthToken and QueryConfig except for 2 examples * Fix examples * Fix commas --------- Co-authored-by: Heinrich Klobuczek <heinrich@mail.com>
1 parent 62f8b6f commit 4624051

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

docs/dev_manual_todo_examples.rb

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
######################################
2+
# Query the database
3+
######################################
4+
5+
result = driver.execute_query(
6+
'MATCH (p:Person {age: $age}) RETURN p.name AS name',
7+
nil, # auth_token not specified - nil may be omitted
8+
{ database: 'neo4j' }, # default value may be omitted
9+
age: 42
10+
)
11+
12+
# To access records after
13+
records = result.records
14+
records.each do |r|
15+
puts r
16+
end
17+
18+
# Summary information
19+
summary = result.summary
20+
puts "The query #{summary.query.text} returned #{records.size} records in #{summary.result_available_after} ms."
21+
22+
######################################
23+
# Write to the database
24+
######################################
25+
26+
# Create two nodes and a relationship
27+
result = driver.execute_query(
28+
'CREATE (a:Person {name: $name})
29+
CREATE (b:Person {friend: $name})
30+
CREATE (a)-[:KNOWS]->(b)',
31+
name: 'Alice',
32+
friend: 'David'
33+
)
34+
35+
summary = result.summary
36+
puts "Created #{summary.counters.nodes_created} records in #{summary.result_available_after} ms."
37+
38+
######################################
39+
# Read from the database
40+
######################################
41+
42+
# Retrieve all Person nodes who know other Persons
43+
result = driver.execute_query(
44+
'MATCH (p:Person)-[:KNOWS]->(:Person)
45+
RETURN p.name AS name'
46+
)
47+
48+
records = result.records
49+
records.each do |r|
50+
puts r
51+
end
52+
53+
summary = result.summary
54+
puts "The query #{summary.query} returned #{records.size} records in #{summary.result_available_after} ms."
55+
56+
######################################
57+
# Update the database
58+
######################################
59+
60+
# Update node Alice to add an age property
61+
result = driver.execute_query(
62+
'MATCH (p:Person {name: $name})
63+
SET p.age = $age',
64+
name: 'Alice',
65+
age: 42
66+
)
67+
68+
summary = result.summary
69+
puts "Query updated the database?"
70+
puts summary.counters.contains_updates?
71+
72+
# Create a relationship :KNOWS between Alice and Bob
73+
result = driver.execute_query(
74+
'MATCH (alice:Person {name: $name})
75+
MATCH (bob:Person {name: $friend})
76+
CREATE (alice)-[:KNOWS]->(bob)',
77+
name: 'Alice',
78+
friend: 'Bob'
79+
)
80+
81+
summary = result.summary
82+
puts 'Query updated the database?'
83+
puts summary.counters.contains_updates?
84+
85+
######################################
86+
# Delete from the database
87+
######################################
88+
89+
# Remove the Alice node and all its relationships
90+
result = driver.execute_query(
91+
'MATCH (p:Person {name: $name})
92+
DETACH DELETE p',
93+
name: 'Alice'
94+
)
95+
96+
summary = result.summary
97+
puts "Query updated the database?"
98+
puts summary.counters.contains_updates?
99+
100+
######################################
101+
# Query configuration
102+
######################################
103+
104+
# Database selection
105+
driver.execute_query(
106+
'MATCH (p:Person) RETURN p.name',
107+
{ database: 'neo4j' },
108+
age: 42
109+
)
110+
111+
# Run queries as a different user
112+
auth_token = Neo4j::Driver::AuthTokens.basic('another_user', 'password')
113+
driver.execute_query(
114+
'MATCH (p:Person) RETURN p.name',
115+
auth_token,
116+
age: 42
117+
)
118+
119+
######################################
120+
# Summary
121+
######################################
122+
123+
# Retrieve the execution summary
124+
result = driver.execute_query(
125+
"UNWIND ['Alice', 'Bob'] AS name
126+
MERGE (p:Person {name: name})"
127+
)
128+
129+
puts result.summary
130+
131+
# Query counters
132+
result = driver.execute_query(
133+
"MERGE (p:Person {name: $name})
134+
MERGE (p)-[:KNOWS]->(:Person {name: $friend})",
135+
name: 'Mark',
136+
friend: 'Bob'
137+
)
138+
query_counters = result.summary.counters
139+
puts query_counters
140+
141+
# Query execution plan
142+
result = driver.execute_query(
143+
'EXPLAIN MATCH (p {name: $name}) RETURN p',
144+
name: 'Alice'
145+
)
146+
query_plan = result.summary.plan.arguments['string-representation']
147+
puts query_plan
148+
149+
# Notifications with Neo4j status codes
150+
result = driver.execute_query(
151+
"MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end}))
152+
RETURN p",
153+
start: 'Alice',
154+
end: 'Bob'
155+
)
156+
notifications = result.summary.notifications
157+
puts notifications
158+
159+
# Notifications with GQL status codes
160+
result = execute_query(
161+
"MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end}))
162+
RETURN p",
163+
start: 'Alice',
164+
end: 'Bob'
165+
)
166+
statuses = result.summary.gql_status_objects
167+
puts statuses
168+
169+
# Filter notifications
170+
# Allow only WARNING notifications, but not of HINT or GENERIC classifications
171+
driver = Neo4j::Driver::GraphDatabase.driver(
172+
'bolt://localhost:7687',
173+
Neo4j::Driver::AuthTokens.basic('neo4j', 'password'),
174+
notification_config: {
175+
minimum_severity: :warning, # use :off to disable entirely
176+
disabled_categories: [:hint, :generic]
177+
}
178+
)

0 commit comments

Comments
 (0)