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](cloud.google.com/spanner/docs/data-definition-language#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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ Fields

Returns a new instance of Fields.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/google/cloud/spanner/fields.rb', line 45

def initialize types
  @fields = []
  if types.is_a? Array
    types.each do |type|
      @fields << field(type)
    end
  elsif types.is_a? Hash
    types.each do |type|
      @fields << field(type)
    end
  else
    fail ArgumentError, "can only accept Array or Hash"
  end
end

Class Method Details

.from_grpc(fields) ⇒ Object

Google::Spanner::V1::Metadata::Row_type::Fields.



220
221
222
223
224
# File 'lib/google/cloud/spanner/fields.rb', line 220

def self.from_grpc fields
  new([]).tap do |f|
    f.instance_variable_set :@fields, fields
  end
end

Instance Method Details

#==(other) ⇒ Object



195
196
197
198
# File 'lib/google/cloud/spanner/fields.rb', line 195

def == other
  return false unless other.is_a? Fields
  pairs == other.pairs
end

#[](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 == 0
  fail DuplicateNameError if name_count > 1
  index = @fields.find_index { |f| f.name == String(key) }
  types[index]
end

#data(data) ⇒ Object Also known as: new



186
187
188
189
190
191
# File 'lib/google/cloud/spanner/fields.rb', line 186

def data data
  # TODO: match order of types
  data = data.values if data.is_a?(Hash)
  values = data.map { |datum| Convert.raw_to_value datum }
  Data.from_grpc values, @fields
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

#inspectObject



213
214
215
# File 'lib/google/cloud/spanner/fields.rb', line 213

def inspect
  "#<#{self.class.name} #{self}>"
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
# File 'lib/google/cloud/spanner/fields.rb', line 153

def to_a
  keys.count.times.map { |i| self[i] }.map do |field|
    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.



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

def to_h
  fail 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

#to_sObject



201
202
203
204
205
206
207
208
209
210
# File 'lib/google/cloud/spanner/fields.rb', line 201

def to_s
  named_types = pairs.map do |key, type|
    if key.is_a? Integer
      "#{type.inspect}"
    else
      "(#{key})#{type.inspect}"
    end
  end
  "(#{named_types.join ', '})"
end

#typesArray<Symbol>

Returns the types of the data.

See [Data types](cloud.google.com/spanner/docs/data-definition-language#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