Python
Queries#
Create / interact with gcloud datastore queries.
- 
class gcloud.datastore.query.Iterator(query, connection, limit=None, offset=0, start_cursor=None, end_cursor=None)[source]#
- Bases: - object- Represent the state of a given execution of a Query. 
- 
class gcloud.datastore.query.Query(kind=None, dataset_id=None, namespace=None, ancestor=None, filters=(), projection=(), order=(), group_by=())[source]#
- Bases: - object- A Query against the Cloud Datastore. - This class serves as an abstraction for creating a query over data stored in the Cloud Datastore. - Parameters: - kind (string) – The kind to query.
- dataset_id (string) – The ID of the dataset to query. If not passed, uses the implicit default.
- namespace (string or None) – The namespace to which to restrict results.
- ancestor (gcloud.datastore.key.Keyor None) – key of the ancestor to which this query’s results are restricted.
- filters (sequence of (property_name, operator, value) tuples) – property filters applied by this query.
- projection (sequence of string) – fields returned as part of query results.
- order (sequence of string) – field names used to order query results. Prepend ‘-‘ to a field name to sort it in descending order.
- group_by (sequence of string) – field names used to group query results.
 - Raises: - ValueError if - dataset_idis not passed and no implicit default is set.- 
OPERATORS= {'>=': 4, '<=': 2, '>': 3, '=': 5, '<': 1}#
- Mapping of operator strings and their protobuf equivalents. 
 - 
add_filter(property_name, operator, value)[source]#
- Filter the query based on a property name, operator and a value. - Expressions take the form of: - .add_filter('<property>', '<operator>', <value>)- where property is a property stored on the entity in the datastore and operator is one of - OPERATORS(ie,- =,- <,- <=,- >,- >=):- >>> from gcloud import datastore >>> query = datastore.Query('Person') >>> query.add_filter('name', '=', 'James') >>> query.add_filter('age', '>', 50) - Parameters: - property_name (string) – A property name.
- operator (string) – One of =,<,<=,>,>=.
- value (integer, string, boolean, float, None, datetime) – The value to filter on.
 - Raises: - ValueErrorif- operationis not one of the specified values, or if a filter names- '__key__'but passes invalid operator (- ==is required) or value (a key is required).
 - 
ancestor#
- The ancestor key for the query. - Return type: - Key or None 
 - 
dataset_id#
- Get the dataset ID for this Query. - Return type: - str 
 - 
fetch(limit=None, offset=0, start_cursor=None, end_cursor=None, connection=None)[source]#
- Execute the Query; return an iterator for the matching entities. - For example: - >>> from gcloud import datastore >>> query = datastore.Query('Person') >>> query.add_filter('name', '=', 'Sally') >>> list(query.fetch()) [<Entity object>, <Entity object>, ...] >>> list(query.fetch(1)) [<Entity object>] - Parameters: - limit (integer or None) – An optional limit passed through to the iterator.
- offset (integer) – An optional offset passed through to the iterator.
- start_cursor (bytes) – An optional cursor passed through to the iterator.
- end_cursor (bytes) – An optional cursor passed through to the iterator.
- connection (gcloud.datastore.connection.Connection) – An optional cursor passed through to the iterator. If not supplied, uses the implicit default.
 - Return type: - Raises: - ValueError if - connectionis not passed and no implicit default has been set.
 - 
filters#
- Filters set on the query. - Return type: - sequence of (property_name, operator, value) tuples. 
 - 
group_by#
- Names of fields used to group query results. - Return type: - sequence of string 
 - 
kind#
- Get the Kind of the Query. - Return type: - string 
 - 
namespace#
- This query’s namespace - Return type: - string or None - Returns: - the namespace assigned to this query 
 - 
order#
- Names of fields used to sort query results. - Return type: - sequence of string 
 - 
projection#
- Fields names returned by the query. - Return type: - sequence of string - Returns: - Names of fields in query results.