Class: Gcloud::Datastore::Query

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

Overview

Query

Represents the search criteria against a Datastore.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  where("completed", "=", true)

entities = dataset.run query

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new query object.

Examples:

query = Gcloud::Datastore::Query.new

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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  ancestor(parent.key)

completed_tasks = dataset.run query

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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  group_by("completed")

grouped_tasks = dataset.run query

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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind "Task"

all_tasks = dataset.run 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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  limit(10)

paginated_tasks = dataset.run query

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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  limit(10).
  offset(20)

paginated_tasks = dataset.run query

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

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  order("due", :desc)

sorted_tasks = dataset.run query

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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  select("completed", "due")

partial_tasks = dataset.run query

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.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  limit(10).
  cursor(task_cursor)

paginated_tasks = dataset.run query

167
168
169
170
# File 'lib/gcloud/datastore/query.rb', line 167

def start cursor
  @_query.start_cursor = Proto.decode_cursor cursor
  self
end

#where(name, operator, value) ⇒ Object Also known as: filter

Add a property filter to the query.

Examples:

query = Gcloud::Datastore::Query.new
query.kind("Task").
  where("completed", "=", true)

completed_tasks = dataset.run 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