Class: Google::Cloud::Spanner::Fields

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

Overview

Fields

Represents the field names and types of data returned by Cloud Spanner.

See Data types.

Examples:

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

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

results = db.execute "SELECT * FROM users"

results.fields.pairs.each do |name, type|
  puts "Column #{name} is type {type}"
end

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Symbol?

Returns the type code 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. (See #duplicate_names?)

Parameters:

  • key (String, Integer)

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

Returns:

  • (Symbol, nil)

    The type code, or nil if no value is found.

Raises:



135
136
137
138
139
140
141
142
# File 'lib/google/cloud/spanner/fields.rb', line 135

def [] key
  return types[key] if key.is_a? Integer
  name_count = @fields.find_all { |f| f.name == String(key) }.count
  return nil if name_count.zero?
  raise DuplicateNameError if name_count > 1
  index = @fields.find_index { |f| f.name == String(key) }
  types[index]
end

#duplicate_names?Boolean

Detects duplicate names in the keys for the fields.

Returns:

  • (Boolean)

    Returns true if there are duplicate names.



107
108
109
# File 'lib/google/cloud/spanner/fields.rb', line 107

def duplicate_names?
  keys.count != keys.uniq.count
end

#keysArray<(String,Integer)>

Returns the names of the data 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.



92
93
94
95
96
97
98
99
100
# File 'lib/google/cloud/spanner/fields.rb', line 92

def keys
  @fields.each_with_index.map do |field, index|
    if field.name.empty?
      index
    else
      field.name.to_sym
    end
  end
end

#pairsArray<Array>

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

Returns:

  • (Array<Array>)

    An array containing name/position and types pairs.



118
119
120
# File 'lib/google/cloud/spanner/fields.rb', line 118

def pairs
  keys.zip types
end

#to_aArray<Symbol>

Returns the type codes as an array. Do not use this method if the data has more than one member with the same name. (See #duplicate_names?)

Returns:

  • (Array<Symbol>)

    An array containing the type codes.

Raises:



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/google/cloud/spanner/fields.rb', line 153

def to_a
  Array.new(keys.count) do |i|
    field = self[i]
    if field.is_a? Fields
      field.to_h
    elsif field.is_a? Array
      field.map { |f| f.is_a?(Fields) ? f.to_h : f }
    else
      field
    end
  end
end

#to_hHash<(String,Integer)=>Symbol>

Returns the names or indexes and corresponding type codes as a hash.

Returns:

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

    A hash containing the names or indexes and corresponding types.

Raises:



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/google/cloud/spanner/fields.rb', line 172

def to_h
  raise DuplicateNameError if duplicate_names?
  hashified_pairs = pairs.map do |key, value|
    if value.is_a? Fields
      [key, value.to_h]
    elsif value.is_a? Array
      [key, value.map { |v| v.is_a?(Fields) ? v.to_h : v }]
    else
      [key, value]
    end
  end
  Hash[hashified_pairs]
end

#typesArray<Symbol>

Returns the types of the data.

See Data types.

Returns:

  • (Array<Symbol>)

    An array containing the types of the data.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/google/cloud/spanner/fields.rb', line 68

def types
  @fields.map(&:type).map do |type|
    if type.code == :ARRAY
      if type.array_element_type.code == :STRUCT
        [Fields.from_grpc(type.array_element_type.struct_type.fields)]
      else
        [type.array_element_type.code]
      end
    elsif type.code == :STRUCT
      Fields.from_grpc type.struct_type.fields
    else
      type.code
    end
  end
end