Skip to content

Commit c65d99b

Browse files
committed
Result.close->discard
1 parent 803a50e commit c65d99b

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Session API
2626

2727
.. autofunction:: neo4j.v1.record
2828

29-
.. autoclass:: neo4j.v1.ResultCursor
29+
.. autoclass:: neo4j.v1.StatementResult
3030
:members:
3131

3232
.. autoclass:: neo4j.v1.ResultSummary

examples/test_examples.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,18 @@ def test_statement(self):
9696
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
9797
session = driver.session()
9898
# tag::statement[]
99-
session.run("CREATE (person:Person {name: {name}})", {"name": "Arthur"}).close()
99+
result = session.run("CREATE (person:Person {name: {name}})", {"name": "Arthur"})
100100
# end::statement[]
101+
result.discard()
101102
session.close()
102103

103104
def test_statement_without_parameters(self):
104105
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
105106
session = driver.session()
106107
# tag::statement-without-parameters[]
107-
session.run("CREATE (person:Person {name: 'Arthur'})").close()
108+
result = session.run("CREATE (person:Person {name: 'Arthur'})")
108109
# end::statement-without-parameters[]
110+
result.discard()
109111
session.close()
110112

111113
def test_result_cursor(self):

neo4j/v1/session.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def recycle(self, session):
148148
pool.appendleft(session)
149149

150150

151-
class ResultCursor(object):
151+
class StatementResult(object):
152152
""" A handler for the result of Cypher statement execution.
153153
"""
154154

@@ -163,7 +163,7 @@ class ResultCursor(object):
163163
summary = None
164164

165165
def __init__(self, connection, run_response, pull_all_response):
166-
super(ResultCursor, self).__init__()
166+
super(StatementResult, self).__init__()
167167

168168
# The Connection instance behind this cursor.
169169
self.connection = connection
@@ -205,6 +205,9 @@ def on_failure(metadata):
205205
pull_all_response.on_success = on_footer
206206
pull_all_response.on_failure = on_failure
207207

208+
def __iter__(self):
209+
return self
210+
208211
def __next__(self):
209212
if self._buffer:
210213
values = self._buffer.popleft()
@@ -216,10 +219,15 @@ def __next__(self):
216219
self.connection.fetch()
217220
return self.__next__()
218221

219-
def __iter__(self):
220-
return self
222+
def keys(self):
223+
""" Return the keys for the records.
224+
"""
225+
# Fetch messages until we have the header or a failure
226+
while self._keys is None and not self._consumed:
227+
self.connection.fetch()
228+
return self._keys
221229

222-
def close(self):
230+
def discard(self):
223231
""" Consume the remainder of this result and detach the connection
224232
from this cursor.
225233
"""
@@ -229,17 +237,9 @@ def close(self):
229237
fetch()
230238
self.connection = None
231239

232-
def keys(self):
233-
""" Return the keys for the records.
234-
"""
235-
# Fetch messages until we have the header or a failure
236-
while self._keys is None and not self._consumed:
237-
self.connection.fetch()
238-
return self._keys
239-
240240

241241
class ResultSummary(object):
242-
""" A summary of execution returned with a :class:`.ResultCursor` object.
242+
""" A summary of execution returned with a :class:`.StatementResult` object.
243243
"""
244244

245245
#: The statement that was executed to produce this result.
@@ -408,7 +408,7 @@ def __init__(self, driver):
408408
self.driver = driver
409409
self.connection = connect(driver.host, driver.port, driver.ssl_context, **driver.config)
410410
self.transaction = None
411-
self.last_cursor = None
411+
self.last_result = None
412412

413413
def __del__(self):
414414
try:
@@ -437,7 +437,7 @@ def run(self, statement, parameters=None):
437437
:param statement: Cypher statement to execute
438438
:param parameters: dictionary of parameters
439439
:return: Cypher result
440-
:rtype: :class:`.ResultCursor`
440+
:rtype: :class:`.StatementResult`
441441
"""
442442

443443
# Ensure the statement is a Unicode value
@@ -456,22 +456,22 @@ def run(self, statement, parameters=None):
456456

457457
run_response = Response(self.connection)
458458
pull_all_response = Response(self.connection)
459-
cursor = ResultCursor(self.connection, run_response, pull_all_response)
459+
cursor = StatementResult(self.connection, run_response, pull_all_response)
460460
cursor.statement = statement
461461
cursor.parameters = parameters
462462

463463
self.connection.append(RUN, (statement, parameters), response=run_response)
464464
self.connection.append(PULL_ALL, response=pull_all_response)
465465
self.connection.send()
466466

467-
self.last_cursor = cursor
467+
self.last_result = cursor
468468
return cursor
469469

470470
def close(self):
471471
""" Recycle this session through the driver it came from.
472472
"""
473-
if self.last_cursor:
474-
self.last_cursor.close()
473+
if self.last_result:
474+
self.last_result.discard()
475475
self.driver.recycle(self)
476476

477477
def begin_transaction(self):

test/test_session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ def test_can_run_simple_statement_with_params(self):
156156
def test_fails_on_bad_syntax(self):
157157
session = GraphDatabase.driver("bolt://localhost", auth=auth_token).session()
158158
with self.assertRaises(CypherError):
159-
session.run("X").close()
159+
session.run("X").discard()
160160

161161
def test_fails_on_missing_parameter(self):
162162
session = GraphDatabase.driver("bolt://localhost", auth=auth_token).session()
163163
with self.assertRaises(CypherError):
164-
session.run("RETURN {x}").close()
164+
session.run("RETURN {x}").discard()
165165

166166
def test_can_run_simple_statement_from_bytes_string(self):
167167
session = GraphDatabase.driver("bolt://localhost", auth=auth_token).session()
@@ -227,7 +227,7 @@ def test_can_return_path(self):
227227
def test_can_handle_cypher_error(self):
228228
with GraphDatabase.driver("bolt://localhost", auth=auth_token).session() as session:
229229
with self.assertRaises(CypherError):
230-
session.run("X").close()
230+
session.run("X").discard()
231231

232232
def test_keys_are_available_before_and_after_stream(self):
233233
with GraphDatabase.driver("bolt://localhost", auth=auth_token).session() as session:
@@ -332,7 +332,7 @@ class ResetTestCase(ServerTestCase):
332332
def test_automatic_reset_after_failure(self):
333333
with GraphDatabase.driver("bolt://localhost", auth=auth_token).session() as session:
334334
try:
335-
session.run("X").close()
335+
session.run("X").discard()
336336
except CypherError:
337337
result = session.run("RETURN 1")
338338
record = next(result)
@@ -346,7 +346,7 @@ def test_defunct(self):
346346
assert not session.connection.defunct
347347
with patch.object(ChunkChannel, "chunk_reader", side_effect=ProtocolError()):
348348
with self.assertRaises(ProtocolError):
349-
session.run("RETURN 1").close()
349+
session.run("RETURN 1").discard()
350350
assert session.connection.defunct
351351
assert session.connection.closed
352352

0 commit comments

Comments
 (0)