Class: Gcloud::Datastore::Query
- Inherits:
-
Object
- Object
- Gcloud::Datastore::Query
- Defined in:
- lib/gcloud/datastore/query.rb
Overview
# Query
Represents the search criteria against a Datastore.
Instance Method Summary collapse
-
#ancestor(parent) ⇒ Object
Add a filter for entities that inherit from a key.
-
#group_by(*names) ⇒ Object
Group results by a list of properties.
-
#initialize ⇒ Query
constructor
Returns a new query object.
-
#kind(*kinds) ⇒ Object
Add the kind of entities to query.
-
#limit(num) ⇒ Object
Set a limit on the number of results to be returned.
-
#offset(num) ⇒ Object
Set an offset for the results to be returned.
-
#order(name, direction = :asc) ⇒ Object
Sort the results by a property name.
-
#select(*names) ⇒ Object
(also: #projection)
Retrieve only select properties from the matched entities.
-
#start(cursor) ⇒ Object
(also: #cursor)
Set the cursor to start the results at.
- #to_proto ⇒ Object
-
#where(name, operator, value) ⇒ Object
(also: #filter)
Add a property filter to the query.
Constructor Details
#initialize ⇒ Query
Returns a new query object.
41 42 43 |
# File 'lib/gcloud/datastore/query.rb', line 41 def initialize @_query = Proto::Query.new end |
Instance Method Details
#ancestor(parent) ⇒ Object
Add a filter for entities that inherit from a key.
96 97 98 99 100 |
# File 'lib/gcloud/datastore/query.rb', line 96 def ancestor parent # Use key if given an entity parent = parent.key if parent.respond_to? :key where "__key__", "~", parent end |
#group_by(*names) ⇒ Object
Group results by a list of properties.
200 201 202 203 204 |
# File 'lib/gcloud/datastore/query.rb', line 200 def group_by *names @_query.group_by ||= [] @_query.group_by += Proto.new_property_references(*names) self end |
#kind(*kinds) ⇒ Object
Add the kind of entities to query.
54 55 56 57 58 59 |
# File 'lib/gcloud/datastore/query.rb', line 54 def kind *kinds @_query.kind ||= Proto::KindExpression.new @_query.kind.name ||= [] @_query.kind.name |= kinds self end |
#limit(num) ⇒ Object
Set a limit on the number of results to be returned.
135 136 137 138 |
# File 'lib/gcloud/datastore/query.rb', line 135 def limit num @_query.limit = num self end |
#offset(num) ⇒ Object
Set an offset for the results to be returned.
151 152 153 154 |
# File 'lib/gcloud/datastore/query.rb', line 151 def offset num @_query.offset = num self end |
#order(name, direction = :asc) ⇒ Object
Sort the results by a property name. By default, an ascending sort order will be used. To sort in descending order, provide a second argument of a string or symbol that starts with “d”.
115 116 117 118 119 120 121 122 123 |
# File 'lib/gcloud/datastore/query.rb', line 115 def order name, direction = :asc @_query.order ||= [] po = Proto::PropertyOrder.new po.property = Proto::PropertyReference.new po.property.name = name po.direction = Proto.to_prop_order_direction direction @_query.order << po self end |
#select(*names) ⇒ Object Also known as: projection
Retrieve only select properties from the matched entities.
183 184 185 186 187 |
# File 'lib/gcloud/datastore/query.rb', line 183 def select *names @_query.projection ||= [] @_query.projection += Proto.new_property_expressions(*names) self end |
#start(cursor) ⇒ Object Also known as: cursor
Set the cursor to start the results at.
167 168 169 170 |
# File 'lib/gcloud/datastore/query.rb', line 167 def start cursor @_query.start_cursor = Proto.decode_cursor cursor self end |
#to_proto ⇒ Object
207 208 209 |
# File 'lib/gcloud/datastore/query.rb', line 207 def to_proto @_query end |
#where(name, operator, value) ⇒ Object Also known as: filter
Add a property filter to the query.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gcloud/datastore/query.rb', line 71 def where name, operator, value # Initialize filter @_query.filter ||= Proto.new_filter.tap do |f| f.composite_filter = Proto.new_composite_filter end # Create new property filter filter = Proto.new_filter.tap do |f| f.property_filter = Proto.new_property_filter name, operator, value end # Add new property filter to the list @_query.filter.composite_filter.filter << filter self end |