Skip to content
Closed
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
86 changes: 40 additions & 46 deletions neo4j/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def session(self, **config):
return Session(connect(self.host, self.port, **config))


class Result(list):
class ResultCursor(list):
""" A handler for the result of Cypher statement execution.
"""

Expand All @@ -112,57 +112,51 @@ class Result(list):
#: Dictionary of parameters passed with the statement.
parameters = None

def __init__(self, session, statement, parameters):
super(Result, self).__init__()
self.session = session
def __init__(self, connection, statement, parameters):
super(ResultCursor, self).__init__()
self.statement = statement
self.parameters = parameters
self.keys = None
self.complete = False
self.summary = None
self.bench_test = None
self._connection = connection
self._complete = False
self._summary = None
self._bench_test = None

def summarize(self):
""" Consume the remainder of this result and produce a summary.

def on_header(self, metadata):
""" Called on receipt of the result header.
:rtype: ResultSummary
"""
self._consume()
return self._summary

def _on_header(self, metadata):
# Called on receipt of the result header.
self.keys = metadata["fields"]
if self.bench_test:
self.bench_test.start_recv = perf_counter()
if self._bench_test:
self._bench_test.start_recv = perf_counter()

def on_record(self, values):
""" Called on receipt of each result record.
"""
def _on_record(self, values):
# Called on receipt of each result record.
self.append(Record(self.keys, tuple(map(hydrated, values))))

def on_footer(self, metadata):
""" Called on receipt of the result footer.
"""
self.complete = True
self.summary = ResultSummary(self.statement, self.parameters, **metadata)
if self.bench_test:
self.bench_test.end_recv = perf_counter()
def _on_footer(self, metadata):
# Called on receipt of the result footer.
self._complete = True
self._summary = ResultSummary(self.statement, self.parameters, **metadata)
if self._bench_test:
self._bench_test.end_recv = perf_counter()

def on_failure(self, metadata):
""" Called on execution failure.
"""
def _on_failure(self, metadata):
# Called on execution failure.
raise CypherError(metadata)

def consume(self):
""" Consume the remainder of this result, triggering all appropriate
callback functions.
"""
fetch_next = self.session.connection.fetch_next
while not self.complete:
def _consume(self):
# Consume the remainder of this result, triggering all appropriate callback functions.
fetch_next = self._connection.fetch_next
while not self._complete:
fetch_next()

def summarize(self):
""" Consume the remainder of this result and produce a summary.

:rtype: ResultSummary
"""
self.consume()
return self.summary


class ResultSummary(object):
""" A summary of execution returned with a :class:`.Result` object.
Expand Down Expand Up @@ -367,25 +361,25 @@ def run(self, statement, parameters=None):
t = BenchTest()
t.init = perf_counter()

result = Result(self, statement, parameters)
result.bench_test = t
result = ResultCursor(self.connection, statement, parameters)
result._bench_test = t

run_response = Response(self.connection)
run_response.on_success = result.on_header
run_response.on_failure = result.on_failure
run_response.on_success = result._on_header
run_response.on_failure = result._on_failure

pull_all_response = Response(self.connection)
pull_all_response.on_record = result.on_record
pull_all_response.on_success = result.on_footer
pull_all_response.on_failure = result.on_failure
pull_all_response.on_record = result._on_record
pull_all_response.on_success = result._on_footer
pull_all_response.on_failure = result._on_failure

self.connection.append(RUN, (statement, parameters), response=run_response)
self.connection.append(PULL_ALL, response=pull_all_response)
t.start_send = perf_counter()
self.connection.send()
t.end_send = perf_counter()

result.consume()
result._consume()

t.done = perf_counter()
self.bench_tests.append(t)
Expand Down
4 changes: 2 additions & 2 deletions test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def test_can_run_simple_statement_with_params(self):
def test_fails_on_bad_syntax(self):
session = GraphDatabase.driver("bolt://localhost").session()
with self.assertRaises(CypherError):
session.run("X").consume()
session.run("X")._consume()

def test_fails_on_missing_parameter(self):
session = GraphDatabase.driver("bolt://localhost").session()
with self.assertRaises(CypherError):
session.run("RETURN {x}").consume()
session.run("RETURN {x}")._consume()

def test_can_run_simple_statement_from_bytes_string(self):
session = GraphDatabase.driver("bolt://localhost").session()
Expand Down