Class: Vertica::RowDescription
- Inherits:
-
Object
- Object
- Vertica::RowDescription
- Includes:
- Enumerable
- Defined in:
- lib/vertica/row_description.rb
Overview
Class Method Summary collapse
-
.build(columns) ⇒ Vertica::RowDescription
Builds a new Vertica::RowDescription instance given a list of columns.
Instance Method Summary collapse
-
#build_row(values) ⇒ Vertica::Row
Builds a Row instance given a list of values that conforms to this row description.
-
#column(name_or_index) ⇒ Vertica::Column
(also: #[])
Returns a column in this row description.
-
#column_with_index(name_or_index) ⇒ Array<Vertica::Column, Integer>
Returns a column, accompanied by the index of that column in this row description.
-
#each {|column| ... }
Iterates over the columns in this row description.
-
#eql?(other) ⇒ Boolean
(also: #==)
Returns true iff this record is equal to the other provided object.
-
#hash ⇒ Integer
Returns a hash digtest of this object.
-
#initialize(columns) ⇒ RowDescription
constructor
A new instance of RowDescription.
-
#inspect ⇒ String
Returns a user-consumable string representation of this row description.
-
#size ⇒ Integer
(also: #length)
Returns the number of columns in this row description.
-
#to_a ⇒ Array<Vertica::Column>
Returns the columns of this row description as an array.
-
#to_h(symbolize_keys: false) ⇒ Hash
Returns the columns of this row description as a hash, index by the column name.
Constructor Details
#initialize(columns) ⇒ RowDescription
Returns a new instance of RowDescription.
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.
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.
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.
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.
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.
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
126 127 128 |
# File 'lib/vertica/row_description.rb', line 126 def eql?(other) self.class === other && other.to_a == self.to_a end |
#hash ⇒ Integer
Returns a hash digtest of this object.
133 134 135 |
# File 'lib/vertica/row_description.rb', line 133 def hash @columns.hash end |
#inspect ⇒ 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 |
#size ⇒ Integer Also known as: length
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_a ⇒ 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.
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 |