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
Class Method Summary collapse
-
.from_json(json, client) ⇒ Query
Deserializes a JSON text string serialized from this class and returns it as a new instance.
Instance Method Summary collapse
-
#aggregate_query ⇒ AggregateQuery
Creates an AggregateQuery object for the query.
-
#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.
-
#explain(read_time: false, analyze: false) ⇒ QueryExplainResult
Retrieves the query explanation for the query.
-
#get(read_time: nil) {|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.
-
#to_json(options = nil) ⇒ String
Serializes the instance to a JSON text string.
-
#where(filter_or_field = nil, operator = nil, value = nil) ⇒ Query
Adds filter to the where clause.
Class Method Details
.from_json(json, client) ⇒ Query
Deserializes a JSON text string serialized from this class and returns it as a new instance. See also #to_json.
1192 1193 1194 1195 1196 1197 1198 1199 1200 |
# File 'lib/google/cloud/firestore/query.rb', line 1192 def self.from_json json, client raise ArgumentError, "client is required" unless client json = JSON.parse json query_json = json["query"] raise ArgumentError, "Field 'query' is required" unless query_json query = Google::Cloud::Firestore::V1::StructuredQuery.decode_json query_json.to_json start query, json["parent_path"], client, limit_type: json["limit_type"]&.to_sym end |
Instance Method Details
#aggregate_query ⇒ AggregateQuery
Creates an AggregateQuery object for the query.
1104 1105 1106 |
# File 'lib/google/cloud/firestore/query.rb', line 1104 def aggregate_query AggregateQuery.new self, parent_path, client end |
#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.
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
# File 'lib/google/cloud/firestore/query.rb', line 933 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 start_new_query new_query 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.
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
# File 'lib/google/cloud/firestore/query.rb', line 824 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 start_new_query new_query end |
#explain(read_time: false, analyze: false) ⇒ QueryExplainResult
Retrieves the query explanation for the query.
By default the query is only planned, not executed. returning only metrics from the
planning stages. If analyze is set to true the query will be planned and executed,
returning the full query results alongside both planning and execution stage metrics.
Unlike the Enumerator object that is returned from the Query#get,
iterating over QueryExplainResult multiple times will not result in
multiple requests to the server. The first set of results will be saved
and re-used instead.
This is to avoid the situations where the metrics change unpredictably when results are looked at.
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 |
# File 'lib/google/cloud/firestore/query.rb', line 1070 def explain read_time: false, analyze: false ensure_service! # Validate analyze parameter unless [true, false].include? analyze raise ArgumentError, "analyze must be a boolean" end = ::Google::Cloud::Firestore::V1::ExplainOptions.new .analyze = analyze results = service.run_query parent_path, @query, read_time: read_time, explain_options: # 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 QueryExplainResult.new results, client end |
#get(read_time: nil) {|documents| ... } ⇒ Enumerator<DocumentSnapshot> Also known as: run
Retrieves document snapshots for the query.
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 |
# File 'lib/google/cloud/firestore/query.rb', line 994 def get read_time: nil ensure_service! return enum_for :get, read_time: read_time unless block_given? results = service.run_query parent_path, @query, read_time: read_time # 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.
443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/google/cloud/firestore/query.rb', line 443 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 start_new_query new_query, limit_type_override: :first end |
#limit_to_last(num) ⇒ Query
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/google/cloud/firestore/query.rb', line 485 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 end if new_start_at new_start_at.before = !new_start_at.before end new_query.end_at = new_end_at new_query.start_at = new_start_at end new_query.limit = Google::Protobuf::Int32Value.new value: num start_new_query new_query, limit_type_override: :last end |
#listen {|callback| ... } ⇒ QueryListener Also known as: on_snapshot
Listen to this query for changes.
1132 1133 1134 1135 1136 1137 1138 |
# File 'lib/google/cloud/firestore/query.rb', line 1132 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.
410 411 412 413 414 415 416 417 |
# File 'lib/google/cloud/firestore/query.rb', line 410 def offset num new_query = @query.dup new_query ||= StructuredQuery.new new_query.offset = num start_new_query new_query end |
#order(field, direction = :asc) ⇒ Query Also known as: order_by
Specifies an "order by" clause on a field.
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/google/cloud/firestore/query.rb', line 366 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) ) start_new_query new_query end |
#select(*fields) ⇒ Query
Restricts documents matching the query to return only data for the provided fields.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/google/cloud/firestore/query.rb', line 128 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 start_new_query new_query 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.
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 |
# File 'lib/google/cloud/firestore/query.rb', line 715 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 start_new_query new_query 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.
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/google/cloud/firestore/query.rb', line 607 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 start_new_query new_query end |
#to_json(options = nil) ⇒ String
Serializes the instance to a JSON text string. See also from_json.
1160 1161 1162 1163 1164 1165 1166 1167 |
# File 'lib/google/cloud/firestore/query.rb', line 1160 def to_json = nil query_json = Google::Cloud::Firestore::V1::StructuredQuery.encode_json query { "query" => JSON.parse(query_json), "parent_path" => parent_path, "limit_type" => limit_type }.to_json end |
#where(filter) ⇒ Query #where(field, operator, value) ⇒ Query
Adds filter to the where clause
@param filter [::Google::Cloud::Firestore::Filter]
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/google/cloud/firestore/query.rb', line 299 def where filter_or_field = nil, operator = nil, value = nil 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 if filter_or_field.is_a? Google::Cloud::Firestore::Filter new_query.where = filter_or_field.filter else new_filter = Google::Cloud::Firestore::Filter.new filter_or_field, operator, value add_filters_to_query new_query, new_filter.filter end start_new_query new_query end |