|
4 | 4 | from gcloud.datastore import helpers |
5 | 5 | from gcloud.datastore.entity import Entity |
6 | 6 | from gcloud.datastore.key import Key |
| 7 | +import base64 |
7 | 8 |
|
8 | 9 |
|
9 | 10 | # TODO: Figure out how to properly handle namespaces. |
@@ -55,6 +56,7 @@ class Query(object): |
55 | 56 | def __init__(self, kind=None, dataset=None): |
56 | 57 | self._dataset = dataset |
57 | 58 | self._pb = datastore_pb.Query() |
| 59 | + self._cursor = None |
58 | 60 |
|
59 | 61 | if kind: |
60 | 62 | self._pb.kind.add().name = kind |
@@ -316,11 +318,31 @@ def fetch(self, limit=None): |
316 | 318 | for entity in entity_pbs] |
317 | 319 |
|
318 | 320 | def cursor(self): |
319 | | - return self._cursor |
| 321 | + """Returns a base64-encoded cursor string denoting the position in the query's result |
| 322 | + set following the last result retrieved. |
| 323 | +
|
| 324 | + .. Caution:: Invoking this method on a query that has not yet has been |
| 325 | + executed will raise an AssertionError exception. |
| 326 | +
|
| 327 | + :rtype: string |
| 328 | + :returns: The lastest end_cursor for query |
| 329 | + """ |
| 330 | + assert self._cursor |
| 331 | + return base64.b64encode(self._cursor) |
320 | 332 |
|
321 | 333 | def with_cursor(self, start_cursor, end_cursor=None): |
| 334 | + """Specifies the starting and (optionally) ending positions within a query's |
| 335 | + result set from which to retrieve results. |
| 336 | +
|
| 337 | + :type start_cursor: bytes |
| 338 | + :param start_cursor: Base64-encoded cursor string specifying where to start the query. |
| 339 | +
|
| 340 | + :type end_cursor: bytes |
| 341 | + :param end_cursor: Base64-encoded cursor string specifying where to end the query. |
| 342 | +
|
| 343 | + """ |
322 | 344 | if start_cursor: |
323 | | - self._pb.start_cursor = start_cursor |
| 345 | + self._pb.start_cursor = base64.b64decode(start_cursor) |
324 | 346 | if end_cursor: |
325 | | - self._pb.end_cursor = end_cursor |
| 347 | + self._pb.end_cursor = base64.b64decode(end_cursor) |
326 | 348 |
|
0 commit comments