Class: Google::Cloud::Firestore::Query
- Inherits:
-
Object
- Object
- Google::Cloud::Firestore::Query
- Defined in:
- lib/google/cloud/firestore/query.rb
Overview
Query
Represents a query to the Firestore API.
Instances of this class are immutable. All methods that refine the query return new instances.
Direct Known Subclasses
Instance Method Summary collapse
-
#end_at(*values) ⇒ Query
Ends query results at a set of field values.
-
#end_before(*values) ⇒ Query
Ends query results before a set of field values.
-
#get {|documents| ... } ⇒ Enumerator<DocumentSnapshot>
(also: #run)
Retrieves document snapshots for the query.
-
#limit(num) ⇒ Query
Limits a query to return only the first matching documents.
-
#limit_to_last(num) ⇒ Query
Limits a query to return only the last matching documents.
-
#listen {|callback| ... } ⇒ QueryListener
(also: #on_snapshot)
Listen to this query for changes.
-
#offset(num) ⇒ Query
Skips to an offset in a query.
-
#order(field, direction = :asc) ⇒ Query
(also: #order_by)
Specifies an "order by" clause on a field.
-
#select(*fields) ⇒ Query
Restricts documents matching the query to return only data for the provided fields.
-
#start_after(*values) ⇒ Query
Starts query results after a set of field values.
-
#start_at(*values) ⇒ Query
Starts query results at a set of field values.
-
#where(field, operator, value) ⇒ Query
Filters the query on a field.
Instance Method Details
#end_at(*values) ⇒ Query
Ends query results at a set of field values. The field values can
be specified explicitly as arguments, or can be specified implicitly
by providing a DocumentSnapshot object instead. The result set will
include the document specified by values
.
If the current query already has specified end_before
or
end_at
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
# File 'lib/google/cloud/firestore/query.rb', line 874 def end_at *values raise ArgumentError, "must provide values" if values.empty? if limit_type == :last raise "cannot call end_at after calling limit_to_last" end new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = false new_query.end_at = cursor Query.start new_query, parent_path, client, limit_type: limit_type end |
#end_before(*values) ⇒ Query
Ends query results before a set of field values. The field values can
be specified explicitly as arguments, or can be specified implicitly
by providing a DocumentSnapshot object instead. The result set will
not include the document specified by values
.
If the current query already has specified end_before
or
end_at
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 |
# File 'lib/google/cloud/firestore/query.rb', line 765 def end_before *values raise ArgumentError, "must provide values" if values.empty? if limit_type == :last raise "cannot call end_before after calling limit_to_last" end new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = true new_query.end_at = cursor Query.start new_query, parent_path, client, limit_type: limit_type end |
#get {|documents| ... } ⇒ Enumerator<DocumentSnapshot> Also known as: run
Retrieves document snapshots for the query.
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
# File 'lib/google/cloud/firestore/query.rb', line 915 def get ensure_service! return enum_for :get unless block_given? results = service.run_query parent_path, @query # Reverse the results for Query#limit_to_last queries since that method reversed the order_by directions. results = results.to_a.reverse if limit_type == :last results.each do |result| next if result.document.nil? yield DocumentSnapshot.from_query_result result, client end end |
#limit(num) ⇒ Query
Limits a query to return only the first matching documents.
If the current query already has a limit set, this will overwrite it.
384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/google/cloud/firestore/query.rb', line 384 def limit num if limit_type == :last raise "cannot call limit after calling limit_to_last" end new_query = @query.dup new_query ||= StructuredQuery.new new_query.limit = Google::Protobuf::Int32Value.new value: num Query.start new_query, parent_path, client, limit_type: :first end |
#limit_to_last(num) ⇒ Query
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/google/cloud/firestore/query.rb', line 426 def limit_to_last num new_query = @query.dup if new_query.nil? || new_query.order_by.nil? || new_query.order_by.empty? raise "specify at least one order clause before calling limit_to_last" end if limit_type != :last # Don't reverse order_by more than once. # Reverse the order_by directions since we want the last results. new_query.order_by.each do |order| order.direction = order.direction.to_sym == :DESCENDING ? :ASCENDING : :DESCENDING end # Swap the cursors to match the reversed query ordering. new_end_at = new_query.start_at.dup new_start_at = new_query.end_at.dup if new_end_at new_end_at.before = !new_end_at.before new_query.end_at = new_end_at end if new_start_at new_start_at.before = !new_start_at.before new_query.start_at = new_start_at end end new_query.limit = Google::Protobuf::Int32Value.new value: num Query.start new_query, parent_path, client, limit_type: :last end |
#listen {|callback| ... } ⇒ QueryListener Also known as: on_snapshot
Listen to this query for changes.
956 957 958 959 960 961 962 |
# File 'lib/google/cloud/firestore/query.rb', line 956 def listen &callback raise ArgumentError, "callback required" if callback.nil? ensure_service! QueryListener.new(self, &callback).start end |
#offset(num) ⇒ Query
Skips to an offset in a query. If the current query already has specified an offset, this will overwrite it.
351 352 353 354 355 356 357 358 |
# File 'lib/google/cloud/firestore/query.rb', line 351 def offset num new_query = @query.dup new_query ||= StructuredQuery.new new_query.offset = num Query.start new_query, parent_path, client, limit_type: limit_type end |
#order(field, direction = :asc) ⇒ Query Also known as: order_by
Specifies an "order by" clause on a field.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/google/cloud/firestore/query.rb', line 307 def order field, direction = :asc if query_has_cursors? || limit_type == :last raise "cannot call order after calling limit_to_last, start_at, start_after, end_before, or end_at" end new_query = @query.dup new_query ||= StructuredQuery.new field = FieldPath.parse field unless field.is_a? FieldPath new_query.order_by << StructuredQuery::Order.new( field: StructuredQuery::FieldReference.new( field_path: field.formatted_string ), direction: order_direction(direction) ) Query.start new_query, parent_path, client, limit_type: limit_type end |
#select(*fields) ⇒ Query
Restricts documents matching the query to return only data for the provided fields.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/google/cloud/firestore/query.rb', line 108 def select *fields new_query = @query.dup new_query ||= StructuredQuery.new fields = Array(fields).flatten.compact fields = [FieldPath.document_id] if fields.empty? field_refs = fields.flatten.compact.map do |field| field = FieldPath.parse field unless field.is_a? FieldPath StructuredQuery::FieldReference.new \ field_path: field.formatted_string end new_query.select = StructuredQuery::Projection.new field_refs.each do |field_ref| new_query.select.fields << field_ref end Query.start new_query, parent_path, client, limit_type: limit_type end |
#start_after(*values) ⇒ Query
Starts query results after a set of field values. The field values can
be specified explicitly as arguments, or can be specified implicitly
by providing a DocumentSnapshot object instead. The result set will
not include the document specified by values
.
If the current query already has specified start_at
or
start_after
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/google/cloud/firestore/query.rb', line 656 def start_after *values raise ArgumentError, "must provide values" if values.empty? if limit_type == :last raise "cannot call start_after after calling limit_to_last" end new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = false new_query.start_at = cursor Query.start new_query, parent_path, client, limit_type: limit_type end |
#start_at(*values) ⇒ Query
Starts query results at a set of field values. The field values can be
specified explicitly as arguments, or can be specified implicitly by
providing a DocumentSnapshot object instead. The result set will
include the document specified by values
.
If the current query already has specified start_at
or
start_after
, this will overwrite it.
The values are associated with the field paths that have been provided
to order
, and must match the same sort order. An ArgumentError will
be raised if more explicit values are given than are present in
order
.
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/google/cloud/firestore/query.rb', line 548 def start_at *values raise ArgumentError, "must provide values" if values.empty? if limit_type == :last raise "cannot call start_at after calling limit_to_last" end new_query = @query.dup new_query ||= StructuredQuery.new cursor = values_to_cursor values, new_query cursor.before = true new_query.start_at = cursor Query.start new_query, parent_path, client, limit_type: limit_type end |
#where(field, operator, value) ⇒ Query
Filters the query on a field.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/google/cloud/firestore/query.rb', line 242 def where field, operator, value if query_has_cursors? raise "cannot call where after calling " \ "start_at, start_after, end_before, or end_at" end new_query = @query.dup new_query ||= StructuredQuery.new field = FieldPath.parse field unless field.is_a? FieldPath new_filter = filter field.formatted_string, operator, value add_filters_to_query new_query, new_filter Query.start new_query, parent_path, client, limit_type: limit_type end |