Sha256: 09ed937666f0f47d8a7fa7b059a61ad87d5fe3a7df3b5bdc12f6192cb05bfc1b

Contents?: true

Size: 1.66 KB

Versions: 18

Compression:

Stored size: 1.66 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 = String.new.respond_to?(:force_encoding) ? lambda { |s| s.force_encoding('utf-8') } : nil

    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

18 entries across 18 versions & 1 rubygems

Version Path
vertica-0.11.0 lib/vertica/column.rb
vertica-0.10.5 lib/vertica/column.rb
vertica-0.10.4 lib/vertica/column.rb
vertica-0.10.3 lib/vertica/column.rb
vertica-0.10.2 lib/vertica/column.rb
vertica-0.10.1 lib/vertica/column.rb
vertica-0.10.0 lib/vertica/column.rb
vertica-0.9.6 lib/vertica/column.rb
vertica-0.9.5 lib/vertica/column.rb
vertica-0.9.4 lib/vertica/column.rb
vertica-0.9.3 lib/vertica/column.rb
vertica-0.9.2 lib/vertica/column.rb
vertica-0.9.1 lib/vertica/column.rb
vertica-0.9.0 lib/vertica/column.rb
vertica-0.9.0.beta9 lib/vertica/column.rb
vertica-0.9.0.beta8 lib/vertica/column.rb
vertica-0.9.0.beta7 lib/vertica/column.rb
vertica-0.9.0.beta6 lib/vertica/column.rb