Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Example Usage
driver = GraphDatabase.driver("bolt://localhost")
session = driver.session()
session.run("CREATE (a:Person {name:'Bob'})")
cursor = session.run("MATCH (a:Person) RETURN a.name AS name")
while cursor.next()
print(cursor["name"])
cursor.close()
result = session.run("MATCH (a:Person) RETURN a.name AS name")
for record in result:
print(record["name"])
result.close()
session.close()


Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Session API

.. autofunction:: neo4j.v1.record

.. autoclass:: neo4j.v1.ResultCursor
.. autoclass:: neo4j.v1.StatementResult
:members:

.. autoclass:: neo4j.v1.ResultSummary
Expand Down
39 changes: 18 additions & 21 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_minimal_working_example(self):
session.run("CREATE (a:Person {name:'Arthur', title:'King'})", )

result = session.run("MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title")
while result.next():
print("%s %s" % (result["title"], result["name"]))
for record in result:
print("%s %s" % (record["title"], record["name"]))

session.close()
# end::minimal-example[]
Expand Down Expand Up @@ -96,16 +96,18 @@ def test_statement(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
session = driver.session()
# tag::statement[]
session.run("CREATE (person:Person {name: {name}})", {"name": "Arthur"}).close()
result = session.run("CREATE (person:Person {name: {name}})", {"name": "Arthur"})
# end::statement[]
result.discard()
session.close()

def test_statement_without_parameters(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
session = driver.session()
# tag::statement-without-parameters[]
session.run("CREATE (person:Person {name: 'Arthur'})").close()
result = session.run("CREATE (person:Person {name: 'Arthur'})")
# end::statement-without-parameters[]
result.discard()
session.close()

def test_result_cursor(self):
Expand All @@ -116,8 +118,8 @@ def test_result_cursor(self):
result = session.run("MATCH (tool:Tool) WHERE tool.name CONTAINS {term} "
"RETURN tool.name", {"term": search_term})
print("List of tools called %r:" % search_term)
while result.next():
print(result["tool.name"])
for record in result:
print(record["tool.name"])
# end::result-cursor[]
session.close()

Expand All @@ -127,10 +129,10 @@ def test_cursor_nesting(self):
# tag::retain-result-query[]
result = session.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} "
"RETURN id(knight) AS knight_id", {"castle": "Camelot"})
while result.next():
for record in result:
session.run("MATCH (knight) WHERE id(knight) = {id} "
"MATCH (king:Person) WHERE king.name = {king} "
"CREATE (knight)-[:DEFENDS]->(king)", {"id": result["knight_id"], "king": "Arthur"})
"CREATE (knight)-[:DEFENDS]->(king)", {"id": record["knight_id"], "king": "Arthur"})
# end::retain-result-query[]
session.close()

Expand All @@ -140,9 +142,8 @@ def test_result_retention(self):
# tag::retain-result-process[]
result = session.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} "
"RETURN id(knight) AS knight_id", {"castle": "Camelot"})
id_records = list(result.stream())

for record in id_records:
retained_result = list(result)
for record in retained_result:
session.run("MATCH (knight) WHERE id(knight) = {id} "
"MATCH (king:Person) WHERE king.name = {king} "
"CREATE (knight)-[:DEFENDS]->(king)", {"id": record["knight_id"], "king": "Arthur"})
Expand All @@ -158,9 +159,8 @@ def test_transaction_commit(self):
tx.commit()
# end::transaction-commit[]
result = session.run("MATCH (p:Person {name: 'Guinevere'}) RETURN count(p)")
assert result.next()
assert result["count(p)"] == 1
assert result.at_end
record = next(result)
assert record["count(p)"] == 1
session.close()

def test_transaction_rollback(self):
Expand All @@ -172,9 +172,8 @@ def test_transaction_rollback(self):
tx.rollback()
# end::transaction-rollback[]
result = session.run("MATCH (p:Person {name: 'Merlin'}) RETURN count(p)")
assert result.next()
assert result["count(p)"] == 0
assert result.at_end
record = next(result)
assert record["count(p)"] == 0
session.close()

def test_result_summary_query_profile(self):
Expand All @@ -183,8 +182,7 @@ def test_result_summary_query_profile(self):
# tag::result-summary-query-profile[]
result = session.run("PROFILE MATCH (p:Person {name: {name}}) "
"RETURN id(p)", {"name": "Arthur"})
while result.next():
pass # skip the records to get to the summary
list(result) # skip the records to get to the summary
print(result.summary.statement_type)
print(result.summary.profile)
# end::result-summary-query-profile[]
Expand All @@ -195,8 +193,7 @@ def test_result_summary_notifications(self):
session = driver.session()
# tag::result-summary-notifications[]
result = session.run("EXPLAIN MATCH (king), (queen) RETURN king, queen")
while result.next():
pass # skip the records to get to the summary
list(result) # skip the records to get to the summary
for notification in result.summary.notifications:
print(notification)
# end::result-summary-notifications[]
Expand Down
30 changes: 14 additions & 16 deletions neo4j/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@
from sys import stdout, stderr

from .util import Watcher
from .v1.session import GraphDatabase, CypherError
from .v1.session import GraphDatabase, CypherError, basic_auth


def main():
parser = ArgumentParser(description="Execute one or more Cypher statements using Bolt.")
parser.add_argument("statement", nargs="+")
parser.add_argument("-u", "--url", default="bolt://localhost", metavar="CONNECTION_URL")
parser.add_argument("-k", "--keys", action="store_true")
parser.add_argument("-P", "--password")
parser.add_argument("-p", "--parameter", action="append", metavar="NAME=VALUE")
parser.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("-s", "--secure", action="store_true")
parser.add_argument("-U", "--user", default="neo4j")
parser.add_argument("-u", "--url", default="bolt://localhost", metavar="CONNECTION_URL")
parser.add_argument("-v", "--verbose", action="count")
parser.add_argument("-x", "--times", type=int, default=1)
parser.add_argument("-z", "--summarize", action="store_true")
parser.add_argument("-z", "--summary", action="store_true")
args = parser.parse_args()

if args.verbose:
Expand All @@ -57,30 +59,26 @@ def main():
except ValueError:
parameters[name] = value

driver = GraphDatabase.driver(args.url, secure=args.secure)
driver = GraphDatabase.driver(args.url, auth=basic_auth(args.user, args.password))
session = driver.session()
for _ in range(args.times):
for statement in args.statement:
try:
cursor = session.run(statement, parameters)
result = session.run(statement, parameters)
except CypherError as error:
stderr.write("%s: %s\r\n" % (error.code, error.message))
else:
if not args.quiet:
has_results = False
for i, record in enumerate(cursor.stream()):
has_results = True
if i == 0:
stdout.write("%s\r\n" % "\t".join(record.keys()))
stdout.write("%s\r\n" % "\t".join(map(repr, record)))
if has_results:
stdout.write("\r\n")
if args.keys:
stdout.write("%s\r\n" % "\t".join(result.keys()))
for i, record in enumerate(result):
stdout.write("%s\r\n" % "\t".join(map(repr, record.values())))
if args.summary:
summary = cursor.summary
summary = result.summary
stdout.write("Statement : %r\r\n" % summary.statement)
stdout.write("Parameters : %r\r\n" % summary.parameters)
stdout.write("Statement Type : %r\r\n" % summary.statement_type)
stdout.write("Statistics : %r\r\n" % summary.statistics)
stdout.write("Counters : %r\r\n" % summary.counters)
stdout.write("\r\n")
session.close()

Expand Down
2 changes: 1 addition & 1 deletion neo4j/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@

from .constants import *
from .session import *
from .typesystem import *
from .types import *
7 changes: 0 additions & 7 deletions neo4j/v1/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,3 @@ def __init__(self, data):
for key, value in data.items():
if not key.startswith("_"):
setattr(self, key, value)


class ResultError(Exception):
""" Raised when the cursor encounters a problem.
"""

pass
Loading