Skip to content

Add execute_query #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: 5.0
Choose a base branch
from
9 changes: 5 additions & 4 deletions docs/dev_manual_todo_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

result = driver.execute_query(
'MATCH (p:Person {age: $age}) RETURN p.name AS name',
nil, # auth_token not specified - nil may be omitted
{ database: 'neo4j' }, # default value may be omitted
nil, # auth_token - positional argument can't be omitted when config is provided
{ database: 'neo4j' }, # config - default value may be omitted
age: 42
)

Expand All @@ -26,7 +26,7 @@
# Create two nodes and a relationship
result = driver.execute_query(
'CREATE (a:Person {name: $name})
CREATE (b:Person {friend: $name})
CREATE (b:Person {name: $friend})
CREATE (a)-[:KNOWS]->(b)',
name: 'Alice',
friend: 'David'
Expand Down Expand Up @@ -104,6 +104,7 @@
# Database selection
driver.execute_query(
'MATCH (p:Person) RETURN p.name',
nil,
{ database: 'neo4j' },
age: 42
)
Expand Down Expand Up @@ -157,7 +158,7 @@
puts notifications

# Notifications with GQL status codes
result = execute_query(
result = driver.execute_query(
"MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end}))
RETURN p",
start: 'Alice',
Expand Down
10 changes: 10 additions & 0 deletions jruby/neo4j/driver/ext/internal_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ module InternalDriver

auto_closable :session

def execute_query(query, auth_token = nil, config = {}, **parameters)
check do
driver.executable_query(query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was stumbling on the driver until I realized that it might refer to partial package name. We don't such references. The method driver in this context is confusing. Just remove it.

.with_parameters(to_neo(parameters))
.with_auth_token(auth_token)
.with_config(to_java_config(Neo4j::Driver::QueryConfig, **config))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the order of calls the same as the parameter order.

.execute
end
end

def session(**session_config)
java_method(:session, [org.neo4j.driver.SessionConfig])
.call(to_java_config(Neo4j::Driver::SessionConfig, **session_config))
Expand Down
157 changes: 157 additions & 0 deletions spec/neo4j/driver/execute_query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# frozen_string_literal: true

RSpec.describe Neo4j::Driver, concurrency: true do

describe '#execute_query' do
context 'when querying the database' do
it 'accepts query with default auth_token, config hash and parameters' do
expect {
driver.execute_query(
'MATCH (p:Person {age: $age}) RETURN p.name AS name',
nil,
{ database: 'neo4j' },
age: 42
)
}.not_to raise_error
end
end

context 'when writing to the database' do
it 'accepts query with only keyword parameters' do
expect {
driver.execute_query(
'CREATE (a:Person {name: $name}) CREATE (b:Person {name: $friend}) CREATE (a)-[:KNOWS]->(b)',
name: 'Alice',
friend: 'David'
)
}.not_to raise_error
end
end

context 'when reading from the database' do
it 'accepts query with no additional parameters' do
expect {
driver.execute_query('MATCH (p:Person)-[:KNOWS]->(:Person) RETURN p.name AS name')
}.not_to raise_error
end
end

context 'when updating the database' do
it 'accepts update query with keyword parameters' do
expect {
driver.execute_query(
'MATCH (p:Person {name: $name}) SET p.age = $age',
name: 'Alice',
age: 42
)
}.not_to raise_error
end

it 'accepts relationship creation with keyword parameters' do
expect {
driver.execute_query(
'MATCH (alice:Person {name: $name}) MATCH (bob:Person {name: $friend}) CREATE (alice)-[:KNOWS]->(bob)',
name: 'Alice',
friend: 'Bob'
)
}.not_to raise_error
end
end

context 'when deleting from the database' do
it 'accepts delete query with keyword parameters' do
expect {
driver.execute_query(
'MATCH (p:Person {name: $name}) DETACH DELETE p',
name: 'Alice'
)
}.not_to raise_error
end
end

context 'when using query configuration' do
it 'accepts nil auth_token with config hash and parameters' do
expect {
driver.execute_query(
'MATCH (p:Person) RETURN p.name',
nil,
{ database: 'neo4j' },
age: 42
)
}.not_to raise_error
end

it 'accepts auth_token with keyword parameters' do
auth_token = Neo4j::Driver::AuthTokens.basic(neo4j_user, neo4j_password)

expect {
driver.execute_query(
'MATCH (p:Person) RETURN p.name',
auth_token,
age: 42
)
}.not_to raise_error
end
end

context 'when working with query summaries' do
it 'accepts UNWIND query without parameters' do
expect {
driver.execute_query("UNWIND ['Alice', 'Bob'] AS name MERGE (p:Person {name: name})")
}.not_to raise_error
end

it 'accepts MERGE query with keyword parameters' do
expect {
driver.execute_query(
"MERGE (p:Person {name: $name}) MERGE (p)-[:KNOWS]->(:Person {name: $friend})",
name: 'Mark',
friend: 'Bob'
)
}.not_to raise_error
end

it 'accepts EXPLAIN query with keyword parameters' do
expect {
driver.execute_query(
'EXPLAIN MATCH (p {name: $name}) RETURN p',
name: 'Alice'
)
}.not_to raise_error
end

it 'accepts shortestPath query with keyword parameters' do
expect {
driver.execute_query(
"MATCH p=shortestPath((:Person {name: $start})-[*]->(:Person {name: $end})) RETURN p",
start: 'Alice',
end: 'Bob'
)
}.not_to raise_error
end
end

context 'parameter handling' do
it 'handles mixed positional and keyword arguments correctly' do
expect {
driver.execute_query(
'MATCH (p:Person {age: $age, name: $name}) RETURN p',
nil,
{ database: 'neo4j' },
age: 42,
name: 'Alice'
)
}.not_to raise_error
end

it 'handles only keyword arguments' do
expect {
driver.execute_query(
'MATCH (p:Person {name: $name}) RETURN p',
name: 'Alice'
)
}.not_to raise_error
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GetFeatures < Request
{
'Feature:API:BookmarkManager' => 'a',
'Feature:API:ConnectionAcquisitionTimeout' => 'ja',
'Feature:API:Driver.ExecuteQuery' => 'a',
'Feature:API:Driver.ExecuteQuery' => 'ja',
'Feature:API:Driver:GetServerInfo' => '',
'Feature:API:Driver.IsEncrypted' => 'jar',
'Feature:API:Driver:NotificationsConfig' => 'ja',
Expand Down
Loading