Skip to content

Commit 1dc0c67

Browse files
committed
Review docblocks
1 parent e54337e commit 1dc0c67

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

sqlalchemy_bind_manager/_repository/base_repository.py

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def _filter_select(self, stmt: Select, search_params: Mapping[str, Any]) -> Sele
8282
:param stmt: a Select statement
8383
:type stmt: Select
8484
:param search_params: Any keyword argument to be used as equality filter
85+
:type search_params: Mapping[str, Any]
8586
:return: The filtered query
8687
"""
8788
# TODO: Add support for relationship eager load
@@ -110,6 +111,7 @@ def _filter_order_by(
110111
:param stmt: a Select statement
111112
:type stmt: Select
112113
:param order_by: a list of columns, or tuples (column, direction)
114+
:type order_by: Iterable[Union[str, Tuple[str, SortDirection]]]
113115
:return: The filtered query
114116
"""
115117
for value in order_by:
@@ -127,6 +129,24 @@ def _find_query(
127129
search_params: Union[None, Mapping[str, Any]] = None,
128130
order_by: Union[None, Iterable[Union[str, Tuple[str, SortDirection]]]] = None,
129131
) -> Select:
132+
"""Build a query with column filters and orders.
133+
134+
E.g.
135+
q = _find_query(search_params={"name":"John"})
136+
finds all models with name = John
137+
138+
q = _find_query(order_by=["name"])
139+
finds all models ordered by `name` column
140+
141+
q = _find_query(order_by=[("name", SortDirection.DESC)])
142+
finds all models with reversed order by `name` column
143+
144+
:param search_params: Any keyword argument to be used as equality filter
145+
:type search_params: Mapping[str, Any]
146+
:param order_by: a list of columns, or tuples (column, direction)
147+
:type order_by: Iterable[Union[str, Tuple[str, SortDirection]]]
148+
:return: The filtered query
149+
"""
130150
stmt = select(self._model)
131151

132152
if search_params:
@@ -177,19 +197,22 @@ def _cursor_paginated_query(
177197
is_before_cursor: bool = False,
178198
items_per_page: int = _max_query_limit,
179199
) -> Select:
180-
"""Build the query offset and limit clauses from submitted parameters.
200+
"""Adds the clauses to retrieve the requested slice of models, after
201+
or before the cursor value, plus a model before the slice and one after
202+
the slice, to identify if previous or next results are available.
181203
182204
:param stmt: a Select statement
183205
:type stmt: Select
184-
:param before: Identifier of the last node to skip
185-
:type before: Union[int, str]
186-
:param after: Identifier of the last node to skip
187-
:type after: Union[int, str]
206+
:param cursor_reference: A cursor reference containing ordering column
207+
and threshold value
208+
:type cursor_reference: Union[CursorReference, None]
209+
:param is_before_cursor: If True it will return items before the cursor,
210+
otherwise items after
211+
:type is_before_cursor: bool
188212
:param items_per_page: Number of models to retrieve
189213
:type items_per_page: int
190214
:return: The filtered query
191215
"""
192-
193216
forward_limit = self._sanitised_query_limit(items_per_page) + 1
194217

195218
if not cursor_reference:
@@ -220,9 +243,24 @@ def _cursor_pagination_slice_query(
220243
self,
221244
stmt: Select,
222245
cursor_reference: CursorReference,
223-
forward_limit: int,
246+
limit: int,
224247
is_before_cursor: bool,
225248
):
249+
"""Adds the clauses to retrieve a requested slice of models,
250+
after or before the cursor value (excluding the cursor itself)
251+
252+
:param stmt: a Select statement
253+
:type stmt: Select
254+
:param cursor_reference: A cursor reference containing ordering column
255+
and threshold value
256+
:type cursor_reference: Union[CursorReference, None]
257+
:param is_before_cursor: If True it will return items before the cursor,
258+
otherwise items after
259+
:type is_before_cursor: bool
260+
:param limit: Number of models to retrieve
261+
:type limit: int
262+
:return: The filtered query
263+
"""
226264
if not is_before_cursor:
227265
page_query = stmt.where(
228266
getattr(self._model, cursor_reference.column) > cursor_reference.value
@@ -237,11 +275,24 @@ def _cursor_pagination_slice_query(
237275
page_query = self._filter_order_by(
238276
page_query, [(cursor_reference.column, SortDirection.DESC)]
239277
)
240-
return page_query.limit(forward_limit)
278+
return page_query.limit(limit)
241279

242280
def _cursor_pagination_previous_item_query(
243281
self, stmt: Select, cursor_reference: CursorReference, is_before_cursor: bool
244282
) -> Select:
283+
"""Adds the clauses to retrieve a single model, after or before
284+
the cursor value (including the cursor itself).
285+
286+
:param stmt: a Select statement
287+
:type stmt: Select
288+
:param cursor_reference: A cursor reference containing ordering column
289+
and threshold value
290+
:type cursor_reference: Union[CursorReference, None]
291+
:param is_before_cursor: If True it will return items before the cursor,
292+
otherwise items after
293+
:type is_before_cursor: bool
294+
:return: The filtered query
295+
"""
245296
if not is_before_cursor:
246297
previous_query = stmt.where(
247298
getattr(self._model, cursor_reference.column) <= cursor_reference.value
@@ -263,6 +314,11 @@ def _sanitised_query_limit(self, limit):
263314
return max(min(limit, self._max_query_limit), 0)
264315

265316
def _model_pk(self) -> str:
317+
"""
318+
Retrieves the primary key name from the repository model class.
319+
320+
:return:
321+
"""
266322
primary_keys = inspect(self._model).primary_key # type: ignore
267323
if len(primary_keys) > 1:
268324
raise NotImplementedError("Composite primary keys are not supported.")

sqlalchemy_bind_manager/_repository/sync.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,6 @@ def cursor_paginated_find(
123123
is_before_cursor: bool = False,
124124
search_params: Union[None, Mapping[str, Any]] = None,
125125
) -> CursorPaginatedResult[MODEL]:
126-
"""Find models using filters and cursor based pagination
127-
128-
E.g.
129-
find(name="John") finds all models with name = John
130-
131-
:param items_per_page: Number of models to retrieve
132-
:type items_per_page: int
133-
:param order_by: Model property to use for ordering and before/after evaluation
134-
:type order_by: str
135-
:param before: Identifier of the last node to skip
136-
:type before: Union[int, str]
137-
:param after: Identifier of the last node to skip
138-
:type after: Union[int, str]
139-
:param search_params: A dictionary containing equality filters
140-
:return: A collection of models
141-
:rtype: List
142-
"""
143126
find_stmt = self._find_query(search_params)
144127

145128
paginated_stmt = self._cursor_paginated_query(

0 commit comments

Comments
 (0)