Class: Google::Cloud::Datastore::Query
- Inherits:
-
Object
- Object
- Google::Cloud::Datastore::Query
- Defined in:
- lib/google/cloud/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
(also: #distinct_on)
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.
-
#where(name, operator, value) ⇒ Object
(also: #filter)
Add a property filter to the query.
Constructor Details
Instance Method Details
#ancestor(parent) ⇒ Object
Add a filter for entities that inherit from a key.
216 217 218 219 220 |
# File 'lib/google/cloud/datastore/query.rb', line 216 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 Also known as: distinct_on
Group results by a list of properties.
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/google/cloud/datastore/query.rb', line 415 def group_by *names names.each do |name| grpc_property = Google::Datastore::V1::PropertyReference.new( name: name ) @grpc.distinct_on << grpc_property end self end |
#kind(*kinds) ⇒ Object
Add the kind of entities to query.
Special entity kinds such as __namespace__
, __kind__
, and
__property__
can be used for metadata
queries.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/google/cloud/datastore/query.rb', line 84 def kind *kinds kinds.each do |kind| grpc_kind = Google::Datastore::V1::KindExpression.new( name: kind ) @grpc.kind << grpc_kind end self end |
#limit(num) ⇒ Object
Set a limit on the number of results to be returned.
300 301 302 303 304 |
# File 'lib/google/cloud/datastore/query.rb', line 300 def limit num @grpc.limit = Google::Protobuf::Int32Value.new(value: num) self end |
#offset(num) ⇒ Object
Set an offset for the results to be returned.
321 322 323 324 325 |
# File 'lib/google/cloud/datastore/query.rb', line 321 def offset num @grpc.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".
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/google/cloud/datastore/query.rb', line 275 def order name, direction = :asc @grpc.order << Google::Datastore::V1::PropertyOrder.new( property: Google::Datastore::V1::PropertyReference.new( name: name ), direction: prop_order_direction(direction) ) self end |
#select(*names) ⇒ Object Also known as: projection
Retrieve only select properties from the matched entities.
385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/google/cloud/datastore/query.rb', line 385 def select *names names.each do |name| grpc_projection = Google::Datastore::V1::Projection.new( property: Google::Datastore::V1::PropertyReference.new( name: name ) ) @grpc.projection << grpc_projection end self end |
#start(cursor) ⇒ Object Also known as: cursor
Set the cursor to start the results at.
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/google/cloud/datastore/query.rb', line 342 def start cursor if cursor.is_a? Cursor @grpc.start_cursor = cursor.to_grpc elsif cursor.is_a? String @grpc.start_cursor = Convert.decode_bytes cursor else raise ArgumentError, "Can't set a cursor using a #{cursor.class}." end self end |
#where(name, operator, value) ⇒ Object Also known as: filter
Add a property filter to the query.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/google/cloud/datastore/query.rb', line 179 def where name, operator, value @grpc.filter ||= Google::Datastore::V1::Filter.new( composite_filter: Google::Datastore::V1::CompositeFilter.new( op: :AND ) ) @grpc.filter.composite_filter.filters << \ Google::Datastore::V1::Filter.new( property_filter: Google::Datastore::V1::PropertyFilter.new( property: Google::Datastore::V1::PropertyReference.new( name: name ), op: Convert.to_prop_filter_op(operator), value: Convert.to_value(value) ) ) self end |