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
-
#aggregate_query ⇒ AggregateQuery
Creates an AggregateQuery object for the query.
-
#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_or_filter, operator = nil, value = nil) ⇒ Object
(also: #filter)
Add a property filter to the query.
Constructor Details
Instance Method Details
#aggregate_query ⇒ AggregateQuery
Creates an AggregateQuery object for the query.
492 493 494 |
# File 'lib/google/cloud/datastore/query.rb', line 492 def aggregate_query AggregateQuery.new @grpc end |
#ancestor(parent) ⇒ Object
Add a filter for entities that inherit from a key.
263 264 265 266 267 |
# File 'lib/google/cloud/datastore/query.rb', line 263 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.
463 464 465 466 467 468 469 470 471 472 |
# File 'lib/google/cloud/datastore/query.rb', line 463 def group_by *names names.each do |name| grpc_property = Google::Cloud::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.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/google/cloud/datastore/query.rb', line 86 def kind *kinds kinds.each do |kind| grpc_kind = Google::Cloud::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.
347 348 349 350 351 |
# File 'lib/google/cloud/datastore/query.rb', line 347 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.
368 369 370 371 372 |
# File 'lib/google/cloud/datastore/query.rb', line 368 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".
322 323 324 325 326 327 328 329 330 331 |
# File 'lib/google/cloud/datastore/query.rb', line 322 def order name, direction = :asc @grpc.order << Google::Cloud::Datastore::V1::PropertyOrder.new( property: Google::Cloud::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.
433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/google/cloud/datastore/query.rb', line 433 def select *names names.each do |name| grpc_projection = Google::Cloud::Datastore::V1::Projection.new( property: Google::Cloud::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.
389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/google/cloud/datastore/query.rb', line 389 def start cursor case cursor when Cursor @grpc.start_cursor = cursor.to_grpc when String @grpc.start_cursor = Convert.decode_bytes cursor else raise ArgumentError, "Can't set a cursor using a #{cursor.class}." end self end |
#and(name, operator, value) ⇒ Object #and(filter) ⇒ Object Also known as: filter
Add a property filter to the query.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/google/cloud/datastore/query.rb', line 230 def where name_or_filter, operator = nil, value = nil @grpc.filter ||= Google::Cloud::Datastore::V1::Filter.new( composite_filter: Google::Cloud::Datastore::V1::CompositeFilter.new( op: :AND ) ) if name_or_filter.is_a? Google::Cloud::Datastore::Filter @grpc.filter.composite_filter.filters << name_or_filter.to_grpc else @grpc.filter.composite_filter.filters << \ Google::Cloud::Datastore::Filter.new(name_or_filter, operator, value).to_grpc end self end |