class Google::Cloud::Spanner::Data

# Data

Represents a row in a result from Cloud Spanner. Provides access to data in a hash-like structure. Values can be retrieved by name (String), or in cases in which values are unnamed, by zero-based index position (Integer).

@example

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

results = db.execute_query "SELECT * FROM users"

results.rows.each do |row|
  puts "User #{row[:id]} is #{row[:name]}"
end

Public Class Methods

from_grpc(grpc_values, grpc_fields) click to toggle source

@private Creates a new Data instance from Spanner values and fields.

# File lib/google/cloud/spanner/data.rb, line 235
def self.from_grpc grpc_values, grpc_fields
  new.tap do |d|
    d.instance_variable_set :@grpc_values, Array(grpc_values)
    d.instance_variable_set :@grpc_fields, Array(grpc_fields)
  end
end

Public Instance Methods

==(other) click to toggle source

@private

# File lib/google/cloud/spanner/data.rb, line 179
def == other
  return false unless other.is_a? Data
  pairs == other.pairs
end
[](key) click to toggle source

Returns the value object for the provided name (String) or index (Integer). Do not pass a name to this method if the data has more than one member with the same name.

@param [String, Integer] key The name (String) or zero-based index

position (Integer) of the value.

@raise [Google::Cloud::Spanner::DuplicateNameError] if the data

contains duplicate names.

@return [Object, nil] The value, or nil if no value is found.

# File lib/google/cloud/spanner/data.rb, line 111
def [] key
  if key.is_a? Integer
    return Convert.grpc_value_to_object(@grpc_values[key],
                                        @grpc_fields[key].type)
  end
  name_count = @grpc_fields.find_all { |f| f.name == String(key) }.count
  return nil if name_count.zero?
  raise DuplicateNameError if name_count > 1
  index = @grpc_fields.find_index { |f| f.name == String(key) }
  Convert.grpc_value_to_object(@grpc_values[index],
                               @grpc_fields[index].type)
end
fields() click to toggle source

Returns the configuration object ({Fields}) of the names and types of the data.

@return [Array<Array>] An array containing name and value pairs.

# File lib/google/cloud/spanner/data.rb, line 50
def fields
  @fields ||= Fields.from_grpc @grpc_fields
end
inspect() click to toggle source

@private

# File lib/google/cloud/spanner/data.rb, line 197
def inspect
  "#<#{self.class.name} #{self}>"
end
keys() click to toggle source

Returns the names of values, or in cases in which values are unnamed, the zero-based index position of values.

@return [Array<(String,Integer)>] An array containing the names

(String) or position (Integer) for the corresponding values of the
data.
# File lib/google/cloud/spanner/data.rb, line 74
def keys
  fields.keys
end
pairs() click to toggle source

Returns the names or positions and their corresponding values as an array of arrays.

@return [Array<Array>] An array containing name/position and value

pairs.
# File lib/google/cloud/spanner/data.rb, line 94
def pairs
  keys.zip values
end
to_a(skip_dup_check: nil) click to toggle source

Returns the values as an array. This method will raise {DuplicateNameError} if nested data has more than one member with the same name, unless `skip_dup_check` is set to `true`, in which case it will lose values.

@param [Boolean] skip_dup_check Skip the check for whether nested data

contains duplicate names, which will improve performance. When
skipped, the nested hash may lose values. Default is `false`.
Optional.

@raise [Google::Cloud::Spanner::DuplicateNameError] if nested data

contains duplicate names.

@return [Array<Object>] An array containing the values of the data.

# File lib/google/cloud/spanner/data.rb, line 140
def to_a skip_dup_check: nil
  values.map do |value|
    case value
    when Data
      value.to_h skip_dup_check: skip_dup_check
    when Array
      value.map do |v|
        v.is_a?(Data) ? v.to_h(skip_dup_check: skip_dup_check) : v
      end
    else
      value
    end
  end
end
to_grpc_type() click to toggle source

@private

# File lib/google/cloud/spanner/data.rb, line 217
def to_grpc_type
  V1::Type.new(
    code: :STRUCT,
    struct_type: V1::StructType.new(
      fields: @grpc_fields
    )
  )
end
to_grpc_value() click to toggle source

@private

# File lib/google/cloud/spanner/data.rb, line 203
def to_grpc_value
  if @grpc_values.nil?
    return Google::Protobuf::Value.new null_value: :NULL_VALUE
  end

  Google::Protobuf::Value.new(
    list_value: Google::Protobuf::ListValue.new(
      values: @grpc_values
    )
  )
end
to_grpc_value_and_type() click to toggle source

@private

# File lib/google/cloud/spanner/data.rb, line 228
def to_grpc_value_and_type
  [to_grpc_value, to_grpc_type]
end
to_h(skip_dup_check: nil) click to toggle source

Returns the names or indexes and corresponding values of the data as a hash. This method will raise {DuplicateNameError} if the data has more than one member with the same name, unless `skip_dup_check` is set to `true`, in which case it will lose values.

@param [Boolean] skip_dup_check Skip the check for whether the data

contains duplicate names, which will improve performance. When
skipped, the returned hash may lose values. Default is `false`.
Optional.

@raise [Google::Cloud::Spanner::DuplicateNameError] if the data

contains duplicate names.

@return [Hash<(String,Integer)=>Object>] A hash containing the names

or indexes and corresponding values.
# File lib/google/cloud/spanner/data.rb, line 172
def to_h skip_dup_check: nil
  raise DuplicateNameError if !skip_dup_check && fields.duplicate_names?

  Hash[keys.zip to_a(skip_dup_check: skip_dup_check)]
end
to_s() click to toggle source

@private

# File lib/google/cloud/spanner/data.rb, line 185
def to_s
  named_values = pairs.map do |key, value|
    if key.is_a? Integer
      value.inspect
    else
      "(#{key})#{value.inspect}"
    end
  end
  "(#{named_values.join ', '})"
end
types() click to toggle source

Returns the types of the data.

See [Data types](cloud.google.com/spanner/docs/data-definition-language#data_types).

@return [Array<Symbol>] An array containing the types of the values.

# File lib/google/cloud/spanner/data.rb, line 62
def types
  fields.types
end
values() click to toggle source

Returns the values of the data.

@return [Array<Object>] An array containing the values.

# File lib/google/cloud/spanner/data.rb, line 83
def values
  Array.new(keys.count) { |i| self[i] }
end