1919from gcloud .datastore import _implicit_environ
2020from gcloud .datastore import datastore_v1_pb2 as datastore_pb
2121from gcloud .datastore import helpers
22- from gcloud .datastore .dataset import Dataset
2322from gcloud .datastore .key import Key
2423
2524
26- class Query (_implicit_environ . _DatastoreBase ):
25+ class Query (object ):
2726 """A Query against the Cloud Datastore.
2827
2928 This class serves as an abstraction for creating a query over data
@@ -32,8 +31,9 @@ class Query(_implicit_environ._DatastoreBase):
3231 :type kind: string.
3332 :param kind: The kind to query.
3433
35- :type dataset: :class:`gcloud.datastore.dataset.Dataset`.
36- :param dataset: The dataset to query.
34+ :type dataset_id: str
35+ :param dataset_id: The ID of the dataset to query. If not passed,
36+ uses the implicit default.
3737
3838 :type namespace: string or None.
3939 :param namespace: The namespace to which to restrict results.
@@ -54,6 +54,9 @@ class Query(_implicit_environ._DatastoreBase):
5454
5555 :type group_by: sequence_of_string.
5656 :param group_by: field names used to group query results.
57+
58+ :raises: ValueError if ``dataset_id`` is not passed and no implicit
59+ default is set.
5760 """
5861
5962 OPERATORS = {
@@ -66,15 +69,22 @@ class Query(_implicit_environ._DatastoreBase):
6669 """Mapping of operator strings and their protobuf equivalents."""
6770
6871 def __init__ (self ,
72+ dataset_id = None ,
6973 kind = None ,
70- dataset = None ,
7174 namespace = None ,
7275 ancestor = None ,
7376 filters = (),
7477 projection = (),
7578 order = (),
7679 group_by = ()):
77- super (Query , self ).__init__ (dataset = dataset )
80+
81+ if dataset_id is None :
82+ dataset_id = _implicit_environ .DATASET_ID
83+
84+ if dataset_id is None :
85+ raise ValueError ("No dataset ID supplied, and no default set." )
86+
87+ self ._dataset_id = dataset_id
7888 self ._kind = kind
7989 self ._namespace = namespace
8090 self ._ancestor = ancestor
@@ -84,26 +94,12 @@ def __init__(self,
8494 self ._group_by = list (group_by )
8595
8696 @property
87- def dataset (self ):
88- """Get the dataset for this Query.
89-
90- The dataset against which the Query will be run.
91-
92- :rtype: :class:`gcloud.datastore.dataset.Dataset` or None,
93- :returns: the current dataset.
94- """
95- return self ._dataset
96-
97- @dataset .setter
98- def dataset (self , value ):
99- """Set the dataset for the query
97+ def dataset_id (self ):
98+ """Get the dataset ID for this Query.
10099
101- :type value: class:`gcloud.datastore.dataset.Dataset`
102- :param value: the new dataset
100+ :rtype: str
103101 """
104- if not isinstance (value , Dataset ):
105- raise ValueError ("Dataset must be a Dataset" )
106- self ._dataset = value
102+ return self ._dataset_id
107103
108104 @property
109105 def namespace (self ):
@@ -294,34 +290,49 @@ def group_by(self, value):
294290 value = [value ]
295291 self ._group_by [:] = value
296292
297- def fetch (self , limit = 0 , offset = 0 , start_cursor = None , end_cursor = None ):
293+ def fetch (self , limit = None , offset = 0 , start_cursor = None , end_cursor = None ,
294+ connection = None ):
298295 """Execute the Query; return an iterator for the matching entities.
299296
300297 For example::
301298
302- >>> from gcloud import datastore
303- >>> dataset = datastore.get_dataset ('dataset-id')
304- >>> query = dataset.query('Person').filter ('name', '=', 'Sally')
299+ >>> from gcloud.datastore.query import Query
300+ >>> query = Query ('dataset-id', 'Person ')
301+ >>> query.add_filter ('name', '=', 'Sally')
305302 >>> list(query.fetch())
306303 [<Entity object>, <Entity object>, ...]
307304 >>> list(query.fetch(1))
308305 [<Entity object>]
309306
310- :type limit: integer
307+ :type limit: integer or None
311308 :param limit: An optional limit passed through to the iterator.
312309
313- :type limit: offset
314- :param limit : An optional offset passed through to the iterator.
310+ :type offset: integer
311+ :param offset : An optional offset passed through to the iterator.
315312
316- :type start_cursor: offset
313+ :type start_cursor: bytes
317314 :param start_cursor: An optional cursor passed through to the iterator.
318315
319- :type end_cursor: offset
316+ :type end_cursor: bytes
320317 :param end_cursor: An optional cursor passed through to the iterator.
321318
319+ :type connection: :class:`gcloud.datastore.connection.Connection`
320+ :param connection: An optional cursor passed through to the iterator.
321+ If not supplied, uses the implicit default.
322+
323+
322324 :rtype: :class:`Iterator`
325+ :raises: ValueError if ``connection`` is not passed and no implicit
326+ default has been set.
323327 """
324- return Iterator (self , limit , offset , start_cursor , end_cursor )
328+ if connection is None :
329+ connection = _implicit_environ .CONNECTION
330+
331+ if connection is None :
332+ raise ValueError ("No connection passed, and no default set" )
333+
334+ return Iterator (
335+ self , connection , limit , offset , start_cursor , end_cursor )
325336
326337
327338class Iterator (object ):
@@ -334,9 +345,10 @@ class Iterator(object):
334345 datastore_pb .QueryResultBatch .MORE_RESULTS_AFTER_LIMIT ,
335346 )
336347
337- def __init__ (self , query , limit = None , offset = 0 ,
348+ def __init__ (self , query , connection , limit = None , offset = 0 ,
338349 start_cursor = None , end_cursor = None ):
339350 self ._query = query
351+ self ._connection = connection
340352 self ._limit = limit
341353 self ._offset = offset
342354 self ._start_cursor = start_cursor
@@ -366,9 +378,9 @@ def next_page(self):
366378
367379 pb .offset = self ._offset
368380
369- query_results = self ._query . dataset . connection () .run_query (
381+ query_results = self ._connection .run_query (
370382 query_pb = pb ,
371- dataset_id = self ._query .dataset . id () ,
383+ dataset_id = self ._query .dataset_id ,
372384 namespace = self ._query .namespace ,
373385 )
374386 # NOTE: `query_results` contains an extra value that we don't use,
0 commit comments