-
Notifications
You must be signed in to change notification settings - Fork 203
Introduce API Redesign #763
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
Changes from 22 commits
7043662
3db7a70
c4d2c89
e0c879c
d0293e7
5d52bab
26b19a9
e3b1223
5759926
7fe9204
7896b06
9de9924
2c7004c
62fb7ae
056ac54
915875d
7af3bd9
b6fcfb2
7fb2afd
6d5e550
ed575f9
8bb4498
b64b3c8
cc875d2
0e8cbfc
ecf966f
0ad6459
a8d541f
d147e62
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 |
|---|---|---|
|
|
@@ -28,19 +28,26 @@ | |
| ) | ||
| from ...api import ( | ||
| Bookmarks, | ||
| CLUSTER_AUTO_ACCESS, | ||
| CLUSTER_READERS_ACCESS, | ||
| CLUSTER_WRITERS_ACCESS, | ||
| READ_ACCESS, | ||
| WRITE_ACCESS, | ||
| ) | ||
| from ...exceptions import ( | ||
| ClientError, | ||
| ConfigurationError, | ||
| DriverError, | ||
| Neo4jError, | ||
| ServiceUnavailable, | ||
| SessionExpired, | ||
| TransactionError, | ||
| ) | ||
| from ...work import Query | ||
| from .result import AsyncResult | ||
| from .result import ( | ||
| AsyncResult, | ||
| QueryResult, | ||
| ) | ||
| from .transaction import ( | ||
| AsyncManagedTransaction, | ||
| AsyncTransaction, | ||
|
|
@@ -239,6 +246,125 @@ async def run(self, query, parameters=None, **kwargs): | |
|
|
||
| return self._auto_result | ||
|
|
||
| async def query(self, query, parameters=None, | ||
| cluster_member_access=CLUSTER_AUTO_ACCESS, | ||
| skip_records=False, | ||
| **kwargs): | ||
| """ | ||
| Run a Cypher query within an managed transaction. | ||
|
|
||
| The query is sent and the full result is fetched and returned as | ||
| :class:`neo4j.QueryResult`. | ||
|
|
||
| For more usage details, see :meth:`.AsyncTransaction.query`. | ||
|
|
||
| For auto-commit queries, use :class:`AsyncSession.run`. | ||
|
|
||
| For access to the :class:`neo4j.AsyncResult` object, | ||
| use :meth:`AsyncSession.execute` and :meth:`.AsyncTransaction.run` | ||
|
|
||
| :param query: cypher query | ||
| :type query: str, neo4j.Query | ||
| :param parameters: dictionary of parameters | ||
| :type parameters: dict | ||
| :param kwargs: additional keyword parameters | ||
|
|
||
| :returns: a new :class:`neo4j.QueryResult` object | ||
| :rtype: QueryResult | ||
bigmontz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| async def job(tx, **job_kwargs): | ||
| if skip_records: | ||
| result = await tx.run(query, parameters, **job_kwargs) | ||
| summary = await result.consume() | ||
| return QueryResult([], summary) | ||
| return await tx.query(query, parameters, **job_kwargs) | ||
|
|
||
| return await self.execute( | ||
| job, | ||
| cluster_member_access=cluster_member_access, | ||
| **kwargs | ||
| ) | ||
|
|
||
| async def execute(self, transaction_function, *args, | ||
| cluster_member_access=CLUSTER_AUTO_ACCESS, | ||
| **kwargs): | ||
bigmontz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Execute a unit of work in a managed transaction. | ||
|
|
||
| This transaction will automatically be committed unless an exception | ||
| is thrown during query execution or by the user code. | ||
| Note, that this function perform retries and that the supplied | ||
| ``transaction_function`` might get invoked more than once. | ||
|
|
||
|
|
||
| Example:: | ||
|
|
||
| async def do_cypher_tx(tx, cypher): | ||
| records, _ = await tx.query(cypher) | ||
| return records | ||
|
|
||
| async with driver.session() as session: | ||
| values = session.execute(do_cypher_tx, "RETURN 1 AS x") | ||
|
|
||
| Example:: | ||
|
|
||
| async def do_cypher_tx(tx): | ||
| records, _ = await tx.query("RETURN 1 AS x") | ||
| return records | ||
|
|
||
| async with driver.session() as session: | ||
| values = await session.execute( | ||
| do_cypher_tx, | ||
| cluster_member_access=neo4j.api.CLUSTER_READERS_ACCESS | ||
| ) | ||
|
|
||
| Example:: | ||
|
|
||
| async def get_two_tx(tx): | ||
| result = await tx.run("UNWIND [1,2,3,4] AS x RETURN x") | ||
| values = [] | ||
| async for record in result: | ||
| if len(values) >= 2: | ||
| break | ||
| values.append(record.values()) | ||
| # or shorter: values = [record.values() | ||
| # for record in await result.fetch(2)] | ||
|
|
||
| # discard the remaining records if there are any | ||
| summary = await result.consume() | ||
| # use the summary for logging etc. | ||
| return values | ||
|
|
||
| async with driver.session() as session: | ||
| values = await session.execute(get_two_tx) | ||
|
|
||
| :param transaction_function: a function that takes a transaction as an | ||
| argument and does work with the transaction. | ||
| ``transaction_function(tx, *args, **kwargs)`` where ``tx`` is a | ||
| :class:`.Transaction`. | ||
| :param args: arguments for the ``transaction_function`` | ||
| :param kwargs: key word arguments for the ``transaction_function`` | ||
|
|
||
| :return: a result as returned by the given unit of work | ||
bigmontz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| if cluster_member_access == CLUSTER_AUTO_ACCESS: | ||
| if await self._supports_auto_routing(): | ||
| access_mode = READ_ACCESS | ||
| else: | ||
| raise ConfigurationError( | ||
| "Server does not support CLUSTER_AUTO_ACCESS" | ||
| ) | ||
| elif cluster_member_access == CLUSTER_READERS_ACCESS: | ||
| access_mode = READ_ACCESS | ||
| elif cluster_member_access == CLUSTER_WRITERS_ACCESS: | ||
| access_mode = WRITE_ACCESS | ||
| else: | ||
| raise ClientError("Invalid cluster_member_access") | ||
|
|
||
| return await self._run_transaction( | ||
| access_mode, transaction_function, *args, **kwargs | ||
| ) | ||
|
|
||
| @deprecated( | ||
| "`last_bookmark` has been deprecated in favor of `last_bookmarks`. " | ||
| "This method can lead to unexpected behaviour." | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.