Class: Google::Cloud::Datastore::Dataset::AggregateQueryResults

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

Overview

AggregateQueryResults

An AggregateQueryResult object is a representation for a result of an AggregateQuery or a GqlQuery.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

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

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#read_timeObject (readonly)

Read timestamp the query was done on the database at.

Returns:

  • Google::Protobuf::Timestamp



57
58
59
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 57

def read_time
  @read_time
end

Instance Method Details

#get(aggregate_alias = nil) ⇒ Integer, ...

Retrieves the aggregate data.

if the aggregate_alias does not exist.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

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

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Alias an aggregate query

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

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

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count aggregate_alias: 'total'

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get('total')

Parameters:

  • aggregate_alias (String) (defaults to: nil)

    The alias used to access the aggregate value. For an AggregateQuery with a single aggregate field, this parameter can be omitted.

Returns:

  • (Integer, Float, nil)

    The aggregate value. Returns nil



100
101
102
103
104
105
106
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 100

def get aggregate_alias = nil
  if @aggregate_fields.count > 1 && aggregate_alias.nil?
    raise ArgumentError, "Required param aggregate_alias for AggregateQuery with multiple aggregate fields"
  end
  aggregate_alias ||= @aggregate_fields.keys.first
  @aggregate_fields[aggregate_alias]
end