Class: Vertica::Row

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

Overview

Class to represent a row returns by a query.

Row instances can either be yielded to the block passed to Connection#query, or be part of a buffered Result returned by Connection#query.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row_description, values) ⇒ Row

Initializes a new row instance for a given row description and values. The number of values MUST match the number of columns in the row description.

Parameters:

  • row_description (Vertica::RowDescription)

    The row description the values will conform to.

  • values (Array)

    The values for the columns in the row description

See Also:



21
22
23
# File 'lib/vertica/row.rb', line 21

def initialize(row_description, values)
  @row_description, @values = row_description, values
end

Instance Attribute Details

#row_descriptionVertica::RowDescription (readonly)

The ordered list of columns this row conforms to.

Returns:



10
11
12
# File 'lib/vertica/row.rb', line 10

def row_description
  @row_description
end

Instance Method Details

#each {|value| ... } ⇒ Object

Iterates over the values in this row.

Yields:

  • (value)

    The provided block will get called with the values in the order of the columns in the row_description.



28
29
30
# File 'lib/vertica/row.rb', line 28

def each(&block)
  @values.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



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

def eql?(other)
  other.kind_of?(Vertica::Row) && other.row_description == row_description && other.to_a == to_a
end

#fetch(name_or_index) ⇒ Object Also known as: []

Fetches a value from this row.

Parameters:

  • name_or_index (Symbol, String, Integer)

    The name of the column as string or symbol, or the index of the column in the row description.

Raises:

  • KeyError A KeyError is raised if the field connot be found.



36
37
38
# File 'lib/vertica/row.rb', line 36

def fetch(name_or_index)
  @values.fetch(column_index(name_or_index))
end

#hashInteger

Returns a hash digtest of this object.

Returns:

  • (Integer)

    Returns a hash digtest of this object.



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

def hash
  [row_description, values].hash
end

#inspectString

Returns a user-consumable string representation of this row.

Returns:

  • (String)

    Returns a user-consumable string representation of this row.



78
79
80
# File 'lib/vertica/row.rb', line 78

def inspect
  "#<#{self.class.name}#{@values.inspect}>"
end

#to_aArray

Returns an array representation of the row. The values will be ordered like the order of the fields in the #row_description.

Returns:

  • (Array)


45
46
47
# File 'lib/vertica/row.rb', line 45

def to_a
  @values
end

#to_h(symbolize_keys: false) ⇒ Hash

Returns a hash representation of this rows, using the name of the fields as keys.

Parameters:

  • symbolize_keys (true, false) (defaults to: false)

    Set to true to use symbols instead of strings for the field names.

Returns:

  • (Hash)

Raises:



56
57
58
59
60
61
62
63
# File 'lib/vertica/row.rb', line 56

def to_h(symbolize_keys: false)
  @row_description.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." if carry.key?(key)
    carry[key] = fetch(column.name)
    carry
  end
end