Class: Google::Cloud::Spanner::Results
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::Results
- Defined in:
- lib/google/cloud/spanner/results.rb
Overview
# Results
Represents the result set from an operation returning data.
See Client#execute and Client#read.
Class Method Summary collapse
- .execute(service, session_path, sql, params: nil, types: nil, transaction: nil, partition_token: nil) ⇒ Object
- .from_enum(enum, service) ⇒ Object
- .read(service, session_path, table, columns, keys: nil, index: nil, limit: nil, transaction: nil, partition_token: nil) ⇒ Object
Instance Method Summary collapse
-
#fields ⇒ Fields
Returns the configuration object (Fields) of the names and types of the rows in the returned data.
- #inspect ⇒ Object
-
#rows {|row| ... } ⇒ Object
The values returned from the request.
-
#timestamp ⇒ Time
The read timestamp chosen for single-use snapshots (read-only transactions).
- #to_s ⇒ Object
Class Method Details
.execute(service, session_path, sql, params: nil, types: nil, transaction: nil, partition_token: nil) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/google/cloud/spanner/results.rb', line 214 def self.execute service, session_path, sql, params: nil, types: nil, transaction: nil, partition_token: nil = { transaction: transaction, params: params, types: types, partition_token: partition_token } enum = service.streaming_execute_sql session_path, sql, from_enum(enum, service).tap do |results| results.instance_variable_set :@session_path, session_path results.instance_variable_set :@sql, sql results.instance_variable_set :@execute_options, end end |
.from_enum(enum, service) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/google/cloud/spanner/results.rb', line 201 def self.from_enum enum, service grpc = enum.peek new.tap do |results| results.instance_variable_set :@metadata, grpc. results.instance_variable_set :@stats, grpc.stats results.instance_variable_set :@enum, enum results.instance_variable_set :@service, service end rescue GRPC::BadStatus => e raise Google::Cloud::Error.from_error(e) end |
.read(service, session_path, table, columns, keys: nil, index: nil, limit: nil, transaction: nil, partition_token: nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/google/cloud/spanner/results.rb', line 228 def self.read service, session_path, table, columns, keys: nil, index: nil, limit: nil, transaction: nil, partition_token: nil = { keys: keys, index: index, limit: limit, transaction: transaction, partition_token: partition_token } enum = service.streaming_read_table \ session_path, table, columns, from_enum(enum, service).tap do |results| results.instance_variable_set :@session_path, session_path results.instance_variable_set :@table, table results.instance_variable_set :@columns, columns results.instance_variable_set :@read_options, end end |
Instance Method Details
#fields ⇒ Fields
Returns the configuration object (Fields) of the names and types of the rows in the returned data.
72 73 74 |
# File 'lib/google/cloud/spanner/results.rb', line 72 def fields @fields ||= Fields.from_grpc .row_type.fields end |
#inspect ⇒ Object
250 251 252 |
# File 'lib/google/cloud/spanner/results.rb', line 250 def inspect "#<#{self.class.name} #{self}>" end |
#rows {|row| ... } ⇒ Object
The values returned from the request.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/google/cloud/spanner/results.rb', line 97 def rows return nil if @closed unless block_given? return enum_for(:rows) end fields = .row_type.fields if fields.count.zero? @closed = true return [] end values = [] buffered_responses = [] buffer_upper_bound = 10 chunked_value = nil resume_token = nil # Cannot call Enumerator#each because it won't return the first # value that was already identified when calling Enumerator#peek. # Iterate only using Enumerator#next and break on StopIteration. loop do begin grpc = @enum.next # metadata should be set before the first iteration... ||= grpc. @stats ||= grpc.stats buffered_responses << grpc if (grpc.resume_token && grpc.resume_token != "") || buffered_responses.size >= buffer_upper_bound # This can set the resume_token to nil resume_token = grpc.resume_token buffered_responses.each do |resp| if chunked_value resp.values.unshift merge(chunked_value, resp.values.shift) chunked_value = nil end to_iterate = values + Array(resp.values) chunked_value = to_iterate.pop if resp.chunked_value values = to_iterate.pop(to_iterate.count % fields.count) to_iterate.each_slice(fields.count) do |slice| yield Data.from_grpc(slice, fields) end end # Flush the buffered responses now that they are all handled buffered_responses = [] end rescue GRPC::Unavailable => err if resume_token.nil? || resume_token.empty? # Re-raise if the resume_token is not a valid value. # This can happen if the buffer was flushed. raise Google::Cloud::Error.from_error(err) end # Resume the stream from the last known resume_token if @enum = @service.streaming_execute_sql \ @session_path, @sql, .merge(resume_token: resume_token) else @enum = @service.streaming_read_table \ @session_path, @table, @columns, .merge(resume_token: resume_token) end # Flush the buffered responses to reset to the resume_token buffered_responses = [] rescue GRPC::BadStatus => err raise Google::Cloud::Error.from_error(err) rescue StopIteration break end end # clear out any remaining values left over buffered_responses.each do |resp| if chunked_value resp.values.unshift merge(chunked_value, resp.values.shift) chunked_value = nil end to_iterate = values + Array(resp.values) chunked_value = to_iterate.pop if resp.chunked_value values = to_iterate.pop(to_iterate.count % fields.count) to_iterate.each_slice(fields.count) do |slice| yield Data.from_grpc(slice, fields) end end values.each_slice(fields.count) do |slice| yield Data.from_grpc(slice, fields) end # If we get this far then we can release the session @closed = true nil end |
#timestamp ⇒ Time
The read timestamp chosen for single-use snapshots (read-only transactions).
48 49 50 51 |
# File 'lib/google/cloud/spanner/results.rb', line 48 def return nil if .nil? || .transaction.nil? Convert. .transaction. end |
#to_s ⇒ Object
245 246 247 |
# File 'lib/google/cloud/spanner/results.rb', line 245 def to_s "(#{fields.inspect} streaming)" end |