Class: RubySnowflake::Row
- Inherits:
-
Object
- Object
- RubySnowflake::Row
- Includes:
- Enumerable
- Defined in:
- lib/ruby_snowflake/row.rb
Constant Summary collapse
- EPOCH_JULIAN_DAY_NUMBER =
Date.new(1970,1,1).jd
- TIME_FORMAT =
"%s.%N".freeze
Instance Method Summary collapse
- #[](column) ⇒ Object
- #each ⇒ Object
-
#initialize(row_types, column_to_index, data) ⇒ Row
constructor
A new instance of Row.
- #keys ⇒ Object (also: #columns)
- #to_s ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(row_types, column_to_index, data) ⇒ Row
Returns a new instance of Row.
13 14 15 16 17 |
# File 'lib/ruby_snowflake/row.rb', line 13 def initialize(row_types, column_to_index, data) @row_types = row_types @data = data @column_to_index = column_to_index end |
Instance Method Details
#[](column) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruby_snowflake/row.rb', line 20 def [](column) index = if column.is_a?(Numeric) Integer(column) else # Handle column names case-insensitively regardless of string or symbol @column_to_index[column.to_s.downcase] end return nil if index.nil? return nil if @data[index].nil? case @row_types[index][:type] when :boolean @data[index] == "true" when :date Date.jd(Integer(@data[index]) + EPOCH_JULIAN_DAY_NUMBER) when :fixed if @row_types[index][:scale] == 0 Integer(@data[index]) else BigDecimal(@data[index]).round(@row_types[index][:scale]) end # snowflake treats these all as 64 bit IEEE 754 floating point numbers, and will we too when :float, :double, :"double precision", :real Float(@data[index]) # Despite snowflake indicating that it sends the offset in minutes, the actual time in UTC # is always sent in the first half of the data. If an offset is sent it looks like: # "1641008096.123000000 1980" # If there isn't one, it's just like this: # "1641065696.123000000" # in all cases, the actual time, in UTC is the float value, and the offset is ignorable when :time, :datetime, :timestamp, :timestamp_ntz, :timestamp_ltz, :timestamp_tz Time.strptime(@data[index], TIME_FORMAT).utc else @data[index] end end |
#each ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby_snowflake/row.rb', line 60 def each return to_enum __method__ unless block_given? @column_to_index.each_pair do |name, index| yield(name, self[index]) end self end |
#keys ⇒ Object Also known as: columns
70 71 72 |
# File 'lib/ruby_snowflake/row.rb', line 70 def keys map { |k, _| k } end |
#to_s ⇒ Object
80 81 82 |
# File 'lib/ruby_snowflake/row.rb', line 80 def to_s to_h.to_s end |
#values ⇒ Object
76 77 78 |
# File 'lib/ruby_snowflake/row.rb', line 76 def values map { |_, v| v } end |