Class: Google::Cloud::Spanner::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/spanner/data.rb

Overview

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).

Examples:

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

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object?

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.

Parameters:

  • key (String, Integer)

    The name (String) or zero-based index position (Integer) of the value.

Returns:

  • (Object, nil)

    The value, or nil if no value is found.

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
# 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

#fieldsArray<Array>

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

Returns:

  • (Array<Array>)

    An array containing name and value pairs.



50
51
52
# File 'lib/google/cloud/spanner/data.rb', line 50

def fields
  @fields ||= Fields.from_grpc @grpc_fields
end

#keysArray<(String,Integer)>

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

Returns:

  • (Array<(String,Integer)>)

    An array containing the names (String) or position (Integer) for the corresponding values of the data.



74
75
76
# File 'lib/google/cloud/spanner/data.rb', line 74

def keys
  fields.keys
end

#pairsArray<Array>

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

Returns:

  • (Array<Array>)

    An array containing name/position and value pairs.



94
95
96
# File 'lib/google/cloud/spanner/data.rb', line 94

def pairs
  keys.zip values
end

#to_a(skip_dup_check: nil) ⇒ Array<Object>

Returns the values as an array. This method will raise Google::Cloud::Spanner::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.

Parameters:

  • skip_dup_check (Boolean) (defaults to: nil)

    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.

Returns:

  • (Array<Object>)

    An array containing the values of the data.

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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_h(skip_dup_check: nil) ⇒ Hash<(String,Integer)=>Object>

Returns the names or indexes and corresponding values of the data as a hash. This method will raise Google::Cloud::Spanner::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.

Parameters:

  • skip_dup_check (Boolean) (defaults to: nil)

    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.

Returns:

  • (Hash<(String,Integer)=>Object>)

    A hash containing the names or indexes and corresponding values.

Raises:



172
173
174
175
176
# 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?

  keys.zip(to_a(skip_dup_check: skip_dup_check)).to_h
end

#typesArray<Symbol>

Returns the types of the data.

See Data types.

Returns:

  • (Array<Symbol>)

    An array containing the types of the values.



62
63
64
# File 'lib/google/cloud/spanner/data.rb', line 62

def types
  fields.types
end

#valuesArray<Object>

Returns the values of the data.

Returns:

  • (Array<Object>)

    An array containing the values.



83
84
85
# File 'lib/google/cloud/spanner/data.rb', line 83

def values
  Array.new(keys.count) { |i| self[i] }
end