-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: 5.0
Are you sure you want to change the base?
Add execute_query #258
Changes from 6 commits
d5b7beb
3bb25dc
751f7c8
a9437f4
5233b88
66958bf
be8ebe7
8472b12
02e7c57
d6b0c2a
1367a7e
31639e3
ed76a81
60b3ebf
7afda0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,19 @@ module InternalDriver | |
|
||
auto_closable :session | ||
|
||
def execute_query(query, auth_token = nil, config = {}, **parameters) | ||
java_method(:executableQuery, [java.lang.String]) | ||
.call(query) | ||
.java_method(:withParameters, [java.util.Map]) | ||
.call(parameters.transform_keys(&:to_s)) | ||
.java_method(:withAuthToken, [org.neo4j.driver.AuthToken]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. always check if you really need the explicit method lookup. The dynamic dispatch is so much more readable. |
||
.call(auth_token) | ||
.java_method(:withConfig, [org.neo4j.driver.QueryConfig]) | ||
.call(to_java_config(Neo4j::Driver::QueryConfig, **config)) | ||
.java_method(:execute, []) | ||
.call | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you will need to wrap the entire method with
which converts potential exceptions from java to ruby exceptions. |
||
end | ||
|
||
def session(**session_config) | ||
java_method(:session, [org.neo4j.driver.SessionConfig]) | ||
.call(to_java_config(Neo4j::Driver::SessionConfig, **session_config)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Neo4j::Driver 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to get hold of the actual java method object only in relatively seldom cases. Mostly the methods can be determined dynamically.
I'm quite sure this will work:
please note the conversion
to_neo
. Keys and values have to be converted.