Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Add Support for Query Cursor & Order#126
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25672e1
add cursor support
lucemia 7a001fd
modify doc
lucemia 90ac07d
add doc, change cursor type from bytes to base64
lucemia 5f796e6
add order by function
lucemia 4a772c7
fix bug
lucemia 14019fc
Merge pull request #1 from Tagtoo/feature-order
0475d5e
add support for list_value, entity_value
lucemia 9bd8642
add support entity
lucemia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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 |
|---|---|---|
| @@ -4,6 +4,7 @@ | ||
| from gcloud.datastore import helpers | ||
| from gcloud.datastore.entity import Entity | ||
| from gcloud.datastore.key import Key | ||
| import base64 | ||
| # TODO: Figure out how to properly handle namespaces. | ||
| @@ -55,6 +56,7 @@ class Query(object): | ||
| def __init__(self, kind=None, dataset=None): | ||
| self._dataset = dataset | ||
| self._pb = datastore_pb.Query() | ||
| self._cursor = None | ||
| if kind: | ||
| self._pb.kind.add().name = kind | ||
| @@ -308,8 +310,66 @@ def fetch(self, limit=None): | ||
| if limit: | ||
| clone = self.limit(limit) | ||
| entity_pbs = self.dataset().connection().run_query( | ||
| entity_pbs, end_cursor, more_results, skipped_results = self.dataset().connection().run_query( | ||
| query_pb=clone.to_protobuf(), dataset_id=self.dataset().id()) | ||
| self._cursor = end_cursor | ||
| return [Entity.from_protobuf(entity, dataset=self.dataset()) | ||
| for entity in entity_pbs] | ||
| def cursor(self): | ||
| """Returns a base64-encoded cursor string denoting the position in the query's result | ||
| set following the last result retrieved. | ||
| .. Caution:: Invoking this method on a query that has not yet has been | ||
| executed will raise an AssertionError exception. | ||
| :rtype: string | ||
| :returns: The lastest end_cursor for query | ||
| """ | ||
| assert self._cursor | ||
| return base64.b64encode(self._cursor) | ||
| def with_cursor(self, start_cursor, end_cursor=None): | ||
| """Specifies the starting and (optionally) ending positions within a query's | ||
| result set from which to retrieve results. | ||
| :type start_cursor: bytes | ||
| :param start_cursor: Base64-encoded cursor string specifying where to start the query. | ||
| :type end_cursor: bytes | ||
| :param end_cursor: Base64-encoded cursor string specifying where to end the query. | ||
| """ | ||
| if start_cursor: | ||
| self._pb.start_cursor = base64.b64decode(start_cursor) | ||
| if end_cursor: | ||
| self._pb.end_cursor = base64.b64decode(end_cursor) | ||
| def order(self, *properties): | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| """Adds a sort order to the query. If more than one sort order is added, | ||
| they will be applied in the order specified. | ||
| :type properties: string | ||
| :param properties: String giving the name of the property on which to sort, | ||
| optionally preceded by a hyphen (-) to specify descending order. | ||
| Omitting the hyphen specifies ascending order by default. | ||
| :rtype: :class:`Query` | ||
| :returns: A Query order by properties. | ||
| """ | ||
| clone = self._clone() | ||
| for p in properties: | ||
| property_order = clone._pb.order.add() | ||
| if p.startswith('-'): | ||
| property_order.property.name = p[1:] | ||
| property_order.direct = property_order.DESCENDING | ||
| else: | ||
| property_order.property.name = p | ||
| property_order.direction = property_order.ASCENDING | ||
| return clone | ||
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.