Class: Gcloud::Bigquery::QueryData

Inherits:
Data
  • Object
show all
Defined in:
lib/gcloud/bigquery/query_data.rb

Overview

QueryData

Represents Data returned from a query a a list of name/value pairs.

Instance Attribute Summary collapse

Attributes inherited from Data

#gapi, #table

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#etag, format_rows, format_values, from_response, #kind, #raw, #token, #total

Constructor Details

#initialize(arr = []) ⇒ QueryData

:nodoc:



29
30
31
32
# File 'lib/gcloud/bigquery/query_data.rb', line 29

def initialize arr = [] #:nodoc:
  @job = nil
  super
end

Instance Attribute Details

#connectionObject

The Connection object.



27
28
29
# File 'lib/gcloud/bigquery/query_data.rb', line 27

def connection
  @connection
end

Class Method Details

.from_gapi(gapi, connection) ⇒ Object

New Data from a response object.



108
109
110
111
112
113
114
115
116
# File 'lib/gcloud/bigquery/query_data.rb', line 108

def self.from_gapi gapi, connection #:nodoc:
  formatted_rows = format_rows gapi["rows"],
                               gapi["schema"]["fields"]

  data = new formatted_rows
  data.gapi = gapi
  data.connection = connection
  data
end

Instance Method Details

#cache_hit?Boolean

Whether the query result was fetched from the query cache.

Returns:

  • (Boolean)


46
47
48
# File 'lib/gcloud/bigquery/query_data.rb', line 46

def cache_hit?
  @gapi["cacheHit"]
end

#complete?Boolean

Whether the query has completed or not. When data is present this will always be true. When false, total will not be available.

Returns:

  • (Boolean)


41
42
43
# File 'lib/gcloud/bigquery/query_data.rb', line 41

def complete?
  @gapi["jobComplete"]
end

#fieldsObject

The fields of the data.



61
62
63
64
65
66
# File 'lib/gcloud/bigquery/query_data.rb', line 61

def fields
  f = schema["fields"]
  f = f.to_hash if f.respond_to? :to_hash
  f = [] if f.nil?
  f
end

#headersObject

The name of the columns in the data.



70
71
72
# File 'lib/gcloud/bigquery/query_data.rb', line 70

def headers
  fields.map { |f| f["name"] }
end

#jobObject

The BigQuery Job that was created to run the query.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gcloud/bigquery/query_data.rb', line 93

def job
  return @job if @job
  return nil unless job?
  ensure_connection!
  resp = connection.get_job job_id
  if resp.success?
    @job = Job.from_gapi resp.data, connection
  else
    return nil if resp.status == 404
    fail ApiError.from_response(resp)
  end
end

#nextObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/gcloud/bigquery/query_data.rb', line 80

def next
  return nil unless next?
  ensure_connection!
  resp = connection.job_query_results job_id, token: token
  if resp.success?
    QueryData.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#next?Boolean

Is there a next page of data?

Returns:

  • (Boolean)


76
77
78
# File 'lib/gcloud/bigquery/query_data.rb', line 76

def next?
  !token.nil?
end

#schemaObject

The schema of the data.



52
53
54
55
56
57
# File 'lib/gcloud/bigquery/query_data.rb', line 52

def schema
  s = @gapi["schema"]
  s = s.to_hash if s.respond_to? :to_hash
  s = {} if s.nil?
  s
end

#total_bytesObject

The total number of bytes processed for this query.



35
36
37
# File 'lib/gcloud/bigquery/query_data.rb', line 35

def total_bytes
  @gapi["totalBytesProcessed"]
end