-
Notifications
You must be signed in to change notification settings - Fork 78
add support for sort in collection.find function #359
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 2 commits
32cdc62
107df52
1149f01
df2ab2f
71776c3
856743a
bedc4d4
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 |
---|---|---|
|
@@ -124,3 +124,6 @@ arango/version.py | |
|
||
# test results | ||
*_results.txt | ||
|
||
# devcontainers | ||
.devcontainer |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from typing import Any, Iterator, Sequence, Union | ||
|
||
from arango.exceptions import DocumentParseError | ||
from arango.typings import Json | ||
from arango.typings import Json, Jsons | ||
|
||
|
||
@contextmanager | ||
|
@@ -126,3 +126,42 @@ def build_filter_conditions(filters: Json) -> str: | |
conditions.append(f"doc.{field} == {json.dumps(v)}") | ||
|
||
return "FILTER " + " AND ".join(conditions) | ||
|
||
|
||
def validate_sort_parameters(sort: Sequence[Json]) -> bool: | ||
"""Validate sort parameters for an AQL query. | ||
|
||
:param sort: Document sort parameters. | ||
:type sort: Sequence[Json] | ||
:return: Validation success. | ||
:rtype: bool | ||
:raise arango.exceptions.DocumentGetError: If sort parameters are invalid. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring says There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I created a new section (Parameter Validation Exceptions) under exceptions.py, which can come handy for other types of validation errors (filter etc.) in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
""" | ||
assert isinstance(sort, Sequence) | ||
for param in sort: | ||
if "sort_by" not in param or "sort_order" not in param: | ||
raise DocumentParseError( | ||
"Each sort parameter must have 'sort_by' and 'sort_order'." | ||
) | ||
if param["sort_order"].upper() not in ["ASC", "DESC"]: | ||
raise DocumentParseError("'sort_order' must be either 'ASC' or 'DESC'") | ||
return True | ||
|
||
|
||
def build_sort_expression(sort: Jsons) -> str: | ||
"""Build a sort condition for an AQL query. | ||
|
||
:param sort: Document sort parameters. | ||
:type sort: Jsons | ||
:return: The complete AQL sort condition. | ||
:rtype: str | ||
""" | ||
if not sort: | ||
return "" | ||
|
||
sort_chunks = [] | ||
for sort_param in sort: | ||
chunk = f"doc.{sort_param['sort_by']} {sort_param['sort_order']}" | ||
sort_chunks.append(chunk) | ||
|
||
return "SORT " + ", ".join(sort_chunks) |
Uh oh!
There was an error while loading. Please reload this page.