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
(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.
- #to_grpc ⇒ Object
-
#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.
161 162 163 164 165 |
# File 'lib/gcloud/datastore/query.rb', line 161 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.
317 318 319 320 321 322 323 324 325 |
# File 'lib/gcloud/datastore/query.rb', line 317 def group_by *names names.each do |name| grpc_property = Google::Datastore::V1beta3::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](cloud.google.com/datastore/docs/concepts/metadataqueries).
64 65 66 67 68 69 70 71 |
# File 'lib/gcloud/datastore/query.rb', line 64 def kind *kinds kinds.each do |kind| grpc_kind = Google::Datastore::V1beta3::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.
224 225 226 227 228 |
# File 'lib/gcloud/datastore/query.rb', line 224 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.
241 242 243 244 245 |
# File 'lib/gcloud/datastore/query.rb', line 241 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”.
204 205 206 207 208 209 210 211 212 |
# File 'lib/gcloud/datastore/query.rb', line 204 def order name, direction = :asc @grpc.order << Google::Datastore::V1beta3::PropertyOrder.new( property: Google::Datastore::V1beta3::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.
293 294 295 296 297 298 299 300 301 302 |
# File 'lib/gcloud/datastore/query.rb', line 293 def select *names names.each do |name| grpc_projection = Google::Datastore::V1beta3::Projection.new( property: Google::Datastore::V1beta3::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.
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/gcloud/datastore/query.rb', line 258 def start cursor if cursor.is_a? Cursor @grpc.start_cursor = cursor.to_grpc elsif cursor.is_a? String @grpc.start_cursor = GRPCUtils.decode_bytes cursor else fail ArgumentError, "Can't set a cursor using a #{cursor.class}." end self end |
#to_grpc ⇒ Object
329 330 331 |
# File 'lib/gcloud/datastore/query.rb', line 329 def to_grpc @grpc end |
#where(name, operator, value) ⇒ Object Also known as: filter
Add a property filter to the query.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/gcloud/datastore/query.rb', line 129 def where name, operator, value @grpc.filter ||= Google::Datastore::V1beta3::Filter.new( composite_filter: Google::Datastore::V1beta3::CompositeFilter.new( op: :AND ) ) @grpc.filter.composite_filter.filters << \ Google::Datastore::V1beta3::Filter.new( property_filter: Google::Datastore::V1beta3::PropertyFilter.new( property: Google::Datastore::V1beta3::PropertyReference.new( name: name), op: GRPCUtils.to_prop_filter_op(operator), value: GRPCUtils.to_value(value) ) ) self end |