Sha256: a68eb6c2a09e553d4f26a63035c852b8d1fd1f8668c9884377e4b382a1cb6857

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module Vertica
  class Column
    attr_reader :name
    attr_reader :table_oid
    attr_reader :type_modifier
    attr_reader :size
    attr_reader :data_type

    STRING_CONVERTER = lambda { |s| s.force_encoding('utf-8') }

    DATA_TYPE_CONVERSIONS = [
      [:unspecified,  nil],
      [:tuple,        nil],
      [:pos,          nil],
      [:record,       nil],
      [:unknown,      nil],
      [:bool,         lambda { |s| s == 't' }],
      [:integer,      lambda { |s| s.to_i }],
      [:float,        lambda { |s| s.to_f }],
      [:char,         STRING_CONVERTER],
      [:varchar,      STRING_CONVERTER],
      [:date,         lambda { |s| Date.new(*s.split("-").map{|x| x.to_i}) }],
      [:time,         nil],
      [:timestamp,    lambda { |s| DateTime.parse(s, true) }],
      [:timestamp_tz, lambda { |s| DateTime.parse(s, true) }],
      [:interval,     nil],
      [:time_tz,      nil],
      [:numeric,      lambda { |s| BigDecimal.new(s) }],
      [:bytea,        nil],
      [:rle_tuple,    nil]
    ]

    DATA_TYPES = DATA_TYPE_CONVERSIONS.map { |t| t[0] }

    def initialize(col)
      @type_modifier    = col[:type_modifier]
      @format           = col[:format_code] == 0 ? :text : :binary
      @table_oid        = col[:table_oid]
      @name             = col[:name].to_sym
      @attribute_number = col[:attribute_number]
      @data_type        = DATA_TYPE_CONVERSIONS[col[:data_type_oid]][0]
      @converter        = DATA_TYPE_CONVERSIONS[col[:data_type_oid]][1]
      @size             = col[:data_type_size]
    end

    def convert(s)
      return unless s
      @converter ? @converter.call(s) : s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vertica-0.11.2 lib/vertica/column.rb
vertica-0.11.1 lib/vertica/column.rb