Class: Google::Cloud::Datastore::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/query.rb

Overview

Query

Represents the search criteria against a Datastore.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("done", "=", false).
  where("priority", ">=", 4).
  order("priority", :desc)

tasks = datastore.run query

Run the query within a namespace with the namespace option:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = datastore.query("Task").
  where("done", "=", false)
tasks = datastore.run query, namespace: "example-ns"

See Also:

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new query object.

Examples:

require "google/cloud/datastore"

query = Google::Cloud::Datastore::Query.new


63
64
65
# File 'lib/google/cloud/datastore/query.rb', line 63

def initialize
  @grpc = Google::Datastore::V1::Query.new
end

Instance Method Details

#ancestor(parent) ⇒ Object

Add a filter for entities that inherit from a key.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

task_list_key = datastore.key "TaskList", "default"

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  ancestor(task_list_key)

tasks = datastore.run query


214
215
216
217
218
# File 'lib/google/cloud/datastore/query.rb', line 214

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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  distinct_on("type", "priority").
  order("type").
  order("priority")

tasks = datastore.run query


410
411
412
413
414
415
416
417
418
# File 'lib/google/cloud/datastore/query.rb', line 410

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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind "Task"

tasks = datastore.run query


84
85
86
87
88
89
90
91
92
# 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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  limit(5)

tasks = datastore.run query


297
298
299
300
301
# File 'lib/google/cloud/datastore/query.rb', line 297

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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  limit(5).
  offset(10)

tasks = datastore.run query


318
319
320
321
322
# File 'lib/google/cloud/datastore/query.rb', line 318

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".

Examples:

With ascending sort order:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  order("created")

tasks = datastore.run query

With descending sort order:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  order("created", :desc)

tasks = datastore.run query

With multiple sort orders:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  order("priority", :desc).
  order("created")

tasks = datastore.run query

A property used in inequality filter must be ordered first:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("priority", ">", 3).
  order("priority").
  order("created")

tasks = datastore.run query


273
274
275
276
277
278
279
280
281
# File 'lib/google/cloud/datastore/query.rb', line 273

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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  select("priority", "percent_complete")

priorities = []
percent_completes = []
datastore.run(query).each do |t|
  priorities << t["priority"]
  percent_completes << t["percent_complete"]
end

A keys-only query using the special property __key__:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  select("__key__")

keys = datastore.run(query).map(&:key)


382
383
384
385
386
387
388
389
390
391
# File 'lib/google/cloud/datastore/query.rb', line 382

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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  limit(page_size).
  start(page_cursor)

tasks = datastore.run query


339
340
341
342
343
344
345
346
347
348
349
# File 'lib/google/cloud/datastore/query.rb', line 339

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
    fail 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.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("done", "=", false)

tasks = datastore.run query

Add a composite property filter:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("done", "=", false).
  where("priority", ">=", 4)

tasks = datastore.run query

Add an inequality filter on a single property only:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("created", ">=", Time.utc(1990, 1, 1)).
  where("created", "<", Time.utc(2000, 1, 1))

tasks = datastore.run query

Add a composite filter on an array property:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("tag", "=", "fun").
  where("tag", "=", "programming")

tasks = datastore.run query

Add an inequality filter on an array property :

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("tag", ">", "learn").
  where("tag", "<", "math")

tasks = datastore.run query

Add a key filter using the special property __key__:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task").
  where("__key__", ">", datastore.key("Task", "someTask"))

tasks = datastore.run query

Add a key filter to a kindless query:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

last_seen_key = datastore.key "Task", "a"
query = Google::Cloud::Datastore::Query.new
query.where("__key__", ">", last_seen_key)

tasks = datastore.run query


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/google/cloud/datastore/query.rb', line 178

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