Skip to content

Commit a751903

Browse files
committed
Transaction docs
1 parent bd02ae7 commit a751903

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

neo4j/v1/api.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,14 @@ def closed(self):
185185
"""
186186

187187
def run(self, statement, parameters=None, **kwparameters):
188-
""" Run a parameterised Cypher statement. If an explicit transaction
189-
has been created, the statement will be executed within that
190-
transactional context. Otherwise, this will take place within an
191-
*auto-commit transaction*.
188+
""" Run a Cypher statement within an auto-commit transaction.
192189
193-
:param statement: Cypher statement to execute
190+
For usage details, see :meth:`.Transaction.run`.
191+
192+
:param statement: Cypher statement
194193
:param parameters: dictionary of parameters
195-
:return: Cypher result
196-
:rtype: :class:`.StatementResult`
194+
:param kwparameters: additional keyword parameters
195+
:return: :class:`.StatementResult` object
197196
"""
198197

199198
def fetch(self):
@@ -258,14 +257,12 @@ class Transaction(object):
258257
259258
"""
260259

261-
#: When closed, the transaction will be committed if marked as successful
262-
#: and rolled back otherwise. This attribute can be set in user code
260+
#: When set, the transaction will be committed on close, otherwise it
261+
#: will be rolled back. This attribute can be set in user code
263262
#: multiple times before a transaction completes with only the final
264263
#: value taking effect.
265264
success = None
266265

267-
#: Indicator to show whether the transaction has been closed, either
268-
#: with commit or rollback.
269266
_closed = False
270267

271268
def __init__(self, session, on_close):
@@ -283,22 +280,46 @@ def __exit__(self, exc_type, exc_value, traceback):
283280
def run(self, statement, parameters=None, **kwparameters):
284281
""" Run a Cypher statement within the context of this transaction.
285282
283+
Cypher is typically expressed as a statement template plus a
284+
set of named parameters. In Python, parameters may be expressed
285+
through a dictionary of parameters, through individual parameter
286+
arguments, or as a mixture of both. For example, the `run`
287+
statements below are all equivalent::
288+
289+
>>> statement = "CREATE (a:Person {name:{name}, age:{age}})"
290+
>>> tx.run(statement, {"name": "Alice", "age": 33})
291+
>>> tx.run(statement, {"name": "Alice"}, age=33)
292+
>>> tx.run(statement, name="Alice", age=33)
293+
294+
Parameter values can be of any type supported by the Neo4j type
295+
system. In Python, this includes :class:`bool`, :class:`int`,
296+
:class:`str`, :class:`list` and :class:`dict`. Note however that
297+
:class:`list` properties must be homogenous.
298+
286299
:param statement: Cypher statement
287300
:param parameters: dictionary of parameters
288-
:return: result object
301+
:param kwparameters: additional keyword parameters
302+
:return: :class:`.StatementResult` object
289303
"""
290304
if self.closed():
291305
raise TransactionError("Cannot use closed transaction")
292306
return self.session.run(statement, parameters, **kwparameters)
293307

294308
def sync(self):
309+
""" Send and receive all outstanding messages for this
310+
transaction.
311+
312+
:raise TransactionError: if the transaction is closed
313+
"""
295314
if self.closed():
296315
raise TransactionError("Cannot use closed transaction")
297316
self.session.sync()
298317

299318
def commit(self):
300319
""" Mark this transaction as successful and close in order to
301320
trigger a COMMIT.
321+
322+
:raise TransactionError: if already closed
302323
"""
303324
if self.closed():
304325
raise TransactionError("Cannot use closed transaction")
@@ -308,6 +329,8 @@ def commit(self):
308329
def rollback(self):
309330
""" Mark this transaction as unsuccessful and close in order to
310331
trigger a ROLLBACK.
332+
333+
:raise TransactionError: if already closed
311334
"""
312335
if self.closed():
313336
raise TransactionError("Cannot use closed transaction")
@@ -326,6 +349,9 @@ def close(self):
326349
self.on_close()
327350

328351
def closed(self):
352+
""" Indicator to show whether the transaction has been closed.
353+
:return: :const:`True` if closed, :const:`False` otherwise.
354+
"""
329355
return self._closed
330356

331357

0 commit comments

Comments
 (0)