This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Convert additional database methods to async (select list, search, insert_many, delete_*) #8168
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
84a94a1
Convert simple_select_list_paginate
clokep 7bd64d7
Convert simple_search_list.
clokep 02a24bc
Convert simple_insert_many.
clokep 8edafe9
Convert execute.
clokep feb1030
Convert simple_delete_one.
clokep d485998
Convert simple_delete_many.
clokep 7622cb1
Add newsfragment.
clokep 85614a8
Lint.
clokep 90a1b56
Merge remote-tracking branch 'origin/develop' into clokep/async-simpl…
clokep 68975df
Remove unused method.
clokep 9972ddb
Handle review comments.
clokep a5892fc
Merge remote-tracking branch 'origin/develop' into clokep/async-simpl…
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Convert various parts of the codebase to async/await. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -605,7 +605,9 @@ def cursor_to_dict(cursor: Cursor) -> List[Dict[str, Any]]: | |
| results = [dict(zip(col_headers, row)) for row in cursor] | ||
| return results | ||
|
|
||
| def execute(self, desc: str, decoder: Callable, query: str, *args: Any): | ||
| async def execute( | ||
| self, desc: str, decoder: "Optional[Callable[..., R]]", query: str, *args: Any | ||
| ) -> R: | ||
| """Runs a single query for a result set. | ||
|
|
||
| Args: | ||
|
|
@@ -614,7 +616,7 @@ def execute(self, desc: str, decoder: Callable, query: str, *args: Any): | |
| query - The query string to execute | ||
| *args - Query args. | ||
| Returns: | ||
| Deferred which results to the result of decoder(results) | ||
| The result of decoder(results) | ||
| """ | ||
|
|
||
| def interaction(txn): | ||
|
|
@@ -624,7 +626,7 @@ def interaction(txn): | |
| else: | ||
| return txn.fetchall() | ||
|
|
||
| return self.runInteraction(desc, interaction) | ||
| return await self.runInteraction(desc, interaction) | ||
|
|
||
| # "Simple" SQL API methods that operate on a single table with no JOINs, | ||
| # no complex WHERE clauses, just a dict of values for columns. | ||
|
|
@@ -673,15 +675,30 @@ def simple_insert_txn( | |
|
|
||
| txn.execute(sql, vals) | ||
|
|
||
| def simple_insert_many( | ||
| async def simple_insert_many( | ||
| self, table: str, values: List[Dict[str, Any]], desc: str | ||
| ) -> defer.Deferred: | ||
| return self.runInteraction(desc, self.simple_insert_many_txn, table, values) | ||
| ) -> None: | ||
| """Executes an INSERT query on the named table. | ||
|
|
||
| Args: | ||
| table: string giving the table name | ||
| values: dict of new column names and values for them | ||
| desc: string giving a description of the transaction | ||
| """ | ||
| await self.runInteraction(desc, self.simple_insert_many_txn, table, values) | ||
|
|
||
| @staticmethod | ||
| def simple_insert_many_txn( | ||
| txn: LoggingTransaction, table: str, values: List[Dict[str, Any]] | ||
| ) -> None: | ||
| """Executes an INSERT query on the named table. | ||
|
|
||
| Args: | ||
| txn: The transaction to use. | ||
| table: string giving the table name | ||
| values: dict of new column names and values for them | ||
| desc: string giving a description of the transaction | ||
| """ | ||
| if not values: | ||
| return | ||
|
|
||
|
|
@@ -1396,17 +1413,17 @@ def simple_select_one_txn( | |
|
|
||
| return dict(zip(retcols, row)) | ||
|
|
||
| def simple_delete_one( | ||
| async def simple_delete_one( | ||
| self, table: str, keyvalues: Dict[str, Any], desc: str = "simple_delete_one" | ||
| ) -> defer.Deferred: | ||
| ) -> None: | ||
| """Executes a DELETE query on the named table, expecting to delete a | ||
| single row. | ||
|
|
||
| Args: | ||
| table: string giving the table name | ||
| keyvalues: dict of column names and values to select the row with | ||
| """ | ||
| return self.runInteraction(desc, self.simple_delete_one_txn, table, keyvalues) | ||
| await self.runInteraction(desc, self.simple_delete_one_txn, table, keyvalues) | ||
|
|
||
| @staticmethod | ||
| def simple_delete_one_txn( | ||
|
|
@@ -1445,15 +1462,15 @@ def simple_delete_txn( | |
| txn.execute(sql, list(keyvalues.values())) | ||
| return txn.rowcount | ||
|
|
||
| def simple_delete_many( | ||
| async def simple_delete_many( | ||
| self, | ||
| table: str, | ||
| column: str, | ||
| iterable: Iterable[Any], | ||
| keyvalues: Dict[str, Any], | ||
| desc: str, | ||
| ) -> defer.Deferred: | ||
| return self.runInteraction( | ||
| ) -> int: | ||
| return await self.runInteraction( | ||
| desc, self.simple_delete_many_txn, table, column, iterable, keyvalues | ||
| ) | ||
|
|
||
|
|
@@ -1536,7 +1553,7 @@ def get_cache_dict( | |
|
|
||
| return cache, min_val | ||
|
|
||
| def simple_select_list_paginate( | ||
| async def simple_select_list_paginate( | ||
|
||
| self, | ||
| table: str, | ||
| orderby: str, | ||
|
|
@@ -1547,7 +1564,7 @@ def simple_select_list_paginate( | |
| keyvalues: Optional[Dict[str, Any]] = None, | ||
| order_direction: str = "ASC", | ||
| desc: str = "simple_select_list_paginate", | ||
| ) -> defer.Deferred: | ||
| ) -> List[Dict[str, Any]]: | ||
| """ | ||
| Executes a SELECT query on the named table with start and limit, | ||
| of row numbers, which may return zero or number of rows from start to limit, | ||
|
|
@@ -1566,10 +1583,11 @@ def simple_select_list_paginate( | |
| column names and values to select the rows with, or None to not | ||
| apply a WHERE clause. | ||
| order_direction: Whether the results should be ordered "ASC" or "DESC". | ||
|
|
||
| Returns: | ||
| defer.Deferred: resolves to list[dict[str, Any]] | ||
| A list of dictionaries. | ||
| """ | ||
| return self.runInteraction( | ||
| return await self.runInteraction( | ||
| desc, | ||
| self.simple_select_list_paginate_txn, | ||
| table, | ||
|
|
@@ -1646,14 +1664,14 @@ def simple_select_list_paginate_txn( | |
|
|
||
| return cls.cursor_to_dict(txn) | ||
|
|
||
| def simple_search_list( | ||
| async def simple_search_list( | ||
| self, | ||
| table: str, | ||
| term: Optional[str], | ||
| col: str, | ||
| retcols: Iterable[str], | ||
| desc="simple_search_list", | ||
| ): | ||
| ) -> Optional[List[Dict[str, Any]]]: | ||
| """Executes a SELECT query on the named table, which may return zero or | ||
| more rows, returning the result as a list of dicts. | ||
|
|
||
|
|
@@ -1664,10 +1682,10 @@ def simple_search_list( | |
| retcols: the names of the columns to return | ||
|
|
||
| Returns: | ||
| defer.Deferred: resolves to list[dict[str, Any]] or None | ||
| A list of dictionaries or None. | ||
| """ | ||
|
|
||
| return self.runInteraction( | ||
| return await self.runInteraction( | ||
| desc, self.simple_search_list_txn, table, term, col, retcols | ||
| ) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.