Class: Vertica::RowDescription

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vertica/row_description.rb

Overview

Class that describes the shape of a query result. It contains an ordered list of Column instances, which describes the columns that will apear for every Row part of the result of a query.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns) ⇒ RowDescription

Returns a new instance of RowDescription.

Parameters:

  • columns (Array<Vertica::Column>)

    The list of columns as they will appear in the rows of a query result.

See Also:



33
34
35
# File 'lib/vertica/row_description.rb', line 33

def initialize(columns)
  @columns = columns
end

Class Method Details

.build(columns) ⇒ Vertica::RowDescription

Builds a new Vertica::RowDescription instance given a list of columns.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If no Vertica::RowDescription could be constructed given the provided argument.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vertica/row_description.rb', line 15

def self.build(columns)
  case columns
  when Vertica::Protocol::RowDescription
    new(columns.fields.map { |fd| Vertica::Column.build(fd) })
  when Vertica::RowDescription
    columns
  when Array
    new(columns)
  when nil
    nil
  else
    raise ArgumentError, "Invalid list of columns: #{columns.inspect}"
  end
end

Instance Method Details

#build_row(values) ⇒ Vertica::Row

Builds a Vertica::Row instance given a list of values that conforms to this row description.

Parameters:

  • values (Array, Hash, Vertica::Protocol::DataRow)

    A list of values. The number of values should match the number of columns in the row description.

Returns:

Raises:

  • (ArgumentError)

    An ArgumentErrpr is raised if the number of values does not match the row description, or if a type is provided that it cannot handle.



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
# File 'lib/vertica/row_description.rb', line 98

def build_row(values)
  case values
  when Vertica::Row
    raise ArgumentError, "Row description of provided row does match this row description" if values.row_description != self
    values

  when Vertica::Protocol::DataRow
    raise ArgumentError, "Number of values does not match row description" if values.values.size != size
    converted_values = @columns.map.with_index do |column, index|
      column.data_type.deserialize(values.values.fetch(index))
    end
    Vertica::Row.new(self, converted_values)

  when Array
    raise ArgumentError, "Number of values does not match row description" if values.size != size
    Vertica::Row.new(self, values)

  when Hash
    raise ArgumentError, "Number of values does not match row description" if values.size != size
    values_as_array = @columns.map { |column| values[column.name] || values[column.name.to_sym] }
    Vertica::Row.new(self, values_as_array)

  else
    raise ArgumentError, "Don't know how to build a row from a #{values.class.name} instance"
  end
end

#column(name_or_index) ⇒ Vertica::Column Also known as: []

Returns a column in this row description.

Parameters:

  • name_or_index (String, Symbol, Integer)

    The name of the column, or the index of the column in this row description.

Returns:

Raises:

  • (KeyError)

    if the column could not be found in this row description.



42
43
44
# File 'lib/vertica/row_description.rb', line 42

def column(name_or_index)
  column_with_index(name_or_index).first
end

#column_with_index(name_or_index) ⇒ Array<Vertica::Column, Integer>

Returns a column, accompanied by the index of that column in this row description.

Parameters:

  • name_or_index (String, Symbol, Integer)

    The name of the column, or the index of the column in this row description.

Returns:

Raises:

  • (KeyError)

    if the column could not be found in this row description.



53
54
55
# File 'lib/vertica/row_description.rb', line 53

def column_with_index(name_or_index)
  columns_index.fetch(name_or_index)
end

#each {|column| ... }

This method returns an undefined value.

Iterates over the columns in this row description.

Yields:

  • The provided block will be called with every column.

Yield Parameters:



61
62
63
# File 'lib/vertica/row_description.rb', line 61

def each(&block)
  @columns.each(&block)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns true iff this record is equal to the other provided object

Returns:

  • (Boolean)

    Returns true iff this record is equal to the other provided object



126
127
128
# File 'lib/vertica/row_description.rb', line 126

def eql?(other)
  self.class === other && other.to_a == self.to_a
end

#hashInteger

Returns a hash digtest of this object.

Returns:

  • (Integer)

    Returns a hash digtest of this object.



133
134
135
# File 'lib/vertica/row_description.rb', line 133

def hash
  @columns.hash
end

#inspectString

Returns a user-consumable string representation of this row description.

Returns:

  • (String)

    Returns a user-consumable string representation of this row description.



138
139
140
# File 'lib/vertica/row_description.rb', line 138

def inspect
  "#<#{self.class.name}[#{@columns.map(&:name).join(', ')}]>"
end

#sizeInteger Also known as: length

Returns the number of columns in this row description.

Returns:

  • (Integer)

    Returns the number of columns in this row description.



66
67
68
# File 'lib/vertica/row_description.rb', line 66

def size
  @columns.size
end

#to_aArray<Vertica::Column>

Returns the columns of this row description as an array.

Returns:

  • (Array<Vertica::Column>)

    Returns the columns of this row description as an array.



73
74
75
# File 'lib/vertica/row_description.rb', line 73

def to_a
  @columns.clone
end

#to_h(symbolize_keys: false) ⇒ Hash

Returns the columns of this row description as a hash, index by the column name.

Parameters:

  • symbolize_keys (Boolean) (defaults to: false)

    Whether to use symbols instead of strings as keys.

Returns:

  • (Hash)

    Returns the columns of this row description as a hash, index by the column name.

Raises:



82
83
84
85
86
87
88
89
# File 'lib/vertica/row_description.rb', line 82

def to_h(symbolize_keys: false)
  @columns.inject({}) do |carry, column|
    key = symbolize_keys ? column.name.to_sym : column.name
    raise Vertica::Error::DuplicateColumnName, "Column with name #{key} occurs more than once in this row description." if carry.key?(key)
    carry[key] = column
    carry
  end
end